import {z} from 'zod' export type GetApiV1ZonesArgs = {size?: number | undefined, offset?: number | undefined, isDeleted?: boolean | undefined}; export type GetApiV1ZonesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const storeLocationAddressResponse = z.object({number: z.string().nullable().optional(), secondaryNumber: z.string().nullable().optional(), street: z.string(), county: z.string().nullable().optional(), city: z.string(), state: z.string().nullable().optional(), postalCode: z.string().nullable().optional(), country: z.string(), latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional(), timezoneId: z.string().nullable().optional()}); export const storeLocationResponse = z.object({externalId: z.string().nullable().optional(), operatingHours: z.record(z.string(), z.unknown()).nullable().optional(), prepTimeMinutes: z.number().int().nullable().optional(), blackoutDates: z.array(z.string()).nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), email: z.string().nullable().optional(), pickupInstructions: z.string().nullable().optional(), id: z.string(), name: z.string(), phoneNumber: z.string(), location: storeLocationAddressResponse, zoneIds: z.array(z.string()).nullable(), zoneAssociations: z.array(z.record(z.string(), z.unknown())).nullable(), tags: z.array(z.string()).nullable(), integrationMetadata: z.record(z.string(), z.unknown()).nullable()}); export const zoneRestrictionResponse = z.object({id: z.string(), externalId: z.string(), name: z.string(), isActive: z.boolean(), restrictionMetadata: z.record(z.string(), z.unknown()).nullable()}); export const zoneResponse = z.object({id: z.string(), name: z.string().nullable(), externalId: z.string().nullable(), portalUrl: z.string().nullable(), coverageArea: z.record(z.string(), z.unknown()).nullable(), storeLocations: z.array(storeLocationResponse).nullable(), storeLocationAssociations: z.array(z.record(z.string(), z.unknown())).nullable(), zoneRestrictions: z.array(zoneRestrictionResponse).nullable(), tags: z.array(z.string()).nullable(), zoneMetadata: z.record(z.string(), z.unknown()).nullable(), isDeleted: z.boolean().nullable()}); export const listZonesResponse = z.object({zones: z.array(zoneResponse)}); 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 zones * * GET /v1/zones */ export const getApiV1Zones = async (args: GetApiV1ZonesArgs, options?: GetApiV1ZonesOptions) => { 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.size != null) query.append("size", String(args.size)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.isDeleted != null) query.append("isDeleted", String(args.isDeleted)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/zones${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(listZonesResponse, await res.json()) };