import {z} from 'zod' export type GetApiV1OrdersArgs = {limit?: number | undefined, offset?: number | undefined, sortBy?: string | undefined, sortDirection?: string | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined, status?: (string | null) | undefined, ids?: (string | null) | undefined, referenceId?: (string | null) | undefined, tags?: (string | null) | undefined, search?: (string | null) | undefined, hasRoute?: (string | null) | undefined, pickupStoreLocationIds?: (string | null) | undefined, dropoffStoreLocationIds?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined, pickupBusinessName?: (string | null) | undefined, dropoffBusinessName?: (string | null) | undefined, creationSource?: (string | null) | undefined, dispatchStrategyId?: (string | null) | undefined}; export type GetApiV1OrdersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const orderLocationSummary = z.object({businessName: z.string().nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), address: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), startTime: z.string().nullable().optional(), endTime: z.string().nullable().optional()}); export const orderListItem = z.object({id: z.string(), externalId: z.string().nullable().optional(), referenceId: z.string().nullable().optional(), status: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), valueCents: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), pickup: orderLocationSummary.nullable().optional(), dropoff: orderLocationSummary.nullable().optional(), routeId: z.string().nullable().optional(), portalUrl: z.string().nullable().optional()}); export const ordersListResponse = z.object({results: z.array(orderListItem), totalCount: z.number().int(), limit: z.number().int(), offset: z.number().int()}); 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 }; /** * List/Search Orders * * GET /v1/orders */ export const getApiV1Orders = async (args: GetApiV1OrdersArgs, options?: GetApiV1OrdersOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.status != null) query.append("status", String(args.status)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.referenceId != null) query.append("referenceId", String(args.referenceId)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.search != null) query.append("search", String(args.search)) if (args.hasRoute != null) query.append("hasRoute", String(args.hasRoute)) if (args.pickupStoreLocationIds != null) query.append("pickupStoreLocationIds", String(args.pickupStoreLocationIds)) if (args.dropoffStoreLocationIds != null) query.append("dropoffStoreLocationIds", String(args.dropoffStoreLocationIds)) if (args.city != null) query.append("city", String(args.city)) if (args.state != null) query.append("state", String(args.state)) if (args.country != null) query.append("country", String(args.country)) if (args.pickupBusinessName != null) query.append("pickupBusinessName", String(args.pickupBusinessName)) if (args.dropoffBusinessName != null) query.append("dropoffBusinessName", String(args.dropoffBusinessName)) if (args.creationSource != null) query.append("creationSource", String(args.creationSource)) if (args.dispatchStrategyId != null) query.append("dispatchStrategyId", String(args.dispatchStrategyId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ordersListResponse, await res.json()) };