import {z} from 'zod' export type GetApiApiV1DeliveryNotesIdDeliveriesArgs = {id: string}; export type GetApiApiV1DeliveryNotesIdDeliveriesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1DeliveryNotesIdDeliveriesResponse = z.object({data: z.array(z.object({id: z.string(), tracking: z.object({number: z.string(), link: z.string().optional(), carrier: z.string().optional()}), deliveryNote: z.object({id: z.string()}).nullable(), salesOrder: z.object({id: z.string()}).nullable(), shippingMethod: z.object({id: z.string()}).nullable(), sentAt: z.string().nullable().optional(), sentAtTimestamp: z.string().nullable().optional(), label: z.object({fileType: z.string(), fileContent: z.string()}).nullable().optional(), additionalPackages: z.array(z.object({id: z.string(), trackingNumber: z.string().nullable(), weight: z.number().nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional()})).optional()})), extra: z.object({totalCount: z.number().int(), page: z.object({number: z.number().int(), size: z.number().int()})})}); 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 delivery note deliveries * * GET /api/v1/deliveryNotes/{id}/deliveries */ export const getApiApiV1DeliveryNotesIdDeliveries = async (args: GetApiApiV1DeliveryNotesIdDeliveriesArgs, options?: GetApiApiV1DeliveryNotesIdDeliveriesOptions) => { const {baseUrl = "https://xentral.xentral.biz", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/deliveryNotes/${encodeURIComponent(String(args.id))}/deliveries${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(getApiApiV1DeliveryNotesIdDeliveriesResponse, await res.json()) };