import {z} from 'zod' export type SourceSelector = {source_types: Array, tags_all?: Array | undefined}; export type PrecedenceRuleCreate = {over: SourceSelector, under: SourceSelector, later_only?: boolean | undefined, tie?: ('later_valid_from' | 'none') | undefined, effect?: ('supersede' | 'flag_only') | undefined, priority?: number | undefined, active?: boolean | undefined, axiom_id?: (string | null) | undefined, updated_by?: (string | null) | undefined}; export type CreateApiPotsPotIdConstitutionPrecedenceRulesArgs = {pot_id: string, body: PrecedenceRuleCreate}; export type CreateApiPotsPotIdConstitutionPrecedenceRulesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const sourceSelector = z.object({source_types: z.array(z.string()), tags_all: z.array(z.string()).optional()}); export const precedenceRule = z.object({over: sourceSelector, under: sourceSelector, later_only: z.boolean().optional(), tie: z.enum(["later_valid_from", "none"]).optional(), effect: z.enum(["supersede", "flag_only"]).optional(), priority: z.number().int().gte(0).lte(1000).optional(), active: z.boolean().optional(), axiom_id: z.string().max(64).nullable().optional(), id: z.string(), created_at: z.string(), created_by: z.string().nullable().optional(), updated_at: z.string(), updated_by: z.string().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 }; /** * Declare which kind of source prevails over which * * POST /pots/{pot_id}/constitution/precedence-rules */ export const createApiPotsPotIdConstitutionPrecedenceRules = async (args: CreateApiPotsPotIdConstitutionPrecedenceRulesArgs, options: CreateApiPotsPotIdConstitutionPrecedenceRulesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/pots/${encodeURIComponent(String(args.pot_id))}/constitution/precedence-rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(precedenceRule, await res.json()) };