Skip to main content
Home
Works with
This package works with Deno
This package works with Deno
JSR Score52%
Downloads13/wk
Published2 years ago (0.2.1)

Système de gestion des ressources de cohabit.

import type { DateString, ToJson, UrlString, UUID } from '../../../types.ts' import { regex } from '../../../utils.ts' import { Ref } from '../utils/ref.ts' export class Resource { static fromJSON( json: ToJson<Resource>, ): Resource { return new Resource({ name: json.name, avatar: json.avatar, uuid: json.uuid, createdAt: json.createdAt, updatedAt: json.updatedAt, }) } static create( { name, avatar }: Pick<Resource, 'name' | 'avatar'>, ): Resource { const uuid = crypto.randomUUID() as UUID const createdAt = new Date().toISOString() as DateString const updatedAt = createdAt return new Resource({ name, avatar, uuid, createdAt, updatedAt }) } static load(_options: never): Resource { throw new Error('not implemented') } #name: string #uuid: UUID #createdAt: DateString #updatedAt: DateString #avatar: UrlString protected constructor( { name, avatar, uuid, createdAt, updatedAt }: Pick< Resource, 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt' >, ) { this.#name = name if (!regex.uuidV4.test(uuid)) { throw new SyntaxError( `the following uuid "${uuid}" is not a valid UUID V4`, ) } this.#avatar = new URL(avatar).href as UrlString this.#uuid = uuid if (!regex.isoDateString.test(createdAt)) { throw new SyntaxError( `the following create date "${createdAt}" is not a valid UTC ISO string`, ) } this.#createdAt = createdAt if (!regex.isoDateString.test(updatedAt)) { throw new SyntaxError( `the following update date "${updatedAt}" is not a valid UTC ISO string`, ) } this.#updatedAt = updatedAt } get type(): 'user' | 'machine' | 'service' | 'group' | 'credential' { throw new Error('ressource super class has no type') } get uuid(): UUID { return this.#uuid } get name(): string { return this.#name } get avatar(): UrlString { return this.#avatar } get createdAt(): DateString { return this.#createdAt } get updatedAt(): DateString { return this.#updatedAt } update( props: Partial<Omit<Resource, 'type' | 'uuid' | 'createdAt'>>, ): Resource { return new Resource({ name: this.name, avatar: this.avatar, uuid: this.uuid, createdAt: this.createdAt, updatedAt: new Date().toISOString() as DateString, ...props, }) } toJSON(): ResourceJson< Resource, 'type' | 'uuid' | 'name' | 'avatar' | 'createdAt' | 'updatedAt' > { return { type: this.type, uuid: this.uuid, name: this.name, avatar: this.avatar, createdAt: this.createdAt, updatedAt: this.updatedAt, } as const } toString(): string { return `Ressource (${JSON.stringify(this)})` } toRef(): Ref<Resource> { return Ref.fromResource(this) } } export interface Resource { type: 'user' | 'machine' | 'service' | 'group' | 'credential' uuid: UUID name: string avatar: UrlString 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']>[]> }