import {z} from 'zod' export type GetApiInspectionsArgs = {siteId?: string | undefined, missionId?: string | undefined, assetId?: string | undefined, hasComments?: boolean | undefined, siteName?: string | undefined, 'siteType[]'?: (Array<'distribution_pole' | 'transmission_pole' | 'substation' | 'transmission_line' | 'distribution_line' | 'pipeline' | 'railway' | 'road' | 'other'>) | undefined, 'geometryType[]'?: (Array<'Point' | 'LineString' | 'Polygon'>) | undefined, 'priority[]'?: (Array<'high' | 'medium' | 'low' | 'none'>) | undefined, 'resolution[]'?: (Array<'all_resolved' | 'some_resolved' | 'none_resolved'>) | undefined, 'review[]'?: (Array<'all_reviewed' | 'some_reviewed' | 'none_reviewed'>) | undefined, includeUncapturable?: boolean | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiInspectionsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const siteInspection = z.object({id: z.string(), organizationId: z.string(), siteId: z.string(), missionId: z.string(), status: z.enum(["planned", "capture-pending", "capture-submitted", "capture-verified", "capture-failed", "analysis-pending", "analysis-submitted", "analysis-verified", "analysis-failed", "delivered"]), deletedAt: z.number().nullable(), createdAt: z.number(), not_capturable: z.boolean().optional()}); export const paginationMeta = z.object({cursor: z.string().nullable(), total: z.number().int().optional()}); export const getApiInspectionsResponse = z.object({data: z.array(siteInspection), meta: paginationMeta}); 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 }; /** * List site inspections * * GET /inspections * * Also accepts auth: ApiKeyAuth */ export const getApiInspections = async (args: GetApiInspectionsArgs, options: GetApiInspectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.siteId != null) query.append("siteId", String(args.siteId)) if (args.missionId != null) query.append("missionId", String(args.missionId)) if (args.assetId != null) query.append("assetId", String(args.assetId)) if (args.hasComments != null) query.append("hasComments", String(args.hasComments)) if (args.siteName != null) query.append("siteName", String(args.siteName)) for (const item of args['siteType[]'] ?? []) query.append("siteType[]", String(item)) for (const item of args['geometryType[]'] ?? []) query.append("geometryType[]", String(item)) for (const item of args['priority[]'] ?? []) query.append("priority[]", String(item)) for (const item of args['resolution[]'] ?? []) query.append("resolution[]", String(item)) for (const item of args['review[]'] ?? []) query.append("review[]", String(item)) if (args.includeUncapturable != null) query.append("includeUncapturable", String(args.includeUncapturable)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/inspections${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(getApiInspectionsResponse, await res.json()) };