import {z} from 'zod' export type GetApiV1MerchantsArgs = {query?: string | undefined, place?: string | undefined, placeIds?: string | undefined, centerPoint?: string | undefined, radius?: number | undefined, bookable?: boolean | undefined, tags?: string | undefined, tagIds?: string | undefined, reviewScoreMax?: number | undefined, onlyWithAttributes?: string | undefined, capacity: number, date?: string | undefined, fromTime?: string | undefined, offset?: number | undefined, limit?: number | undefined, 'Accept-Language'?: string | undefined, ivrNumber?: string | undefined}; export type GetApiV1MerchantsOptions = {baseUrl: string, APITOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const coordinatesDto = z.object({latitude: z.number().optional(), longitude: z.number().optional()}); export const merchantAddressDto = z.object({street: z.string().optional(), number: z.string().optional(), zipcode: z.string().optional(), city: z.string().optional(), country: z.string().optional(), district: z.string().optional()}); export const locationDto = z.object({coordinates: coordinatesDto.optional(), address: merchantAddressDto.optional()}); export const translatedTagDto = z.object({id: z.string().optional(), name: z.string().optional()}); export const tagGroupDto = z.object({type: z.enum(["CUISINE", "MEAL_TYPE", "ESTABLISHMENT_TYPE", "AREA", "INTERNET", "FOOD_RELATED", "ACCESSIBILITY", "PAYMENT", "PRICE", "ATMOSPHERE", "PARKING", "EXTRAS", "GOOD_FOR", "MENU_DETAILS"]).optional(), tags: z.array(translatedTagDto).optional()}); export const imageDto = z.object({url: z.string().optional()}); export const documentDto = z.object({name: z.string().optional(), url: z.string().optional(), format: z.string().optional(), description: z.string().optional()}); export const linkRelationDto = z.object({href: z.string().optional(), method: z.string().optional(), rel: z.enum(["DETAILS", "WIDGET", "WIDGET_DETAILS", "GET_MERCHANT", "PARENT", "SELF", "SETTINGS", "AVAILABILITY_DAYS", "AVAILABILITIES", "CREATE_RESERVATION", "GET_RESERVATION", "UPDATE_RESERVATION", "GET_CUSTOMER_RESERVATION", "GET_REVIEW", "PORTAL_PREORDER_FORM", "RESERVATION_CHECKOUT_PAGE", "CREDIT_CARD_DETAILS_PAGE", "CUSTOMER_EDIT_RESERVATION"]).optional()}); export const standardOpeningTimesDto = z.object({}); export const openingTimesDto = z.object({standardOpeningTimes: standardOpeningTimesDto}); export const chainDto = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const merchantDetailsDto = z.object({id: z.number().int(), name: z.string(), phoneNumber: z.string(), currency: z.string().optional(), locale: z.string().optional(), timezone: z.string(), location: locationDto, reviewScore: z.string(), tagGroups: z.array(tagGroupDto).optional(), images: z.array(imageDto).optional(), documents: z.array(documentDto).optional(), links: z.array(linkRelationDto), bookable: z.boolean(), openingTimes: openingTimesDto, ccvEnabled: z.boolean(), chain: chainDto.optional(), ivrRedirectNumber: z.string().optional(), shortDescription: z.string().optional(), longDescription: z.string().optional()}); export const merchantDetailsDtoList = z.object({merchants: z.array(merchantDetailsDto), size: z.number().int().optional(), offset: z.number().int().optional(), limit: z.number().int().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 }; /** * Allows to get merchant information by parameters * * GET /v1/merchants */ export const getApiV1Merchants = async (args: GetApiV1MerchantsArgs, options: GetApiV1MerchantsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APITOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.place != null) query.append("place", String(args.place)) if (args.placeIds != null) query.append("placeIds", String(args.placeIds)) if (args.centerPoint != null) query.append("centerPoint", String(args.centerPoint)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.bookable != null) query.append("bookable", String(args.bookable)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.tagIds != null) query.append("tagIds", String(args.tagIds)) if (args.reviewScoreMax != null) query.append("reviewScoreMax", String(args.reviewScoreMax)) if (args.onlyWithAttributes != null) query.append("onlyWithAttributes", String(args.onlyWithAttributes)) if (args.capacity != null) query.append("capacity", String(args.capacity)) if (args.date != null) query.append("date", String(args.date)) if (args.fromTime != null) query.append("fromTime", String(args.fromTime)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args.ivrNumber != null) query.append("ivrNumber", String(args.ivrNumber)) if (APITOKEN !== undefined) headers.set("X-Quandoo-AuthToken", APITOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/v1/merchants${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(merchantDetailsDtoList, await res.json()) };