import {z} from 'zod' export type GetApiApiopsProjectsProjectNameConsumerOrganizationsOrganizationCodeUsageHierarchyArgs = {projectName: string, organizationCode: string, from?: string | undefined, to?: string | undefined, level?: string | undefined, includeDescendants?: boolean | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiopsProjectsProjectNameConsumerOrganizationsOrganizationCodeUsageHierarchyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scope = z.object({type: z.enum(["ORGANIZATION", "CONSUMER", "APPLICATION", "API_CLIENT", "API_PROXY"]).optional(), key: z.string().optional(), name: z.string().optional()}); export const period = z.object({from: z.string().optional(), to: z.string().optional(), effectiveFrom: z.string().optional(), effectiveTo: z.string().optional(), timeZone: z.string().optional()}); export const filters = z.object({model: z.string().optional(), environmentName: z.string().optional(), environmentApplied: z.boolean().optional()}); export const totals = z.object({tokenInput: z.number().int().optional(), tokenOutput: z.number().int().optional(), tokenCached: z.number().int().optional(), tokenTotal: z.number().int().optional(), costUsd: z.number().optional(), requests: z.number().int().optional(), failedRequests: z.number().int().optional()}); export const node = z.object({code: z.string().optional(), name: z.string().optional(), classification: z.enum(["ORGANIZATION", "UNASSIGNED", "ANONYMOUS"]).optional(), depth: z.number().int().optional(), shared: z.boolean().optional(), memberCount: z.number().int().optional(), descendantMemberCount: z.number().int().optional(), direct: totals.optional(), rollup: totals.optional(), budgetPercent: z.number().optional(), children: z.unknown()}); export const member = z.object({consumerKey: z.string().optional(), name: z.string().optional(), organizationCode: z.string().optional(), organizationPath: z.string().optional(), usage: totals.optional(), budgetPercent: z.number().optional()}); export const memberPage = z.object({content: z.array(member).optional(), totalElements: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional()}); export const apiOpsUsageHierarchyDTO = z.object({scope: scope.optional(), period: period.optional(), filters: filters.optional(), level: z.enum(["ORGANIZATION", "CONSUMER"]).optional(), dataSource: z.string().optional(), freshness: z.string().optional(), nodes: z.array(node).optional(), members: memberPage.optional(), partial: z.boolean().optional(), notes: z.array(z.string()).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 }; /** * Organization hierarchy with rolled-up consumption * * GET /apiops/projects/{projectName}/consumer-organizations/{organizationCode}/usage/hierarchy */ export const getApiApiopsProjectsProjectNameConsumerOrganizationsOrganizationCodeUsageHierarchy = async (args: GetApiApiopsProjectsProjectNameConsumerOrganizationsOrganizationCodeUsageHierarchyArgs, options?: GetApiApiopsProjectsProjectNameConsumerOrganizationsOrganizationCodeUsageHierarchyOptions) => { const {baseUrl = "http://167.86.118.166:32080", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.level != null) query.append("level", String(args.level)) if (args.includeDescendants != null) query.append("includeDescendants", String(args.includeDescendants)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) const url = `${baseUrl.replace(/\/+$/, '')}/apiops/projects/${encodeURIComponent(String(args.projectName))}/consumer-organizations/${encodeURIComponent(String(args.organizationCode))}/usage/hierarchy${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(apiOpsUsageHierarchyDTO, await res.json()) };