import {z} from 'zod' export type GetApiZatcaDebitNotesIdDownloadArgs = {'X-ZATCA-Branch'?: string | undefined, 'X-ZATCA-Connected-Account-Id'?: string | undefined, 'X-ZATCA-Environment'?: string | undefined, id: string, regenerate?: boolean | undefined, type?: ('pdf' | 'xml') | undefined}; export type GetApiZatcaDebitNotesIdDownloadOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiZatcaDebitNotesIdDownloadResponse = 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 }; /** * Download an debit note * * * GET /zatca/debit-notes/{id}/download/ */ export const getApiZatcaDebitNotesIdDownload = async (args: GetApiZatcaDebitNotesIdDownloadArgs, options?: GetApiZatcaDebitNotesIdDownloadOptions) => { const {baseUrl = "https://api.wafeq.com/v1", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-ZATCA-Branch'] != null) headers.set("X-ZATCA-Branch", String(args['X-ZATCA-Branch'])) if (args['X-ZATCA-Connected-Account-Id'] != null) headers.set("X-ZATCA-Connected-Account-Id", String(args['X-ZATCA-Connected-Account-Id'])) if (args['X-ZATCA-Environment'] != null) headers.set("X-ZATCA-Environment", String(args['X-ZATCA-Environment'])) if (args.regenerate != null) query.append("regenerate", String(args.regenerate)) if (args.type != null) query.append("type", String(args.type)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/zatca/debit-notes/${encodeURIComponent(String(args.id))}/download/${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(getApiZatcaDebitNotesIdDownloadResponse, await res.json()) };