import {z} from 'zod' export type GetApiV2OrdersArgs = {created_at?: number | undefined, 'created_at.gt'?: number | undefined, 'created_at.gte'?: number | undefined, 'created_at.lt'?: number | undefined, 'created_at.lte'?: number | undefined, starting_after?: string | undefined, ending_before?: string | undefined, limit?: string | undefined, name?: string | undefined, email?: string | undefined, txn_id?: string | undefined, barcode?: string | undefined, event_id?: string | undefined, event_series_id?: string | undefined, status?: ('completed' | 'pending' | 'cancelled') | undefined, store_id?: string | undefined, referral_tag?: string | undefined}; export type GetApiV2OrdersOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const lineItemV2 = z.object({object: z.string().optional(), id: z.string().optional(), booking_fee: z.number().int().optional(), bundle_id: z.string().nullable().optional(), description: z.string().optional(), discount_id: z.string().nullable().optional(), event_summary: z.object({end_date: z.string().optional(), event_id: z.string().optional(), event_series_id: z.string().optional(), name: z.string().optional(), start_date: z.string().optional()}).nullable().optional(), fulfilment: z.string().nullable().optional(), fulfilments: z.array(z.object({reference_id: z.string().optional(), reference_name: z.string().nullable().optional()})).optional(), item_id: z.string().nullable().optional(), list_booking_fee: z.number().int().optional(), list_price: z.number().int().optional(), quantity: z.number().int().optional(), store_summary: z.object({name: z.string().nullable().optional(), store_id: z.string().optional()}).nullable().optional(), tax: z.object({amount: z.number().int().nullable().optional(), label: z.string().nullable().optional(), rate: z.number().optional(), treatment: z.string().nullable().optional()}).nullable().optional(), total: z.number().int().optional(), type: z.string().optional(), value: z.number().int().optional()}); export const orderV2 = z.object({object: z.string().optional(), id: z.string().optional(), buyer_details: z.object({address: z.object({address_1: z.string().nullable().optional(), address_2: z.string().nullable().optional(), address_3: z.string().nullable().optional(), postal_code: z.string().nullable().optional()}).optional(), custom_questions: z.array(z.object({question: z.string().optional(), answer: z.string().nullable().optional()})).optional(), email: z.string().nullable().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), name: z.string().nullable().optional(), phone: z.string().nullable().optional()}).optional(), created_at: z.string().optional(), currency: z.object({base_multiplier: z.number().int().optional(), code: z.string().optional(), symbol: z.string().optional()}).optional(), line_items: z.array(lineItemV2).optional(), marketing_opt_in: z.boolean().nullable().optional(), meta_data: z.array(z.object({key: z.string().optional(), value: z.unknown()})).optional(), notes: z.string().nullable().optional(), referral_tag: z.string().nullable().optional(), status: z.string().optional(), status_message: z.string().nullable().optional(), total: z.number().int().nullable().optional(), transactions: z.array(z.object({transaction_id: z.string().nullable().optional(), type: z.string().optional(), amount: z.number().int().optional(), currency: z.string().optional(), payment_method: z.string().optional(), pending: z.boolean().optional(), created_at: z.string().optional()})).optional(), payment_plan: z.object({status: z.string().optional(), currency: z.string().optional(), total: z.number().int().optional(), amount_paid: z.number().int().optional(), amount_outstanding: z.number().int().optional(), instalments: z.array(z.object({id: z.string().optional(), sequence_number: z.number().int().optional(), amount: z.number().int().optional(), currency: z.string().optional(), due_date: z.string().optional(), status: z.string().optional(), is_deposit: z.boolean().optional()})).optional()}).nullable().optional()}); export const getApiV2OrdersResponse = z.object({data: z.array(orderV2).optional(), links: z.object({next: z.string().nullable().optional(), previous: z.string().nullable().optional()}).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 orders * * GET /v2/orders */ export const getApiV2Orders = async (args: GetApiV2OrdersArgs, options?: GetApiV2OrdersOptions) => { const {baseUrl = "https://api.tickettailor.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_at != null) query.append("created_at", String(args.created_at)) if (args['created_at.gt'] != null) query.append("created_at.gt", String(args['created_at.gt'])) if (args['created_at.gte'] != null) query.append("created_at.gte", String(args['created_at.gte'])) if (args['created_at.lt'] != null) query.append("created_at.lt", String(args['created_at.lt'])) if (args['created_at.lte'] != null) query.append("created_at.lte", String(args['created_at.lte'])) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", String(args.name)) if (args.email != null) query.append("email", String(args.email)) if (args.txn_id != null) query.append("txn_id", String(args.txn_id)) if (args.barcode != null) query.append("barcode", String(args.barcode)) if (args.event_id != null) query.append("event_id", String(args.event_id)) if (args.event_series_id != null) query.append("event_series_id", String(args.event_series_id)) if (args.status != null) query.append("status", String(args.status)) if (args.store_id != null) query.append("store_id", String(args.store_id)) if (args.referral_tag != null) query.append("referral_tag", String(args.referral_tag)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/orders${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(getApiV2OrdersResponse, await res.json()) };