import {z} from 'zod' export type UUID = string; export type TinyText = string; export type ShippingInfo = {return_shipping_company: unknown, return_tracking_number: unknown, return_tracking_url: unknown, shipping_company: unknown, shipping_method: unknown, tracking_number: unknown, tracking_url: unknown}; export type AmountDTO = {gross: number, net: number, tax: number}; export type LineItemsRequest = {external_id?: (string | null) | undefined, title?: TinyText | undefined, description?: (string | null) | undefined, quantity?: number | undefined, category?: (string | null) | undefined, brand?: (string | null) | undefined, gtin?: (string | null) | undefined, mpn?: (string | null) | undefined, amount_gross?: number | undefined, amount_net?: (number | null) | undefined, amount_tax?: (number | null) | undefined, tax_rate?: (number | null) | undefined, unit_price?: number | undefined, total_discount_amount?: (number | null) | undefined, product_url?: (string | null) | undefined, image_url?: (string | null) | undefined, quantity_unit?: (string | null) | undefined, type?: (string | null) | undefined}; export type CreateInvoiceRequest = {orders: Array, external_code: TinyText, invoice_url: TinyText, shipping_info?: ShippingInfo | undefined, amount: AmountDTO, line_items?: Array | undefined}; export type CreateApiInvoicesArgs = {body: CreateInvoiceRequest}; export type CreateApiInvoicesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(36).max(36); export const createApiInvoicesResponse = z.object({uuid: uUID}); 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 }; /** * Create Invoice * * POST /invoices */ export const createApiInvoices = async (args: CreateApiInvoicesArgs, options?: CreateApiInvoicesOptions) => { const {baseUrl = "https://paella-sandbox.billie.io/api/v2", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoices${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiInvoicesResponse, await res.json()) };