import {z} from 'zod' export type GetApiV1RetirementsIdArgs = {id: string}; export type GetApiV1RetirementsIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const apiEntityDto = z.object({fullName: z.string(), logo: z.string(), id: z.string(), publicUrl: z.string().optional()}); export const apiProject = z.object({id: z.string(), fullName: z.string(), url: z.string(), publicUrl: z.string(), num: z.number()}); export const apiRetirementDto = z.object({createdAt: z.string(), id: z.string(), isClaimed: z.boolean().optional(), reason: z.string().optional(), comment: z.string().optional(), beneficiary: z.string().optional(), organizationId: z.string(), organizationOwner: apiEntityDto.optional(), serialization: z.string().optional(), vintage: z.string().optional(), amount: z.number(), project: apiProject}); export const retirementResponseDto = z.object({retirement: apiRetirementDto}); 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 a specific retirement by ID * * GET /v1/retirements/{id} */ export const getApiV1RetirementsId = async (args: GetApiV1RetirementsIdArgs, options?: GetApiV1RetirementsIdOptions) => { const {baseUrl = "https://api.carbonregistry.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/retirements/${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(retirementResponseDto, await res.json()) };