From 54394f06e92fa070c8e69275af6e0bd781beedc8 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 10 Apr 2024 17:23:05 -0400 Subject: [PATCH 1/3] fix(node): Make fastify types more broad --- .../node-fastify-app/src/{app.js => app.ts} | 15 ++++++++++++--- .../node-fastify-app/src/tracing.js | 10 ---------- packages/node/src/integrations/tracing/fastify.ts | 8 +++++--- 3 files changed, 17 insertions(+), 16 deletions(-) rename dev-packages/e2e-tests/test-applications/node-fastify-app/src/{app.js => app.ts} (89%) delete mode 100644 dev-packages/e2e-tests/test-applications/node-fastify-app/src/tracing.js diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js b/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts similarity index 89% rename from dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js rename to dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts index dbb239a4e340..107fa71a4761 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js +++ b/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts @@ -1,6 +1,15 @@ -require('./tracing'); - const Sentry = require('@sentry/node'); + +Sentry.init({ + environment: 'qa', // dynamic sampling bias to keep transactions + dsn: process.env.E2E_TEST_DSN, + integrations: [], + tracesSampleRate: 1, + tunnel: 'http://localhost:3031/', // proxy server + tracePropagationTargets: ['http://localhost:3030', '/external-allowed'], +}); + +// Make sure fastify is imported after Sentry is initialized const { fastify } = require('fastify'); const http = require('http'); @@ -103,7 +112,7 @@ app2.listen({ port: port2 }); function makeHttpRequest(url) { return new Promise(resolve => { - const data = []; + const data: any[] = []; http .request(url, httpRes => { diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-app/src/tracing.js b/dev-packages/e2e-tests/test-applications/node-fastify-app/src/tracing.js deleted file mode 100644 index 136b401cbd73..000000000000 --- a/dev-packages/e2e-tests/test-applications/node-fastify-app/src/tracing.js +++ /dev/null @@ -1,10 +0,0 @@ -const Sentry = require('@sentry/node'); - -Sentry.init({ - environment: 'qa', // dynamic sampling bias to keep transactions - dsn: process.env.E2E_TEST_DSN, - integrations: [], - tracesSampleRate: 1, - tunnel: 'http://localhost:3031/', // proxy server - tracePropagationTargets: ['http://localhost:3030', '/external-allowed'], -}); diff --git a/packages/node/src/integrations/tracing/fastify.ts b/packages/node/src/integrations/tracing/fastify.ts index 87a820bd238e..aa2b2f275525 100644 --- a/packages/node/src/integrations/tracing/fastify.ts +++ b/packages/node/src/integrations/tracing/fastify.ts @@ -31,8 +31,10 @@ export const fastifyIntegration = defineIntegration(_fastifyIntegration); // We inline the types we care about here interface Fastify { - register: (plugin: unknown) => void; - addHook: (hook: string, handler: (request: unknown, reply: unknown, error: Error) => void) => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + register: (plugin: any) => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + addHook: (hook: string, handler: (request: any, reply: any, error: Error) => void) => void; } /** @@ -53,7 +55,7 @@ interface FastifyRequestRouteInfo { */ export function setupFastifyErrorHandler(fastify: Fastify): void { const plugin = Object.assign( - function (fastify: Fastify, options: unknown, done: () => void): void { + function (fastify: Fastify, _options: unknown, done: () => void): void { fastify.addHook('onError', async (_request, _reply, error) => { captureException(error); }); From ebf6591431ca979eed57fdc110b77f64094cb419 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 10 Apr 2024 23:04:01 -0400 Subject: [PATCH 2/3] adjust package.json --- .../test-applications/node-fastify-app/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-app/package.json b/dev-packages/e2e-tests/test-applications/node-fastify-app/package.json index 9e8779cf9bdd..56c8818933af 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-app/package.json +++ b/dev-packages/e2e-tests/test-applications/node-fastify-app/package.json @@ -3,10 +3,11 @@ "version": "1.0.0", "private": true, "scripts": { - "start": "node src/app.js", + "start": "ts-node src/app.ts", "test": "playwright test", "clean": "npx rimraf node_modules pnpm-lock.yaml", - "test:build": "pnpm install", + "typecheck": "tsc", + "test:build": "pnpm install && pnpm run typecheck", "test:assert": "pnpm test" }, "dependencies": { From 01c4ff88091eda893b157e1c9cbaccff9ecabcee Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 11 Apr 2024 15:21:57 -0400 Subject: [PATCH 3/3] adjust fastify types --- .../node-fastify-app/src/app.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts b/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts index 107fa71a4761..187d259b1f5b 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts +++ b/dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts @@ -1,4 +1,5 @@ -const Sentry = require('@sentry/node'); +import type * as S from '@sentry/node'; +const Sentry = require('@sentry/node') as typeof S; Sentry.init({ environment: 'qa', // dynamic sampling bias to keep transactions @@ -9,9 +10,12 @@ Sentry.init({ tracePropagationTargets: ['http://localhost:3030', '/external-allowed'], }); +import type * as H from 'http'; +import type * as F from 'fastify'; + // Make sure fastify is imported after Sentry is initialized -const { fastify } = require('fastify'); -const http = require('http'); +const { fastify } = require('fastify') as typeof F; +const http = require('http') as typeof H; const app = fastify(); const port = 3030; @@ -19,28 +23,28 @@ const port2 = 3040; Sentry.setupFastifyErrorHandler(app); -app.get('/test-success', function (req, res) { +app.get('/test-success', function (_req, res) { res.send({ version: 'v1' }); }); -app.get('/test-param/:param', function (req, res) { +app.get<{ Params: { param: string } }>('/test-param/:param', function (req, res) { res.send({ paramWas: req.params.param }); }); -app.get('/test-inbound-headers/:id', function (req, res) { +app.get<{ Params: { id: string } }>('/test-inbound-headers/:id', function (req, res) { const headers = req.headers; res.send({ headers, id: req.params.id }); }); -app.get('/test-outgoing-http/:id', async function (req, res) { +app.get<{ Params: { id: string } }>('/test-outgoing-http/:id', async function (req, res) { const id = req.params.id; const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`); res.send(data); }); -app.get('/test-outgoing-fetch/:id', async function (req, res) { +app.get<{ Params: { id: string } }>('/test-outgoing-fetch/:id', async function (req, res) { const id = req.params.id; const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`); const data = await response.json(); @@ -64,7 +68,7 @@ app.get('/test-error', async function (req, res) { res.send({ exceptionId }); }); -app.get('/test-exception/:id', async function (req, res) { +app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) { throw new Error(`This is an exception with id ${req.params.id}`); }); @@ -110,7 +114,7 @@ app2.get('/external-disallowed', function (req, res) { app2.listen({ port: port2 }); -function makeHttpRequest(url) { +function makeHttpRequest(url: string) { return new Promise(resolve => { const data: any[] = [];