import {z} from 'zod' export type GetApiReportsAccessTokensArgs = {format?: string | undefined, 'filter[is-expired]'?: string | undefined, 'filter[user][status]'?: ('Active' | 'Inactive') | undefined, 'filter[created-at]'?: string | undefined, 'page[number]'?: string | undefined, 'page[size]'?: string | undefined, query?: string | undefined, sort?: (Array<'created-at' | 'expires-at' | 'last-used-at' | 'owner-email'>) | undefined}; export type GetApiReportsAccessTokensOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const accessTokenUsage = z.object({attributes: z.object({"created-at": z.string().optional(), description: z.string().max(255).nullable().optional(), "expires-at": z.string().nullable().optional(), "last-characters": z.string().max(512).optional(), "last-used-at": z.string().nullable().optional(), name: z.string().max(64).nullable().optional(), "owner-email": z.string().optional(), "owner-status": z.enum(["Active", "Inactive", "Pending"]).optional(), "owner-type": z.enum(["user", "service-account"]).optional()}).optional(), id: z.string().optional(), links: z.record(z.string(), z.string()).optional(), type: z.literal("access-token-usages")}); export const accessTokenUsageListingDocument = z.object({data: z.array(accessTokenUsage).optional(), included: z.array(z.object({})).optional(), links: z.record(z.string(), z.string()).optional(), meta: z.object({}).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 Access Token Usage * * GET /reports/access-tokens */ export const getApiReportsAccessTokens = async (args: GetApiReportsAccessTokensArgs, options?: GetApiReportsAccessTokensOptions) => { const {baseUrl = "https://example.scalr.io/api/iacp/v3", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.format != null) query.append("format", String(args.format)) if (args['filter[is-expired]'] != null) query.append("filter[is-expired]", String(args['filter[is-expired]'])) if (args['filter[user][status]'] != null) query.append("filter[user][status]", String(args['filter[user][status]'])) if (args['filter[created-at]'] != null) query.append("filter[created-at]", String(args['filter[created-at]'])) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.query != null) query.append("query", String(args.query)) if (args.sort != null) query.append("sort", args.sort.join(',')) if (APIKey !== undefined) headers.set('authorization', `Bearer ${APIKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/access-tokens${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(accessTokenUsageListingDocument, await res.json()) };