import {z} from 'zod' export type Status = 'ACTIVE' | 'COMMITTED'; export type Breakdown = 'ARR' | 'MRR'; export type PageLimit = number; export type PageCursor = string; export type GetApiReportsArrWaterfallArgs = {month: unknown, status: Status, breakdown: Breakdown, subsidiary_id?: string | undefined, limit?: PageLimit | undefined, cursor?: PageCursor | undefined}; export type GetApiReportsArrWaterfallOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const revenue = z.object({year_month: z.string(), amount: roundedMonetaryAmount}); export const customerRevenue = z.object({id: z.string(), name: z.string(), total: revenue.optional()}); export const pageCursor = z.string(); export const pagination = z.object({next_cursor: pageCursor.optional()}); export const revenueWaterfall = z.object({customers: z.array(customerRevenue), pagination: pagination.optional()}); 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 }; /** * Retrieves ARR waterfall report * * GET /reports/arr-waterfall */ export const getApiReportsArrWaterfall = async (args: GetApiReportsArrWaterfallArgs, options?: GetApiReportsArrWaterfallOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.month != null) query.append("month", String(args.month)) if (args.status != null) query.append("status", String(args.status)) if (args.breakdown != null) query.append("breakdown", String(args.breakdown)) if (args.subsidiary_id != null) query.append("subsidiary_id", String(args.subsidiary_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/arr-waterfall${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(revenueWaterfall, await res.json()) };