import {z} from 'zod' export type GetApiV4OrganizationsOrganizationIdBillingPrePaidCreditsArgs = {page?: number | undefined, perPage?: number | undefined}; export type GetApiV4OrganizationsOrganizationIdBillingPrePaidCreditsOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const credit = z.object({id: z.string(), creditName: z.string(), supportPlan: z.string(), startDate: z.string(), expirationDate: z.string(), total: z.number(), used: z.number(), remaining: z.number(), remainingPercent: z.number()}); export const pages = z.object({page: z.number().int(), next: z.number().int().optional(), previous: z.number().int().optional(), last: z.number().int(), perPage: z.number().int(), totalItems: z.number().int()}); export const hrefs = z.object({first: z.string(), last: z.string(), previous: z.string(), next: z.string()}); export const cursor = z.object({pages: pages, hrefs: hrefs}); export const prepaidCreditsResponse = z.object({data: z.array(credit), cursor: cursor}); 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 Prepaid Credits Billing * * GET /v4/organizations/{organizationId}/billing/prePaidCredits */ export const getApiV4OrganizationsOrganizationIdBillingPrePaidCredits = async (args: GetApiV4OrganizationsOrganizationIdBillingPrePaidCreditsArgs, options?: GetApiV4OrganizationsOrganizationIdBillingPrePaidCreditsOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = 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.perPage != null) query.append("perPage", String(args.perPage)) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/billing/prePaidCredits${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(prepaidCreditsResponse, await res.json()) };