import {z} from 'zod' export type GetApiApiV1CustomersIdInvoicesArgs = {id: string, limit?: number | undefined, after?: string | undefined, before?: string | undefined, contractExternalId?: string | undefined, stateIn?: string | undefined, issuedAfter?: string | undefined, issuedBefore?: string | undefined, orderBy?: ('issueDate' | 'dueDate' | 'total') | undefined, orderDir?: ('ASC' | 'DESC') | undefined, 'X-ACCOUNT-ID'?: string | undefined, 'X-ENVIRONMENT-ID'?: string | undefined}; export type GetApiApiV1CustomersIdInvoicesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceListResponseDto = z.object({data: z.array(z.object({invoiceId: z.string().max(255), invoiceExternalId: z.string().max(255).nullable(), invoiceNumber: z.string().max(255).nullable(), contractExternalId: z.string().max(255).nullable(), customerExternalId: z.string().max(255).nullable(), state: z.enum(["OPEN", "CANCELED", "PAID"]), issueDate: z.string().nullable(), dueDate: z.string().nullable(), paidDate: z.string().nullable(), currency: z.string().max(255).nullable(), subtotal: z.number().nullable(), tax: z.number().nullable(), discount: z.number().nullable(), total: z.number().nullable(), lineItems: z.array(z.object({description: z.string().max(255).nullable(), quantity: z.number().nullable(), unitPrice: z.number().nullable(), amount: z.number().nullable(), productExternalId: z.string().max(255).nullable()}))})), pagination: z.object({next: z.string().max(255).nullable(), prev: z.string().max(255).nullable()})}); 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 invoices * * GET /api/v1/customers/{id}/invoices */ export const getApiApiV1CustomersIdInvoices = async (args: GetApiApiV1CustomersIdInvoicesArgs, options?: GetApiApiV1CustomersIdInvoicesOptions) => { const {baseUrl = "https://api.stigg.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.contractExternalId != null) query.append("contractExternalId", String(args.contractExternalId)) if (args.stateIn != null) query.append("stateIn", String(args.stateIn)) if (args.issuedAfter != null) query.append("issuedAfter", String(args.issuedAfter)) if (args.issuedBefore != null) query.append("issuedBefore", String(args.issuedBefore)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.orderDir != null) query.append("orderDir", String(args.orderDir)) if (args['X-ACCOUNT-ID'] != null) headers.set("X-ACCOUNT-ID", String(args['X-ACCOUNT-ID'])) if (args['X-ENVIRONMENT-ID'] != null) headers.set("X-ENVIRONMENT-ID", String(args['X-ENVIRONMENT-ID'])) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/customers/${encodeURIComponent(String(args.id))}/invoices${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(invoiceListResponseDto, await res.json()) };