Compare commits

...

23 commits

Author SHA1 Message Date
Julien Oculi 28a517323b feat(model): add new static method load to create ressources from partial entry 2024-06-18 14:05:45 +02:00
Julien Oculi 873f4d23dc feat(chore): add avatar utility class 2024-06-18 14:04:38 +02:00
Julien Oculi 3e3fd59fe7 fix(model): 🐛 wrong definition for static method create 2024-06-18 13:52:24 +02:00
Julien Oculi 5ecebbec14 refactor(model): ♻️ move avatar attribute to Ressource base model 2024-06-18 13:35:49 +02:00
Julien Oculi 1fb1f1ce15 refactor(model): 🏷️ force type casting for toRef method 2024-06-18 11:49:28 +02:00
Julien Oculi aefdacbf59 refactor: ♻️ fix types and update import to import type 2024-06-18 10:29:21 +02:00
Julien Oculi 2028c0134a fix(db): 🏷️ update types to allow async filter for listing ressources 2024-06-18 10:14:22 +02:00
Julien Oculi 3b378b0968 chore(chore): 🙈 untrack sqlite artifacts and tmp test directories 2024-06-18 10:12:18 +02:00
Julien Oculi 8214dcd625 feat(db): allow async filter for ressource listing 2024-06-18 10:09:29 +02:00
Julien Oculi fa88ab2f68 refactor(db): ♻️ update types to fit recents commits 2024-06-18 10:07:45 +02:00
Julien Oculi 626bc3c0f6 feat(chore): add global module exports 2024-06-17 18:41:05 +02:00
Julien Oculi ba678692a5 refactor(chore): ♻️ update import to import type 2024-06-17 18:40:38 +02:00
Julien Oculi 9520d222ae refactor(model): ♻️ update references to credentials 2024-06-17 18:38:57 +02:00
Julien Oculi a5b31dd967 feat(model): refactor Credential model and add Passkey credential store type 2024-06-17 18:31:28 +02:00
Julien Oculi f0899797d8 refactor(model): ♻️ refactor Machine model to remove URL class attributes 2024-06-17 18:15:47 +02:00
Julien Oculi 6d08698d10 feat(model): refactor Group model and add groups attributes for inheritance 2024-06-17 18:10:07 +02:00
Julien Oculi fda7b03e59 feat(model): refactor and add tags to Service model 2024-06-17 18:00:28 +02:00
Julien Oculi 42006cc18e refactor(model): ♻️ update url string type to be more specific 2024-06-17 17:08:04 +02:00
Julien Oculi 33868a3995 chore(model): 🏷️ add meaningful string type aliases 2024-06-17 17:03:39 +02:00
Julien Oculi cbd14996d4 refactor(model): ♻️ use import type instead of import when possible 2024-06-17 13:49:42 +02:00
Julien Oculi 92a6ae01a3 chore(chore): 🔧 add package version and exports 2024-06-17 13:46:10 +02:00
Julien Oculi 98444ea685 chore: 🔧 update vscode commit scopes 2024-06-17 13:41:21 +02:00
Julien Oculi 3c773bca6b refactor(model): ♻️ rename type string to Base64String where is meaningful 2024-06-17 13:40:18 +02:00
15 changed files with 371 additions and 149 deletions

4
.gitignore vendored
View file

@ -1,6 +1,8 @@
# Directories
docs
tmp
temp
# Files
.env
*.sqlite
*.sqlite*

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"conventionalCommits.scopes": ["model", "chore", "db"]
}

View file

@ -1,5 +1,10 @@
{
"name": "@cohabit/ressources",
"version": "0.1.0",
"exports": {
".": "./mod.ts",
"./models": "./src/models/mod.ts"
},
"tasks": {
"start": "deno run --unstable-kv ./mod.ts"
},

2
mod.ts Normal file
View file

@ -0,0 +1,2 @@
export * from '@models'
export { Db } from '@db'

View file

@ -1,4 +1,12 @@
import { Credential, Group, Machine, Ressource, Service, User } from '@models'
import {
Credential,
Group,
Machine,
type Ressource,
Service,
User,
} from '@models'
import type { CredentialCategory } from '@models/credential.ts'
//!TODO link ressources (get, list)
//!TODO Purge unused ressources (delete)
@ -26,7 +34,10 @@ export class Db {
get ressource() {
return {
credential: this.#ressourceStorage<Credential>('credential', Credential),
credential: this.#ressourceStorage<Credential<CredentialCategory>>(
'credential',
Credential,
),
group: this.#ressourceStorage<Group>('group', Group),
machine: this.#ressourceStorage<Machine>('machine', Machine),
service: this.#ressourceStorage<Service>('service', Service),
@ -44,8 +55,9 @@ export class Db {
set: (ressources: T[]) => this.#set<T>(type, ressources),
delete: (ressources: Pick<T, 'uuid'>[]) =>
this.#delete<T>(type, ressources),
list: (filter: (ressource: T) => boolean = () => true) =>
this.#list<T>(type, Builder, filter),
list: (
filter: (ressource: T) => boolean | Promise<boolean> = () => true,
) => this.#list<T>(type, Builder, filter),
}
}
@ -94,7 +106,7 @@ export class Db {
async *#list<T extends Ressource>(
type: RessourceType<T>,
Builder: RessourceBuilder<T>,
filter: (entry: T) => boolean,
filter: (entry: T) => boolean | Promise<boolean>,
): AsyncGenerator<T, void, void> {
const list = this.#kv.list<RessourceJson<T>>({
prefix: [this.prefix.ressource, type],
@ -103,22 +115,23 @@ export class Db {
const value = entry.value
//@ts-expect-error Type union of Ressource types for Builder
const ressource = Builder.fromJSON(value) as T
if (filter(ressource)) {
if (await filter(ressource)) {
yield ressource
}
}
}
}
type RessourceType<T extends Ressource> = T extends Credential ? 'credential'
type RessourceType<T extends Ressource> = T extends
Credential<CredentialCategory> ? 'credential'
: T extends Group ? 'group'
: T extends Machine ? 'machine'
: T extends Service ? 'service'
: T extends User ? 'user'
: never
type RessourceBuilder<T extends Ressource> = T extends Credential
? typeof Credential
type RessourceBuilder<T extends Ressource> = T extends
Credential<CredentialCategory> ? typeof Credential
: T extends Group ? typeof Group
: T extends Machine ? typeof Machine
: T extends Service ? typeof Service

View file

@ -1,27 +1,54 @@
import { ToJson } from '@/types.ts'
import type { Base64String, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts'
import type { Ref } from '@models'
import { Avatar } from '@/src/models/utils/avatar.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 {
const { uuid, createdAt, updatedAt } = super.create({ name })
return new Credential({ uuid, createdAt, updatedAt, name, category, store })
static create<T extends CredentialCategory>(
{ category, store, name, avatar }: Pick<
Credential<T>,
'category' | 'store' | 'name' | 'avatar'
>,
): Credential<T> {
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
return new Credential({
uuid,
createdAt,
updatedAt,
name,
category,
store,
avatar,
})
}
#category: 'password' | 'ssh' | 'passkey'
#store: CredentialCategory<Credential['category']>
static load<T extends CredentialCategory>({
category,
store,
name,
avatar = Avatar.fromEmoji('🔑'),
}: {
name: Credential<T>['name']
category: T
store: Credential<T>['store']
avatar?: Credential<T>['avatar']
}): Credential<T> {
return this.create({ category, store, name, avatar })
}
#category: T
#store: Readonly<CredentialStore<T>>
private constructor(
{ category, store, ...props }:
& Pick<Credential, 'category' | 'store'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
& Pick<Credential<T>, 'category' | 'store'>
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -43,14 +70,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
}
@ -66,40 +93,55 @@ export class Credential extends Ressource {
toString(): string {
return `Credential (${JSON.stringify(this)})`
}
toRef(): Ref<Credential<T>> {
return super.toRef() as Ref<Credential<T>>
}
}
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' ? {
export type CredentialCategory = 'password' | 'ssh' | 'passkey'
export type CredentialStore<T extends CredentialCategory> = T extends 'password'
? {
store: {
hash: string //hex or b64 of Uint8Array
hash: Base64String
alg: string
salt: string //hex or b64 of Uint8Array
salt: Base64String
}
}
: T extends 'ssh' ? {
store: {
publicKey: string
publicKey: Base64String
}
}
: T extends 'passkey' ? {
store: Record<string, unknown>
store: Passkey
}
: never
/*
PassKey store:
{
publicKey: Uint8Array
keyId: string
transport: string
counter: number
/** Passkey store */
export type Passkey = {
/** User UUID */
user: UUID
/** WebAuthn registration key id */
webAuthnUserID: string
/** Passkey credential unique id */
id: string
/** Passkey user public key */
publicKey: Base64String
/** Number of times the authenticator has been used */
counter: number
/** Whether the passkey is single-device or multi-device */
deviceType: 'singleDevice' | 'multiDevice'
/** Whether the passkey has been backed up in some way */
backedUp: boolean
/** Passkey physical transport layer */
transports?:
('ble' | 'cable' | 'hybrid' | 'internal' | 'nfc' | 'smart-card' | 'usb')[]
}
*/
//new Uint8Array(Object.values(JSON.parse(JSON.stringify(new Uint8Array([1, 2, 3])))))

View file

@ -1,33 +1,68 @@
import { Posix, ToJson, UUID } from '@/types.ts'
import type { Posix, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts'
import { Ref } from '@models'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Group extends Ressource {
static fromJSON(
{ posix, ...json }: ToJson<Group>,
{ posix, groups, ...json }: ToJson<Group>,
): Group {
return new Group({ ...json, posix: posix ?? undefined })
return new Group({
...json,
posix: posix ?? undefined,
groups: groups.map((group) => Ref.fromString<Group>(group)),
})
}
static create(
{ posix, permissions, name }: Pick<Group, 'posix' | 'permissions' | 'name'>,
{ posix, permissions, name, avatar, groups }: Pick<
Group,
'posix' | 'permissions' | 'name' | 'avatar' | 'groups'
>,
): Group {
const { uuid, createdAt, updatedAt } = super.create({ name })
return new Group({ uuid, createdAt, updatedAt, name, posix, permissions })
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
return new Group({
uuid,
avatar,
createdAt,
updatedAt,
name,
posix,
permissions,
groups,
})
}
static load({
name,
groups = [],
permissions = {},
posix,
avatar = Avatar.fromEmoji('👥'),
}: {
name: Group['name']
permissions?: Group['permissions']
posix?: Group['posix']
avatar?: Group['avatar']
groups?: Group['groups']
}): Group {
return this.create({ name, groups, permissions, posix, avatar })
}
#posix?: Posix
#permissions: {
#permissions: Readonly<{
[serviceOrMachine: UUID]: {
read: boolean
write: boolean
execute: boolean
}
} = {}
}>
#groups: readonly Ref<Group>[]
private constructor(
{ posix, permissions, ...props }:
& Pick<Group, 'posix' | 'permissions'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
{ posix, permissions, groups, ...props }:
& Pick<Group, 'posix' | 'permissions' | 'groups'>
& Pick<Ressource, 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -50,7 +85,8 @@ export class Group extends Ressource {
this.#posix = posix
}
this.permissions = Object.freeze(permissions)
this.#permissions = Object.freeze(permissions)
this.#groups = Object.freeze(groups)
}
get type(): 'group' {
@ -62,7 +98,11 @@ export class Group extends Ressource {
}
get permissions() {
return Object.freeze(this.#permissions)
return this.#permissions
}
get groups() {
return this.#groups
}
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
@ -77,17 +117,23 @@ export class Group extends Ressource {
type: this.type,
posix: this.posix ?? null,
permissions: this.permissions,
groups: this.groups.map((group) => group.toJSON()),
} as const
}
toString(): string {
return `Group (${JSON.stringify(this)})`
}
toRef(): Ref<Group> {
return super.toRef() as Ref<Group>
}
}
export interface Group extends Ressource {
type: 'group'
posix: Posix | undefined
groups: readonly Ref<Group>[]
permissions: Readonly<{
[serviceOrMachine: UUID]: {
read: boolean

View file

@ -1,29 +1,58 @@
import { ToJson } from '@/types.ts'
import { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts'
import { Ref } from '@/src/models/utils/ref.ts'
import type { ToJson, UrlString } from '@/types.ts'
import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Machine extends Ressource {
static fromJSON(
json: ToJson<Machine>,
{ url, ...json }: ToJson<Machine>,
): Machine {
const url = new URL(json.url)
const avatar = new URL(json.avatar)
const groups = json.groups.map((group) => Ref.fromString<Group>(group))
return new Machine({ ...json, url, avatar, groups })
return new Machine({ ...json, url, groups })
}
static create(
{ tags, url, status, avatar, groups, ...props }:
& Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
{ tags, url, status, groups, avatar }: Pick<
Machine,
'name' | 'tags' | 'url' | 'status' | 'avatar' | 'groups'
>,
): Machine {
return new Machine({ ...props, tags, url, status, avatar, groups })
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
return new Machine({
uuid,
createdAt,
updatedAt,
avatar,
name,
tags,
url,
status,
groups,
})
}
static load({
name,
avatar = Avatar.fromEmoji('🖨️'),
tags = [],
url,
status = 'unknown',
groups = [],
}: {
name: Machine['name']
avatar?: Machine['avatar']
tags?: Machine['tags']
url: Machine['url']
status?: Machine['status']
groups?: Machine['groups']
}): Machine {
return this.create({ name, avatar, tags, url, status, groups })
}
#tags: readonly string[]
#url: URL
#url: UrlString
#status:
| 'ready'
| 'busy'
@ -31,25 +60,23 @@ export class Machine extends Ressource {
| 'discontinued'
| 'error'
| 'unknown'
#avatar: URL
#groups: readonly Ref<Group>[]
private constructor(
{ tags, url, status, avatar, groups, ...props }:
{ tags, url, status, groups, ...props }:
& Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
) {
super(props)
this.#tags = Object.freeze(tags)
this.#url = new URL(url)
this.#url = new URL(url).href as UrlString
if (!['working', 'pending', 'ready', 'unavailable'].includes(status)) {
throw new TypeError(
`status is "${status}" but ('ready' | 'busy' | 'unavailable' | 'discontinued' | 'error' | 'unknown') is required`,
)
}
this.#status = status
this.#avatar = new URL(avatar)
this.#groups = Object.freeze(groups)
}
@ -57,17 +84,14 @@ export class Machine extends Ressource {
return 'machine'
}
get tags() {
return this.#tags.slice()
return this.#tags
}
get url() {
return new URL(this.#url)
return this.#url
}
get status() {
return this.#status
}
get avatar() {
return new URL(this.#avatar)
}
get groups() {
return this.#groups
}
@ -84,10 +108,9 @@ export class Machine extends Ressource {
return {
...super.toJSON(),
type: this.type,
tags: this.tags.slice(),
url: this.url.toJSON(),
tags: this.tags,
url: this.url,
status: this.status,
avatar: this.avatar.toJSON(),
groups: Object.freeze(this.groups.map((group) => group.toJSON())),
} as const
}
@ -95,12 +118,16 @@ export class Machine extends Ressource {
toString(): string {
return `Machine (${JSON.stringify(this)})`
}
toRef(): Ref<Machine> {
return super.toRef() as Ref<Machine>
}
}
export interface Machine extends Ressource {
type: 'machine'
tags: string[]
url: URL
tags: readonly string[]
url: UrlString
status:
| 'ready'
| 'busy'
@ -108,6 +135,5 @@ export interface Machine extends Ressource {
| 'discontinued'
| 'error'
| 'unknown'
avatar: URL
groups: readonly Ref<Group>[]
}

View file

@ -1,5 +1,5 @@
import { Ref } from '@/src/models/utils/ref.ts'
import { DateString, ToJson, UUID } from '@/types.ts'
import type { DateString, ToJson, UrlString, UUID } from '@/types.ts'
import { regex } from '@/utils.ts'
export class Ressource {
@ -8,29 +8,37 @@ export class Ressource {
): Ressource {
return new Ressource({
name: json.name,
avatar: json.avatar,
uuid: json.uuid,
createdAt: json.createdAt,
updatedAt: json.updatedAt,
})
}
static create({ name }: Pick<Ressource, 'name'>): Ressource {
static create(
{ name, avatar }: Pick<Ressource, 'name' | 'avatar'>,
): Ressource {
const uuid = crypto.randomUUID() as UUID
const createdAt = new Date().toISOString() as DateString
const updatedAt = createdAt
return new Ressource({ name, uuid, createdAt, updatedAt })
return new Ressource({ name, avatar, uuid, createdAt, updatedAt })
}
static load(_options: never): Ressource {
throw new Error('not implemented')
}
#name: string
#uuid: UUID
#createdAt: DateString
#updatedAt: DateString
#avatar: UrlString
protected constructor(
{ name, uuid, createdAt, updatedAt }: Pick<
{ name, avatar, uuid, createdAt, updatedAt }: Pick<
Ressource,
'name' | 'uuid' | 'createdAt' | 'updatedAt'
'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'
>,
) {
this.#name = name
@ -39,6 +47,7 @@ export class Ressource {
`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(
@ -63,6 +72,9 @@ export class Ressource {
get name(): string {
return this.#name
}
get avatar(): UrlString {
return this.#avatar
}
get createdAt(): DateString {
return this.#createdAt
}
@ -83,6 +95,7 @@ export class Ressource {
type: this.type,
uuid: this.uuid,
name: this.name,
avatar: this.avatar,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
} as const
@ -101,6 +114,7 @@ export interface Ressource {
type: 'user' | 'machine' | 'service' | 'group' | 'credential'
uuid: UUID
name: string
avatar: UrlString
createdAt: DateString
updatedAt: DateString
}

View file

@ -1,27 +1,26 @@
import { ToJson } from '@/types.ts'
import { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts'
import { Ref } from '@/src/models/utils/ref.ts'
import type { ToJson, UrlString } from '@/types.ts'
import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Service extends Ressource {
static fromJSON(
{ groups, url, avatar, ...json }: ToJson<Service>,
{ groups, ...json }: ToJson<Service>,
): Service {
return new Service({
groups: groups.map((group) => Ref.fromString<Group>(group)),
url: new URL(url),
avatar: new URL(avatar),
...json,
})
}
static create(
{ category, url, groups, avatar, name }: Pick<
{ category, tags, url, groups, avatar, name }: Pick<
Service,
'category' | 'url' | 'groups' | 'avatar' | 'name'
'category' | 'tags' | 'url' | 'groups' | 'avatar' | 'name'
>,
): Service {
const { uuid, createdAt, updatedAt } = super.create({ name })
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
return new Service({
uuid,
createdAt,
@ -29,21 +28,40 @@ export class Service extends Ressource {
name,
category,
url,
tags,
groups,
avatar,
})
}
#category: 'bin' | 'web' | 'fs' | 'git'
#url: URL
static load({
name,
avatar = Avatar.fromEmoji('⚙️'),
tags = [],
url,
category,
groups = [],
}: {
name: Service['name']
url: Service['url']
avatar?: Service['avatar']
tags?: Service['tags']
category: Service['category']
groups?: Service['groups']
}): Service {
return this.create({ name, avatar, tags, url, category, groups })
}
#category: 'web' | 'fs' | 'git'
#url: UrlString
#groups: readonly Ref<Group>[]
#avatar: URL
#tags: readonly string[]
private constructor({
category,
tags,
url,
groups,
avatar,
...ressource
}: Pick<
Service,
@ -53,20 +71,21 @@ export class Service extends Ressource {
| 'name'
| 'category'
| 'url'
| 'tags'
| 'groups'
| 'avatar'
>) {
super(ressource)
if (!['bin', 'web', 'fs', 'git'].includes(category)) {
if (!['web', 'fs', 'git'].includes(category)) {
throw new TypeError(
`category is "${category}" but ('bin' | 'web' | 'fs' | 'git') is required`,
`category is "${category}" but ('web' | 'fs' | 'git') is required`,
)
}
this.#category = category
this.#url = url
this.#url = new URL(url).href as UrlString
this.#groups = Object.freeze(groups)
this.#avatar = avatar
this.#tags = Object.freeze(tags)
}
get type(): 'service' {
@ -77,16 +96,16 @@ export class Service extends Ressource {
return this.#category
}
get tags() {
return this.#tags
}
get url() {
return new URL(this.#url)
return this.#url
}
get groups() {
return Object.freeze(this.#groups)
}
get avatar() {
return new URL(this.#avatar)
return this.#groups
}
update(
@ -102,21 +121,25 @@ export class Service extends Ressource {
...super.toJSON(),
type: this.type,
category: this.category,
url: this.url.toJSON(),
tags: this.tags,
url: this.url,
groups: this.groups.map((group) => group.toJSON()),
avatar: this.avatar.toJSON(),
} as const
}
toString(): string {
return `Service (${JSON.stringify(this)})`
}
toRef(): Ref<Service> {
return super.toRef() as Ref<Service>
}
}
export interface Service extends Ressource {
type: 'service'
category: 'bin' | 'web' | 'fs' | 'git'
url: URL
category: 'web' | 'fs' | 'git'
tags: readonly string[]
url: UrlString
groups: readonly Ref<Group>[]
avatar: URL
}

View file

@ -1,14 +1,13 @@
import { Login, MailAddress, Posix, ToJson } from '@/types.ts'
import { regex, toLogin } from '@/utils.ts'
import { Credential } from '@models/credential.ts'
import { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts'
import { Ref } from '@/src/models/utils/ref.ts'
import type { Login, MailAddress, Posix, ToJson } from '@/types.ts'
import { regex, toLogin } from '@/utils.ts'
import type { Credential, CredentialCategory } from '@models/credential.ts'
import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class User extends Ressource {
static fromJSON(
json: ToJson<User>,
): User {
static fromJSON(json: ToJson<User>): User {
if (json.type !== 'user') {
throw new TypeError(`type is "${json.type}" but "user" is required`)
}
@ -22,7 +21,10 @@ export class User extends Ressource {
`firstname type is "${typeof json.firstname}" but "string" is required`,
)
}
if (typeof json.login !== 'string' && /(\w|_)+\.(\w|_)+/.test(json.login)) {
if (
typeof json.login !== 'string' &&
/(\w|_)+\.(\w|_)+/.test(json.login)
) {
throw new TypeError(
`login is "${json.login}" but "\${string}.\${string}" is required`,
)
@ -35,20 +37,20 @@ export class User extends Ressource {
const credentials = Object.freeze(
json.credentials.map((credential) =>
Ref.fromString<Credential>(credential)
Ref.fromString<Credential<CredentialCategory>>(credential)
),
)
const groups = Object.freeze(
json.groups.map((group) => Ref.fromString<Group>(group)),
)
const avatar = new URL(json.avatar)
const posix = json.posix ?? undefined
return new User({ ...json, posix, credentials, groups, avatar })
return new User({ ...json, posix, credentials, groups })
}
static create({
name,
avatar,
...props
}: Pick<
User,
@ -61,8 +63,38 @@ export class User extends Ressource {
| 'avatar'
| 'credentials'
>): User {
const { uuid, createdAt, updatedAt } = super.create({ name })
return new User({ name, uuid, createdAt, updatedAt, ...props })
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
return new User({ name, avatar, uuid, createdAt, updatedAt, ...props })
}
static load({
lastname,
firstname,
mail,
groups = [],
posix,
avatar = Avatar.fromEmoji('👤'),
credentials = [],
}: {
lastname: User['lastname']
firstname: User['firstname']
mail: User['mail']
groups?: User['groups']
posix?: User['posix']
avatar?: User['avatar']
credentials?: User['credentials']
}): User {
const name = `${firstname} ${lastname}`
return this.create({
name,
lastname,
firstname,
mail,
groups,
posix,
avatar,
credentials,
})
}
#lastname: string
@ -71,8 +103,7 @@ export class User extends Ressource {
#mail: MailAddress
#groups: readonly Ref<Group>[]
#posix?: Posix
#avatar: URL
#credentials: readonly Ref<Credential>[]
#credentials: readonly Ref<Credential<CredentialCategory>>[]
private constructor({
lastname,
@ -80,7 +111,6 @@ export class User extends Ressource {
mail,
groups,
posix,
avatar,
credentials,
...ressource
}: Pick<
@ -113,7 +143,6 @@ export class User extends Ressource {
}
}
this.#posix = posix
this.#avatar = avatar
this.#login = toLogin({ firstname, lastname })
this.#credentials = Object.freeze(credentials)
}
@ -139,9 +168,6 @@ export class User extends Ressource {
get posix() {
return this.#posix
}
get avatar() {
return this.#avatar
}
get credentials() {
return this.#credentials
}
@ -161,7 +187,7 @@ export class User extends Ressource {
mail: this.mail,
groups: this.groups.map((group) => group.toJSON()),
posix: this.posix ?? null,
avatar: this.avatar.toJSON(),
avatar: this.avatar,
credentials: this.credentials.map((credential) => credential.toJSON()),
} as const
}
@ -169,6 +195,10 @@ export class User extends Ressource {
toString(): string {
return `User (${JSON.stringify(this)})`
}
toRef(): Ref<User> {
return super.toRef() as Ref<User>
}
}
export interface User extends Ressource {
@ -179,6 +209,5 @@ export interface User extends Ressource {
mail: MailAddress
groups: readonly Ref<Group>[]
posix: Posix | undefined
avatar: URL
credentials: readonly Ref<Credential>[]
credentials: readonly Ref<Credential<CredentialCategory>>[]
}

View file

@ -0,0 +1,8 @@
import type { UrlString } from '@/types.ts'
export class Avatar {
static fromEmoji(emoji: string): UrlString {
return `data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%2210 0 100 100%22><text y=%22.90em%22 font-size=%2290%22>${emoji}</text></svg>`
}
private constructor() {}
}

View file

@ -1,4 +1,4 @@
import { Db } from '@/mod.ts'
import type { Db } from '@/mod.ts'
import type { UUID } from '@/types.ts'
import {
Credential,
@ -38,8 +38,9 @@ export class Ref<T extends Ressource> extends String {
}
static dbResolver(db: Db) {
return <T extends Ressource>(ref: RefString<T>) => {
return <T extends Ressource>(ref: RefString<T>): Promise<T> => {
const { type, uuid } = Ref.parse(ref)
//@ts-expect-error force type casting to fix
return db.ressource[type].get({ uuid })
}
}

View file

@ -26,3 +26,11 @@ export type Posix = {
home: string
shell: string
}
export type Base64String = `${string}`
export type UrlString = `${
| 'data:'
| 'file://'
| 'http://'
| 'https://'
| 'git:'}${string}`

View file

@ -1,4 +1,4 @@
import { Login } from '@/types.ts'
import type { Login } from '@/types.ts'
export function toLogin({
firstname,