import {z} from 'zod' export type ProductAnalyticsSankeyDefinition = {entries_per_step?: number | undefined, number_of_steps?: number | undefined, source: string, target: string}; export type ProductAnalyticsAudienceAccountSubquery = {name: string, query?: string | undefined}; export type ProductAnalyticsAudienceSegmentSubquery = {name: string, segment_id: string}; export type ProductAnalyticsAudienceUserSubquery = {name: string, query?: string | undefined}; export type ProductAnalyticsAudienceFilters = {accounts?: Array | undefined, formula?: string | undefined, segments?: Array | undefined, users?: Array | undefined}; export type ProductAnalyticsJoinKeys = {primary?: string | undefined, secondary?: Array | undefined}; export type ProductAnalyticsSankeySearch = {audience_filters?: ProductAnalyticsAudienceFilters | undefined, join_keys?: ProductAnalyticsJoinKeys | undefined, query?: string | undefined}; export type ProductAnalyticsSankeyTime = {from: number, to: number}; export type ProductAnalyticsSankeyRequestAttributes = {definition: ProductAnalyticsSankeyDefinition, search: ProductAnalyticsSankeySearch, time: ProductAnalyticsSankeyTime}; export type ProductAnalyticsSankeyRequestType = 'sankey_request'; export type ProductAnalyticsSankeyRequestData = {attributes: ProductAnalyticsSankeyRequestAttributes, type: ProductAnalyticsSankeyRequestType}; export type ProductAnalyticsSankeyRequest = {data: ProductAnalyticsSankeyRequestData}; export type CreateApiApiV2ProductAnalyticsSankeyArgs = {body: ProductAnalyticsSankeyRequest}; export type CreateApiApiV2ProductAnalyticsSankeyOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const productAnalyticsSankeyLink = z.object({column: z.number().int().optional(), id: z.string().optional(), source: z.string().optional(), target: z.string().optional(), value: z.number().int().optional()}); export const productAnalyticsSankeyAggregatedNodeType = z.literal("aggregated"); export const productAnalyticsSankeyAggregatedNode = z.object({id: z.string().optional(), incoming_value: z.number().int().optional(), name: z.string().optional(), outgoing_value: z.number().int().optional(), type: productAnalyticsSankeyAggregatedNodeType.optional(), value: z.number().int().optional()}); export const productAnalyticsSankeyNodeType = z.enum(["regular", "other", "dropoff"]); export const productAnalyticsSankeyNode = z.object({aggregated_nodes: z.array(productAnalyticsSankeyAggregatedNode).optional(), column: z.number().int().optional(), dropoff_value: z.number().int().optional(), id: z.string().optional(), incoming_value: z.number().int().optional(), name: z.string().optional(), outgoing_value: z.number().int().optional(), type: productAnalyticsSankeyNodeType.optional(), value: z.number().int().optional()}); export const productAnalyticsSankeyResponseAttributes = z.object({links: z.array(productAnalyticsSankeyLink).optional(), nodes: z.array(productAnalyticsSankeyNode).optional()}); export const productAnalyticsSankeyResponseType = z.literal("sankey_response"); export const productAnalyticsSankeyResponseData = z.object({attributes: productAnalyticsSankeyResponseAttributes, id: z.string(), type: productAnalyticsSankeyResponseType}); export const productAnalyticsSankeyResponse = z.object({data: productAnalyticsSankeyResponseData}); 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 }; /** * Compute a Sankey diagram * * POST /api/v2/product-analytics/sankey * * Also accepts auth: AuthZ */ export const createApiApiV2ProductAnalyticsSankey = async (args: CreateApiApiV2ProductAnalyticsSankeyArgs, options?: CreateApiApiV2ProductAnalyticsSankeyOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/product-analytics/sankey${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(productAnalyticsSankeyResponse, await res.json()) };