import {z} from 'zod' export type GetApiV1LogsArchivesArgs = {startTime: string, endTime: string, pageSize?: number | undefined, skipToken?: string | undefined}; export type GetApiV1LogsArchivesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const logsArchive = z.object({id: z.string(), name: z.string(), downloadUrl: z.string(), archivedTimestamp: z.string(), archiveSize: z.number()}); export const commonPageInfo = z.object({prevPage: z.string(), nextPage: z.string()}); export const getApiV1LogsArchivesResponse = z.object({logArchives: z.array(logsArchive), pageInfo: commonPageInfo}); 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 }; /** * Retrieve location and metadata of log archives * * GET /v1/logs/archives */ export const getApiV1LogsArchives = async (args: GetApiV1LogsArchivesArgs, options?: GetApiV1LogsArchivesOptions) => { const {baseUrl = "https://api.na-01.cloud.solarwinds.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skipToken != null) query.append("skipToken", String(args.skipToken)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/logs/archives${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(getApiV1LogsArchivesResponse, await res.json()) };