import {z} from 'zod' export type GetApiOrdersOrderIdentifiersLabelArgs = {orderIdentifiers: string, documentType: 'postageLabel' | 'despatchNote' | 'CN22' | 'CN23' | 'commercialInvoice', includeReturnsLabel?: boolean | undefined, includeCN?: boolean | undefined, includeCommercialInvoice?: boolean | undefined}; export type GetApiOrdersOrderIdentifiersLabelOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOrdersOrderIdentifiersLabelResponse = z.string(); 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 }; /** * Return a single PDF file with generated label and/or associated document(s) * * GET /orders/{orderIdentifiers}/label */ export const getApiOrdersOrderIdentifiersLabel = async (args: GetApiOrdersOrderIdentifiersLabelArgs, options: GetApiOrdersOrderIdentifiersLabelOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.documentType != null) query.append("documentType", String(args.documentType)) if (args.includeReturnsLabel != null) query.append("includeReturnsLabel", String(args.includeReturnsLabel)) if (args.includeCN != null) query.append("includeCN", String(args.includeCN)) if (args.includeCommercialInvoice != null) query.append("includeCommercialInvoice", String(args.includeCommercialInvoice)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.orderIdentifiers))}/label${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(getApiOrdersOrderIdentifiersLabelResponse, await res.json()) };