import {z} from 'zod' export type GetApiPotsBySlugSlugArgs = {slug: string}; export type GetApiPotsBySlugSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const axiomKind = z.enum(["fact", "rule", "scope"]); export const constitutionFactResponse = z.object({id: z.string(), content: z.string(), title: z.string().nullable().optional(), pot_score: z.number().gte(0).lte(1), level: z.string(), rationale: z.string().nullable().optional(), keywords: z.array(z.string()).optional(), active: z.boolean().optional(), deactivated_at: z.string().nullable().optional(), deactivation_reason: z.string().nullable().optional(), kind: axiomKind.optional()}); export const constitutionResponse = z.object({facts: z.array(constitutionFactResponse), version: z.string().optional(), last_updated: z.string(), approved_by: z.string(), fact_count: z.number().int(), active_count: z.number().int().optional()}); export const pOTAboutView = z.object({text: z.string().nullable().optional(), source: z.enum(["generated", "manual"]).nullable().optional(), updated_at: z.string().nullable().optional(), suggestion_pending: z.boolean().optional()}); export const pOTResponse = z.object({id: z.string(), slug: z.string(), workspace_id: z.string(), name: z.string(), description: z.string(), domain: z.string().optional(), constitution: constitutionResponse, about: pOTAboutView.nullable().optional(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Get a POT by slug * * GET /pots/by-slug/{slug} */ export const getApiPotsBySlugSlug = async (args: GetApiPotsBySlugSlugArgs, options: GetApiPotsBySlugSlugOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/pots/by-slug/${encodeURIComponent(String(args.slug))}${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(pOTResponse, await res.json()) };