import {z} from 'zod' export type MetricType = 'percentile' | 'average' | 'count' | 'count_distinct' | 'sum' | 'sum_distinct' | 'average_distinct' | 'min' | 'max' | 'percent_of_previous' | 'percent_of_total' | 'running_total' | 'number' | 'median' | 'string' | 'date' | 'timestamp' | 'boolean'; export type Compact = 'auto' | 'thousands' | 'millions' | 'billions' | 'trillions' | 'kilobytes' | 'megabytes' | 'gigabytes' | 'terabytes' | 'petabytes' | 'kibibytes' | 'mebibytes' | 'gibibytes' | 'tebibytes' | 'pebibytes'; export type CompactOrAlias = Compact | 'K' | 'thousand' | 'M' | 'million' | 'B' | 'billion' | 'T' | 'trillion' | 'KB' | 'kilobyte' | 'MB' | 'megabyte' | 'GB' | 'gigabyte' | 'TB' | 'terabyte' | 'PB' | 'petabyte' | 'KiB' | 'kibibyte' | 'MiB' | 'mebibyte' | 'GiB' | 'gibibyte' | 'TiB' | 'tebibyte' | 'PiB' | 'pebibyte'; export type Format = 'km' | 'mi' | 'si' | 'usd' | 'gbp' | 'eur' | 'jpy' | 'dkk' | 'id' | 'percent'; export type NumberSeparator = 'default' | 'commaPeriod' | 'spacePeriod' | 'periodComma' | 'noSeparatorPeriod' | 'apostrophePeriod'; export type AnyType = unknown; export type FilterOperator = 'isNull' | 'notNull' | 'equals' | 'notEquals' | 'startsWith' | 'endsWith' | 'include' | 'doesNotInclude' | 'lessThan' | 'lessThanOrEqual' | 'greaterThan' | 'greaterThanOrEqual' | 'inThePast' | 'notInThePast' | 'inTheNext' | 'inTheCurrent' | 'notInTheCurrent' | 'inBetween' | 'notInBetween' | 'inPeriodToDate'; export type MetricFilterRule = {includeNull?: boolean | undefined, values?: Array | undefined, operator: FilterOperator, id: string, target: {fieldRef: string}, settings?: AnyType | undefined, disabled?: boolean | undefined, required?: boolean | undefined, caseSensitive?: boolean | undefined} | {[key: string]: unknown}; export type CustomFormatType = 'default' | 'percent' | 'currency' | 'number' | 'id' | 'date' | 'timestamp' | 'bytes_si' | 'bytes_iec' | 'custom'; export type TimeFrames = 'RAW' | 'YEAR' | 'QUARTER' | 'MONTH' | 'WEEK' | 'DAY' | 'HOUR' | 'MINUTE' | 'SECOND' | 'MILLISECOND' | 'DAY_OF_WEEK_INDEX' | 'DAY_OF_MONTH_NUM' | 'DAY_OF_YEAR_NUM' | 'WEEK_NUM' | 'MONTH_NUM' | 'QUARTER_NUM' | 'YEAR_NUM' | 'DAY_OF_WEEK_NAME' | 'MONTH_NAME' | 'QUARTER_NAME' | 'HOUR_OF_DAY_NUM' | 'MINUTE_OF_HOUR_NUM'; export type CustomFormat = {type: CustomFormatType, round?: number | undefined, separator?: NumberSeparator | undefined, currency?: string | undefined, compact?: CompactOrAlias | undefined, prefix?: string | undefined, suffix?: string | undefined, timeInterval?: TimeFrames | undefined, custom?: string | undefined} | {[key: string]: unknown}; export type FieldId = string; export type AdditionalMetric = {label?: string | undefined, type: MetricType, description?: string | undefined, sql: string, hidden?: boolean | undefined, round?: number | undefined, compact?: CompactOrAlias | undefined, format?: (Format | string) | undefined, separator?: NumberSeparator | undefined, table: string, name: string, index?: number | undefined, filters?: Array | undefined, baseDimensionName?: string | undefined, baseMetricName?: string | undefined, uuid?: (string | null) | undefined, percentile?: number | undefined, distinctKeys?: Array | undefined, formatOptions?: CustomFormat | undefined, generationType?: 'periodOverPeriod' | undefined, baseMetricId?: FieldId | undefined, timeDimensionId?: FieldId | undefined, granularity?: TimeFrames | undefined, periodOffset?: number | undefined} | {[key: string]: unknown}; export type CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetricsArgs = {projectUuid: string, body: {quoteChar?: ('"' | ''') | undefined, customMetrics: Array}}; export type CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetricsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const pullRequestCreated = z.object({prUrl: z.string(), prTitle: z.string()}); export const createApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetricsResponse = z.object({results: pullRequestCreated, status: z.literal("ok")}); 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 }; /** * Create PR for custom metrics * * POST /api/v1/projects/{projectUuid}/git-integration/pull-requests/custom-metrics */ export const createApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetrics = async (args: CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetricsArgs, options: CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetricsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/git-integration/pull-requests/custom-metrics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomMetricsResponse, await res.json()) };