import {z} from 'zod' export type GetApiApiV4TlEquipmentValidateArgs = {'capacityProviderIdentifier.type'?: ('SCAC' | 'DOT_NUMBER' | 'MC_NUMBER' | 'P44_EU' | 'SYSTEM' | 'P44_GLOBAL' | 'VAT') | undefined, 'capacityProviderIdentifier.value'?: string | undefined, 'equipmentIdentifier.shouldDelete'?: boolean | undefined, 'equipmentIdentifier.type'?: ('MOBILE_PHONE_NUMBER' | 'VEHICLE_ID' | 'LICENSE_PLATE' | 'SENSITECH_DEVICE_ID' | 'EMERSON_DEVICE_ID' | 'TIVE_DEVICE_ID' | 'CONTAINER_ID') | undefined, 'equipmentIdentifier.value'?: string | undefined}; export type GetApiApiV4TlEquipmentValidateOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const truckloadEquipmentValidationResult = z.object({exists: z.boolean().optional(), ownershipStatus: z.enum(["CONNECTED", "NOT_CONNECTED", "INDETERMINATE", "UNKNOWN"]).optional(), valid: z.boolean().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 }; /** * Validate equipment identifiers * * GET /api/v4/tl/equipment/validate */ export const getApiApiV4TlEquipmentValidate = async (args: GetApiApiV4TlEquipmentValidateArgs, options?: GetApiApiV4TlEquipmentValidateOptions) => { 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['capacityProviderIdentifier.type'] != null) query.append("capacityProviderIdentifier.type", String(args['capacityProviderIdentifier.type'])) if (args['capacityProviderIdentifier.value'] != null) query.append("capacityProviderIdentifier.value", String(args['capacityProviderIdentifier.value'])) if (args['equipmentIdentifier.shouldDelete'] != null) query.append("equipmentIdentifier.shouldDelete", String(args['equipmentIdentifier.shouldDelete'])) if (args['equipmentIdentifier.type'] != null) query.append("equipmentIdentifier.type", String(args['equipmentIdentifier.type'])) if (args['equipmentIdentifier.value'] != null) query.append("equipmentIdentifier.value", String(args['equipmentIdentifier.value'])) if (oAuth2 !== undefined) headers.set('authorization', `Bearer ${oAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/tl/equipment/validate${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(truckloadEquipmentValidationResult, await res.json()) };