import {z} from 'zod' export type GetApiApiV1BillingStatementsIdArgs = {id: string}; export type GetApiApiV1BillingStatementsIdOptions = {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 getApiApiV1BillingStatementsIdResponse = z.object({success: z.literal(true), data: z.object({statement: trustSignedStatement})}); 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 footprint statement * * GET /api/v1/billing/statements/{id} * * Also accepts auth: apiKey */ export const getApiApiV1BillingStatementsId = async (args: GetApiApiV1BillingStatementsIdArgs, options?: GetApiApiV1BillingStatementsIdOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/billing/statements/${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(getApiApiV1BillingStatementsIdResponse, await res.json()) };