import {z} from 'zod' export type GetApiApiV1BillingStatementsArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiApiV1BillingStatementsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const trustSignedStatement = z.object({id: z.string(), billingPeriodId: z.string().nullable(), artifactType: z.literal("footprint_statement"), statementVersion: z.number().int().gt(0), status: z.enum(["pending", "signing", "signed", "failed", "unavailable"]), period: z.object({start: z.string(), end: z.string()}), usage: z.record(z.string(), z.string()), footprint: z.record(z.string(), z.string()), methodology: z.object({factorVersions: z.array(z.string()), waterFactorVersions: z.array(z.string()), layers: z.record(z.string(), z.unknown()).nullable()}), statementUrl: z.string().nullable(), verificationUrl: z.string().nullable(), signedAt: z.string().nullable(), createdAt: z.string(), supersededByStatementId: z.string().nullable()}); export const paginationMeta = z.object({page: z.number().int().gte(0), limit: z.number().int().gt(0), total: z.number().int().gte(0), totalPages: z.number().int().gte(0)}); export const getApiApiV1BillingStatementsResponse = z.object({success: z.literal(true), data: z.array(trustSignedStatement), pagination: paginationMeta}); 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 }; /** * List footprint statements * * GET /api/v1/billing/statements * * Also accepts auth: apiKey */ export const getApiApiV1BillingStatements = async (args: GetApiApiV1BillingStatementsArgs, options?: GetApiApiV1BillingStatementsOptions) => { const {baseUrl = "https://api.crbonfree.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/billing/statements${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(getApiApiV1BillingStatementsResponse, await res.json()) };