feat(model): refactor Credential model and add Passkey credential store type

This commit is contained in:
Julien Oculi 2024-06-17 18:31:28 +02:00
parent f0899797d8
commit a5b31dd967

View file

@ -1,26 +1,29 @@
import type { Base64String, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts'
export class Credential extends Ressource {
static fromJSON(
json: ToJson<Credential>,
): Credential {
export class Credential<T extends CredentialCategory> extends Ressource {
static fromJSON<T extends CredentialCategory>(
json: ToJson<Credential<T>>,
): Credential<T> {
return new Credential(json)
}
static create(
{ category, store, name }: Pick<Credential, 'category' | 'store' | 'name'>,
): Credential {
static create<T extends CredentialCategory>(
{ category, store, name }: Pick<
Credential<T>,
'category' | 'store' | 'name'
>,
): Credential<T> {
const { uuid, createdAt, updatedAt } = super.create({ name })
return new Credential({ uuid, createdAt, updatedAt, name, category, store })
}
#category: 'password' | 'ssh' | 'passkey'
#store: CredentialCategory<Credential['category']>
#category: T
#store: Readonly<CredentialStore<T>>
private constructor(
{ category, store, ...props }:
& Pick<Credential, 'category' | 'store'>
& Pick<Credential<T>, 'category' | 'store'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -43,14 +46,14 @@ export class Credential extends Ressource {
}
get store() {
return Object.freeze(this.#store)
return this.#store
}
update(
props: Partial<Omit<Credential, 'type' | 'uuid' | 'createdAt'>>,
): Credential {
update<T extends CredentialCategory>(
props: Partial<Omit<Credential<T>, 'type' | 'uuid' | 'createdAt'>>,
): Credential<T> {
const { updatedAt } = super.update(props)
const credential = new Credential({ ...this, ...props, updatedAt })
const credential = new Credential<T>({ ...this, ...props, updatedAt })
return credential
}
@ -68,14 +71,15 @@ export class Credential extends Ressource {
}
}
export interface Credential extends Ressource {
export interface Credential<T extends CredentialCategory> extends Ressource {
type: 'credential'
category: 'password' | 'ssh' | 'passkey'
store: Readonly<CredentialCategory<Credential['category']>>
category: T
store: Readonly<CredentialStore<T>>
}
type CredentialCategory<T extends 'password' | 'ssh' | 'passkey'> = T extends
'password' ? {
type CredentialCategory = 'password' | 'ssh' | 'passkey'
type CredentialStore<T extends CredentialCategory> = T extends 'password' ? {
store: {
hash: Base64String
alg: string