Skip to content

Commit 20706b8

Browse files
authored
fix(node): Make fastify types more broad (#11544)
As per #4784 (comment) make sure fastify types use any is that we don't collide with what fastify types expect. Also convert e2e tests to use typescript to validate build.
1 parent ce959cd commit 20706b8

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

dev-packages/e2e-tests/test-applications/node-fastify-app/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"version": "1.0.0",
44
"private": true,
55
"scripts": {
6-
"start": "node src/app.js",
6+
"start": "ts-node src/app.ts",
77
"test": "playwright test",
88
"clean": "npx rimraf node_modules pnpm-lock.yaml",
9-
"test:build": "pnpm install",
9+
"typecheck": "tsc",
10+
"test:build": "pnpm install && pnpm run typecheck",
1011
"test:assert": "pnpm test"
1112
},
1213
"dependencies": {

dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js renamed to dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1-
require('./tracing');
1+
import type * as S from '@sentry/node';
2+
const Sentry = require('@sentry/node') as typeof S;
3+
4+
Sentry.init({
5+
environment: 'qa', // dynamic sampling bias to keep transactions
6+
dsn: process.env.E2E_TEST_DSN,
7+
integrations: [],
8+
tracesSampleRate: 1,
9+
tunnel: 'http://localhost:3031/', // proxy server
10+
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
11+
});
12+
13+
import type * as H from 'http';
14+
import type * as F from 'fastify';
215

3-
const Sentry = require('@sentry/node');
4-
const { fastify } = require('fastify');
5-
const http = require('http');
16+
// Make sure fastify is imported after Sentry is initialized
17+
const { fastify } = require('fastify') as typeof F;
18+
const http = require('http') as typeof H;
619

720
const app = fastify();
821
const port = 3030;
922
const port2 = 3040;
1023

1124
Sentry.setupFastifyErrorHandler(app);
1225

13-
app.get('/test-success', function (req, res) {
26+
app.get('/test-success', function (_req, res) {
1427
res.send({ version: 'v1' });
1528
});
1629

17-
app.get('/test-param/:param', function (req, res) {
30+
app.get<{ Params: { param: string } }>('/test-param/:param', function (req, res) {
1831
res.send({ paramWas: req.params.param });
1932
});
2033

21-
app.get('/test-inbound-headers/:id', function (req, res) {
34+
app.get<{ Params: { id: string } }>('/test-inbound-headers/:id', function (req, res) {
2235
const headers = req.headers;
2336

2437
res.send({ headers, id: req.params.id });
2538
});
2639

27-
app.get('/test-outgoing-http/:id', async function (req, res) {
40+
app.get<{ Params: { id: string } }>('/test-outgoing-http/:id', async function (req, res) {
2841
const id = req.params.id;
2942
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`);
3043

3144
res.send(data);
3245
});
3346

34-
app.get('/test-outgoing-fetch/:id', async function (req, res) {
47+
app.get<{ Params: { id: string } }>('/test-outgoing-fetch/:id', async function (req, res) {
3548
const id = req.params.id;
3649
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`);
3750
const data = await response.json();
@@ -55,7 +68,7 @@ app.get('/test-error', async function (req, res) {
5568
res.send({ exceptionId });
5669
});
5770

58-
app.get('/test-exception/:id', async function (req, res) {
71+
app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) {
5972
throw new Error(`This is an exception with id ${req.params.id}`);
6073
});
6174

@@ -101,9 +114,9 @@ app2.get('/external-disallowed', function (req, res) {
101114

102115
app2.listen({ port: port2 });
103116

104-
function makeHttpRequest(url) {
117+
function makeHttpRequest(url: string) {
105118
return new Promise(resolve => {
106-
const data = [];
119+
const data: any[] = [];
107120

108121
http
109122
.request(url, httpRes => {

dev-packages/e2e-tests/test-applications/node-fastify-app/src/tracing.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/node/src/integrations/tracing/fastify.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ export const fastifyIntegration = defineIntegration(_fastifyIntegration);
3131

3232
// We inline the types we care about here
3333
interface Fastify {
34-
register: (plugin: unknown) => void;
35-
addHook: (hook: string, handler: (request: unknown, reply: unknown, error: Error) => void) => void;
34+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
35+
register: (plugin: any) => void;
36+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37+
addHook: (hook: string, handler: (request: any, reply: any, error: Error) => void) => void;
3638
}
3739

3840
/**
@@ -53,7 +55,7 @@ interface FastifyRequestRouteInfo {
5355
*/
5456
export function setupFastifyErrorHandler(fastify: Fastify): void {
5557
const plugin = Object.assign(
56-
function (fastify: Fastify, options: unknown, done: () => void): void {
58+
function (fastify: Fastify, _options: unknown, done: () => void): void {
5759
fastify.addHook('onError', async (_request, _reply, error) => {
5860
captureException(error);
5961
});

0 commit comments

Comments
 (0)