import {z} from 'zod' export type CreateDeliveryWindowInputSerializer = {startTime?: (string | null) | undefined, startTimeLocal?: (string | null) | undefined, endTime?: (string | null) | undefined, endTimeLocal?: (string | null) | undefined, cutoffTime?: (string | null) | undefined, cutoffTimeLocal?: (string | null) | undefined, timezoneId?: (string | null) | undefined, name?: (string | null) | undefined, rrule?: (string | null) | undefined, exceptionDates?: (Array | null) | undefined, allowedTags?: (Array | null) | undefined, leadTimeMinutes?: (number | null) | undefined, priceCents?: (number | null) | undefined, minimumOrderValueCents?: (number | null) | undefined, maximumCapacity?: (number | null) | undefined, storeLocationId?: (string | null) | undefined, zoneId?: (string | null) | undefined, zoneExternalId?: (string | null) | undefined, windowMetadata?: ({[key: string]: unknown} | null) | undefined, isActive?: (boolean | null) | undefined, isDeleted?: (boolean | null) | undefined}; export type CreateDeliveryWindowsInputSerializer = {deliveryWindows: Array}; export type CreateApiV1DeliveryWindowsArgs = {body: CreateDeliveryWindowsInputSerializer}; export type CreateApiV1DeliveryWindowsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const deliveryWindowOutput = z.object({id: z.string(), name: z.string(), startTime: z.string(), startTimeLocal: z.string(), endTime: z.string(), endTimeLocal: z.string(), timezoneId: z.string(), cutoffTime: z.string(), cutoffTimeLocal: z.string(), rrule: z.string(), exceptionDates: z.array(z.string()), allowedTags: z.array(z.string()), leadTimeMinutes: z.number().int(), priceCents: z.number().int(), minimumOrderValueCents: z.number().int(), maximumCapacity: z.number().int(), storeLocationId: z.string(), zoneId: z.string(), zoneExternalId: z.string(), windowMetadata: z.record(z.string(), z.unknown()), isActive: z.boolean(), isDeleted: z.boolean()}); export const deliveryWindowsResponse = z.object({deliveryWindows: z.array(deliveryWindowOutput)}); 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 multiple delivery windows * * POST /v1/delivery-windows */ export const createApiV1DeliveryWindows = async (args: CreateApiV1DeliveryWindowsArgs, options?: CreateApiV1DeliveryWindowsOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/delivery-windows${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(deliveryWindowsResponse, await res.json()) };