import {z} from 'zod' export type TrackerCreateRequest = {trackingNumber: string, shipmentReference?: string | undefined, clientTrackerId?: string | undefined, originCountryCode?: unknown | undefined, destinationCountryCode?: unknown | undefined, destinationPostCode?: string | undefined, shippingDate?: string | undefined, courierCode?: (Array | string) | undefined, courierName?: string | undefined, trackingUrl?: string | undefined, orderNumber?: string | undefined, title?: string | undefined, recipient?: ({email?: string | undefined, name?: string | undefined}) | undefined, settings?: ({restrictTrackingToCourierCode?: boolean | undefined}) | undefined}; export type CreateApiPublicV1TrackersArgs = {body: TrackerCreateRequest}; export type CreateApiPublicV1TrackersOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const tracker = z.object({trackerId: z.string(), trackingNumber: z.string(), shipmentReference: z.string().nullable(), courierCode: z.union([z.array(z.string()), z.string()]).optional(), clientTrackerId: z.string().nullable(), isSubscribed: z.boolean(), isTracked: z.boolean(), createdAt: z.string()}); export const createApiPublicV1TrackersResponse = z.object({data: z.object({tracker: tracker.optional()}).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 }; /** * Create a tracker * * POST /public/v1/trackers */ export const createApiPublicV1Trackers = async (args: CreateApiPublicV1TrackersArgs, options?: CreateApiPublicV1TrackersOptions) => { const {baseUrl = "https://api.ship24.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/trackers${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(createApiPublicV1TrackersResponse, await res.json()) };