~james/core

core/web/src/fixtures/orgCreate.ts -rw-r--r-- 2.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { GATE_CREATE_DELEGATE_NSID } from "#lib/api/delegation.ts";
import { installFetch } from "#storybook/installFetch.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}`;
	return installFetch((url, init) => {
		if (!answer || !url.pathname.includes(GATE_CREATE_DELEGATE_NSID)) return;
		const requested = (JSON.parse(String(init?.body)) as { handle?: string }).handle;
		if (requested === handle) return answer(handle);
	});
};