refactor: 🚨 fix deno lint rule no-slow-types

This commit is contained in:
Julien Oculi 2024-07-16 15:01:16 +02:00
parent 2209a09c26
commit 90cfd257aa
8 changed files with 90 additions and 18 deletions

View file

@ -33,7 +33,7 @@ export class Db {
} }
} }
get resource() { get resource(): ResourceAccessors {
return { return {
credential: this.#resourceStorage<Credential<CredentialCategory>>( credential: this.#resourceStorage<Credential<CredentialCategory>>(
'credential', 'credential',
@ -49,10 +49,9 @@ export class Db {
#resourceStorage<T extends Resource>( #resourceStorage<T extends Resource>(
type: ResourceType<T>, type: ResourceType<T>,
Builder: ResourceBuilder<T>, Builder: ResourceBuilder<T>,
) { ): ResourceStorage<T> {
return { return {
get: (resource: Pick<T, 'uuid'>) => get: (resource: Pick<T, 'uuid'>) => this.#get<T>(type, Builder, resource),
this.#get<T>(type, Builder, resource),
set: (resources: T[]) => this.#set<T>(type, resources), set: (resources: T[]) => this.#set<T>(type, resources),
delete: (resources: Pick<T, 'uuid'>[]) => delete: (resources: Pick<T, 'uuid'>[]) =>
this.#delete<T>(type, resources), this.#delete<T>(type, resources),
@ -132,8 +131,8 @@ export class Db {
} }
} }
type ResourceType<T extends Resource> = T extends type ResourceType<T extends Resource> = T extends Credential<CredentialCategory>
Credential<CredentialCategory> ? 'credential' ? 'credential'
: T extends Group ? 'group' : T extends Group ? 'group'
: T extends Machine ? 'machine' : T extends Machine ? 'machine'
: T extends Service ? 'service' : T extends Service ? 'service'
@ -149,3 +148,25 @@ type ResourceBuilder<T extends Resource> = T extends
: never : never
type ResourceJson<T extends Resource> = ReturnType<T['toJSON']> type ResourceJson<T extends Resource> = ReturnType<T['toJSON']>
type ResourceStorage<T extends Resource> = {
get: (resource: Pick<T, 'uuid'>) => Promise<T>
set: (resources: T[]) => Promise<Deno.KvCommitResult | Deno.KvCommitError>
delete: (
resources: Pick<T, 'uuid'>[],
) => Promise<Deno.KvCommitResult | Deno.KvCommitError>
list: (
filter?: (resource: T) => boolean | Promise<boolean>,
) => AsyncGenerator<T, void, void>
listRef: (
filter?: (resource: T) => boolean | Promise<boolean>,
) => Promise<Ref<T>[]>
}
type ResourceAccessors = {
credential: ResourceStorage<Credential<CredentialCategory>>
group: ResourceStorage<Group>
machine: ResourceStorage<Machine>
service: ResourceStorage<Service>
user: ResourceStorage<User>
}

View file

@ -5,4 +5,3 @@ export type { Resource } from './src/resource.ts'
export { Service } from './src/service.ts' export { Service } from './src/service.ts'
export { User } from './src/user.ts' export { User } from './src/user.ts'
export { Ref, type RefResolver, type RefString } from './utils/ref.ts' export { Ref, type RefResolver, type RefString } from './utils/ref.ts'

View file

@ -1,7 +1,7 @@
import type { Base64String, ToJson, UUID } from '../../../types.ts' import type { Base64String, ToJson, UUID } from '../../../types.ts'
import { Avatar } from '../utils/avatar.ts' import { Avatar } from '../utils/avatar.ts'
import type { Ref } from '../utils/ref.ts' import type { Ref } from '../utils/ref.ts'
import { Resource } from './resource.ts' import { Resource, type ResourceJson } from './resource.ts'
export class Credential<T extends CredentialCategory> extends Resource { export class Credential<T extends CredentialCategory> extends Resource {
static fromJSON<T extends CredentialCategory>( static fromJSON<T extends CredentialCategory>(
@ -89,7 +89,7 @@ export class Credential<T extends CredentialCategory> extends Resource {
}) })
} }
toJSON() { toJSON(): ResourceJson<Credential<T>, 'category' | 'store'> {
return { return {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,

View file

@ -1,7 +1,11 @@
import type { Posix, ToJson, UUID } from '../../../types.ts' import type { Posix, ToJson, UUID } from '../../../types.ts'
import { Avatar } from '../utils/avatar.ts' import { Avatar } from '../utils/avatar.ts'
import { Ref } from '../utils/ref.ts' import { Ref } from '../utils/ref.ts'
import { Resource } from './resource.ts' import {
Resource,
type ResourceJson,
type ResourceRefJson,
} from './resource.ts'
export class Group extends Resource { export class Group extends Resource {
static fromJSON( static fromJSON(
@ -114,7 +118,10 @@ export class Group extends Resource {
}) })
} }
toJSON() { toJSON():
& ResourceJson<Group, 'permissions'>
& { posix: Posix | null }
& ResourceRefJson<Group, Group, 'groups'> {
return { return {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,

View file

@ -2,7 +2,11 @@ import type { ToJson, UrlString } from '../../../types.ts'
import { Avatar } from '../utils/avatar.ts' import { Avatar } from '../utils/avatar.ts'
import { Ref } from '../utils/ref.ts' import { Ref } from '../utils/ref.ts'
import type { Group } from './group.ts' import type { Group } from './group.ts'
import { Resource } from './resource.ts' import {
Resource,
type ResourceJson,
type ResourceRefJson,
} from './resource.ts'
export class Machine extends Resource { export class Machine extends Resource {
static fromJSON( static fromJSON(
@ -108,7 +112,9 @@ export class Machine extends Resource {
}) })
} }
toJSON() { toJSON():
& ResourceJson<Machine, 'tags' | 'url' | 'status'>
& ResourceRefJson<Machine, Group, 'groups'> {
return { return {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,

View file

@ -95,7 +95,10 @@ export class Resource {
}) })
} }
toJSON() { toJSON(): ResourceJson<
Resource,
'type' | 'uuid' | 'name' | 'avatar' | 'createdAt' | 'updatedAt'
> {
return { return {
type: this.type, type: this.type,
uuid: this.uuid, uuid: this.uuid,
@ -123,3 +126,24 @@ export interface Resource {
createdAt: DateString createdAt: DateString
updatedAt: DateString updatedAt: DateString
} }
export type ResourceJson<T extends Resource, K extends keyof T> = Readonly<
Pick<
T,
| 'type'
| 'uuid'
| 'name'
| 'avatar'
| 'createdAt'
| 'updatedAt'
| K
>
>
export type ResourceRefJson<
T extends Resource,
Ref extends Resource,
K extends keyof T,
> = {
[k in K]: Readonly<ReturnType<ReturnType<Ref['toRef']>['toString']>[]>
}

View file

@ -2,7 +2,11 @@ import type { ToJson, UrlString } from '../../../types.ts'
import { Avatar } from '../utils/avatar.ts' import { Avatar } from '../utils/avatar.ts'
import { Ref } from '../utils/ref.ts' import { Ref } from '../utils/ref.ts'
import type { Group } from './group.ts' import type { Group } from './group.ts'
import { Resource } from './resource.ts' import {
Resource,
type ResourceJson,
type ResourceRefJson,
} from './resource.ts'
export class Service extends Resource { export class Service extends Resource {
static fromJSON( static fromJSON(
@ -126,7 +130,9 @@ export class Service extends Resource {
}) })
} }
toJSON() { toJSON():
& ResourceJson<Service, 'category' | 'tags' | 'url'>
& ResourceRefJson<Service, Group, 'groups'> {
return { return {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,

View file

@ -4,7 +4,11 @@ import { Avatar } from '../utils/avatar.ts'
import { Ref } from '../utils/ref.ts' import { Ref } from '../utils/ref.ts'
import type { Credential, CredentialCategory } from './credential.ts' import type { Credential, CredentialCategory } from './credential.ts'
import type { Group } from './group.ts' import type { Group } from './group.ts'
import { Resource } from './resource.ts' import {
Resource,
type ResourceJson,
type ResourceRefJson,
} from './resource.ts'
export class User extends Resource { export class User extends Resource {
static fromJSON(json: ToJson<User>): User { static fromJSON(json: ToJson<User>): User {
@ -190,9 +194,14 @@ export class User extends Resource {
}) })
} }
toJSON() { toJSON():
& ResourceJson<User, 'lastname' | 'firstname' | 'login' | 'mail'>
& { posix: Posix | null }
& ResourceRefJson<User, Group, 'groups'>
& ResourceRefJson<User, Credential<CredentialCategory>, 'credentials'> {
return { return {
...super.toJSON(), ...super.toJSON(),
type: this.type,
lastname: this.lastname, lastname: this.lastname,
firstname: this.firstname, firstname: this.firstname,
login: this.login, login: this.login,