import {z} from 'zod' export type GetApiApiV4TlShipmentsTrackingmethodsArgs = {'carrierIdentifier.type'?: ('SCAC' | 'DOT_NUMBER' | 'MC_NUMBER' | 'P44_EU' | 'SYSTEM' | 'P44_GLOBAL' | 'VAT') | undefined, 'carrierIdentifier.value'?: string | undefined}; export type GetApiApiV4TlShipmentsTrackingmethodsOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const capacityProviderAccount = z.object({code: z.string().optional()}); export const truckloadEquipmentIdentifier = z.object({shouldDelete: z.boolean().optional(), type: z.enum(["MOBILE_PHONE_NUMBER", "VEHICLE_ID", "LICENSE_PLATE", "SENSITECH_DEVICE_ID", "TIVE_DEVICE_ID"]).optional(), value: z.string().optional()}); export const message = z.object({diagnostic: z.string().optional(), message: z.string().optional(), severity: z.enum(["ERROR", "WARNING", "INFO"]).optional(), source: z.enum(["SYSTEM", "CAPACITY_PROVIDER"]).optional()}); export const truckloadShipmentIdentifier = z.object({type: z.enum(["BILL_OF_LADING", "ORDER"]).optional(), value: z.string().optional()}); export const truckloadShipmentTrackingMethod = z.object({capacityProviderAccount: capacityProviderAccount.optional(), equipmentIdentifier: truckloadEquipmentIdentifier.optional(), infoMessages: z.array(message).optional(), shipmentIdentifier: truckloadShipmentIdentifier.optional()}); export const truckloadShipmentTrackingMethods = z.object({trackingMethods: z.array(truckloadShipmentTrackingMethod).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 }; /** * Get available shipment tracking methods for a carrier * * GET /api/v4/tl/shipments/trackingmethods */ export const getApiApiV4TlShipmentsTrackingmethods = async (args: GetApiApiV4TlShipmentsTrackingmethodsArgs, options?: GetApiApiV4TlShipmentsTrackingmethodsOptions) => { const {baseUrl = "https://na12.api.project44.com", fetch: fetchFn = fetch, oAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['carrierIdentifier.type'] != null) query.append("carrierIdentifier.type", String(args['carrierIdentifier.type'])) if (args['carrierIdentifier.value'] != null) query.append("carrierIdentifier.value", String(args['carrierIdentifier.value'])) if (oAuth2 !== undefined) headers.set('authorization', `Bearer ${oAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/tl/shipments/trackingmethods${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(truckloadShipmentTrackingMethods, await res.json()) };