import {z} from 'zod' export type GetApiOrganizationsOrgIdUsageArgs = {org_id: string, projects?: Array | undefined, resource_types?: Array | undefined, regions?: Array | undefined, start_date?: string | undefined, end_date?: string | undefined, interval?: string | undefined}; export type GetApiOrganizationsOrgIdUsageOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const customerProductProjectUsage = z.object({billable_metric: z.string().optional(), date: z.string().optional(), project_id: z.string().optional(), quantity: z.number().optional(), region: z.string().optional(), reservation_id: z.string().optional(), resource_type: z.string().optional()}); export const customerOrganizationUsage = z.object({items: z.array(customerProductProjectUsage).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 }; /** * Get project-level usage for products in Crusoe Cloud. * * GET /organizations/{org_id}/usage */ export const getApiOrganizationsOrgIdUsage = async (args: GetApiOrganizationsOrgIdUsageArgs, options?: GetApiOrganizationsOrgIdUsageOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.projects ?? []) query.append("projects", String(item)) for (const item of args.resource_types ?? []) query.append("resource_types", String(item)) for (const item of args.regions ?? []) query.append("regions", String(item)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.interval != null) query.append("interval", String(args.interval)) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.org_id))}/usage${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(customerOrganizationUsage, await res.json()) };