refactor: 🚨 fix deno lint rule no-slow-types
This commit is contained in:
parent
2209a09c26
commit
90cfd257aa
|
@ -33,7 +33,7 @@ export class Db {
|
|||
}
|
||||
}
|
||||
|
||||
get resource() {
|
||||
get resource(): ResourceAccessors {
|
||||
return {
|
||||
credential: this.#resourceStorage<Credential<CredentialCategory>>(
|
||||
'credential',
|
||||
|
@ -49,10 +49,9 @@ export class Db {
|
|||
#resourceStorage<T extends Resource>(
|
||||
type: ResourceType<T>,
|
||||
Builder: ResourceBuilder<T>,
|
||||
) {
|
||||
): ResourceStorage<T> {
|
||||
return {
|
||||
get: (resource: Pick<T, 'uuid'>) =>
|
||||
this.#get<T>(type, Builder, resource),
|
||||
get: (resource: Pick<T, 'uuid'>) => this.#get<T>(type, Builder, resource),
|
||||
set: (resources: T[]) => this.#set<T>(type, resources),
|
||||
delete: (resources: Pick<T, 'uuid'>[]) =>
|
||||
this.#delete<T>(type, resources),
|
||||
|
@ -132,8 +131,8 @@ export class Db {
|
|||
}
|
||||
}
|
||||
|
||||
type ResourceType<T extends Resource> = T extends
|
||||
Credential<CredentialCategory> ? 'credential'
|
||||
type ResourceType<T extends Resource> = T extends Credential<CredentialCategory>
|
||||
? 'credential'
|
||||
: T extends Group ? 'group'
|
||||
: T extends Machine ? 'machine'
|
||||
: T extends Service ? 'service'
|
||||
|
@ -149,3 +148,25 @@ type ResourceBuilder<T extends Resource> = T extends
|
|||
: never
|
||||
|
||||
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>
|
||||
}
|
||||
|
|
|
@ -5,4 +5,3 @@ export type { Resource } from './src/resource.ts'
|
|||
export { Service } from './src/service.ts'
|
||||
export { User } from './src/user.ts'
|
||||
export { Ref, type RefResolver, type RefString } from './utils/ref.ts'
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import type { Base64String, ToJson, UUID } from '../../../types.ts'
|
||||
import { Avatar } from '../utils/avatar.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 {
|
||||
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 {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
import type { Posix, ToJson, UUID } from '../../../types.ts'
|
||||
import { Avatar } from '../utils/avatar.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 {
|
||||
static fromJSON(
|
||||
|
@ -114,7 +118,10 @@ export class Group extends Resource {
|
|||
})
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
toJSON():
|
||||
& ResourceJson<Group, 'permissions'>
|
||||
& { posix: Posix | null }
|
||||
& ResourceRefJson<Group, Group, 'groups'> {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
|
|
|
@ -2,7 +2,11 @@ import type { ToJson, UrlString } from '../../../types.ts'
|
|||
import { Avatar } from '../utils/avatar.ts'
|
||||
import { Ref } from '../utils/ref.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 {
|
||||
static fromJSON(
|
||||
|
@ -108,7 +112,9 @@ export class Machine extends Resource {
|
|||
})
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
toJSON():
|
||||
& ResourceJson<Machine, 'tags' | 'url' | 'status'>
|
||||
& ResourceRefJson<Machine, Group, 'groups'> {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
|
|
|
@ -95,7 +95,10 @@ export class Resource {
|
|||
})
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
toJSON(): ResourceJson<
|
||||
Resource,
|
||||
'type' | 'uuid' | 'name' | 'avatar' | 'createdAt' | 'updatedAt'
|
||||
> {
|
||||
return {
|
||||
type: this.type,
|
||||
uuid: this.uuid,
|
||||
|
@ -123,3 +126,24 @@ export interface Resource {
|
|||
createdAt: 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']>[]>
|
||||
}
|
||||
|
|
|
@ -2,7 +2,11 @@ import type { ToJson, UrlString } from '../../../types.ts'
|
|||
import { Avatar } from '../utils/avatar.ts'
|
||||
import { Ref } from '../utils/ref.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 {
|
||||
static fromJSON(
|
||||
|
@ -126,7 +130,9 @@ export class Service extends Resource {
|
|||
})
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
toJSON():
|
||||
& ResourceJson<Service, 'category' | 'tags' | 'url'>
|
||||
& ResourceRefJson<Service, Group, 'groups'> {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
|
|
|
@ -4,7 +4,11 @@ import { Avatar } from '../utils/avatar.ts'
|
|||
import { Ref } from '../utils/ref.ts'
|
||||
import type { Credential, CredentialCategory } from './credential.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 {
|
||||
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 {
|
||||
...super.toJSON(),
|
||||
type: this.type,
|
||||
lastname: this.lastname,
|
||||
firstname: this.firstname,
|
||||
login: this.login,
|
||||
|
|
Loading…
Reference in a new issue