import {z} from 'zod' export type APLRequestWithOptionsDefaultOrder = {desc?: boolean | undefined, field?: string | undefined}; export type BooleanString = 'true' | 'false' | ''; export type QueryOptions = {disableCache?: boolean | undefined, disableStats?: boolean | undefined, disableTrace?: boolean | undefined, maxDataPoints?: number | undefined, maxSeries?: number | undefined, noAggregation?: boolean | undefined, noFill?: boolean | undefined, noInterpolation?: boolean | undefined, priority?: ('low' | 'medium' | 'high') | undefined, resolution?: string | undefined, displayNull?: ('auto' | 'null' | 'span' | 'zero' | '') | undefined, overlayCharts?: BooleanString | undefined, shownColumns?: string | undefined, timeSeriesVariant?: ('area' | 'bars' | 'line' | 'lines' | '') | undefined, timeSeriesView?: ('charts' | 'resultsTable' | 'charts|resultsTable' | '') | undefined}; export type APLRequestWithOptions = {apl: string, cursor?: string | undefined, defaultLimit?: number | undefined, defaultOrder?: Array | undefined, endTime?: string | undefined, includeCursor?: boolean | undefined, includeCursorField?: boolean | undefined, libraries?: Array | undefined, queryOptions?: QueryOptions | undefined, startTime?: string | undefined, variables?: Record | undefined}; export type StarredQuery = {dataset?: string | undefined, kind: 'apl', metadata: {[key: string]: string}, name: string, query: APLRequestWithOptions, who: string}; export type UpdateApiAplStarredQueriesIdArgs = {id: string, body: StarredQuery}; export type UpdateApiAplStarredQueriesIdOptions = {baseUrl?: string | undefined, Auth?: string | undefined, fetch?: typeof fetch | undefined}; export const aPLRequestWithOptionsDefaultOrder = z.object({desc: z.boolean().optional(), field: z.string().optional()}); export const booleanString = z.enum(["true", "false", ""]); export const queryOptions = z.object({disableCache: z.boolean().optional(), disableStats: z.boolean().optional(), disableTrace: z.boolean().optional(), maxDataPoints: z.number().int().optional(), maxSeries: z.number().int().optional(), noAggregation: z.boolean().optional(), noFill: z.boolean().optional(), noInterpolation: z.boolean().optional(), priority: z.enum(["low", "medium", "high"]).optional(), resolution: z.string().optional(), displayNull: z.enum(["auto", "null", "span", "zero", ""]).optional(), overlayCharts: booleanString.optional(), shownColumns: z.string().max(10000).optional(), timeSeriesVariant: z.enum(["area", "bars", "line", "lines", ""]).optional(), timeSeriesView: z.enum(["charts", "resultsTable", "charts|resultsTable", ""]).optional()}); export const aPLRequestWithOptions = z.object({apl: z.string(), cursor: z.string().optional(), defaultLimit: z.number().int().optional(), defaultOrder: z.array(aPLRequestWithOptionsDefaultOrder).optional(), endTime: z.string().optional(), includeCursor: z.boolean().optional(), includeCursorField: z.boolean().optional(), libraries: z.array(z.string()).optional(), queryOptions: queryOptions.optional(), startTime: z.string().optional(), variables: z.object({}).optional()}); export const starredQueryWithId = z.object({dataset: z.string().optional(), kind: z.literal("apl"), metadata: z.record(z.string(), z.string()), name: z.string(), query: aPLRequestWithOptions, who: z.string(), id: z.string()}); 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 }; /** * Replaces the `name`, `dataset`, `query`, and `metadata` of a saved query. With a personal access token, you can only update queries you saved. * * PUT /apl-starred-queries/{id} */ export const updateApiAplStarredQueriesId = async (args: UpdateApiAplStarredQueriesIdArgs, options?: UpdateApiAplStarredQueriesIdOptions) => { const {baseUrl = "https://api.axiom.co/v2", fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/apl-starred-queries/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(starredQueryWithId, await res.json()) };