import {z} from 'zod' export type PatchApiPublicV1TrackersTrackerIdArgs = {trackerId: string, searchBy?: ('trackerId' | 'clientTrackerId') | undefined, body: {isSubscribed?: boolean | undefined, courierCode?: (Array | string) | undefined, originCountryCode?: unknown | undefined, destinationCountryCode?: unknown | undefined, destinationPostCode?: string | undefined, shippingDate?: string | undefined}}; export type PatchApiPublicV1TrackersTrackerIdOptions = {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 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 }; /** * Update an existing tracker * * PATCH /public/v1/trackers/{trackerId} */ export const patchApiPublicV1TrackersTrackerId = async (args: PatchApiPublicV1TrackersTrackerIdArgs, options?: PatchApiPublicV1TrackersTrackerIdOptions) => { 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 (args.searchBy != null) query.append("searchBy", String(args.searchBy)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/trackers/${encodeURIComponent(String(args.trackerId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(tracker, await res.json()) };