import {z} from 'zod' export type GetApiApiV4TlAssetsArgs = {capacityProviderIdentifierType?: ('SCAC' | 'DOT_NUMBER' | 'MC_NUMBER' | 'P44_EU' | 'SYSTEM' | 'P44_GLOBAL' | 'VAT') | undefined, capacityProviderIdentifierValue?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV4TlAssetsOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; 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 paginationInfo = z.object({pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), total: z.number().int().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 truckloadAssetCollection = z.object({infoMessages: z.array(message).optional(), paginationInfo: paginationInfo.optional(), results: z.array(truckloadEquipmentIdentifier).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 list of assets for a given carrier * * GET /api/v4/tl/assets */ export const getApiApiV4TlAssets = async (args: GetApiApiV4TlAssetsArgs, options?: GetApiApiV4TlAssetsOptions) => { 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.capacityProviderIdentifierType != null) query.append("capacityProviderIdentifierType", String(args.capacityProviderIdentifierType)) if (args.capacityProviderIdentifierValue != null) query.append("capacityProviderIdentifierValue", String(args.capacityProviderIdentifierValue)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (oAuth2 !== undefined) headers.set('authorization', `Bearer ${oAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/tl/assets${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(truckloadAssetCollection, await res.json()) };