import {z} from 'zod' export type CreateApiV1HoldsArgs = {body: {event_id: string, note: string, ticket_type_id: {[key: string]: string}}}; export type CreateApiV1HoldsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const hold = z.object({object: z.string().optional(), id: z.string().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()}).optional(), event_id: z.string().optional(), note: z.string().optional(), total_on_hold: z.number().int().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()}).optional(), quantities: z.array(z.object({ticket_type_id: z.string().optional(), quantity: z.number().int().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 }; /** * Create a hold * * POST /v1/holds */ export const createApiV1Holds = async (args: CreateApiV1HoldsArgs, options?: CreateApiV1HoldsOptions) => { 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/holds${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(hold, await res.json()) };