import {z} from 'zod' export type GetApiApiV2UiVouchersArgs = {'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'page[size]'?: number | undefined, filter?: {[key: string]: string} | undefined, include?: Array | undefined}; export type GetApiApiV2UiVouchersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const voucherListResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string(), first: z.string(), last: z.string().nullable(), next: z.string().nullable(), prev: z.string().nullable()}), meta: z.object({page: z.object({total_elements: z.number().int()})}), data: z.array(z.object({id: z.number().int(), type: z.literal("voucher"), attributes: z.object({voucher: z.string(), time: z.number().int()}), links: z.object({self: z.string()})}))}); 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 Vouchers * * GET /api/v2/ui/vouchers */ export const getApiApiV2UiVouchers = async (args: GetApiApiV2UiVouchersArgs, options: GetApiApiV2UiVouchersOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.include != null) query.append("include", args.include.join(',')) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/vouchers${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(voucherListResponse, await res.json()) };