import {z} from 'zod' export type CreateApiV1RuleBooksRuleBookIdDraftArgs = {rule_book_id: string}; export type CreateApiV1RuleBooksRuleBookIdDraftOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const actorType = z.enum(["person", "agent", "integration"]); export const actor = z.object({type: actorType, id: z.string(), display: z.string()}); export const actorAt = z.object({actor: actor, at: z.string()}); export const draftSummary = z.object({id: z.string(), number: z.number().int(), etag: z.string(), opened: actorAt, parent_version_number: z.number().int().nullable()}); export const draftOpenResult = z.object({draft: draftSummary}); 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 }; /** * Open Rule Book Draft * * POST /v1/rule-books/{rule_book_id}/draft */ export const createApiV1RuleBooksRuleBookIdDraft = async (args: CreateApiV1RuleBooksRuleBookIdDraftArgs, options?: CreateApiV1RuleBooksRuleBookIdDraftOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/rule-books/${encodeURIComponent(String(args.rule_book_id))}/draft${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(draftOpenResult, await res.json()) };