import {z} from 'zod' export type GetApiV2SchoolsIdArgs = {id: number}; export type GetApiV2SchoolsIdOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const schoolsV2SchoolProfile = z.object({url: z.string().nullable().optional(), yearRange: z.string().nullable().optional(), icsea: z.number().int().nullable().optional(), bottomSeaQuarter: z.number().int().nullable().optional(), lowerMiddleSeaQuarter: z.number().int().nullable().optional(), upperMiddleSeaQuarter: z.number().int().nullable().optional(), topSeaQuarter: z.number().int().nullable().optional(), totalEnrolments: z.number().int().nullable().optional(), girlsEnrolments: z.number().int().nullable().optional(), boysEnrolments: z.number().int().nullable().optional()}); export const schoolsV2SchoolCatchment = z.object({type: z.enum(["Primary", "Secondary"]).nullable().optional()}); export const schoolsV2School = z.object({schoolSector: z.enum(["Catholic", "Government", "Independent"]).nullable().optional(), schoolType: z.enum(["Combined", "Primary", "Secondary", "Special"]).nullable().optional(), gender: z.enum(["Boys", "Girls", "CoEd"]).nullable().optional(), name: z.string().nullable().optional(), suburb: z.string().nullable().optional(), state: z.string().nullable().optional(), postcode: z.string().nullable().optional(), centroid: z.string().nullable().optional(), profile: schoolsV2SchoolProfile.optional(), catchments: z.array(schoolsV2SchoolCatchment).nullable().optional(), domainId: z.number().int().nullable().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 }; /** * Retrieve the details of a school by Id * * GET /v2/schools/{id} * * Also accepts auth: oauth2 */ export const getApiV2SchoolsId = async (args: GetApiV2SchoolsIdArgs, options?: GetApiV2SchoolsIdOptions) => { const {baseUrl = "https://api.domain.com.au", fetch: fetchFn = fetch, apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/schools/${encodeURIComponent(String(args.id))}${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(schoolsV2School, await res.json()) };