Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import { GATE_CREATE_DELEGATE_NSID } from "#lib/api/delegation.ts";
export const gateUrl = "https://gate.example.test";export const handleDomain = "orgs.example.test";
export const delegationConfig = { delegationUrl: gateUrl, delegationDid: "did:web:gate.example.test", delegationHandleDomain: handleDomain};
export type Answer = (handle: string) => Promise<Response>;
export const created = (did: string, controllerDid = "did:plc:alice"): Answer => (handle) => Promise.resolve(Response.json({ did, handle, controllerDid }));
export const refused = (status: number, error: string, message?: string): Answer => () => Promise.resolve(Response.json({ error, message }, { status }));
export const offline: Answer = () => Promise.reject(new TypeError("Failed to fetch"));
export const pdsAnswers = (answers: { serviceAuth?: number; getRecord?: number } = {}) => (pathname: string): Response => { if (pathname.includes("getServiceAuth")) { return answers.serviceAuth ? Response.json({ error: "AuthRequired" }, { status: answers.serviceAuth }) : Response.json({ token: "service-auth-jwt" }); } if (pathname.includes("getRecord") && answers.getRecord) { return Response.json( { error: "InternalServerError", message: "Repo is not ready yet" }, { status: answers.getRecord } ); } return new Response(null, { status: 404 }); };
export const orgNameOf = (parameters: Record<string, unknown>): string => (parameters.orgName as string | undefined) ?? "acme";
export const installGateFetch = ({ parameters }: { parameters: Record<string, unknown> }) => { const answer = parameters.gate as Answer | undefined; const handle = `${orgNameOf(parameters)}.${handleDomain}`; const original = window.fetch; window.fetch = (input, init) => { const url = String(input instanceof Request ? input.url : input); const requested = url.includes(GATE_CREATE_DELEGATE_NSID) ? (JSON.parse(String(init?.body)) as { handle?: string }).handle : undefined; if (answer && requested === handle) return answer(handle); return original(input, init); }; return () => { window.fetch = original; };};