import {z} from 'zod' export type CustomDimensionTypeBIN = 'bin'; export type FieldId = string; export type BinTypeFIXEDNUMBER = 'fixed_number'; export type FixedNumberBinDimension = {id: string, name: string, table: string, type: CustomDimensionTypeBIN, dimensionId: FieldId, binType: BinTypeFIXEDNUMBER, binNumber: number} | {[key: string]: unknown}; export type BinTypeFIXEDWIDTH = 'fixed_width'; export type FixedWidthBinDimension = {id: string, name: string, table: string, type: CustomDimensionTypeBIN, dimensionId: FieldId, binType: BinTypeFIXEDWIDTH, binWidth: number} | {[key: string]: unknown}; export type BinTypeCUSTOMRANGE = 'custom_range'; export type BinRange = {to?: number | undefined, from?: number | undefined}; export type CustomRangeBinDimension = {id: string, name: string, table: string, type: CustomDimensionTypeBIN, dimensionId: FieldId, binType: BinTypeCUSTOMRANGE, customRange: Array} | {[key: string]: unknown}; export type BinTypeCUSTOMGROUP = 'custom_group'; export type GroupValueMatchType = 'exact' | 'startsWith' | 'endsWith' | 'includes'; export type GroupValueRule = {value: string, matchType: GroupValueMatchType}; export type BinGroup = {values: Array, name: string}; export type CustomGroupBinDimension = {id: string, name: string, table: string, type: CustomDimensionTypeBIN, dimensionId: FieldId, binType: BinTypeCUSTOMGROUP, customGroups: Array} | {[key: string]: unknown}; export type CustomBinDimension = FixedNumberBinDimension | FixedWidthBinDimension | CustomRangeBinDimension | CustomGroupBinDimension; export type CustomDimensionTypeSQL = 'sql'; export type DimensionType = 'string' | 'number' | 'timestamp' | 'date' | 'boolean'; export type CustomSqlDimension = {id: string, name: string, table: string, type: CustomDimensionTypeSQL, sql: string, dimensionType: DimensionType} | {[key: string]: unknown}; export type CustomDimension = CustomBinDimension | CustomSqlDimension; export type CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensionsArgs = {projectUuid: string, body: {quoteChar?: ('"' | ''') | undefined, customDimensions: Array}}; export type CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const pullRequestCreated = z.object({prUrl: z.string(), prTitle: z.string()}); export const createApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensionsResponse = 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 dimensions * * POST /api/v1/projects/{projectUuid}/git-integration/pull-requests/custom-dimensions */ export const createApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensions = async (args: CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensionsArgs, options: CreateApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensionsOptions) => { 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-dimensions${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(createApiApiV1ProjectsProjectUuidGitIntegrationPullRequestsCustomDimensionsResponse, await res.json()) };