import {z} from 'zod' export type UUID = string; export type AirScrubberRequest = {organization_id: UUID, supported_air_scrubber_id: number, serial_number?: (string | null) | undefined, is_rental: boolean, asset_identifier: string}; export type CreateApiV1AirScrubbersArgs = {'X-Encircle-Attribution'?: string | undefined, 'X-Encircle-Request'?: string | undefined, body: AirScrubberRequest}; export type CreateApiV1AirScrubbersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const airScrubberResponse = z.object({id: z.number().int().gte(0), id_v2: uUID, organization_id: uUID, supported_air_scrubber_id: z.number().int(), manufacturer: z.string(), model: z.string(), serial_number: z.string().nullable(), is_rental: z.boolean(), asset_identifier: z.string(), status: z.enum(["available", "active", "retired"]), property_claim_id: z.number().int().nullable()}); 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 }; /** * Create an organization Air Scrubber * * POST /v1/air_scrubbers */ export const createApiV1AirScrubbers = async (args: CreateApiV1AirScrubbersArgs, options: CreateApiV1AirScrubbersOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Encircle-Attribution'] != null) headers.set("X-Encircle-Attribution", String(args['X-Encircle-Attribution'])) if (args['X-Encircle-Request'] != null) headers.set("X-Encircle-Request", String(args['X-Encircle-Request'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/air_scrubbers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(airScrubberResponse, await res.json()) };