import {z} from 'zod' export type GetApiKeyTransactionsIdJsonArgs = {id: number}; export type GetApiKeyTransactionsIdJsonOptions = {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 keyTransactionLinksResponse = z.object({application: z.number().int().optional()}); export const keyTransactionResponseType = z.object({application_summary: appSummaryResponse.optional(), end_user_summary: endUserSummaryResponse.optional(), health_status: z.string().optional(), id: z.number().int().optional(), last_reported_at: z.string().optional(), links: keyTransactionLinksResponse.optional(), name: z.string().optional(), reporting: z.boolean().optional(), transaction_name: z.string().optional()}); export const keyTransactionResponse = z.object({key_transaction: keyTransactionResponseType.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 }; /** * Show * * GET /key_transactions/{id}.json */ export const getApiKeyTransactionsIdJson = async (args: GetApiKeyTransactionsIdJsonArgs, options?: GetApiKeyTransactionsIdJsonOptions) => { const {baseUrl = "https://api.newrelic.com/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Api-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/key_transactions/${encodeURIComponent(String(args.id))}.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(keyTransactionResponse, await res.json()) };