import {z} from 'zod' export type GetApiDatamatesDatamateIdRulesArgs = {datamate_id: number, view?: ('pending' | 'active' | 'history') | undefined, repo_identity?: (string | null) | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiDatamatesDatamateIdRulesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const replacedRule = z.object({public_id: z.string(), text: z.string(), applies_to: z.array(z.string()), version: z.number().int(), status: z.string()}); export const correctionOut = z.object({excerpt: z.string(), corrected_at: z.string(), corrected_by_name: z.string().nullable().optional()}); export const ruleUsageTotals = z.object({applied: z.number().int().optional(), helpful: z.number().int().optional(), harmful: z.number().int().optional(), corrected_again: z.number().int().optional()}); export const ruleDetail = z.object({public_id: z.string(), rule_key: z.string(), repo_identity: z.string().nullable().optional(), text: z.string(), applies_to: z.array(z.string()), tags: z.array(z.string()), pinned: z.boolean(), status: z.string(), change: z.string(), origin: z.string(), why: z.string().nullable().optional(), replaces: replacedRule.nullable().optional(), evidence_sessions: z.number().int(), corrections: z.array(correctionOut), weakens_verification: z.boolean(), verification_reason: z.string().nullable().optional(), proposed_by: z.number().int().nullable().optional(), proposed_by_name: z.string().nullable().optional(), proposed_at: z.string(), reviewed_by_name: z.string().nullable().optional(), reviewed_at: z.string().nullable().optional(), review_reason: z.string().nullable().optional(), usage: ruleUsageTotals, edit_pending: z.boolean().optional(), version: z.number().int(), updated_at: z.string()}); export const pageRuleDetail = z.object({items: z.array(ruleDetail), total: z.number().int().gte(0).nullable(), page: z.number().int().gte(1).nullable(), size: z.number().int().gte(1).nullable(), pages: z.number().int().gte(0).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 }; /** * List Rules * * GET /datamates/{datamate_id}/rules * * Also accepts auth: HTTPBearer */ export const getApiDatamatesDatamateIdRules = async (args: GetApiDatamatesDatamateIdRulesArgs, options: GetApiDatamatesDatamateIdRulesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.view != null) query.append("view", String(args.view)) if (args.repo_identity != null) query.append("repo_identity", String(args.repo_identity)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/datamates/${encodeURIComponent(String(args.datamate_id))}/rules${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(pageRuleDetail, await res.json()) };