From a5b31dd96745db32e075bc14ce49f27abc40694f Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Mon, 17 Jun 2024 18:31:28 +0200 Subject: [PATCH] feat(model): :sparkles: refactor `Credential` model and add `Passkey` credential store type --- src/models/src/credential.ts | 44 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/models/src/credential.ts b/src/models/src/credential.ts index ef092a9..d014ec7 100644 --- a/src/models/src/credential.ts +++ b/src/models/src/credential.ts @@ -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 { +export class Credential extends Ressource { + static fromJSON( + json: ToJson>, + ): Credential { return new Credential(json) } - static create( - { category, store, name }: Pick, - ): Credential { + static create( + { category, store, name }: Pick< + Credential, + 'category' | 'store' | 'name' + >, + ): Credential { const { uuid, createdAt, updatedAt } = super.create({ name }) return new Credential({ uuid, createdAt, updatedAt, name, category, store }) } - #category: 'password' | 'ssh' | 'passkey' - #store: CredentialCategory + #category: T + #store: Readonly> private constructor( { category, store, ...props }: - & Pick + & Pick, 'category' | 'store'> & Pick, ) { super(props) @@ -43,14 +46,14 @@ export class Credential extends Ressource { } get store() { - return Object.freeze(this.#store) + return this.#store } - update( - props: Partial>, - ): Credential { + update( + props: Partial, 'type' | 'uuid' | 'createdAt'>>, + ): Credential { const { updatedAt } = super.update(props) - const credential = new Credential({ ...this, ...props, updatedAt }) + const credential = new Credential({ ...this, ...props, updatedAt }) return credential } @@ -68,14 +71,15 @@ export class Credential extends Ressource { } } -export interface Credential extends Ressource { +export interface Credential extends Ressource { type: 'credential' - category: 'password' | 'ssh' | 'passkey' - store: Readonly> + category: T + store: Readonly> } -type CredentialCategory = T extends - 'password' ? { +type CredentialCategory = 'password' | 'ssh' | 'passkey' + +type CredentialStore = T extends 'password' ? { store: { hash: Base64String alg: string