import {z} from 'zod' export type GetApiV1OverviewOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const overview = z.object({box_office_name: z.string().optional(), climate_impact: z.object({contribution: z.number().int(), estimated_footprint: z.number().int()}).nullable().optional(), credits: z.number().optional(), currency: z.object({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"]), base_multiplier: z.number().int(), symbol: z.string()}).optional(), event_occurrences_draft: z.number().optional(), event_occurrences_published: z.number().optional(), event_series_draft: z.number().optional(), event_series_published: z.number().optional(), next_event_in: z.number().nullable().optional(), orders_received: z.number().optional(), period: z.string().optional(), revenue: z.number().optional(), total_issued_tickets: z.number().optional(), waitlist_signups: z.number().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 }; /** * Displays an overview for the Box Office * * GET /v1/overview */ export const getApiV1Overview = async (options?: GetApiV1OverviewOptions) => { 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 (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/overview${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(overview, await res.json()) };