import {z} from 'zod' export type GetApiV2PlacesArgs = {search?: string | undefined, placeCategoryId?: string | undefined, 'placeSubCategoryId[]'?: Array | undefined, siteId?: string | undefined, buildingId?: string | undefined, floorId?: string | undefined, departmentId?: string | undefined}; export type GetApiV2PlacesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const placeCategory = z.object({id: z.string(), name: z.string(), showInMenu: z.boolean().optional(), color: z.string().optional(), markerUrl: z.string().optional(), priority: z.number().optional(), highlighted: z.boolean().optional()}); export const placeCategoryOverview = z.object({id: z.string(), name: z.string(), showInMenu: z.boolean().optional(), color: z.string().optional(), markerUrl: z.string().optional(), priority: z.number().optional(), highlighted: z.boolean().optional(), subCategory: placeCategory.optional()}); export const geoJsonPolygon = z.object({type: z.literal("Feature"), geometry: z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number())))})}); export const buildingOverview = z.object({id: z.string(), name: z.string(), siteId: z.string(), category: placeCategoryOverview, geoJson: geoJsonPolygon.optional()}); export const floor = z.object({id: z.string(), name: z.string(), level: z.number()}); export const department = z.object({id: z.string(), name: z.string().nullable(), type: z.string()}); export const placeOverview = z.object({latitude: z.number(), longitude: z.number(), id: z.string(), room: z.object({id: z.string(), name: z.string()}), category: placeCategoryOverview, site: z.object({id: z.string(), name: z.string()}), building: buildingOverview, floor: floor, department: department}); export const getApiV2PlacesResponse = z.object({data: z.array(placeOverview)}); 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 places | Cerca luoghi * * GET /v2/places */ export const getApiV2Places = async (args: GetApiV2PlacesArgs, options?: GetApiV2PlacesOptions) => { const {baseUrl = "https://app.didattica.polito.it/api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.placeCategoryId != null) query.append("placeCategoryId", String(args.placeCategoryId)) for (const item of args['placeSubCategoryId[]'] ?? []) query.append("placeSubCategoryId[]", String(item)) if (args.siteId != null) query.append("siteId", String(args.siteId)) if (args.buildingId != null) query.append("buildingId", String(args.buildingId)) if (args.floorId != null) query.append("floorId", String(args.floorId)) if (args.departmentId != null) query.append("departmentId", String(args.departmentId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/places${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(getApiV2PlacesResponse, await res.json()) };