import {z} from 'zod' export type CreateApiV1IssuedTicketsArgs = {body: {event_id?: string | undefined, ticket_type_id?: string | undefined, hold_id?: string | undefined, full_name: string, email?: string | undefined, send_email?: boolean | undefined, barcode?: string | undefined, reference?: string | undefined}}; export type CreateApiV1IssuedTicketsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const issuedTicket = z.object({object: z.string(), id: z.string(), add_on_id: z.string().nullable().optional(), barcode: z.string(), barcode_url: z.string(), checked_in: z.string(), created_at: z.number().int(), custom_questions: z.array(z.object({question: z.string().optional(), answer: z.string().nullable().optional()})), description: z.string(), email: z.string().nullable(), event_id: z.string(), event_series_id: z.string(), group_ticket_barcode: z.string().nullable(), reference: z.string().nullable(), full_name: z.string().nullable(), first_name: z.string().nullable(), last_name: z.string().nullable(), listed_currency: z.object({base_multiplier: z.number().int().optional(), code: 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"]).optional()}).nullable().optional(), listed_price: z.number().int().nullable().optional(), reservation: z.string().nullable(), status: z.enum(["valid", "voided"]), source: z.enum(["api", "dashboard_import"]).nullable(), ticket_type_id: z.string().nullable(), updated_at: z.number().int(), voided_at: z.number().int().nullable(), order_id: z.string().nullable(), qr_code_url: z.string()}); export const createApiV1IssuedTicketsResponse = z.object({data: z.array(issuedTicket).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 an issued ticket * * POST /v1/issued_tickets */ export const createApiV1IssuedTickets = async (args: CreateApiV1IssuedTicketsArgs, options?: CreateApiV1IssuedTicketsOptions) => { 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/issued_tickets${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(createApiV1IssuedTicketsResponse, await res.json()) };