import {z} from 'zod' export type GetApiOrganizationsOrganizationIdInvoicesArgs = {organization_id: string, 'filter[status]'?: ('paid' | 'charged_off' | 'pending' | 'refunded' | 'canceled' | 'refund_pending') | undefined, 'filter[type]'?: ('credit_memo' | 'invoice') | undefined, 'filter[order_id]'?: string | undefined, page?: number | undefined}; export type GetApiOrganizationsOrganizationIdInvoicesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({country: z.string().optional(), name_line: z.string().optional(), premise: z.string().optional(), sub_premise: z.string().optional(), thoroughfare: z.string().optional(), administrative_area: z.string().optional(), sub_administrative_area: z.string().optional(), locality: z.string().optional(), dependent_locality: z.string().optional(), postal_code: z.string().optional()}); export const invoicePDF = z.object({url: z.string().optional(), status: z.enum(["ready", "pending"]).optional()}); export const invoice = z.object({id: z.string().optional(), invoice_number: z.string().optional(), type: z.enum(["invoice", "credit_memo"]).optional(), order_id: z.string().optional(), related_invoice_id: z.string().nullable().optional(), status: z.enum(["paid", "charged_off", "pending", "refunded", "canceled", "refund_pending"]).optional(), owner: z.string().optional(), invoice_date: z.string().nullable().optional(), invoice_due: z.string().nullable().optional(), created: z.string().nullable().optional(), changed: z.string().nullable().optional(), company: z.string().optional(), total: z.number().optional(), address: address.optional(), notes: z.string().optional(), invoice_pdf: invoicePDF.optional()}); export const getApiOrganizationsOrganizationIdInvoicesResponse = z.object({items: z.array(invoice).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 }; /** * List invoices * * GET /organizations/{organization_id}/invoices */ export const getApiOrganizationsOrganizationIdInvoices = async (args: GetApiOrganizationsOrganizationIdInvoicesArgs, options?: GetApiOrganizationsOrganizationIdInvoicesOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[type]'] != null) query.append("filter[type]", String(args['filter[type]'])) if (args['filter[order_id]'] != null) query.append("filter[order_id]", String(args['filter[order_id]'])) if (args.page != null) query.append("page", String(args.page)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.organization_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(getApiOrganizationsOrganizationIdInvoicesResponse, await res.json()) };