import {z} from 'zod' export type GetApiExperimentalOrgsOrgIdCreditsRollupArgs = {'filter[timestamp][gte]'?: string | undefined, 'filter[timestamp][lt]'?: string | undefined, bucket?: ('hour' | 'day' | 'week') | undefined, group?: (string | Array) | undefined, 'filter[actionCode]'?: string | undefined, orgId: string, Authorization?: string | undefined}; export type GetApiExperimentalOrgsOrgIdCreditsRollupOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiExperimentalOrgsOrgIdCreditsRollupResponse = z.object({links: z.object({self: z.string()}), data: z.object({id: z.string(), type: z.literal("org-credit-rollup"), attributes: z.object({bucket: z.union([z.literal("hour"), z.literal("day"), z.literal("week")]), group: z.array(z.string()), current: z.object({start: z.string(), end: z.string(), credits: z.number(), eventCount: z.number().int()}), previous: z.object({start: z.string(), end: z.string(), credits: z.number(), eventCount: z.number().int()}), series: z.array(z.object({key: z.array(z.string().nullable()), label: z.string(), points: z.array(z.object({bucket: z.string(), credits: z.number(), eventCount: z.number().int()}))})), truncated: z.boolean()})})}); 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 org credit rollup * * GET /experimental/orgs/{orgId}/credits/rollup */ export const getApiExperimentalOrgsOrgIdCreditsRollup = async (args: GetApiExperimentalOrgsOrgIdCreditsRollupArgs, options?: GetApiExperimentalOrgsOrgIdCreditsRollupOptions) => { const {baseUrl = "https://api.tessl.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[timestamp][gte]'] != null) query.append("filter[timestamp][gte]", String(args['filter[timestamp][gte]'])) if (args['filter[timestamp][lt]'] != null) query.append("filter[timestamp][lt]", String(args['filter[timestamp][lt]'])) if (args.bucket != null) query.append("bucket", String(args.bucket)) if (args.group != null) query.append("group", String(args.group)) if (args['filter[actionCode]'] != null) query.append("filter[actionCode]", String(args['filter[actionCode]'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/experimental/orgs/${encodeURIComponent(String(args.orgId))}/credits/rollup${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(getApiExperimentalOrgsOrgIdCreditsRollupResponse, await res.json()) };