import {z} from 'zod' export type GetApiIntentsArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, page?: number | undefined, page_size?: number | undefined, subscription?: string | undefined}; export type GetApiIntentsOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const intent = z.object({id: z.number().int().optional(), subscription: z.string().optional(), description: z.string().max(512).optional(), needs_attention_reason: z.enum(["biller_issue", "gateway_issue", "reference_expired", "without_usage_records", "processing_issue", "without_reference"]).optional(), created: z.string().optional()}); export const getApiIntentsResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(intent)}); 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 intents * * GET /intents/ */ export const getApiIntents = async (args: GetApiIntentsArgs, options?: GetApiIntentsOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.subscription != null) query.append("subscription", String(args.subscription)) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/intents/${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(getApiIntentsResponse, await res.json()) };