import {z} from 'zod' export type UserRole = 'System' | 'Operations' | 'Restaurant'; export type LegacyTempOfflineStatus = 'Unset' | 'None' | 'TempOffline' | 'ClosedToday' | 'ClosedDueToEvent' | 'FailedJctConnection' | 'NoTrOverride' | 'IgnoredOrders'; export type OfflineRestaurantsRequestModel = {restaurantIds: string, name: string, reason: string, startDate: string, endDate?: (string | null) | undefined, duration?: string | undefined, allowRestaurantOverride: boolean, legacyTempOfflineType?: LegacyTempOfflineStatus | undefined, category?: string | undefined}; export type CreateApiV1TenantRestaurantsEventOfflineArgs = {tenant: string, 'X-JE-Requester': string, 'X-JE-User-Role': UserRole, body: OfflineRestaurantsRequestModel}; export type CreateApiV1TenantRestaurantsEventOfflineOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1TenantRestaurantsEventOfflineResponse = z.object({restaurantEventId: z.string().optional(), restaurantIds: z.string().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 Offline Event * * POST /v1/{tenant}/restaurants/event/offline */ export const createApiV1TenantRestaurantsEventOffline = async (args: CreateApiV1TenantRestaurantsEventOfflineArgs, options?: CreateApiV1TenantRestaurantsEventOfflineOptions) => { 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") if (args['X-JE-Requester'] != null) headers.set("X-JE-Requester", String(args['X-JE-Requester'])) if (args['X-JE-User-Role'] != null) headers.set("X-JE-User-Role", String(args['X-JE-User-Role'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.tenant))}/restaurants/event/offline${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(createApiV1TenantRestaurantsEventOfflineResponse, await res.json()) };