import {z} from 'zod' export type GetApiApiV1OrgsOrgIdBillingArgs = {org_id: string}; export type GetApiApiV1OrgsOrgIdBillingOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const billingNotConfigured = z.object({state: z.literal("not_configured").optional()}); export const billingUnlinked = z.object({state: z.literal("unlinked").optional(), org_id: z.string()}); export const billingNoSubscription = z.object({state: z.literal("no_subscription").optional()}); export const billingSubscribed = z.object({state: z.literal("subscribed").optional(), status: z.string(), collection_method: z.string(), days_until_due: z.number().int().nullable(), invoiced_seats: z.number().int(), member_seats: z.number().int(), unit_amount: z.number().int(), currency: z.string(), current_period_ends_at: z.string(), cancel_at_period_end: z.boolean()}); export const billingNeedsAttention = z.object({state: z.literal("needs_attention").optional()}); export const billingUnavailable = z.object({state: z.literal("unavailable").optional()}); export const getApiApiV1OrgsOrgIdBillingResponse = z.discriminatedUnion("state", [billingNotConfigured, billingUnlinked, billingNoSubscription, billingSubscribed, billingNeedsAttention, billingUnavailable]); 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 Billing * * GET /api/v1/orgs/{org_id}/billing */ export const getApiApiV1OrgsOrgIdBilling = async (args: GetApiApiV1OrgsOrgIdBillingArgs, options: GetApiApiV1OrgsOrgIdBillingOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/orgs/${encodeURIComponent(String(args.org_id))}/billing${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(getApiApiV1OrgsOrgIdBillingResponse, await res.json()) };