import {z} from 'zod' export type GetApiVouchersIdChargesArgs = {id: string, page?: number | undefined, per_page?: number | undefined, 'filter[created_from]'?: unknown | undefined, 'filter[created_to]'?: unknown | undefined, 'filter[order_number]'?: string | undefined, 'filter[amount]'?: string | undefined}; export type GetApiVouchersIdChargesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiVouchersIdChargesResponse = z.object({data: z.array(z.object({id: z.string().optional(), voucher_id: z.string().optional(), client_id: z.string().optional(), type: z.enum(["charge", "recharge", "refund"]).optional(), amount: z.string().min(4).max(9).optional(), order_number: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()})).optional(), links: z.object({first: z.string().max(255).optional(), last: z.string().max(255).optional(), prev: z.string().max(255).nullable().optional(), next: z.string().max(255).nullable().optional()}).optional(), meta: z.object({current_page: z.number().int().gte(1).optional(), per_page: z.number().int().gte(1).optional(), from: z.number().int().gte(1).nullable().optional(), to: z.number().int().gte(1).nullable().optional(), last_page: z.number().int().gte(1).optional(), total: z.number().int().gte(1).optional(), path: z.string().max(255).optional()}).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 }; /** * Get a list of voucher charges * * GET /vouchers/{id}/charges */ export const getApiVouchersIdCharges = async (args: GetApiVouchersIdChargesArgs, options?: GetApiVouchersIdChargesOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args['filter[created_from]'] != null) query.append("filter[created_from]", String(args['filter[created_from]'])) if (args['filter[created_to]'] != null) query.append("filter[created_to]", String(args['filter[created_to]'])) if (args['filter[order_number]'] != null) query.append("filter[order_number]", String(args['filter[order_number]'])) if (args['filter[amount]'] != null) query.append("filter[amount]", String(args['filter[amount]'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/vouchers/${encodeURIComponent(String(args.id))}/charges${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(getApiVouchersIdChargesResponse, await res.json()) };