import {z} from 'zod' export type EditCity = {name?: string | undefined}; export type EditState = {name?: string | undefined}; export type EditCountry = {iso2?: string | undefined}; export type AddImage = {attachment?: string | undefined, image_data?: string | undefined}; export type AddRestaurantCuisine = {id?: number | undefined}; export type AddRestaurantPhoto = {image?: string | undefined}; export type AddRestaurant = {name?: unknown | undefined, restaurant_id?: number | undefined, email?: string | undefined, phone?: unknown | undefined, mobile?: unknown | undefined, mobile_code?: unknown | undefined, fax?: unknown | undefined, contact_name?: unknown | undefined, contact_phone?: unknown | undefined, website?: unknown | undefined, address1?: unknown | undefined, address?: unknown | undefined, zip_code?: unknown | undefined, city?: EditCity | undefined, state?: EditState | undefined, country?: EditCountry | undefined, latitude?: number | undefined, longitude?: number | undefined, sales_tax?: number | undefined, minimum_order_for_booking?: number | undefined, is_allow_users_to_door_delivery_order?: number | undefined, estimated_time_to_delivery?: number | undefined, delivery_charge?: number | undefined, delivery_miles?: number | undefined, image?: AddImage | undefined, cuisine?: AddRestaurantCuisine | undefined, restaurant_photos?: Array | undefined, shop_trading_certificate?: AddImage | undefined}; export type CreateApiRestaurantsArgs = {body: AddRestaurant}; export type CreateApiRestaurantsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const editCity = z.object({name: z.string().optional()}); export const editState = z.object({name: z.string().optional()}); export const editCountry = z.object({iso2: z.string().optional()}); export const addImage = z.object({attachment: z.string().optional(), image_data: z.string().optional()}); export const addRestaurantCuisine = z.object({id: z.number().int().optional()}); export const addRestaurantPhoto = z.object({image: z.string().optional()}); export const addRestaurant = z.object({name: z.string().optional(), restaurant_id: z.number().int().optional(), email: z.string().optional(), phone: z.string().optional(), mobile: z.string().optional(), mobile_code: z.string().optional(), fax: z.string().optional(), contact_name: z.string().optional(), contact_phone: z.string().optional(), website: z.string().optional(), address1: z.string().optional(), address: z.string().optional(), zip_code: z.string().optional(), city: editCity.optional(), state: editState.optional(), country: editCountry.optional(), latitude: z.number().optional(), longitude: z.number().optional(), sales_tax: z.number().optional(), minimum_order_for_booking: z.number().optional(), is_allow_users_to_door_delivery_order: z.number().int().optional(), estimated_time_to_delivery: z.number().int().optional(), delivery_charge: z.number().optional(), delivery_miles: z.number().int().optional(), image: addImage.optional(), cuisine: addRestaurantCuisine.optional(), restaurant_photos: z.array(addRestaurantPhoto).optional(), shop_trading_certificate: addImage.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 }; /** * Creates a new restaurant * * POST /restaurants */ export const createApiRestaurants = async (args: CreateApiRestaurantsArgs, options: CreateApiRestaurantsOptions) => { const {baseUrl, 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${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(addRestaurant, await res.json()) };