import {z} from 'zod' export type GetApiV1EligibleDeliveryWindowsArgs = {orderId?: (string | null) | undefined, externalOrderId?: (string | null) | undefined, startTime: string, endTime: string, requestedCapacity?: (number | null) | undefined}; export type GetApiV1EligibleDeliveryWindowsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const eligibleDeliveryWindow = z.object({id: z.string(), name: z.string().nullable().optional(), ineligibleReason: z.string().nullable(), allowedTags: z.array(z.string()).nullable(), storeLocationId: z.string().nullable().optional(), storeLocationExternalId: z.string().nullable().optional(), zoneId: z.string().nullable().optional(), zoneExternalId: z.string().nullable().optional(), cutoffTime: z.string().nullable().optional(), cutoffTimeLocal: z.string().nullable().optional(), startTime: z.string().nullable(), startTimeLocal: z.string().nullable(), endTime: z.string().nullable(), endTimeLocal: z.string().nullable(), remainingCapacity: z.number().nullable().optional(), maximumCapacity: z.number().nullable().optional(), minimumOrderValueCents: z.number().int().nullable().optional(), timezoneId: z.string().nullable().optional(), priceCents: z.number().int().nullable().optional(), score: z.number().nullable().optional(), windowMetadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const eligibleDeliveryWindowsResponse = z.object({eligibleDeliveryWindows: z.array(eligibleDeliveryWindow), ineligibleDeliveryWindows: z.array(eligibleDeliveryWindow)}); 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 }; /** * Get eligible delivery windows for order * * GET /v1/eligible_delivery_windows */ export const getApiV1EligibleDeliveryWindows = async (args: GetApiV1EligibleDeliveryWindowsArgs, options?: GetApiV1EligibleDeliveryWindowsOptions) => { 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.orderId != null) query.append("orderId", String(args.orderId)) if (args.externalOrderId != null) query.append("externalOrderId", String(args.externalOrderId)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.requestedCapacity != null) query.append("requestedCapacity", String(args.requestedCapacity)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/eligible_delivery_windows${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(eligibleDeliveryWindowsResponse, await res.json()) };