import {z} from 'zod' export type IsometricIDPathParamRetirement = string; export type GetApiRetirementsIdCreditBatchesArgs = {id: IsometricIDPathParamRetirement, 'x-client-secret': string}; export type GetApiRetirementsIdCreditBatchesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const creditBatchStatus = z.enum(["active", "split", "retired"]); export const creditType = z.enum(["reduction", "removal"]); export const creditQuantity = z.object({credits: z.number(), credit_kgs: z.number().int()}); export const organisation = z.object({id: z.string().min(20).max(37), name: z.string(), domain: z.string().nullable()}); export const carbonRemovalData = z.object({gross_carbon_removed_kg: z.number().int(), net_carbon_removed_kg: z.number().int(), emissions_kg: z.number().int(), counterfactuals_kg: z.number().int(), losses_kg: z.number().int()}); export const creditBatch = z.object({id: z.string().min(20).max(37), serial_number: z.string(), status: creditBatchStatus, credit_type: creditType, size: creditQuantity, issued_at: z.string(), retired_at: z.string().nullable(), delivered_at: z.string().nullable(), sequestered_on: z.string(), country_of_issue: z.string().min(3).max(3), supplier_id: z.string().min(20).max(37), parent_id: z.string().min(20).max(37).nullable(), left_child_id: z.string().min(20).max(37).nullable(), right_child_id: z.string().min(20).max(37).nullable(), issuance_id: z.string().min(20).max(37), delivery_id: z.string().min(20).max(37).nullable(), beneficiary: organisation.nullable(), retirement_id: z.string().min(20).max(37).nullable(), ghg_entry_id: z.string().min(20).max(37), removal_id: z.string().min(20).max(37), owner: organisation, project_id: z.string().min(20).max(37), carbon_removal_data: carbonRemovalData.nullable(), ccp_approved: z.boolean()}); export const listResourceCreditBatch = z.object({nodes: z.array(creditBatch), total_count: z.number().int()}); 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 }; /** * Retirement Credit Batches * * GET /retirements/{id}/credit_batches */ export const getApiRetirementsIdCreditBatches = async (args: GetApiRetirementsIdCreditBatchesArgs, options: GetApiRetirementsIdCreditBatchesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-client-secret'] != null) headers.set("x-client-secret", String(args['x-client-secret'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/retirements/${encodeURIComponent(String(args.id))}/credit_batches${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(listResourceCreditBatch, await res.json()) };