import {z} from 'zod' export type GetApiV1MerchantsMerchantIdAvailabilitiesArgs = {merchantId: number, agentId: number, capacity?: number | undefined}; export type GetApiV1MerchantsMerchantIdAvailabilitiesOptions = {baseUrl: string, APITOKEN?: string | undefined, fetch?: typeof fetch | undefined}; 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 merchantAvailabilityDaysDto = z.object({days: z.array(z.string()).optional(), links: z.array(linkRelationDto).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 }; /** * Get merchant availability days * * GET /v1/merchants/{merchantId}/availabilities */ export const getApiV1MerchantsMerchantIdAvailabilities = async (args: GetApiV1MerchantsMerchantIdAvailabilitiesArgs, options: GetApiV1MerchantsMerchantIdAvailabilitiesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APITOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agentId != null) query.append("agentId", String(args.agentId)) if (args.capacity != null) query.append("capacity", String(args.capacity)) if (APITOKEN !== undefined) headers.set("X-Quandoo-AuthToken", APITOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/v1/merchants/${encodeURIComponent(String(args.merchantId))}/availabilities${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(merchantAvailabilityDaysDto, await res.json()) };