mirror of
https://github.com/versia-pub/server.git
synced 2026-04-27 12:49:16 +02:00
Some checks failed
CodeQL Scan / Analyze (push) Failing after 0s
Deploy Docs to GitHub Pages / build (push) Failing after 0s
Deploy Docs to GitHub Pages / Deploy (push) Has been skipped
Nix Build / check (push) Failing after 0s
Test Publish / build (client) (push) Failing after 0s
Test Publish / build (sdk) (push) Failing after 0s
Build Docker Images / lint (push) Has been cancelled
Build Docker Images / check (push) Has been cancelled
Build Docker Images / tests (push) Has been cancelled
Build Docker Images / detect-circular (push) Has been cancelled
Mirror to Codeberg / Mirror (push) Has been cancelled
Build Docker Images / build (server, Dockerfile, ${{ github.repository_owner }}/server) (push) Has been cancelled
Build Docker Images / build (worker, Worker.Dockerfile, ${{ github.repository_owner }}/worker) (push) Has been cancelled
73 lines
2 KiB
TypeScript
73 lines
2 KiB
TypeScript
import { EntitySchema } from "../schemas/entity.ts";
|
|
import type { JSONObject } from "../types.ts";
|
|
|
|
export class Entity {
|
|
public static name = "Entity";
|
|
|
|
public constructor(
|
|
// biome-ignore lint/suspicious/noExplicitAny: This is a base class that is never instanciated directly
|
|
public data: any,
|
|
public instanceDomain: string,
|
|
) {}
|
|
|
|
public static fromJSON(
|
|
json: JSONObject,
|
|
instanceDomain: string,
|
|
): Promise<Entity> {
|
|
return EntitySchema.parseAsync(json).then(
|
|
(u) => new Entity(u, instanceDomain),
|
|
);
|
|
}
|
|
|
|
public toJSON(): JSONObject {
|
|
return this.data;
|
|
}
|
|
}
|
|
|
|
export class Reference {
|
|
public constructor(
|
|
public id: string,
|
|
public domain: string,
|
|
) {}
|
|
|
|
/**
|
|
* Parses a reference from a string. The string can be in the format "domain:id" or just "id" if the domain is the local instance (in which case a default domain must be provided).
|
|
* @param str
|
|
* @param defaultDomain
|
|
* @returns
|
|
*/
|
|
public static fromString(
|
|
str: string,
|
|
defaultDomain: URL | string,
|
|
): Reference {
|
|
// Expect format: domain:id or id (if domain is the local instance)
|
|
// Handle IPv6 addresses in brackets
|
|
const chunks = str.split(":");
|
|
if (chunks.length === 2) {
|
|
return new Reference(chunks[1], chunks[0]);
|
|
}
|
|
|
|
if (chunks.length > 2) {
|
|
const domain = chunks.slice(0, -1).join(":");
|
|
const id = chunks.at(-1) as string;
|
|
return new Reference(id, domain);
|
|
}
|
|
|
|
if (!defaultDomain) {
|
|
throw new Error(
|
|
`Invalid reference string: ${str}. Expected format "domain:id" or "id" with a default domain provided.`,
|
|
);
|
|
}
|
|
|
|
return new Reference(
|
|
str,
|
|
defaultDomain instanceof URL
|
|
? defaultDomain.hostname
|
|
: defaultDomain,
|
|
);
|
|
}
|
|
|
|
public toString(): string {
|
|
return `${this.domain}:${this.id}`;
|
|
}
|
|
}
|