import {z} from 'zod' export type GetApiV2PlacesPlaceIdArgs = {placeId: string}; export type GetApiV2PlacesPlaceIdOptions = {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 place = 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, geoJson: geoJsonPolygon, capacity: z.number().int(), structure: z.object({name: z.string(), shortName: z.string(), email: z.string().nullable(), phone: z.string().nullable()}).nullable(), resources: z.array(z.object({name: z.string(), description: z.string(), category: z.string()}))}); export const getApiV2PlacesPlaceIdResponse = z.object({data: place}); 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 }; /** * Show place | Mostra luogo * * GET /v2/places/{placeId} */ export const getApiV2PlacesPlaceId = async (args: GetApiV2PlacesPlaceIdArgs, options?: GetApiV2PlacesPlaceIdOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/places/${encodeURIComponent(String(args.placeId))}${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(getApiV2PlacesPlaceIdResponse, await res.json()) };