Compare commits

..

No commits in common. "main" and "0.1.1" have entirely different histories.
main ... 0.1.1

21 changed files with 243 additions and 576 deletions

View file

@ -1 +0,0 @@
DB_PATH = './db.sqlite'

View file

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

View file

@ -1,3 +1,3 @@
# KM - Coh@bit resources manager
# Coh@bit ressources
Système de gestion des ressources de cohabit.
Server de gestion des ressources de cohabit.

View file

@ -1,13 +1,12 @@
{
"name": "@cohabit/resources-manager",
"version": "0.2.1",
"name": "@cohabit/ressources",
"version": "0.1.1",
"exports": {
"./models": "./src/models/mod.ts",
"./db": "./src/db/mod.ts",
"./types": "./types.ts"
".": "./mod.ts",
"./models": "./src/models/mod.ts"
},
"tasks": {
"start": "deno run ./mod.ts"
"start": "deno run --unstable-kv ./mod.ts"
},
"fmt": {
"singleQuote": true,
@ -15,11 +14,16 @@
"semiColons": false
},
"imports": {
"@/": "./",
"@models": "./src/models/mod.ts",
"@models/": "./src/models/src/",
"@api": "./src/api/server.ts",
"@api/": "./src/api/",
"@db": "./src/db/mod.ts",
"@db/": "./src/db/",
"@std/json": "jsr:@std/json@^0.223.0"
},
"exclude": [
"./docs",
"./tmp"
],
"unstable": ["kv"]
"./docs"
]
}

View file

@ -4,7 +4,6 @@
"specifiers": {
"jsr:@std/json@^0.223.0": "jsr:@std/json@0.223.0",
"jsr:@std/streams@^0.223.0": "jsr:@std/streams@0.223.0",
"npm:iterator-polyfill": "npm:iterator-polyfill@1.0.9",
"npm:superjson@1.13.3": "npm:superjson@1.13.3"
},
"jsr": {
@ -29,10 +28,6 @@
"integrity": "sha512-ZhMwEosbFJkA0YhFnNDgTM4ZxDRsS6HqTo7qsZM08fehyRYIYa0yHu5R6mgo1n/8MgaPBXiPimPD77baVFYg+A==",
"dependencies": {}
},
"iterator-polyfill@1.0.9": {
"integrity": "sha512-YKBrosdKd9nqEaJwpYZjlnax8WtLwCbTo7gnUXrQ3ARA92vD2W8hdOWCRzvotHByDRCvPrFvEFF4M3V53lwAHQ==",
"dependencies": {}
},
"superjson@1.13.3": {
"integrity": "sha512-mJiVjfd2vokfDxsQPOwJ/PtanO87LhpYY88ubI5dUB1Ab58Txbyje3+jpm+/83R/fevaq/107NNhtYBLuoTrFg==",
"dependencies": {

2
mod.ts Normal file
View file

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

View file

@ -1,56 +0,0 @@
import type { Db } from '../db/mod.ts'
import type { Resource } from '../models/mod.ts'
import type { UUID } from '../../types.ts'
import type { ResourceBuilder, ResourceType } from './types.ts'
import { respondJson } from './utils.ts'
export async function resourceHandler<T extends Resource>(
type: ResourceType<T>,
Builder: ResourceBuilder<T>,
db: Db,
req: Request,
id?: string,
): Promise<Response> {
if (req.method === 'POST') {
try {
const json = await req.json() as T
try {
//@ts-expect-error Extends from Resource
const resource = Builder.create(json)
//@ts-expect-error not a generic
const result = await db.resource[type].set(resource)
if (result.ok) {
//@ts-expect-error generic to fix
return respondJson(resource)
} else {
return respondJson(new Error(`can't insert ${resource} in db`))
}
} catch (error) {
return respondJson(error)
}
} catch (error) {
return respondJson(error)
}
}
if (req.method === 'GET') {
if (id === undefined) {
return respondJson(new Error('missing "id" for "GET"'))
}
try {
const resource = await db.resource[type].get({ uuid: id as UUID })
//@ts-expect-error generic to fix
return respondJson(resource)
} catch {
return respondJson(
new Error(`can't find any ${type} with the current uuid ${id}`),
)
}
}
if (req.method === 'PATCH') {
throw new Error('not implemented')
}
if (req.method === 'DELETE') {
throw new Error('not implemented')
}
return respondJson(new Error(`method "${req.method}" is not allowed`))
}

View file

@ -1,81 +0,0 @@
// /(users|groups|machines)/:?id
import { stripTypeS } from '../api/utils.ts'
import type { Db } from '../db/mod.ts'
import { Credential, Group, Machine, Service, User } from '../models/mod.ts'
import type { CredentialCategory } from '../models/src/credential.ts'
import { resourceHandler } from './hander.ts'
export class Api {
static init(db: Db, { base = '/' }: { base: `/${string}` }) {
return new Api({ db, base })
}
#db: Db
#base: `/${string}`
private constructor({ db, base }: { db: Db; base: `/${string}` }) {
this.#db = db
this.#base = base
}
serve() {
return Deno.serve((req) => {
const url = new URL(req.url)
if (url.pathname.startsWith(this.#base)) {
const base = new URL(this.#base, req.url)
return router(req, base, this.#db)
}
return new Response(null, {
status: 301,
statusText: 'Not allowed base url',
})
})
}
}
function router(req: Request, base: URL, db: Db): Promise<Response> {
const pattern = new URLPattern({ pathname: '/:type/:id?' }, base.href)
const match = pattern.exec(req.url)
if (!match) {
return Promise.resolve(
new Response(null, {
status: 400,
statusText: 'Bad request',
}),
)
}
const { type, id } = match.pathname.groups as {
type: string
id: string | undefined
}
switch (type) {
case 'credentials':
return resourceHandler<Credential<CredentialCategory>>(
stripTypeS(type),
Credential,
db,
req,
id,
)
case 'groups':
return resourceHandler<Group>(stripTypeS(type), Group, db, req, id)
case 'machines':
return resourceHandler<Machine>(stripTypeS(type), Machine, db, req, id)
case 'services':
return resourceHandler<Service>(stripTypeS(type), Service, db, req, id)
case 'users':
return resourceHandler<User>(stripTypeS(type), User, db, req, id)
default:
return Promise.resolve(
new Response(null, {
status: 400,
statusText: 'Bad request',
}),
)
}
}

View file

@ -1,29 +0,0 @@
import type {
Credential,
Group,
Machine,
Resource,
Service,
User,
} from '../../src/models/mod.ts'
import type { JsonValue } from '@std/json'
import type { CredentialCategory } from '../models/src/credential.ts'
export type ResourceBuilder<T extends Resource> = T extends
Credential<CredentialCategory> ? typeof Credential
: T extends Group ? typeof Group
: T extends Machine ? typeof Machine
: T extends Service ? typeof Service
: T extends User ? typeof User
: never
export type ResourceType<T extends Resource> = T extends
Credential<CredentialCategory> ? 'credential'
: T extends Group ? 'group'
: T extends Machine ? 'machine'
: T extends Service ? 'service'
: T extends User ? 'user'
: never
export type JsonObject = Record<string, JsonValue>
export type JsonStringifiable = { toJSON(): JsonObject } | JsonObject

View file

@ -1,38 +0,0 @@
import type { JsonStringifiable } from '../api/types.ts'
export function respondJson<T extends JsonStringifiable | Error>(
valueOrError: T,
): Response {
if (valueOrError instanceof Error) {
const { name, cause, message } = valueOrError
return new Response(
JSON.stringify({
result: 'error',
error: { name, cause, message },
}),
{
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
status: 500,
statusText: valueOrError.message,
},
)
}
return new Response(
JSON.stringify({
result: 'ok',
value: valueOrError,
}),
{
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
},
)
}
export function stripTypeS<T extends string>(typeS: `${T}s`): T {
return typeS.slice(0, -1) as T
}

View file

@ -3,18 +3,18 @@ import {
Group,
Machine,
type Ref,
type Resource,
type Ressource,
Service,
User,
} from '../models/mod.ts'
import type { CredentialCategory } from '../models/src/credential.ts'
} from '@models'
import type { CredentialCategory } from '@models/credential.ts'
//!TODO link resources (get, list)
//!TODO Purge unused resources (delete)
//!TODO link ressources (get, list)
//!TODO Purge unused ressources (delete)
export class Db {
#kv: Deno.Kv
static async init(path?: string): Promise<Db> {
static async init(path?: string) {
const kv = await Deno.openKv(path ?? Deno.env.get('DB_PATH'))
return new Db(kv)
}
@ -23,123 +23,124 @@ export class Db {
this.#kv = kv
}
get storage(): Deno.Kv {
get storage() {
return this.#kv
}
get prefix(): { resource: 'resource' } {
get prefix() {
return {
resource: 'resource',
ressource: 'ressource',
}
}
get resource(): ResourceAccessors {
get ressource() {
return {
credential: this.#resourceStorage<Credential<CredentialCategory>>(
credential: this.#ressourceStorage<Credential<CredentialCategory>>(
'credential',
Credential,
),
group: this.#resourceStorage<Group>('group', Group),
machine: this.#resourceStorage<Machine>('machine', Machine),
service: this.#resourceStorage<Service>('service', Service),
user: this.#resourceStorage<User>('user', User),
group: this.#ressourceStorage<Group>('group', Group),
machine: this.#ressourceStorage<Machine>('machine', Machine),
service: this.#ressourceStorage<Service>('service', Service),
user: this.#ressourceStorage<User>('user', User),
}
}
#resourceStorage<T extends Resource>(
type: ResourceType<T>,
Builder: ResourceBuilder<T>,
): ResourceStorage<T> {
#ressourceStorage<T extends Ressource>(
type: RessourceType<T>,
Builder: RessourceBuilder<T>,
) {
return {
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),
get: (ressource: Pick<T, 'uuid'>) =>
this.#get<T>(type, Builder, ressource),
set: (ressources: T[]) => this.#set<T>(type, ressources),
delete: (ressources: Pick<T, 'uuid'>[]) =>
this.#delete<T>(type, ressources),
list: (
filter: (resource: T) => boolean | Promise<boolean> = () => true,
filter: (ressource: T) => boolean | Promise<boolean> = () => true,
) => this.#list<T>(type, Builder, filter),
listRef: async (
filter: (resource: T) => boolean | Promise<boolean> = () => true,
filter: (ressource: T) => boolean | Promise<boolean> = () => true,
) => {
const resources: Ref<T>[] = []
for await (const resource of this.#list<T>(type, Builder, filter)) {
resources.push(resource.toRef() as Ref<T>)
const ressources: Ref<T>[] = []
for await (const ressource of this.#list<T>(type, Builder, filter)) {
ressources.push(ressource.toRef() as Ref<T>)
}
return resources
return ressources
},
}
}
async #get<T extends Resource>(
type: ResourceType<T>,
Builder: ResourceBuilder<T>,
async #get<T extends Ressource>(
type: RessourceType<T>,
Builder: RessourceBuilder<T>,
entry: Pick<T, 'uuid'>,
): Promise<T> {
const json = await this.#kv.get<ResourceJson<T>>([
this.prefix.resource,
const json = await this.#kv.get<RessourceJson<T>>([
this.prefix.ressource,
type,
entry.uuid,
])
if (json.value) {
//@ts-expect-error Type union of Resource types for Builder
//@ts-expect-error Type union of Ressource types for Builder
return Builder.fromJSON(json.value)
}
throw new ReferenceError(
`no resource.${type} was found with uuid: "${entry.uuid}"`,
`no ressource.${type} was found with uuid: "${entry.uuid}"`,
)
}
#set<T extends Resource>(type: ResourceType<T>, entries: T[]) {
#set<T extends Ressource>(type: RessourceType<T>, entries: T[]) {
const atomic = this.#kv.atomic()
for (const entry of entries) {
//! TODO check if `refs` exists
const key = [this.prefix.resource, type, entry.uuid]
const key = [this.prefix.ressource, type, entry.uuid]
atomic.set(key, entry.toJSON())
}
return atomic.commit()
}
#delete<T extends Resource>(
type: ResourceType<T>,
#delete<T extends Ressource>(
type: RessourceType<T>,
entries: Pick<T, 'uuid'>[],
): Promise<Deno.KvCommitResult | Deno.KvCommitError> {
const atomic = this.#kv.atomic()
for (const entry of entries) {
//! TODO check if `refs` exists
atomic.delete([this.prefix.resource, type, entry.uuid])
atomic.delete([this.prefix.ressource, type, entry.uuid])
}
return atomic.commit()
}
async *#list<T extends Resource>(
type: ResourceType<T>,
Builder: ResourceBuilder<T>,
async *#list<T extends Ressource>(
type: RessourceType<T>,
Builder: RessourceBuilder<T>,
filter: (entry: T) => boolean | Promise<boolean>,
): AsyncGenerator<T, void, void> {
const list = this.#kv.list<ResourceJson<T>>({
prefix: [this.prefix.resource, type],
const list = this.#kv.list<RessourceJson<T>>({
prefix: [this.prefix.ressource, type],
})
for await (const entry of list) {
const value = entry.value
//@ts-expect-error Type union of Resource types for Builder
const resource = Builder.fromJSON(value) as T
if (await filter(resource)) {
yield resource
//@ts-expect-error Type union of Ressource types for Builder
const ressource = Builder.fromJSON(value) as T
if (await filter(ressource)) {
yield ressource
}
}
}
}
type ResourceType<T extends Resource> = T extends Credential<CredentialCategory>
? '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 ResourceBuilder<T extends Resource> = T extends
type RessourceBuilder<T extends Ressource> = T extends
Credential<CredentialCategory> ? typeof Credential
: T extends Group ? typeof Group
: T extends Machine ? typeof Machine
@ -147,26 +148,4 @@ type ResourceBuilder<T extends Resource> = T extends
: T extends User ? typeof User
: 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>
}
type RessourceJson<T extends Ressource> = ReturnType<T['toJSON']>

View file

@ -1,13 +1,11 @@
export {
Credential,
type CredentialCategory,
type CredentialStore,
type Passkey,
} from './src/credential.ts'
export { Group, type GroupPermissions } from './src/group.ts'
export { Machine, type MachineStatus } from './src/machine.ts'
export type { Resource, ResourceJson, ResourceRefJson } from './src/resource.ts'
export { Service, type ServiceCategory } from './src/service.ts'
export { User } from './src/user.ts'
export { Avatar } from './utils/avatar.ts'
export { Ref, type RefResolver, type RefString } from './utils/ref.ts'
Ref,
type RefResolver,
type RefString,
} from '@/src/models/utils/ref.ts'
export { Credential } from '@models/credential.ts'
export { Group } from '@models/group.ts'
export { Machine } from '@models/machine.ts'
export type { Ressource } from '@models/ressource.ts'
export { Service } from '@models/service.ts'
export { User } from '@models/user.ts'

View file

@ -1,9 +1,9 @@
import type { Base64String, ToJson, UUID } from '../../../types.ts'
import { Avatar } from '../utils/avatar.ts'
import type { Ref } from '../utils/ref.ts'
import { Resource, type ResourceJson } from './resource.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<T extends CredentialCategory> extends Resource {
export class Credential<T extends CredentialCategory> extends Ressource {
static fromJSON<T extends CredentialCategory>(
json: ToJson<Credential<T>>,
): Credential<T> {
@ -48,7 +48,7 @@ export class Credential<T extends CredentialCategory> extends Resource {
private constructor(
{ category, store, ...props }:
& Pick<Credential<T>, 'category' | 'store'>
& Pick<Resource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -65,31 +65,23 @@ export class Credential<T extends CredentialCategory> extends Resource {
return 'credential'
}
get category(): T {
get category() {
return this.#category
}
get store(): Readonly<CredentialStore<T>['store']> {
get store() {
return this.#store
}
update(
update<T extends CredentialCategory>(
props: Partial<Omit<Credential<T>, 'type' | 'uuid' | 'createdAt'>>,
): Credential<T> {
const { updatedAt } = super.update(props)
return new Credential<T>({
uuid: this.uuid,
name: this.name,
avatar: this.avatar,
createdAt: this.createdAt,
category: this.category,
store: this.store,
...props,
updatedAt,
})
const credential = new Credential<T>({ ...this, ...props, updatedAt })
return credential
}
toJSON(): ResourceJson<Credential<T>, 'category' | 'store'> {
toJSON() {
return {
...super.toJSON(),
type: this.type,
@ -107,7 +99,7 @@ export class Credential<T extends CredentialCategory> extends Resource {
}
}
export interface Credential<T extends CredentialCategory> extends Resource {
export interface Credential<T extends CredentialCategory> extends Ressource {
type: 'credential'
category: T
store: Readonly<CredentialStore<T>['store']>

View file

@ -1,13 +1,9 @@
import type { Posix, ToJson, UUID } from '../../../types.ts'
import { Avatar } from '../utils/avatar.ts'
import { Ref } from '../utils/ref.ts'
import {
Resource,
type ResourceJson,
type ResourceRefJson,
} from './resource.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 Resource {
export class Group extends Ressource {
static fromJSON(
{ posix, groups, ...json }: ToJson<Group>,
): Group {
@ -54,13 +50,19 @@ export class Group extends Resource {
}
#posix?: Posix
#permissions: Readonly<GroupPermissions>
#permissions: Readonly<{
[serviceOrMachine: UUID]: {
read: boolean
write: boolean
execute: boolean
}
}>
#groups: readonly Ref<Group>[]
private constructor(
{ posix, permissions, groups, ...props }:
& Pick<Group, 'posix' | 'permissions' | 'groups'>
& Pick<Resource, 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'>,
& Pick<Ressource, 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -91,37 +93,25 @@ export class Group extends Resource {
return 'group'
}
get posix(): Posix | undefined {
get posix() {
return this.#posix
}
get permissions(): Readonly<GroupPermissions> {
get permissions() {
return this.#permissions
}
get groups(): readonly Ref<Group>[] {
get groups() {
return this.#groups
}
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
const { updatedAt } = super.update(props)
return new Group({
uuid: this.uuid,
name: this.name,
avatar: this.avatar,
createdAt: this.createdAt,
posix: this.posix,
permissions: this.permissions,
groups: this.groups,
...props,
updatedAt,
})
const group = new Group({ ...this, ...props, updatedAt })
return group
}
toJSON():
& ResourceJson<Group, 'permissions'>
& { posix: Posix | null }
& ResourceRefJson<Group, Group, 'groups'> {
toJSON() {
return {
...super.toJSON(),
type: this.type,
@ -140,17 +130,15 @@ export class Group extends Resource {
}
}
export interface Group extends Resource {
export interface Group extends Ressource {
type: 'group'
posix: Posix | undefined
groups: readonly Ref<Group>[]
permissions: Readonly<GroupPermissions>
}
export type GroupPermissions = {
[serviceOrMachine: UUID]: {
read: boolean
write: boolean
execute: boolean
}
permissions: Readonly<{
[serviceOrMachine: UUID]: {
read: boolean
write: boolean
execute: boolean
}
}>
}

View file

@ -1,14 +1,10 @@
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,
type ResourceJson,
type ResourceRefJson,
} from './resource.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 Resource {
export class Machine extends Ressource {
static fromJSON(
{ url, ...json }: ToJson<Machine>,
): Machine {
@ -18,7 +14,7 @@ export class Machine extends Resource {
}
static create(
{ tags, url, status, groups, avatar, name }: Pick<
{ tags, url, status, groups, avatar }: Pick<
Machine,
'name' | 'tags' | 'url' | 'status' | 'avatar' | 'groups'
>,
@ -57,13 +53,19 @@ export class Machine extends Resource {
#tags: readonly string[]
#url: UrlString
#status: MachineStatus
#status:
| 'ready'
| 'busy'
| 'unavailable'
| 'discontinued'
| 'error'
| 'unknown'
#groups: readonly Ref<Group>[]
private constructor(
{ tags, url, status, groups, ...props }:
& Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
& Pick<Resource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>,
) {
super(props)
@ -81,16 +83,16 @@ export class Machine extends Resource {
get type(): 'machine' {
return 'machine'
}
get tags(): readonly string[] {
get tags() {
return this.#tags
}
get url(): UrlString {
get url() {
return this.#url
}
get status(): MachineStatus {
get status() {
return this.#status
}
get groups(): readonly Ref<Group>[] {
get groups() {
return this.#groups
}
@ -98,23 +100,11 @@ export class Machine extends Resource {
props: Partial<Omit<Machine, 'type' | 'uuid' | 'createdAt'>>,
): Machine {
const { updatedAt } = super.update(props)
return new Machine({
uuid: this.uuid,
name: this.name,
avatar: this.avatar,
createdAt: this.createdAt,
tags: this.tags,
url: this.url,
status: this.status,
groups: this.groups,
...props,
updatedAt,
})
const machine = new Machine({ ...this, ...props, updatedAt })
return machine
}
toJSON():
& ResourceJson<Machine, 'tags' | 'url' | 'status'>
& ResourceRefJson<Machine, Group, 'groups'> {
toJSON() {
return {
...super.toJSON(),
type: this.type,
@ -134,18 +124,16 @@ export class Machine extends Resource {
}
}
export interface Machine extends Resource {
export interface Machine extends Ressource {
type: 'machine'
tags: readonly string[]
url: UrlString
status: MachineStatus
status:
| 'ready'
| 'busy'
| 'unavailable'
| 'discontinued'
| 'error'
| 'unknown'
groups: readonly Ref<Group>[]
}
export type MachineStatus =
| 'ready'
| 'busy'
| 'unavailable'
| 'discontinued'
| 'error'
| 'unknown'

View file

@ -1,12 +1,12 @@
import type { DateString, ToJson, UrlString, UUID } from '../../../types.ts'
import { regex } from '../../../utils.ts'
import { Ref } from '../utils/ref.ts'
import { Ref } from '@/src/models/utils/ref.ts'
import type { DateString, ToJson, UrlString, UUID } from '@/types.ts'
import { regex } from '@/utils.ts'
export class Resource {
export class Ressource {
static fromJSON(
json: ToJson<Resource>,
): Resource {
return new Resource({
json: ToJson<Ressource>,
): Ressource {
return new Ressource({
name: json.name,
avatar: json.avatar,
uuid: json.uuid,
@ -16,16 +16,16 @@ export class Resource {
}
static create(
{ name, avatar }: Pick<Resource, 'name' | 'avatar'>,
): Resource {
{ 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 Resource({ name, avatar, uuid, createdAt, updatedAt })
return new Ressource({ name, avatar, uuid, createdAt, updatedAt })
}
static load(_options: never): Resource {
static load(_options: never): Ressource {
throw new Error('not implemented')
}
@ -37,7 +37,7 @@ export class Resource {
protected constructor(
{ name, avatar, uuid, createdAt, updatedAt }: Pick<
Resource,
Ressource,
'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'
>,
) {
@ -83,22 +83,14 @@ export class Resource {
}
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,
})
props: Partial<Omit<Ressource, 'type' | 'uuid' | 'createdAt'>>,
): Ressource {
const updated = new Ressource({ ...this, ...props })
updated.#updatedAt = new Date().toISOString() as DateString
return updated
}
toJSON(): ResourceJson<
Resource,
'type' | 'uuid' | 'name' | 'avatar' | 'createdAt' | 'updatedAt'
> {
toJSON() {
return {
type: this.type,
uuid: this.uuid,
@ -113,12 +105,12 @@ export class Resource {
return `Ressource (${JSON.stringify(this)})`
}
toRef(): Ref<Resource> {
return Ref.fromResource(this)
toRef(): Ref<Ressource> {
return Ref.fromRessource(this)
}
}
export interface Resource {
export interface Ressource {
type: 'user' | 'machine' | 'service' | 'group' | 'credential'
uuid: UUID
name: string
@ -126,24 +118,3 @@ 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']>[]>
}

View file

@ -1,14 +1,10 @@
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,
type ResourceJson,
type ResourceRefJson,
} from './resource.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 Resource {
export class Service extends Ressource {
static fromJSON(
{ groups, ...json }: ToJson<Service>,
): Service {
@ -56,7 +52,7 @@ export class Service extends Resource {
return this.create({ name, avatar, tags, url, category, groups })
}
#category: ServiceCategory
#category: 'web' | 'fs' | 'git'
#url: UrlString
#groups: readonly Ref<Group>[]
#tags: readonly string[]
@ -66,7 +62,7 @@ export class Service extends Resource {
tags,
url,
groups,
...resource
...ressource
}: Pick<
Service,
| 'uuid'
@ -79,7 +75,7 @@ export class Service extends Resource {
| 'groups'
| 'avatar'
>) {
super(resource)
super(ressource)
if (!['web', 'fs', 'git'].includes(category)) {
throw new TypeError(
@ -96,19 +92,19 @@ export class Service extends Resource {
return 'service'
}
get category(): ServiceCategory {
get category() {
return this.#category
}
get tags(): readonly string[] {
get tags() {
return this.#tags
}
get url(): UrlString {
get url() {
return this.#url
}
get groups(): readonly Ref<Group>[] {
get groups() {
return this.#groups
}
@ -116,23 +112,11 @@ export class Service extends Resource {
props: Partial<Omit<Service, 'type' | 'uuid' | 'createdAt'>>,
): Service {
const { updatedAt } = super.update(props)
return new Service({
uuid: this.uuid,
name: this.name,
createdAt: this.createdAt,
avatar: this.avatar,
category: this.category,
url: this.url,
tags: this.tags,
groups: this.groups,
...props,
updatedAt,
})
const service = new Service({ ...this, ...props, updatedAt })
return service
}
toJSON():
& ResourceJson<Service, 'category' | 'tags' | 'url'>
& ResourceRefJson<Service, Group, 'groups'> {
toJSON() {
return {
...super.toJSON(),
type: this.type,
@ -152,12 +136,10 @@ export class Service extends Resource {
}
}
export interface Service extends Resource {
export interface Service extends Ressource {
type: 'service'
category: ServiceCategory
category: 'web' | 'fs' | 'git'
tags: readonly string[]
url: UrlString
groups: readonly Ref<Group>[]
}
export type ServiceCategory = 'web' | 'fs' | 'git'

View file

@ -1,16 +1,12 @@
import type { Login, MailAddress, Posix, ToJson } from '../../../types.ts'
import { regex, toLogin } from '../../../utils.ts'
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,
type ResourceJson,
type ResourceRefJson,
} from './resource.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 Resource {
export class User extends Ressource {
static fromJSON(json: ToJson<User>): User {
if (json.type !== 'user') {
throw new TypeError(`type is "${json.type}" but "user" is required`)
@ -116,7 +112,7 @@ export class User extends Resource {
groups,
posix,
credentials,
...resource
...ressource
}: Pick<
User,
| 'uuid'
@ -131,7 +127,7 @@ export class User extends Resource {
| 'avatar'
| 'credentials'
>) {
super(resource)
super(ressource)
this.#lastname = lastname
this.#firstname = firstname
if (!regex.mailAddress) {
@ -154,54 +150,37 @@ export class User extends Resource {
get type(): 'user' {
return 'user'
}
get firstname(): string {
get firstname() {
return this.#firstname
}
get lastname(): string {
get lastname() {
return this.#lastname
}
get login(): Login {
get login() {
return this.#login
}
get mail(): MailAddress {
get mail() {
return this.#mail
}
get groups(): readonly Ref<Group>[] {
get groups() {
return this.#groups
}
get posix(): Posix | undefined {
get posix() {
return this.#posix
}
get credentials(): readonly Ref<Credential<CredentialCategory>>[] {
get credentials() {
return this.#credentials
}
update(props: Partial<Omit<User, 'type' | 'uuid' | 'createdAt'>>): User {
const { updatedAt } = super.update(props)
return new User({
uuid: this.uuid,
name: this.name,
avatar: this.avatar,
createdAt: this.createdAt,
lastname: this.lastname,
firstname: this.firstname,
mail: this.mail,
groups: this.groups,
posix: this.posix,
credentials: this.credentials,
...props,
updatedAt,
})
const user = new User({ ...this, ...props, updatedAt })
return user
}
toJSON():
& ResourceJson<User, 'lastname' | 'firstname' | 'login' | 'mail'>
& { posix: Posix | null }
& ResourceRefJson<User, Group, 'groups'>
& ResourceRefJson<User, Credential<CredentialCategory>, 'credentials'> {
toJSON() {
return {
...super.toJSON(),
type: this.type,
lastname: this.lastname,
firstname: this.firstname,
login: this.login,
@ -222,7 +201,7 @@ export class User extends Resource {
}
}
export interface User extends Resource {
export interface User extends Ressource {
type: 'user'
lastname: string
firstname: string

View file

@ -1,4 +1,4 @@
import type { UrlString } from '../../../types.ts'
import type { UrlString } from '@/types.ts'
export class Avatar {
static fromEmoji(emoji: string): UrlString {

View file

@ -1,77 +1,71 @@
import type { UUID } from '../../../types.ts'
import type { Db } from '../../db/mod.ts'
import type { Db } from '@/mod.ts'
import type { UUID } from '@/types.ts'
import {
Credential,
Group,
Machine,
type Resource,
type Ressource,
Service,
User,
} from '../mod.ts'
} from '@models'
export type RefString<T extends Resource> =
export type RefString<T extends Ressource> =
| `@ref/${T['type']}#${UUID}`
| Ref<T>
export type RefResolver<T extends Resource> = (
export type RefResolver<T extends Ressource> = (
ref: RefString<T>,
) => T | Promise<T>
export class Ref<T extends Resource> extends String {
static #toString<T extends Resource>(
export class Ref<T extends Ressource> extends String {
static #toString<T extends Ressource>(
{ uuid, type }: { uuid: UUID; type: T['type'] },
) {
return `@ref/${type}#${uuid}` as const
}
static parse<T extends Resource>(
string: RefString<T>,
): { type: T['type']; uuid: UUID } {
static parse<T extends Ressource>(string: RefString<T>) {
const [_, value] = string.split('/')
const [type, uuid] = value.split('#') as [T['type'], UUID]
return { type, uuid } as const
}
static fromResource<T extends Resource>(resource: T): Ref<T> {
return new Ref<T>(resource)
static fromRessource<T extends Ressource>(ressource: T): Ref<T> {
return new Ref<T>(ressource)
}
static fromString<T extends Resource>(string: RefString<T>): Ref<T> {
static fromString<T extends Ressource>(string: RefString<T>) {
return new Ref<T>(Ref.parse(string))
}
static dbResolver(
db: Db,
): <T extends Resource>(ref: RefString<T>) => Promise<T> {
return <T extends Resource>(ref: RefString<T>): Promise<T> => {
static dbResolver(db: Db) {
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.resource[type].get({ uuid })
return db.ressource[type].get({ uuid })
}
}
static restResolver(
endpoint: string | URL,
): <T extends Resource>(ref: RefString<T>) => Promise<T> {
return async <T extends Resource>(ref: RefString<T>): Promise<T> => {
static restResolver(endpoint: string | URL) {
return async <T extends Ressource>(ref: RefString<T>) => {
const { type, uuid } = Ref.parse(ref)
const url = new URL(`${type}s/${uuid}`, endpoint)
const response = await fetch(url)
const json = await response.json()
if (type === 'user') {
return User.fromJSON(json) as unknown as T
return User.fromJSON(json)
}
if (type === 'machine') {
return Machine.fromJSON(json) as unknown as T
return Machine.fromJSON(json)
}
if (type === 'service') {
return Service.fromJSON(json) as unknown as T
return Service.fromJSON(json)
}
if (type === 'group') {
return Group.fromJSON(json) as unknown as T
return Group.fromJSON(json)
}
if (type === 'credential') {
return Credential.fromJSON(json) as unknown as T
return Credential.fromJSON(json)
}
throw new TypeError(`unknown ref type "${type}"`)
@ -87,23 +81,23 @@ export class Ref<T extends Resource> extends String {
#type: T['type']
#uuid: UUID
get type(): T['type'] {
get type() {
return this.#type
}
get uuid(): UUID {
get uuid() {
return this.#uuid
}
ref(resolver: RefResolver<T>): T | Promise<T> {
ref(resolver: RefResolver<T>) {
return resolver(this.toString())
}
toString(): `@ref/${T['type']}#${UUID}` {
toString() {
return Ref.#toString<T>({ uuid: this.uuid, type: this.type })
}
toJSON(): `@ref/${T['type']}#${UUID}` {
toJSON() {
return this.toString()
}
}

View file

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