import {z} from 'zod' export type GetApiV2DocumentsDocumentIdTransitionRulesRuleIdArgs = {authorization: string, document_id: string, rule_id: string}; export type GetApiV2DocumentsDocumentIdTransitionRulesRuleIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2TransitionRule = z.object({action: z.string().optional(), attribute_labels: z.array(z.string()).optional(), field: z.string().optional(), roles: z.array(z.string()).optional(), rule_id: z.string().optional(), target_attribute_label: z.array(z.number().int()).optional(), wasm: z.string().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 }; /** * Returns the rule associated with the ruleID in the document. * * GET /v2/documents/{document_id}/transition_rules/{rule_id} */ export const getApiV2DocumentsDocumentIdTransitionRulesRuleId = async (args: GetApiV2DocumentsDocumentIdTransitionRulesRuleIdArgs, options?: GetApiV2DocumentsDocumentIdTransitionRulesRuleIdOptions) => { const {baseUrl = "http://localhost:8082", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/documents/${encodeURIComponent(String(args.document_id))}/transition_rules/${encodeURIComponent(String(args.rule_id))}${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(v2TransitionRule, await res.json()) };