Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import { Client, simpleFetchHandler } from "@atcute/client";import type { OAuthUserAgent } from "@atcute/oauth-browser-client";import { isNsid } from "@atcute/lexicons/syntax";import { mintServiceAuth, serviceDidForHost } from "$lib/auth/agent";import { didOf, nsidOf, type Nsid } from "$lib/api/syntax";
export { ClientResponseError, isXRPCErrorPayload, ok } from "@atcute/client";export type { XRPCErrorPayload } from "@atcute/client";
// generated lexicons register ambient types; don't import the barrel at runtime.
export interface BobbinContext { readonly xrpc: Client; readonly serviceUrl: string; readonly fetch: typeof globalThis.fetch;}
export interface CreateBobbinOptions { serviceUrl: string; fetch?: typeof globalThis.fetch; /** when set, every xrpc request carries a freshly minted service-auth jwt */ agent?: OAuthUserAgent;}
export const nsidFromUrl = (input: RequestInfo | URL): Nsid | null => { const href = input instanceof Request ? input.url : String(input); let pathname: string; try { pathname = new URL(href).pathname; } catch { return null; } const [, prefix, nsid, ...rest] = pathname.split("/"); return prefix === "xrpc" && nsid && rest.length === 0 && isNsid(nsid) ? nsidOf(nsid) : null;};
// pocket keys stored preferences by the token's `aud`export const PREFERENCES_AUD = didOf("did:web:tangled.org");export const PREFERENCES_LXMS: readonly Nsid[] = [ nsidOf("com.bad-example.pocket.getPreferences"), nsidOf("com.bad-example.pocket.putPreferences")];
export const withServiceAuth = ( base: typeof globalThis.fetch, agent: OAuthUserAgent, origin: string): typeof globalThis.fetch => { return async (input, init) => { const lxm = nsidFromUrl(input); if (!lxm) return base(input, init);
const aud = PREFERENCES_LXMS.includes(lxm) ? PREFERENCES_AUD : serviceDidForHost(new URL(origin).host); const token = await mintServiceAuth(agent, { aud, lxm }); const headers = new Headers(init?.headers); headers.set("authorization", `Bearer ${token}`); return base(input, { ...init, headers }); };};
export const createBobbinClient = ({ serviceUrl, fetch, agent}: CreateBobbinOptions): BobbinContext => { const origin = serviceUrl.replace(/\/+$/, ""); const baseFetch = fetch ?? globalThis.fetch; const boundFetch = agent ? withServiceAuth(baseFetch, agent, origin) : baseFetch; const xrpc = new Client({ handler: simpleFetchHandler({ service: origin, fetch: boundFetch }) }); return { xrpc, serviceUrl: origin, fetch: boundFetch };};
export type QueryValue = string | number | boolean | readonly (string | number)[] | undefined;
export interface XrpcRequestInit { signal?: AbortSignal; headers?: HeadersInit;}