import {z} from 'zod' export type CorrectionIn = {excerpt: string, corrected_at: string, session_ref?: (string | null) | undefined}; export type ProposalItem = {change?: string | undefined, text: string, applies_to?: Array | undefined, tags?: Array | undefined, why?: (string | null) | undefined, origin?: string | undefined, replaces?: (string | null) | undefined, replaces_version?: (number | null) | undefined, evidence_sessions?: number | undefined, corrections?: Array | undefined}; export type ProposeRulesRequest = {repo_remote: string, items: Array}; export type CreateApiDatamatesDatamateIdRulesProposalsArgs = {datamate_id: number, body: ProposeRulesRequest}; export type CreateApiDatamatesDatamateIdRulesProposalsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const proposalResult = z.object({index: z.number().int(), status: z.string(), public_id: z.string().nullable().optional(), rule_key: z.string().nullable().optional(), version: z.number().int().nullable().optional(), duplicate: z.boolean().optional(), weakens_verification: z.boolean().optional(), verification_reason: z.string().nullable().optional(), error_code: z.string().nullable().optional(), error_detail: z.string().nullable().optional()}); export const proposeRulesResponse = z.object({results: z.array(proposalResult)}); 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 }; /** * Propose Rules * * POST /datamates/{datamate_id}/rules/proposals * * Also accepts auth: HTTPBearer */ export const createApiDatamatesDatamateIdRulesProposals = async (args: CreateApiDatamatesDatamateIdRulesProposalsArgs, options: CreateApiDatamatesDatamateIdRulesProposalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/datamates/${encodeURIComponent(String(args.datamate_id))}/rules/proposals${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(proposeRulesResponse, await res.json()) };