import {z} from 'zod' export type GetApiApiV1ExternalShipmentsArgs = {page?: number | undefined, limit?: number | undefined, status?: ('draft' | 'booked' | 'pending' | 'in_transit' | 'delivered' | 'cancelled') | undefined, shipmentType?: ('FCL' | 'LCL' | 'Air') | undefined, search?: string | undefined, createdAfter?: string | undefined, createdBefore?: string | undefined, updatedAfter?: string | undefined, updatedBefore?: string | undefined}; export type GetApiApiV1ExternalShipmentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const entityCreationInfo = z.object({source: z.enum(["user", "email", "document_upload", "bulk_upload", "tms_import", "api", "system", "null"]).nullable().optional()}); export const shipmentEmailUpdates = z.object({threads: z.number().int().optional(), lastAt: z.string().nullable().optional()}); export const entitySyncInfo = z.object({method: z.enum(["automation", "manual", "webhook"]).optional(), status: z.string().optional(), syncedAt: z.string().nullable().optional(), syncedBy: z.object({userId: z.string().optional(), name: z.string().nullable().optional(), email: z.string().nullable().optional()}).nullable().optional(), tmsReference: z.string().nullable().optional()}).nullable(); export const portLocation = z.object({portCode: z.string(), portName: z.string().optional(), countryCode: z.string().optional()}); export const airportLocation = z.object({airportCode: z.string(), airportName: z.string().optional(), countryCode: z.string().optional()}); export const addressLocation = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), province: z.string().optional(), postalCode: z.string().optional(), country: z.string().optional()}); export const shipmentSummary = z.object({creation: entityCreationInfo.optional(), emailUpdates: shipmentEmailUpdates.optional(), sync: entitySyncInfo.optional(), id: z.string().optional(), status: z.enum(["draft", "pending", "booked", "in_transit", "delivered", "cancelled"]).optional(), name: z.string().optional(), shipmentType: z.enum(["FCL", "LCL", "AIR", "LTL", "FTL", "RAIL"]).optional(), commodity: z.string().optional(), portOfLoading: portLocation.optional(), portOfDischarge: portLocation.optional(), placeOfReceiptType: z.enum(["Port", "Airport", "Door"]).optional(), placeOfReceiptPort: portLocation.optional(), placeOfReceiptAirport: airportLocation.optional(), placeOfReceiptAddress: addressLocation.optional(), placeOfDeliveryType: z.enum(["Port", "Airport", "Door"]).optional(), placeOfDeliveryPort: portLocation.optional(), placeOfDeliveryAirport: airportLocation.optional(), placeOfDeliveryAddress: addressLocation.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), documentCount: z.number().int().optional()}); export const getApiApiV1ExternalShipmentsResponse = z.object({success: z.boolean().optional(), data: z.object({data: z.array(shipmentSummary).optional(), pagination: z.object({page: z.number().int().optional(), limit: z.number().int().optional(), total: z.number().int().optional(), totalPages: z.number().int().optional(), hasNext: z.boolean().optional(), hasPrev: z.boolean().optional()}).optional()}).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 /api/v1/external/shipments */ export const getApiApiV1ExternalShipments = async (args: GetApiApiV1ExternalShipmentsArgs, options?: GetApiApiV1ExternalShipmentsOptions) => { const {baseUrl = "https://api.wove.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.status != null) query.append("status", String(args.status)) if (args.shipmentType != null) query.append("shipmentType", String(args.shipmentType)) if (args.search != null) query.append("search", String(args.search)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.updatedBefore != null) query.append("updatedBefore", String(args.updatedBefore)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/external/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(getApiApiV1ExternalShipmentsResponse, await res.json()) };