import {z} from 'zod' export type AppSettingsBody = {app_apdex_threshold?: number | undefined, enable_real_user_monitoring?: boolean | undefined, end_user_apdex_threshold?: number | undefined}; export type ApplicationBody = {name?: string | undefined, settings?: AppSettingsBody | undefined}; export type Application = {application?: ApplicationBody | undefined}; export type UpdateApiApplicationsIdJsonArgs = {id: number, body: Application}; export type UpdateApiApplicationsIdJsonOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const appSummaryResponse = z.object({apdex_score: z.number().optional(), apdex_target: z.number().optional(), concurrent_instance_count: z.number().int().optional(), error_rate: z.number().optional(), host_count: z.number().int().optional(), instance_count: z.number().int().optional(), response_time: z.number().optional(), throughput: z.number().optional()}); export const endUserSummaryResponse = z.object({apdex_score: z.number().optional(), apdex_target: z.number().optional(), response_time: z.number().optional(), throughput: z.number().optional()}); export const applicationLinksResponse = z.object({servers: z.array(z.number().int()).optional(), application_hosts: z.array(z.number().int()).optional(), application_instances: z.array(z.number().int()).optional()}); export const appSettingsResponse = z.object({app_apdex_threshold: z.number().optional(), enable_real_user_monitoring: z.boolean().optional(), end_user_apdex_threshold: z.number().optional(), use_server_side_config: z.boolean().optional()}); export const applicationResponseType = z.object({application_summary: appSummaryResponse.optional(), end_user_summary: endUserSummaryResponse.optional(), health_status: z.string().optional(), id: z.number().int().optional(), language: z.string().optional(), last_reported_at: z.string().optional(), links: applicationLinksResponse.optional(), name: z.string().optional(), reporting: z.boolean().optional(), settings: appSettingsResponse.optional()}); export const applicationResponse = z.object({application: applicationResponseType.optional()}); 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 * * PUT /applications/{id}.json */ export const updateApiApplicationsIdJson = async (args: UpdateApiApplicationsIdJsonArgs, options?: UpdateApiApplicationsIdJsonOptions) => { const {baseUrl = "https://api.newrelic.com/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Api-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/applications/${encodeURIComponent(String(args.id))}.json${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(applicationResponse, await res.json()) };