import {z} from 'zod' export type CreateApiReportingUnitArgs = {body: {organizationId?: string | undefined, reportingUnitId?: string | undefined, limit?: number | undefined, nextToken?: string | undefined, branches?: boolean | undefined, historical?: boolean | undefined, drafts?: boolean | undefined, filterReportingUnitIds?: Array | undefined}}; export type CreateApiReportingUnitOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportingUnitEntry: z.ZodType = z.object({organizationId: z.string().optional(), reportingUnitId: z.string().optional(), branchId: z.string().optional(), controlReportingUnitId: z.string().optional(), controlForReportingUnitId: z.string().optional(), tags: z.array(z.string().min(1).max(255)).optional(), label: z.string().optional(), description: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), analysisSubmittedAt: z.string().optional(), analysisCompletedAt: z.string().optional(), qualityControlledAt: z.string().optional(), deletedAt: z.string().optional(), refundedAt: z.string().optional(), usersNotifiedAt: z.string().optional(), webhookNotifiedAt: z.string().optional(), periodChangeStartYear: z.number().int().optional(), periodChangeEndYear: z.number().int().optional(), boundaryPath: z.string().optional(), hidden: z.boolean().optional(), controlReportingUnit: z.lazy(() => reportingUnitEntry.optional()), areaKm2: z.number().optional(), analyzedAreaKm2: z.number().optional(), pointX: z.number().optional(), pointY: z.number().optional(), pointR: z.number().optional(), srcOrganizationId: z.string().optional(), dataPath: z.string().optional(), periodChangeStartYearStock: z.number().optional(), periodChangeStartYearStockError: z.number().optional(), periodChangeEndYearStock: z.number().optional(), periodChangeEndYearStockError: z.number().optional(), periodChange: z.number().optional(), periodChangeError: z.number().optional(), periodChangePercent: z.number().optional(), periodChangeGain: z.number().optional(), periodChangeGainError: z.number().optional(), periodChangeLoss: z.number().optional(), periodChangeLossError: z.number().optional(), dryrun: z.boolean().optional(), calibratedTo: z.string().optional(), calibratedFrom: z.string().optional(), mangrove: z.boolean().optional()}); export const createApiReportingUnitResponse = z.union([z.object({reportingUnits: z.array(reportingUnitEntry).optional(), nextToken: z.string().optional()}), z.array(reportingUnitEntry)]); 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 one or more reporting unit entries. * * POST /reportingUnit */ export const createApiReportingUnit = async (args: CreateApiReportingUnitArgs, options?: CreateApiReportingUnitOptions) => { const {baseUrl = "https://app.chloris.earth/api", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reportingUnit${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(createApiReportingUnitResponse, await res.json()) };