refactor(federation): Make references always have domains
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

This commit is contained in:
Jesse Wierzbinski 2026-04-03 13:34:19 +02:00
parent df2a5ce260
commit 709e1c6087
No known key found for this signature in database
22 changed files with 688 additions and 477 deletions

View file

@ -4,11 +4,19 @@ import type { JSONObject } from "../types.ts";
export class Entity {
public static name = "Entity";
// biome-ignore lint/suspicious/noExplicitAny: This is a base class that is never instanciated directly
public constructor(public data: any) {}
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): Promise<Entity> {
return EntitySchema.parseAsync(json).then((u) => new Entity(u));
public static fromJSON(
json: JSONObject,
instanceDomain: string,
): Promise<Entity> {
return EntitySchema.parseAsync(json).then(
(u) => new Entity(u, instanceDomain),
);
}
public toJSON(): JSONObject {
@ -19,10 +27,19 @@ export class Entity {
export class Reference {
public constructor(
public id: string,
public domain?: string,
public domain: string,
) {}
public static fromString(str: string): Reference {
/**
* 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(":");
@ -36,10 +53,21 @@ export class Reference {
return new Reference(id, domain);
}
return new Reference(str);
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.domain}:${this.id}` : this.id;
return `${this.domain}:${this.id}`;
}
}