import {z} from 'zod' export type TrackingRegistrationRequest = {carrierId: string, carrierTrackingId: string, carrierServiceMethodId?: string | undefined, shipDateTime?: string | undefined, tenantId?: string | undefined, partnerShipmentId?: string | undefined, fromCountryCode?: string | undefined, fromPostalCode?: string | undefined, toCountryCode?: string | undefined, toPostalCode?: string | undefined, partnerReferenceId?: string | undefined, partnerReferenceId2?: string | undefined}; export type BulkTrackingRegistrationRequest = {registrationRequests: Array}; export type CreateApiApiV1TrackingBulkRegisterArgs = {body: BulkTrackingRegistrationRequest}; export type CreateApiApiV1TrackingBulkRegisterOptions = {baseUrl?: string | undefined, oauth2Scheme?: string | undefined, fetch?: typeof fetch | undefined}; export const trackingRegistrationResponse = z.object({carrierId: z.string().optional(), carrierTrackingId: z.string().optional(), shipiumTrackingId: z.string()}); export const bulkTrackingRegistrationFailureError = z.object({field: z.string().optional(), detail: z.string().optional()}); export const bulkTrackingRegistrationFailure = z.object({carrierId: z.string().optional(), carrierTrackingId: z.string().optional(), trackingRegistrationRequestsIndex: z.number().int().optional(), errors: z.array(bulkTrackingRegistrationFailureError).optional()}); export const bulkTrackingRegistrationResponse = z.object({trackingRegistrations: z.array(trackingRegistrationResponse), failures: z.array(bulkTrackingRegistrationFailure)}); 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 }; /** * Tracking registration endpoint to start tracking shipments shipium did not create * * POST /api/v1/tracking/bulkRegister */ export const createApiApiV1TrackingBulkRegister = async (args: CreateApiApiV1TrackingBulkRegisterArgs, options?: CreateApiApiV1TrackingBulkRegisterOptions) => { const {baseUrl = "https://api.shipium.com", fetch: fetchFn = fetch, oauth2Scheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2Scheme !== undefined) headers.set('authorization', `Bearer ${oauth2Scheme}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/tracking/bulkRegister${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(bulkTrackingRegistrationResponse, await res.json()) };