import {z} from 'zod' export type GetApiOrdersArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, page?: number | undefined, page_size?: number | undefined, ordering?: string | undefined, status?: ('draft' | 'finalized' | 'cancelled') | undefined, tax_agency?: string | undefined}; export type GetApiOrdersOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderLine = z.object({id: z.number().int().optional(), name: z.string().max(80), description: z.string().max(200).nullable().optional(), amount: z.number().int(), quantity: z.number(), exempt: z.boolean().optional(), tax: z.number().int().nullable().optional(), tax_percent: z.number().nullable().optional(), total: z.number().int().optional()}); export const orderReference = z.object({date: z.string(), document: z.string(), description: z.string().nullable().optional(), serial: z.string()}); export const order = z.object({id: z.number().int().optional(), customer: z.string(), document: z.number().int(), number: z.number().int().nullable().optional(), date: z.string(), shipping_type: z.enum(["1", "2", "3", "4", "5", "6", "7", "8", "9"]), address: z.string().max(70).nullable().optional(), state: z.number().int().nullable().optional(), carrier_id: z.string().max(10).nullable().optional(), carrier_name: z.string().max(30).nullable().optional(), licence_plate: z.string().max(7).nullable().optional(), note: z.string().max(100).nullable().optional(), amount: z.number().int().nullable().optional(), exempt: z.number().int().nullable().optional(), tax: z.number().int().nullable().optional(), tax_percent: z.number().nullable().optional(), total: z.number().int().nullable().optional(), status: z.enum(["draft", "finalized", "cancelled"]).optional(), biller_status: z.string().optional(), tax_agency: z.object({}).optional(), local_file: z.string().nullable().optional(), imported: z.boolean().optional(), test_mode: z.boolean().optional(), lines: z.array(orderLine), references: z.array(orderReference).optional(), created: z.string().optional(), updated: z.string().optional()}); export const getApiOrdersResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(order)}); 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 orders * * GET /orders/ */ export const getApiOrders = async (args: GetApiOrdersArgs, options?: GetApiOrdersOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.status != null) query.append("status", String(args.status)) if (args.tax_agency != null) query.append("tax_agency", String(args.tax_agency)) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${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(getApiOrdersResponse, await res.json()) };