import {z} from 'zod' export type GetApiFestivalsArgs = {name?: string | undefined, festivalSeriesName?: string | undefined, perPage?: number | undefined, page?: number | undefined}; export type GetApiFestivalsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({page: z.number().int().gte(0).optional(), perPage: z.number().int().gte(1).optional(), totalItems: z.number().int().gte(0).optional(), totalPages: z.number().int().gte(0).optional(), nextPage: z.string().optional(), previousPage: z.string().optional()}); export const request = z.object({endpoint: z.string().optional(), method: z.string().optional(), params: z.array(z.unknown()).optional(), ip: z.union([z.string(), z.string()]).optional(), userAgent: z.string().optional()}); export const getApiFestivalsResponse = z.object({success: z.boolean().optional(), pagination: pagination.optional(), festivalSeries: z.array(z.object({"@type": z.string().optional(), identifier: z.string().optional(), name: z.string().optional(), url: z.string().optional(), image: z.string().optional(), "x-editionCount": z.number().int().optional(), "x-latestYear": z.number().int().optional(), "x-latestCity": z.string().optional(), "x-apiUrl": z.string().optional()})).optional(), request: request.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 }; /** * Search Festival Series * * GET /festivals */ export const getApiFestivals = async (args: GetApiFestivalsArgs, options?: GetApiFestivalsOptions) => { const {baseUrl = "https://api.data.jambase.com/v3", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.festivalSeriesName != null) query.append("festivalSeriesName", String(args.festivalSeriesName)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.page != null) query.append("page", String(args.page)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/festivals${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(getApiFestivalsResponse, await res.json()) };