import {z} from 'zod' export type StoreId = string; export type BootstrapMenuRequest = {templateName: string, externalServiceSlug: string, enableTemplate?: (boolean | null) | undefined, stationId?: (string | null) | undefined, posSlug?: (string | null) | undefined}; export type CreateApiManagerMenuV1BootstrapArgs = {'X-Store-Id': StoreId, body: BootstrapMenuRequest}; export type CreateApiManagerMenuV1BootstrapOptions = {baseUrl?: string | undefined, OAuth20?: string | undefined, fetch?: typeof fetch | undefined}; export const jobReference = z.object({id: z.string().optional(), status: z.enum(["PENDING", "FAILED", "SUCCESS", "UNKNOWN"]).optional()}); export const menuJobType = z.enum(["UPSERT", "PUBLISH", "UPDATE_AVAILABILITY", "SYNC", "UNKNOWN"]); export const menuPublishJobState = z.object({status: z.enum(["PENDING", "FAILED", "SUCCESS", "UNKNOWN"]).optional(), message: z.string().nullable().optional()}); export const menuJobPublishState = z.record(z.string(), menuPublishJobState).nullable(); export const menuAsynchronousJob = z.object({jobReference: jobReference.optional(), jobType: menuJobType.optional(), publishJobState: menuJobPublishState.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 }; /** * Bootstrap menus for a store * * POST /manager/menu/v1/bootstrap */ export const createApiManagerMenuV1Bootstrap = async (args: CreateApiManagerMenuV1BootstrapArgs, options?: CreateApiManagerMenuV1BootstrapOptions) => { const {baseUrl = "https://partners.tryotter.com", fetch: fetchFn = fetch, OAuth20} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Store-Id'] != null) headers.set("X-Store-Id", String(args['X-Store-Id'])) if (OAuth20 !== undefined) headers.set('authorization', `Bearer ${OAuth20}`) const url = `${baseUrl.replace(/\/+$/, '')}/manager/menu/v1/bootstrap${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(menuAsynchronousJob, await res.json()) };