import {z} from 'zod' export type RumConfigUpdateAttributes = {enforced_application_tags: boolean}; export type RumConfigType = 'rum_config'; export type RumConfigUpdateData = {attributes: RumConfigUpdateAttributes, type: RumConfigType}; export type RumConfigUpdateRequest = {data: RumConfigUpdateData}; export type PatchApiApiV2RumConfigArgs = {body: RumConfigUpdateRequest}; export type PatchApiApiV2RumConfigOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rumConfigAttributes = z.object({disabled: z.boolean().optional(), enforced_application_tags: z.boolean(), enforced_application_tags_updated_at: z.string().optional(), enforced_application_tags_updated_by: z.string().optional(), ootb_metrics_version: z.number().int().optional(), ootb_metrics_version_installed_at: z.string().optional(), retention_filters_enabled: z.boolean(), retention_filters_enabled_updated_at: z.string().optional(), retention_filters_enabled_updated_by: z.string().optional()}); export const rumConfigType = z.literal("rum_config"); export const rumConfigData = z.object({attributes: rumConfigAttributes, id: z.string(), type: rumConfigType}); export const rumConfigResponse = z.object({data: rumConfigData}); 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 }; /** * Update the RUM configuration * * PATCH /api/v2/rum/config * * Also accepts auth: AuthZ */ export const patchApiApiV2RumConfig = async (args: PatchApiApiV2RumConfigArgs, options?: PatchApiApiV2RumConfigOptions) => { 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/rum/config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(rumConfigResponse, await res.json()) };