import {z} from 'zod' export type GetApiApiTeachingEventsSearchArgs = {Postcode?: (string | null) | undefined, Radius?: (number | null) | undefined, TypeIds?: (Array | null) | undefined, Online?: (boolean | null) | undefined, StartAfter?: (string | null) | undefined, StartBefore?: (string | null) | undefined, StatusIds?: (Array | null) | undefined, AccessibilityOptions?: (Array | null) | undefined, quantity?: number | undefined}; export type GetApiApiTeachingEventsSearchOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const teachingEventBuilding = z.object({id: z.string().nullable().optional(), venue: z.string().min(1), addressLine1: z.string().nullable().optional(), addressLine2: z.string().nullable().optional(), addressLine3: z.string().nullable().optional(), addressCity: z.string().nullable().optional(), addressPostcode: z.string().nullable().optional(), imageUrl: z.string().nullable().optional()}); export const teachingEvent = z.object({id: z.string().nullable().optional(), typeId: z.number().int().optional(), statusId: z.number().int().optional(), regionId: z.number().int().nullable().optional(), readableId: z.string().min(1), webFeedId: z.string().nullable().optional(), referenceNumber: z.string().nullable().optional(), registrationEmailLink: z.string().nullable().optional(), isOnline: z.boolean().optional(), name: z.string().min(1), summary: z.string().nullable().optional(), message: z.string().nullable().optional(), description: z.string().nullable().optional(), videoUrl: z.string().nullable().optional(), scribbleId: z.string().nullable().optional(), providerWebsiteUrl: z.string().nullable().optional(), providerTargetAudience: z.string().nullable().optional(), providerOrganiser: z.string().nullable().optional(), providerContactEmail: z.string().max(100).nullable().optional(), startAt: z.string().optional(), endAt: z.string().optional(), providersList: z.string().nullable().optional(), building: teachingEventBuilding.optional(), isVirtual: z.boolean().optional(), accessibilityOptions: z.array(z.number().int()).nullable().optional(), isInPerson: z.boolean().optional()}); export const getApiApiTeachingEventsSearchResponse = z.array(teachingEvent); 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 }; /** * Searches for teaching events. * * GET /api/teaching_events/search */ export const getApiApiTeachingEventsSearch = async (args: GetApiApiTeachingEventsSearchArgs, options: GetApiApiTeachingEventsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Postcode != null) query.append("Postcode", String(args.Postcode)) if (args.Radius != null) query.append("Radius", String(args.Radius)) for (const item of args.TypeIds ?? []) query.append("TypeIds", String(item)) if (args.Online != null) query.append("Online", String(args.Online)) if (args.StartAfter != null) query.append("StartAfter", String(args.StartAfter)) if (args.StartBefore != null) query.append("StartBefore", String(args.StartBefore)) for (const item of args.StatusIds ?? []) query.append("StatusIds", String(item)) for (const item of args.AccessibilityOptions ?? []) query.append("AccessibilityOptions", String(item)) if (args.quantity != null) query.append("quantity", String(args.quantity)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/teaching_events/search${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(getApiApiTeachingEventsSearchResponse, await res.json()) };