import {z} from 'zod' export type GetApiLookupsByDomainArgs = {domain: string}; export type GetApiLookupsByDomainOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiLookupsByDomainResponse = z.object({domain: z.string(), org: z.object({id: z.string(), slug: z.string(), name: z.string(), domain: z.string().nullable(), description: z.string().nullable(), category: z.string().nullable(), avatarUrl: z.string().nullable(), matchedVia: z.enum(["primary", "alias"]), status: z.enum(["stub", "tracked"]).optional(), locations: z.array(z.object({url: z.string().optional(), feed: z.string().optional(), github: z.string().optional(), appstore: z.string().optional(), file: z.string().optional(), title: z.string().nullable().optional(), canonical: z.boolean(), basis: z.enum(["curator", "declared", "detected", "generated"]), productId: z.string().nullable(), sourceId: z.string().nullable(), publish: z.literal("push").optional(), path: z.string().optional()})).optional()}).nullable(), products: z.array(z.object({id: z.string(), slug: z.string(), name: z.string(), orgId: z.string(), orgSlug: z.string(), orgName: z.string(), category: z.enum(["ai", "cloud", "commerce", "crm", "database", "design", "developer-tools", "devops", "finance", "framework", "infrastructure", "observability", "productivity", "security"]).nullable(), kind: z.enum(["platform", "sdk", "mobile", "desktop", "docs", "integration", "tool"]).nullable().optional()}))}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Resolve a domain to its owning org and any matching products * * GET /lookups/by-domain */ export const getApiLookupsByDomain = async (args: GetApiLookupsByDomainArgs, options?: GetApiLookupsByDomainOptions) => { const {baseUrl = "https://api.releases.sh", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.domain != null) query.append("domain", String(args.domain)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/lookups/by-domain${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiLookupsByDomainResponse, await res.json()) };