import {z} from 'zod' export type Search = {fields?: (Array | null) | undefined, keyword?: (string | null) | undefined}; export type Filter = {logic?: (string | null) | undefined, filters?: (Array | null) | undefined, field?: (string | null) | undefined, operator?: (string | null) | undefined, value: unknown}; export type SearchSupplierPickupBagsRequest = {advancedSearch?: Search | undefined, keyword?: (string | null) | undefined, advancedFilter?: Filter | undefined, pageSize?: number | undefined, pageNumber?: number | undefined, orderBy?: (Array | null) | undefined, onlyUnprocessed?: boolean | undefined}; export type CreateApiApiVVersionPickupBagsSearchArgs = {version: string, 'X-Tenant': string, body: SearchSupplierPickupBagsRequest}; export type CreateApiApiVVersionPickupBagsSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pickupBagSupplierDto = z.object({supplierName: z.string().nullable().optional(), supplierCityTerritoryId: z.string().optional(), supplierHubId: z.string().optional()}); export const driverInfoDto = z.object({id: z.string().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional()}); export const pickupBagResult = z.object({id: z.string().optional(), trackingNumber: z.string().nullable().optional(), unprocessedParcelsCount: z.number().int().nullable().optional(), state: z.string().nullable().optional(), supplier: pickupBagSupplierDto.optional(), driverId: z.string().nullable().optional(), driver: driverInfoDto.optional(), totalParcels: z.number().int().nullable().optional(), createdAt: z.string().nullable().optional()}); export const pagedListPickupBagResult = z.object({items: z.array(pickupBagResult).nullable().optional(), pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), totalCount: z.number().int().optional(), totalPages: z.number().int().optional(), hasPrevious: z.boolean().optional(), hasNext: z.boolean().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 }; /** * Search all supplier's pickup bags * * POST /api/v{version}/pickup-bags/search */ export const createApiApiVVersionPickupBagsSearch = async (args: CreateApiApiVVersionPickupBagsSearchArgs, options?: CreateApiApiVVersionPickupBagsSearchOptions) => { const {baseUrl = "https://api.zrexpress.app", fetch: fetchFn = fetch, bearerAuth, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Tenant'] != null) headers.set("X-Tenant", String(args['X-Tenant'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) if (apiKey !== undefined) headers.set("X-Api-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v${encodeURIComponent(String(args.version))}/pickup-bags/search${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(pagedListPickupBagResult, await res.json()) };