import {z} from 'zod' export type GetApiV2ExportArgs = {from: string, to: string, size?: string | undefined, hosts?: string | undefined, apps?: string | undefined, levels?: string | undefined, query?: string | undefined, prefer?: string | undefined, pagination_id?: string | undefined}; export type GetApiV2ExportOptions = {baseUrl?: string | undefined, PipelineServiceKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ExportResponse = z.record(z.string(), z.object({})); 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 }; /** * Export log lines * * GET /v2/export */ export const getApiV2Export = async (args: GetApiV2ExportArgs, options?: GetApiV2ExportOptions) => { const {baseUrl = "https://api.mezmo.com/v3", fetch: fetchFn = fetch, PipelineServiceKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.size != null) query.append("size", String(args.size)) if (args.hosts != null) query.append("hosts", String(args.hosts)) if (args.apps != null) query.append("apps", String(args.apps)) if (args.levels != null) query.append("levels", String(args.levels)) if (args.query != null) query.append("query", String(args.query)) if (args.prefer != null) query.append("prefer", String(args.prefer)) if (args.pagination_id != null) query.append("pagination_id", String(args.pagination_id)) if (PipelineServiceKey !== undefined) headers.set("Authorization", PipelineServiceKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/export${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(getApiV2ExportResponse, await res.json()) };