Something went wrong. Try again.
tiny REST API that resolves AT Protocol Lexicon schemas from NSIDs lex.desertthunder.dev
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import { vi } from 'vitest';import { parseNsid } from '../src/lexicon';import { API_VERSION } from '../src/meta';import type { ResolvedLexicon, RuntimeEnv } from '../src/types';
export const originalFetch = globalThis.fetch;
export function resolvedLexicon(nsid = 'com.example.foo'): ResolvedLexicon { const parsed = parseNsid(nsid); if (parsed === null) { throw new Error(`Invalid test NSID: ${nsid}`); }
return { version: API_VERSION, nsid, parsed, hash: 'sha256:test', fetchedAt: '2026-06-18T00:00:00.000Z', source: { name: '_lexicon.example.com', url: `at://did:plc:example/com.atproto.lexicon.schema/${nsid}` }, lexicon: { lexicon: 1, id: nsid, defs: {} }, cache: 'miss', trace: { dnsName: parsed.dnsName, txtRecords: [], selectedDid: 'did:plc:example', didDocument: { url: null, status: null }, pdsEndpoint: null, repoGetRecord: { url: null, status: null }, final: { success: true, message: 'Resolved lexicon schema' } } };}
export function testEnv(storage = new Map<string, string>()): RuntimeEnv { const kv = { async get<T>(key: string, type?: 'json'): Promise<T | string | null> { const value = storage.get(key); if (value === undefined) { return null; }
return type === 'json' ? (JSON.parse(value) as T) : value; }, async put(key: string, value: string): Promise<void> { storage.set(key, value); }, async list(options?: KVNamespaceListOptions): Promise<KVNamespaceListResult<unknown, string>> { const prefix = options?.prefix ?? ''; const limit = options?.limit ?? 1000; const keys = [...storage.keys()] .filter((name) => name.startsWith(prefix)) .slice(0, limit) .map((name) => ({ name }));
return { keys, list_complete: true, cacheStatus: null }; } };
return { LEXICONS: kv as KVNamespace, CACHE_TTL_SECONDS: '86400', DOH_ENDPOINT: 'https://cloudflare-dns.com/dns-query' };}
export function mockFetch(response: Response) { const fetchMock = vi.fn(async (..._args: Parameters<typeof fetch>): Promise<Response> => response); globalThis.fetch = fetchMock as unknown as typeof fetch; return fetchMock;}
export function mockFetchSequence(responses: Response[]) { const fetchMock = vi.fn(async (..._args: Parameters<typeof fetch>): Promise<Response> => { const response = responses.shift(); if (response === undefined) { throw new Error('No mocked response'); }
return response; }); globalThis.fetch = fetchMock as unknown as typeof fetch; return fetchMock;}