import {z} from 'zod' export type ServiceTimesCollection = Array<{start?: string | undefined, end?: string | undefined}>; export type ServiceTimesServices = {collection?: ServiceTimesCollection | undefined, delivery?: ServiceTimesCollection | undefined}; export type ServiceTimesDays = {monday?: ServiceTimesServices | undefined, tuesday?: ServiceTimesServices | undefined, wednesday?: ServiceTimesServices | undefined, thursday?: ServiceTimesServices | undefined, friday?: ServiceTimesServices | undefined, saturday?: ServiceTimesServices | undefined, sunday?: ServiceTimesServices | undefined}; export type ServiceTimes = {serviceTimes?: ServiceTimesDays | undefined}; export type UpdateApiRestaurantsTenantRestaurantIdServicetimesArgs = {tenant: string, restaurantId: string, body: ServiceTimes}; export type UpdateApiRestaurantsTenantRestaurantIdServicetimesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceTimesCollection = z.array(z.object({start: z.string().optional(), end: z.string().optional()})); export const serviceTimesServices = z.object({collection: serviceTimesCollection.optional(), delivery: serviceTimesCollection.optional()}); export const serviceTimesDays = z.object({monday: serviceTimesServices.optional(), tuesday: serviceTimesServices.optional(), wednesday: serviceTimesServices.optional(), thursday: serviceTimesServices.optional(), friday: serviceTimesServices.optional(), saturday: serviceTimesServices.optional(), sunday: serviceTimesServices.optional()}); export const serviceTimes = z.object({serviceTimes: serviceTimesDays.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 }; /** * Create or update service times * * PUT /restaurants/{tenant}/{restaurantId}/servicetimes */ export const updateApiRestaurantsTenantRestaurantIdServicetimes = async (args: UpdateApiRestaurantsTenantRestaurantIdServicetimesArgs, options?: UpdateApiRestaurantsTenantRestaurantIdServicetimesOptions) => { const {baseUrl = "https://uk.api.just-eat.io", 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(/\/+$/, '')}/restaurants/${encodeURIComponent(String(args.tenant))}/${encodeURIComponent(String(args.restaurantId))}/servicetimes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(serviceTimes, await res.json()) };