import {z} from 'zod' export type GetApiProjectsIdArgs = {'x-icr-api-version'?: string | undefined, id: string}; export type GetApiProjectsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiPaginationDto = z.object({total: z.number(), currentPage: z.number(), pageCount: z.number(), nextPage: z.number().optional(), prevPage: z.number().optional()}); export const apiSectorDto = z.object({id: z.string(), title: z.string()}); export const apiOtherBenefitDto = z.object({title: z.string(), checked: z.boolean(), id: z.number(), description: z.string()}); export const apiProponentDto = z.object({fullName: z.string(), logo: z.string(), id: z.string(), publicUrl: z.string()}); export const apiMethodologyDto = z.object({id: z.string(), title: z.string()}); export const apiProjectTypeDto = z.object({id: z.enum(["removal", "avoidance", "hybrid"]), title: z.string(), description: z.string().optional()}); export const apiLocationDto = z.object({lat: z.number(), lng: z.number()}); export const apiDocumentDto = z.object({id: z.string(), uri: z.string(), type: z.enum(["proofOfConcept", "proofOfOwnership", "projectDescription", "validationReport", "monitoringReport", "verificationReport", "otherNote", "noIssuanceStatement", "hostCountryApprovalDocument", "kmlFile", "representationDeed", "projectDesignDescriptionAndMonitoringReport", "vvbAgreement", "validationAndVerificationReport", "methodologyConcept", "nonPerformanceReport", "reversalEventReport"]), name: z.string(), isPublic: z.boolean()}); export const apiProjectDto = z.object({city: z.string().optional(), sector: apiSectorDto.optional(), num: z.number(), id: z.string(), otherBenefits: z.array(apiOtherBenefitDto).optional(), fullName: z.string(), thumbnail: z.string().optional(), description: z.string(), shortDescription: z.string(), proponents: z.array(apiProponentDto), publicUrl: z.string(), registry: z.string(), status: z.enum(["draft", "project_concept", "under_development", "under_validation", "validated", "closed", "retracted"]), countryCode: z.string().optional(), state: z.string().optional(), startDate: z.string(), creditingPeriodStartDate: z.string(), methodology: apiMethodologyDto, type: apiProjectTypeDto, estimatedAnnualMitigations: z.array(z.string()), location: apiLocationDto.optional(), kmlFile: apiDocumentDto.optional(), validators: z.array(apiProponentDto), documentation: z.array(apiDocumentDto)}); export const projectsResponseDto = z.object({pagination: apiPaginationDto, projects: z.array(apiProjectDto)}); 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 }; /** * Get projects * * GET /projects/{id} */ export const getApiProjectsId = async (args: GetApiProjectsIdArgs, options?: GetApiProjectsIdOptions) => { const {baseUrl = "https://api.carbonregistry.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-icr-api-version'] != null) headers.set("x-icr-api-version", String(args['x-icr-api-version'])) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.id))}${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(projectsResponseDto, await res.json()) };