import {z} from 'zod' export type LoadFileType = 'INVOICE' | 'POD' | 'BOL' | 'RATE_CONFIRMATION' | 'LUMPER_RECEIPT' | 'OTHER'; export type LoadFileUploadReferenceNumbers = {invoiceNumber?: string | undefined}; export type LoadFileSourceType = 'CARRIER' | 'BROKER' | 'SHIPPER' | 'CONSIGNEE' | 'DRIVER' | 'OTHER'; export type LoadFileSourceIdentifierType = 'MC_NUMBER' | 'DOT_NUMBER' | 'SCAC' | 'EMAIL' | 'NAME' | 'EXTERNAL_ID'; export type LoadFileUploadSource = {type: LoadFileSourceType, identifierType: LoadFileSourceIdentifierType, identifier: string, name?: string | undefined}; export type LoadFileUploadRequest = {loadNum: string, internalLoadReference?: string | undefined, filename: string, fileType: LoadFileType, contentType: string, content: string, sizeBytes: number, checksum: string, referenceNumbers?: LoadFileUploadReferenceNumbers | undefined, source?: LoadFileUploadSource | undefined, createdAt?: string | undefined}; export type UploadDocumentEvent = {id: string, workflow: 'TRACK_AND_TRACE' | 'CARRIER_SUPPORT' | 'POD_COLLECTION' | 'LOAD_BUILDING' | 'CARRIER_SELECTION' | 'SCHEDULING' | 'CUSTOMER_SUPPORT' | 'null' | null, occurredAt: string, loadNumber: string | null, customerLoadInternalId: string | null, eventType: 'UPLOAD_DOCUMENT', data: LoadFileUploadRequest}; export type CreateApiUPLOADDOCUMENTArgs = {body: UploadDocumentEvent}; export type CreateApiUPLOADDOCUMENTOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fileUploadSummary = z.object({total: z.number().int(), succeeded: z.number().int(), failed: z.number().int()}); export const fileUploadSuccess = z.object({filename: z.string(), success: z.literal(true), data: z.object({size: z.number().int(), contentType: z.string(), id: z.string().optional()})}); export const fileUploadFailure = z.object({filename: z.string(), success: z.literal(false), error: z.string()}); export const fileUploadResult = z.discriminatedUnion("success", [fileUploadSuccess, fileUploadFailure]); export const batchFileUploadResponse = z.object({summary: fileUploadSummary, results: z.array(fileUploadResult)}); 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 }; /** * Upload Document * * POST /UPLOAD_DOCUMENT */ export const createApiUPLOADDOCUMENT = async (args: CreateApiUPLOADDOCUMENTArgs, options: CreateApiUPLOADDOCUMENTOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/UPLOAD_DOCUMENT${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(batchFileUploadResponse, await res.json()) };