import {z} from 'zod' export type ULID = string; export type CurrencyCode = string; export type Numeric = string; export type DateTime = string; export type CreateCostBasisRequest = {fiat_code: CurrencyCode, rate: Numeric, effective_from?: DateTime | undefined, effective_to?: DateTime | undefined}; export type CreateApiOpenmeterCurrenciesCustomCurrencyIdCostBasesArgs = {currencyId: ULID, body: CreateCostBasisRequest}; export type CreateApiOpenmeterCurrenciesCustomCurrencyIdCostBasesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const uLID = z.string(); export const currencyCode = z.string().min(3).max(3); export const numeric = z.string(); export const dateTime = z.string(); export const billingCostBasis = z.object({id: uLID, fiat_code: currencyCode, rate: numeric, effective_from: dateTime.optional(), effective_to: dateTime.optional(), created_at: dateTime}); 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 }; /** * Create cost basis * * POST /openmeter/currencies/custom/{currencyId}/cost-bases */ export const createApiOpenmeterCurrenciesCustomCurrencyIdCostBases = async (args: CreateApiOpenmeterCurrenciesCustomCurrencyIdCostBasesArgs, options?: CreateApiOpenmeterCurrenciesCustomCurrencyIdCostBasesOptions) => { const {baseUrl = "http://localhost:8888/api/v3", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/openmeter/currencies/custom/${encodeURIComponent(String(args.currencyId))}/cost-bases${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(billingCostBasis, await res.json()) };