import {z} from 'zod' export type ApeBusinessLocationId = number; export type ApeCustomerInfo = {firstName: string, lastName?: string | undefined, thirdPartyReference?: string | undefined, email?: string | undefined, contactNumberAsE164?: string | undefined, notes?: string | undefined, salutation?: string | undefined, sendEmailReceipts?: boolean | undefined, emailNotification?: ('ENABLE' | 'DISABLE' | 'DONT_CHANGE') | undefined}; export type ApeOrderPayment = {paymentMethod?: string | undefined, paymentAmount: number, tipAmount?: number | undefined}; export type ApeItemModifier = {modifierId?: string | undefined}; export type ApeSubItemOrderLine = {quantity: number, sku: string, customItemName?: string | undefined, customItemPrice?: number | undefined, modifiers?: Array | undefined, subItems?: Array | undefined}; export type ApeToGoOrderItemLine = {quantity: number, sku: string, customItemName?: string | undefined, customItemPrice?: number | undefined, discountAmountOverride?: number | undefined, discountPercentOverride?: number | undefined, modifiers?: Array | undefined, discountCode?: string | undefined, subItems?: Array | undefined}; export type CreateApiOOp1OrderToGoArgs = {body: {businessLocationId: ApeBusinessLocationId, thirdPartyReference: string, endpointId: string, customerInfo: ApeCustomerInfo, orderNote?: string | undefined, maxTimeToAttemptOrderDeliverToPos?: number | undefined, staffId?: number | undefined, accountProfileCode?: string | undefined, scheduledTimeForOrderAsIso8601?: string | undefined, payment?: ApeOrderPayment | undefined, collectionCode?: string | undefined, orderCollectionTimeAsIso8601?: string | undefined, items?: Array | undefined, deliveryAddress?: ({addressLine1?: string | undefined, addressLine2?: string | undefined, zip?: string | undefined, city?: string | undefined}) | undefined, targetPrinterProfileId?: number | undefined}}; export type CreateApiOOp1OrderToGoOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiOOp1OrderToGoResponse = z.object({status: 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 To Go Order * * POST /o/op/1/order/toGo */ export const createApiOOp1OrderToGo = async (args: CreateApiOOp1OrderToGoArgs, options?: CreateApiOOp1OrderToGoOptions) => { const {baseUrl = "https://api.trial.lsk.lightspeed.app", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/o/op/1/order/toGo${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(createApiOOp1OrderToGoResponse, await res.json()) };