import {z} from 'zod' export type GetApiTransactionsTransactionIdDeliveriesDeliveryIdArgs = {transactionId: number, deliveryId: string, 'orderful-api-version'?: string | undefined}; export type GetApiTransactionsTransactionIdDeliveriesDeliveryIdOptions = {baseUrl?: string | undefined, APIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const hrefLink = z.object({href: z.string()}); export const getTransactionDeliveryResponseDto = z.object({deliveryId: z.string(), transactionId: z.number(), href: z.string(), status: z.enum(["CREATED", "SENT", "FAILED", "DELIVERED"]), createdAt: z.string(), lastUpdatedAt: z.string(), note: z.string(), approve: hrefLink, fail: hrefLink, sendSucceededAt: z.string(), sendFailedAt: z.string(), lastConfirmedAt: 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 }; /** * Get a Delivery. * * GET /transactions/{transactionId}/deliveries/{deliveryId} */ export const getApiTransactionsTransactionIdDeliveriesDeliveryId = async (args: GetApiTransactionsTransactionIdDeliveriesDeliveryIdArgs, options?: GetApiTransactionsTransactionIdDeliveriesDeliveryIdOptions) => { const {baseUrl = "https://api.orderful.com", fetch: fetchFn = fetch, APIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['orderful-api-version'] != null) headers.set("orderful-api-version", String(args['orderful-api-version'])) if (APIKEY !== undefined) headers.set("orderful-api-key", APIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/${encodeURIComponent(String(args.transactionId))}/deliveries/${encodeURIComponent(String(args.deliveryId))}${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(getTransactionDeliveryResponseDto, await res.json()) };