import {z} from 'zod' export type GetApiShipmentsArgs = {merchant_invoice_id?: string | undefined, fulfillment_method?: ('shipping_provider' | 'self_delivery' | 'customer_pickup' | 'services_only') | undefined, verification_status?: ('verified' | 'pending') | undefined, shipment_courier?: string | undefined, limit?: number | undefined, starting_after?: string | undefined, ending_before?: string | undefined, search?: string | undefined, searchBy?: ('tracking_number' | 'courier' | 'id' | 'merchant_invoice_id') | undefined, sort?: string | undefined}; export type GetApiShipmentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const shipmentObject = z.object({id: z.string().optional(), merchant_invoice_id: z.string().optional(), fulfillment_method: z.enum(["shipping_provider", "self_delivery", "customer_pickup", "services_only"]).optional(), shipment_tracking_number: z.string().optional(), shipment_courier: z.string().optional(), verification_status: z.enum(["verified", "pending"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), tracking_status: z.string().nullable().optional(), tracking_substatus: z.string().nullable().optional(), expected_delivery: z.string().nullable().optional()}); export const shipmentsList = z.object({limit: z.number().int().optional(), page: z.number().int().optional(), count: z.number().int().optional(), results: z.array(shipmentObject).optional()}); 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 Shipments * * GET /shipments * * Also accepts auth: basicAuth */ export const getApiShipments = async (args: GetApiShipmentsArgs, options?: GetApiShipmentsOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchant_invoice_id != null) query.append("merchant_invoice_id", String(args.merchant_invoice_id)) if (args.fulfillment_method != null) query.append("fulfillment_method", String(args.fulfillment_method)) if (args.verification_status != null) query.append("verification_status", String(args.verification_status)) if (args.shipment_courier != null) query.append("shipment_courier", String(args.shipment_courier)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.search != null) query.append("search", String(args.search)) if (args.searchBy != null) query.append("searchBy", String(args.searchBy)) if (args.sort != null) query.append("sort", String(args.sort)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/shipments${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(shipmentsList, await res.json()) };