import {z} from 'zod' export type ReportKey = string; export type GetApiReportsReportKeyArgs = {reportKey: ReportKey}; export type GetApiReportsReportKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const reportKey = z.string().min(10).max(120); export const agentKey = z.string(); export const componentsSchemasReference = z.string().max(120); export const reportStatus = z.enum(["completed", "archived", "failed", "scheduled"]); export const schemasResult = z.object({size: z.number(), lastToken: z.string().optional(), errorMessage: z.string().optional()}); export const openapiComponentsSchemasCreated2 = z.string(); export const openapiComponentsSchemasLastModified2 = z.string(); export const openapiComponentsSchemasVersion = z.number().int(); export const distributionKey = z.string().min(10).max(120); export const reportScheduleKey = z.string().min(10).max(120); export const publicReport = z.object({key: reportKey, agentKey: agentKey, reference: componentsSchemasReference.optional(), status: reportStatus, result: schemasResult.optional(), created: openapiComponentsSchemasCreated2, lastModified: openapiComponentsSchemasLastModified2.optional(), version: openapiComponentsSchemasVersion, distributions: z.array(distributionKey).optional(), reportScheduleKey: reportScheduleKey.optional()}); export const report = z.union([publicReport, publicReport, publicReport, publicReport, publicReport, publicReport, publicReport, publicReport, publicReport, publicReport, publicReport]); 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 Report * * GET /reports/{reportKey} * * Also accepts auth: BearerToken */ export const getApiReportsReportKey = async (args: GetApiReportsReportKeyArgs, options?: GetApiReportsReportKeyOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/${encodeURIComponent(String(args.reportKey))}${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(report, await res.json()) };