import {z} from 'zod' export type GetApiPoslinkV1TabsArgs = {store_id: string, status?: (Array<'OPEN' | 'PAYING' | 'PAUSED' | 'COMPLETED' | 'CLOSED'>) | undefined, merchant_reference?: string | undefined, after?: string | undefined, before?: string | undefined, limit?: number | undefined}; export type GetApiPoslinkV1TabsOptions = {baseUrl?: string | undefined, authorisation?: string | undefined, fetch?: typeof fetch | undefined}; export const tabAmountWithCurrency = z.object({amount: z.number().int(), currency: z.string()}); export const tabSummary = z.object({tab_id: z.string(), tab_name: z.string(), merchant_reference: z.string(), status: z.enum(["OPEN", "PAYING", "PAUSED", "COMPLETED", "CLOSED"]), total_amount: tabAmountWithCurrency, total_paid: z.number().int(), remaining: z.number().int(), showing_bill_terminal_id: z.string().optional()}); export const cursors = z.object({before: z.string().optional(), after: z.string().optional()}); export const paths = z.object({previous: z.string().optional(), next: z.string().optional()}); export const cursorPaging = z.object({cursors: cursors.optional(), paths: paths.optional()}); export const tabListResponse = z.object({tabs: z.array(tabSummary).optional(), paging: cursorPaging.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 }; /** * List tabs for a store * * GET /poslink/v1/tabs */ export const getApiPoslinkV1Tabs = async (args: GetApiPoslinkV1TabsArgs, options?: GetApiPoslinkV1TabsOptions) => { const {baseUrl = "https://api.teya.com", fetch: fetchFn = fetch, authorisation} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.store_id != null) query.append("store_id", String(args.store_id)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.merchant_reference != null) query.append("merchant_reference", String(args.merchant_reference)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.limit != null) query.append("limit", String(args.limit)) if (authorisation !== undefined) headers.set('authorization', `Bearer ${authorisation}`) const url = `${baseUrl.replace(/\/+$/, '')}/poslink/v1/tabs${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(tabListResponse, await res.json()) };