import {z} from 'zod' export type GetApiV1IdTransactionsArgs = {id: string}; export type GetApiV1IdTransactionsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const apiProject = z.object({id: z.string(), fullName: z.string(), url: z.string(), publicUrl: z.string(), num: z.number()}); export const transactionDataDto = z.object({creditId: z.string(), tokenId: z.string(), amount: z.number(), serialization: z.string()}); export const apiTransactionDto = z.object({action: z.enum(["transfer", "retire", "transfer_retire", "cancel", "issuance", "receive", "claim", "create_project", "create_vintages", "multi_credit_action"]), type: z.enum(["exAnte", "exPost", "retirementCertificate"]), id: z.string(), toAddress: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), tokenId: z.string(), tokenAddress: z.string(), creditId: z.string(), serialization: z.string(), vintage: z.string(), amount: z.number(), fromOrganizationId: z.string(), toOrganizationId: z.string().optional(), project: apiProject, txId: z.string().optional(), state: z.enum(["processed", "failed", "pending_queue", "pending_onchain", "pending_request", "rejected", "pending_reserved", "cancelled"]), transactionData: transactionDataDto.optional(), retirementId: z.string().optional(), retirementReason: z.string().optional(), retirementComment: z.string().optional(), beneficiaryName: z.string().optional()}); export const entityTransactionsResponseDto = z.object({transactions: z.array(apiTransactionDto)}); 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 transactions for an entity * * GET /v1/{id}/transactions */ export const getApiV1IdTransactions = async (args: GetApiV1IdTransactionsArgs, options?: GetApiV1IdTransactionsOptions) => { 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/${encodeURIComponent(String(args.id))}/transactions${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(entityTransactionsResponseDto, await res.json()) };