import {z} from 'zod' export type CreateApiV1ProductsArgs = {body: {booking_fee?: number | undefined, currency?: string | undefined, description?: string | undefined, event_series_ids?: Array | undefined, image?: string | undefined, instructions?: string | undefined, issue_ticket_voucher?: string | undefined, linked_to_all_event_series?: string | undefined, membership_type_id?: string | undefined, name: string, price: number, quantity?: number | undefined, quantity_per_event?: number | undefined, status?: string | undefined, voucher_id?: string | undefined}}; export type CreateApiV1ProductsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const product = z.object({object: z.string().optional(), id: z.string().optional(), booking_fee: z.number().int().nullable().optional(), created_at: z.object({date: z.string().optional(), formatted: z.string().optional(), iso: z.string().optional(), time: z.string().optional(), timezone: z.string().optional(), unix: z.number().int().optional()}).nullable().optional(), currency: z.enum(["gbp", "usd", "eur", "sgd", "aud", "brl", "cad", "czk", "dkk", "hkd", "huf", "ils", "jpy", "myr", "mxn", "nok", "nzd", "php", "pln", "rub", "sek", "chf", "twd", "thb", "try"]).nullable().optional(), description: z.string().nullable().optional(), event_series_ids: z.array(z.string()).nullable().optional(), image: z.string().nullable().optional(), instructions: z.string().nullable().optional(), issued_count: z.number().int().optional(), issue_ticket_voucher: z.string().nullable().optional(), linked_to_all_event_series: z.string().nullable().optional(), membership_type_id: z.string().nullable().optional(), name: z.string().optional(), price: z.number().int().optional(), quantity: z.number().int().nullable().optional(), quantity_per_event_occurrence: z.number().int().nullable().optional(), sell_in_store: z.string().optional(), status: z.enum(["on_sale", "hidden", "sold_out"]).nullable().optional(), voucher_id: z.string().nullable().optional(), updated_at: z.object({date: z.string().optional(), formatted: z.string().optional(), iso: z.string().optional(), time: z.string().optional(), timezone: z.string().optional(), unix: z.number().int().optional()}).nullable().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 a product * * POST /v1/products */ export const createApiV1Products = async (args: CreateApiV1ProductsArgs, options?: CreateApiV1ProductsOptions) => { const {baseUrl = "https://api.tickettailor.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/products${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(product, await res.json()) };