import {z} from 'zod' export type GetApiApplicationsIdLogsArgs = {id: string, type?: ('access' | 'pageviews' | 'csp-reports' | 'cdn') | undefined, from?: string | undefined, to?: string | undefined, format?: string | undefined, select?: string | undefined, filter?: string | undefined, start?: number | undefined, order?: string | undefined, groupby?: string | undefined, groupfilters?: string | undefined, grouplimit?: number | undefined, groupselect?: string | undefined, grouporder?: string | undefined}; export type GetApiApplicationsIdLogsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.string(); export const applicationLog = z.object({from: timestamp, group: z.object({fields: z.array(z.string()), result: z.array(z.object({}))}), records: z.object({count: z.number(), fields: z.object({}), next: z.number().optional(), result: z.array(z.object({})), start: z.number()}), to: timestamp}); 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 application logs * * GET /applications/{id}/logs */ export const getApiApplicationsIdLogs = async (args: GetApiApplicationsIdLogsArgs, options?: GetApiApplicationsIdLogsOptions) => { const {baseUrl = "https://api.webscale.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.format != null) query.append("format", String(args.format)) if (args.select != null) query.append("select", String(args.select)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.start != null) query.append("start", String(args.start)) if (args.order != null) query.append("order", String(args.order)) if (args.groupby != null) query.append("groupby", String(args.groupby)) if (args.groupfilters != null) query.append("groupfilters", String(args.groupfilters)) if (args.grouplimit != null) query.append("grouplimit", String(args.grouplimit)) if (args.groupselect != null) query.append("groupselect", String(args.groupselect)) if (args.grouporder != null) query.append("grouporder", String(args.grouporder)) const url = `${baseUrl.replace(/\/+$/, '')}/applications/${encodeURIComponent(String(args.id))}/logs${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(applicationLog, await res.json()) };