import {z} from 'zod' export type GetApiReportsNameCadLicenseUsageArgs = {name: string, from_date?: string | undefined, to_date?: string | undefined}; export type GetApiReportsNameCadLicenseUsageOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cADLicenseUsage = z.object({action: z.string(), app_version: z.string(), created_at: z.string(), product_name: z.string(), user_name: z.string(), user_email: z.string()}); export const reportCADLicenseUsage = z.object({account_id: z.string(), created_at: z.string().optional(), from_date: z.string().optional(), to_date: z.string().optional(), data: z.array(cADLicenseUsage)}); 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 CAD license usage report for an account * * GET /reports/{name}/cad-license-usage * * Also accepts auth: APIKeyAuth */ export const getApiReportsNameCadLicenseUsage = async (args: GetApiReportsNameCadLicenseUsageArgs, options: GetApiReportsNameCadLicenseUsageOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/${encodeURIComponent(String(args.name))}/cad-license-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(reportCADLicenseUsage, await res.json()) };