tracebeam-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -0
- package/dist/client-CaHega3m.d.mts +207 -0
- package/dist/client-CaHega3m.d.ts +207 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +580 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +550 -0
- package/dist/index.mjs.map +1 -0
- package/dist/integrations/express.d.mts +70 -0
- package/dist/integrations/express.d.ts +70 -0
- package/dist/integrations/express.js +90 -0
- package/dist/integrations/express.js.map +1 -0
- package/dist/integrations/express.mjs +64 -0
- package/dist/integrations/express.mjs.map +1 -0
- package/dist/integrations/fastify.d.mts +76 -0
- package/dist/integrations/fastify.d.ts +76 -0
- package/dist/integrations/fastify.js +87 -0
- package/dist/integrations/fastify.js.map +1 -0
- package/dist/integrations/fastify.mjs +61 -0
- package/dist/integrations/fastify.mjs.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { a as TraceBeamSDK, c as Tags, d as Extra } from '../client-CaHega3m.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Express error handler types
|
|
5
|
+
*/
|
|
6
|
+
interface ExpressRequest {
|
|
7
|
+
method?: string;
|
|
8
|
+
url?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
11
|
+
ip?: string;
|
|
12
|
+
params?: Record<string, string>;
|
|
13
|
+
query?: Record<string, string>;
|
|
14
|
+
body?: unknown;
|
|
15
|
+
}
|
|
16
|
+
interface ExpressResponse {
|
|
17
|
+
statusCode?: number;
|
|
18
|
+
headersSent?: boolean;
|
|
19
|
+
}
|
|
20
|
+
type ExpressNextFunction = (err?: unknown) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Express error handler middleware
|
|
23
|
+
*
|
|
24
|
+
* Captures unhandled errors and passes them through to the next error handler.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import express from 'express';
|
|
29
|
+
* import { TraceBeamSDK } from 'tracebeam-sdk';
|
|
30
|
+
* import { expressErrorHandler } from 'tracebeam-sdk/integrations/express';
|
|
31
|
+
*
|
|
32
|
+
* const app = express();
|
|
33
|
+
* const sdk = TraceBeamSDK.fromEnv();
|
|
34
|
+
*
|
|
35
|
+
* // Your routes here...
|
|
36
|
+
*
|
|
37
|
+
* // Add error handler AFTER all routes
|
|
38
|
+
* app.use(expressErrorHandler(sdk));
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function expressErrorHandler(sdk: TraceBeamSDK, options?: ExpressErrorHandlerOptions): (err: Error | unknown, req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Options for Express error handler
|
|
44
|
+
*/
|
|
45
|
+
interface ExpressErrorHandlerOptions {
|
|
46
|
+
/** Additional tags to include with every error */
|
|
47
|
+
tags?: Tags;
|
|
48
|
+
/** Additional context to include with every error */
|
|
49
|
+
extra?: Extra;
|
|
50
|
+
/** Extract user ID from request */
|
|
51
|
+
getUserId?: (req: ExpressRequest) => string | undefined;
|
|
52
|
+
/** Filter which errors to capture */
|
|
53
|
+
shouldCapture?: (err: Error, req: ExpressRequest) => boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Express request handler wrapper
|
|
57
|
+
*
|
|
58
|
+
* Wraps an async route handler to automatically capture errors
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* app.get('/users/:id', wrapHandler(sdk, async (req, res) => {
|
|
63
|
+
* const user = await getUser(req.params.id);
|
|
64
|
+
* res.json(user);
|
|
65
|
+
* }));
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function wrapHandler<Req extends ExpressRequest, Res>(sdk: TraceBeamSDK, handler: (req: Req, res: Res, next: ExpressNextFunction) => Promise<unknown> | unknown): (req: Req, res: Res, next: ExpressNextFunction) => Promise<void>;
|
|
69
|
+
|
|
70
|
+
export { type ExpressErrorHandlerOptions, expressErrorHandler, wrapHandler };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/integrations/express.ts
|
|
21
|
+
var express_exports = {};
|
|
22
|
+
__export(express_exports, {
|
|
23
|
+
expressErrorHandler: () => expressErrorHandler,
|
|
24
|
+
wrapHandler: () => wrapHandler
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(express_exports);
|
|
27
|
+
function expressErrorHandler(sdk, options = {}) {
|
|
28
|
+
return function errorHandler(err, req, res, next) {
|
|
29
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
30
|
+
const shouldCapture = typeof options.shouldCapture === "function" ? options.shouldCapture(error, req) : true;
|
|
31
|
+
if (shouldCapture) {
|
|
32
|
+
const captureOptions = buildCaptureOptions(req, options);
|
|
33
|
+
sdk.captureException(error, captureOptions);
|
|
34
|
+
}
|
|
35
|
+
next(err);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function buildCaptureOptions(req, options) {
|
|
39
|
+
const extra = {
|
|
40
|
+
...options.extra,
|
|
41
|
+
request: {
|
|
42
|
+
method: req.method,
|
|
43
|
+
url: req.url,
|
|
44
|
+
path: req.path,
|
|
45
|
+
ip: req.ip,
|
|
46
|
+
params: req.params,
|
|
47
|
+
query: req.query
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
if (options.getUserId) {
|
|
51
|
+
const userId = options.getUserId(req);
|
|
52
|
+
if (userId) {
|
|
53
|
+
extra["user_id"] = userId;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
level: "error",
|
|
58
|
+
tags: {
|
|
59
|
+
...options.tags,
|
|
60
|
+
endpoint: req.path ?? req.url ?? "unknown"
|
|
61
|
+
},
|
|
62
|
+
extra
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function wrapHandler(sdk, handler) {
|
|
66
|
+
return async function wrappedHandler(req, res, next) {
|
|
67
|
+
try {
|
|
68
|
+
await handler(req, res, next);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
71
|
+
sdk.captureException(error, {
|
|
72
|
+
level: "error",
|
|
73
|
+
tags: { endpoint: req.path ?? "unknown" },
|
|
74
|
+
extra: {
|
|
75
|
+
request: {
|
|
76
|
+
method: req.method,
|
|
77
|
+
url: req.url
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
next(err);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
expressErrorHandler,
|
|
88
|
+
wrapHandler
|
|
89
|
+
});
|
|
90
|
+
//# sourceMappingURL=express.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/express.ts"],"sourcesContent":["import type { TraceBeamSDK } from '../client.js';\nimport type { CaptureOptions, Tags, Extra } from '../types.js';\n\n/**\n * Express error handler types\n */\ninterface ExpressRequest {\n method?: string;\n url?: string;\n path?: string;\n headers?: Record<string, string | string[] | undefined>;\n ip?: string;\n params?: Record<string, string>;\n query?: Record<string, string>;\n body?: unknown;\n}\n\ninterface ExpressResponse {\n statusCode?: number;\n headersSent?: boolean;\n}\n\ntype ExpressNextFunction = (err?: unknown) => void;\n\n/**\n * Express error handler middleware\n * \n * Captures unhandled errors and passes them through to the next error handler.\n * \n * @example\n * ```typescript\n * import express from 'express';\n * import { TraceBeamSDK } from 'tracebeam-sdk';\n * import { expressErrorHandler } from 'tracebeam-sdk/integrations/express';\n * \n * const app = express();\n * const sdk = TraceBeamSDK.fromEnv();\n * \n * // Your routes here...\n * \n * // Add error handler AFTER all routes\n * app.use(expressErrorHandler(sdk));\n * ```\n */\nexport function expressErrorHandler(\n sdk: TraceBeamSDK,\n options: ExpressErrorHandlerOptions = {}\n) {\n return function errorHandler(\n err: Error | unknown,\n req: ExpressRequest,\n res: ExpressResponse,\n next: ExpressNextFunction\n ): void {\n // Convert to Error if needed\n const error = err instanceof Error ? err : new Error(String(err));\n\n // Check if we should capture this error\n const shouldCapture =\n typeof options.shouldCapture === 'function'\n ? options.shouldCapture(error, req)\n : true;\n\n if (shouldCapture) {\n const captureOptions = buildCaptureOptions(req, options);\n sdk.captureException(error, captureOptions);\n }\n\n // Pass to next error handler\n next(err);\n };\n}\n\n/**\n * Options for Express error handler\n */\nexport interface ExpressErrorHandlerOptions {\n /** Additional tags to include with every error */\n tags?: Tags;\n /** Additional context to include with every error */\n extra?: Extra;\n /** Extract user ID from request */\n getUserId?: (req: ExpressRequest) => string | undefined;\n /** Filter which errors to capture */\n shouldCapture?: (err: Error, req: ExpressRequest) => boolean;\n}\n\n/**\n * Build capture options from Express request\n */\nfunction buildCaptureOptions(\n req: ExpressRequest,\n options: ExpressErrorHandlerOptions\n): CaptureOptions {\n const extra: Extra = {\n ...options.extra,\n request: {\n method: req.method,\n url: req.url,\n path: req.path,\n ip: req.ip,\n params: req.params,\n query: req.query,\n },\n };\n\n // Add user ID if extractor provided\n if (options.getUserId) {\n const userId = options.getUserId(req);\n if (userId) {\n extra['user_id'] = userId;\n }\n }\n\n return {\n level: 'error',\n tags: {\n ...options.tags,\n endpoint: req.path ?? req.url ?? 'unknown',\n },\n extra,\n };\n}\n\n/**\n * Express request handler wrapper\n * \n * Wraps an async route handler to automatically capture errors\n * \n * @example\n * ```typescript\n * app.get('/users/:id', wrapHandler(sdk, async (req, res) => {\n * const user = await getUser(req.params.id);\n * res.json(user);\n * }));\n * ```\n */\nexport function wrapHandler<Req extends ExpressRequest, Res>(\n sdk: TraceBeamSDK,\n handler: (req: Req, res: Res, next: ExpressNextFunction) => Promise<unknown> | unknown\n) {\n return async function wrappedHandler(\n req: Req,\n res: Res,\n next: ExpressNextFunction\n ): Promise<void> {\n try {\n await handler(req, res, next);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n sdk.captureException(error, {\n level: 'error',\n tags: { endpoint: (req as ExpressRequest).path ?? 'unknown' },\n extra: {\n request: {\n method: (req as ExpressRequest).method,\n url: (req as ExpressRequest).url,\n },\n },\n });\n next(err);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4CO,SAAS,oBACZ,KACA,UAAsC,CAAC,GACzC;AACE,SAAO,SAAS,aACZ,KACA,KACA,KACA,MACI;AAEJ,UAAM,QAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAGhE,UAAM,gBACF,OAAO,QAAQ,kBAAkB,aAC3B,QAAQ,cAAc,OAAO,GAAG,IAChC;AAEV,QAAI,eAAe;AACf,YAAM,iBAAiB,oBAAoB,KAAK,OAAO;AACvD,UAAI,iBAAiB,OAAO,cAAc;AAAA,IAC9C;AAGA,SAAK,GAAG;AAAA,EACZ;AACJ;AAmBA,SAAS,oBACL,KACA,SACc;AACd,QAAM,QAAe;AAAA,IACjB,GAAG,QAAQ;AAAA,IACX,SAAS;AAAA,MACL,QAAQ,IAAI;AAAA,MACZ,KAAK,IAAI;AAAA,MACT,MAAM,IAAI;AAAA,MACV,IAAI,IAAI;AAAA,MACR,QAAQ,IAAI;AAAA,MACZ,OAAO,IAAI;AAAA,IACf;AAAA,EACJ;AAGA,MAAI,QAAQ,WAAW;AACnB,UAAM,SAAS,QAAQ,UAAU,GAAG;AACpC,QAAI,QAAQ;AACR,YAAM,SAAS,IAAI;AAAA,IACvB;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,OAAO;AAAA,IACP,MAAM;AAAA,MACF,GAAG,QAAQ;AAAA,MACX,UAAU,IAAI,QAAQ,IAAI,OAAO;AAAA,IACrC;AAAA,IACA;AAAA,EACJ;AACJ;AAeO,SAAS,YACZ,KACA,SACF;AACE,SAAO,eAAe,eAClB,KACA,KACA,MACa;AACb,QAAI;AACA,YAAM,QAAQ,KAAK,KAAK,IAAI;AAAA,IAChC,SAAS,KAAK;AACV,YAAM,QAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAChE,UAAI,iBAAiB,OAAO;AAAA,QACxB,OAAO;AAAA,QACP,MAAM,EAAE,UAAW,IAAuB,QAAQ,UAAU;AAAA,QAC5D,OAAO;AAAA,UACH,SAAS;AAAA,YACL,QAAS,IAAuB;AAAA,YAChC,KAAM,IAAuB;AAAA,UACjC;AAAA,QACJ;AAAA,MACJ,CAAC;AACD,WAAK,GAAG;AAAA,IACZ;AAAA,EACJ;AACJ;","names":[]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/integrations/express.ts
|
|
2
|
+
function expressErrorHandler(sdk, options = {}) {
|
|
3
|
+
return function errorHandler(err, req, res, next) {
|
|
4
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
5
|
+
const shouldCapture = typeof options.shouldCapture === "function" ? options.shouldCapture(error, req) : true;
|
|
6
|
+
if (shouldCapture) {
|
|
7
|
+
const captureOptions = buildCaptureOptions(req, options);
|
|
8
|
+
sdk.captureException(error, captureOptions);
|
|
9
|
+
}
|
|
10
|
+
next(err);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function buildCaptureOptions(req, options) {
|
|
14
|
+
const extra = {
|
|
15
|
+
...options.extra,
|
|
16
|
+
request: {
|
|
17
|
+
method: req.method,
|
|
18
|
+
url: req.url,
|
|
19
|
+
path: req.path,
|
|
20
|
+
ip: req.ip,
|
|
21
|
+
params: req.params,
|
|
22
|
+
query: req.query
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
if (options.getUserId) {
|
|
26
|
+
const userId = options.getUserId(req);
|
|
27
|
+
if (userId) {
|
|
28
|
+
extra["user_id"] = userId;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
level: "error",
|
|
33
|
+
tags: {
|
|
34
|
+
...options.tags,
|
|
35
|
+
endpoint: req.path ?? req.url ?? "unknown"
|
|
36
|
+
},
|
|
37
|
+
extra
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function wrapHandler(sdk, handler) {
|
|
41
|
+
return async function wrappedHandler(req, res, next) {
|
|
42
|
+
try {
|
|
43
|
+
await handler(req, res, next);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
46
|
+
sdk.captureException(error, {
|
|
47
|
+
level: "error",
|
|
48
|
+
tags: { endpoint: req.path ?? "unknown" },
|
|
49
|
+
extra: {
|
|
50
|
+
request: {
|
|
51
|
+
method: req.method,
|
|
52
|
+
url: req.url
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
next(err);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
expressErrorHandler,
|
|
62
|
+
wrapHandler
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=express.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/express.ts"],"sourcesContent":["import type { TraceBeamSDK } from '../client.js';\nimport type { CaptureOptions, Tags, Extra } from '../types.js';\n\n/**\n * Express error handler types\n */\ninterface ExpressRequest {\n method?: string;\n url?: string;\n path?: string;\n headers?: Record<string, string | string[] | undefined>;\n ip?: string;\n params?: Record<string, string>;\n query?: Record<string, string>;\n body?: unknown;\n}\n\ninterface ExpressResponse {\n statusCode?: number;\n headersSent?: boolean;\n}\n\ntype ExpressNextFunction = (err?: unknown) => void;\n\n/**\n * Express error handler middleware\n * \n * Captures unhandled errors and passes them through to the next error handler.\n * \n * @example\n * ```typescript\n * import express from 'express';\n * import { TraceBeamSDK } from 'tracebeam-sdk';\n * import { expressErrorHandler } from 'tracebeam-sdk/integrations/express';\n * \n * const app = express();\n * const sdk = TraceBeamSDK.fromEnv();\n * \n * // Your routes here...\n * \n * // Add error handler AFTER all routes\n * app.use(expressErrorHandler(sdk));\n * ```\n */\nexport function expressErrorHandler(\n sdk: TraceBeamSDK,\n options: ExpressErrorHandlerOptions = {}\n) {\n return function errorHandler(\n err: Error | unknown,\n req: ExpressRequest,\n res: ExpressResponse,\n next: ExpressNextFunction\n ): void {\n // Convert to Error if needed\n const error = err instanceof Error ? err : new Error(String(err));\n\n // Check if we should capture this error\n const shouldCapture =\n typeof options.shouldCapture === 'function'\n ? options.shouldCapture(error, req)\n : true;\n\n if (shouldCapture) {\n const captureOptions = buildCaptureOptions(req, options);\n sdk.captureException(error, captureOptions);\n }\n\n // Pass to next error handler\n next(err);\n };\n}\n\n/**\n * Options for Express error handler\n */\nexport interface ExpressErrorHandlerOptions {\n /** Additional tags to include with every error */\n tags?: Tags;\n /** Additional context to include with every error */\n extra?: Extra;\n /** Extract user ID from request */\n getUserId?: (req: ExpressRequest) => string | undefined;\n /** Filter which errors to capture */\n shouldCapture?: (err: Error, req: ExpressRequest) => boolean;\n}\n\n/**\n * Build capture options from Express request\n */\nfunction buildCaptureOptions(\n req: ExpressRequest,\n options: ExpressErrorHandlerOptions\n): CaptureOptions {\n const extra: Extra = {\n ...options.extra,\n request: {\n method: req.method,\n url: req.url,\n path: req.path,\n ip: req.ip,\n params: req.params,\n query: req.query,\n },\n };\n\n // Add user ID if extractor provided\n if (options.getUserId) {\n const userId = options.getUserId(req);\n if (userId) {\n extra['user_id'] = userId;\n }\n }\n\n return {\n level: 'error',\n tags: {\n ...options.tags,\n endpoint: req.path ?? req.url ?? 'unknown',\n },\n extra,\n };\n}\n\n/**\n * Express request handler wrapper\n * \n * Wraps an async route handler to automatically capture errors\n * \n * @example\n * ```typescript\n * app.get('/users/:id', wrapHandler(sdk, async (req, res) => {\n * const user = await getUser(req.params.id);\n * res.json(user);\n * }));\n * ```\n */\nexport function wrapHandler<Req extends ExpressRequest, Res>(\n sdk: TraceBeamSDK,\n handler: (req: Req, res: Res, next: ExpressNextFunction) => Promise<unknown> | unknown\n) {\n return async function wrappedHandler(\n req: Req,\n res: Res,\n next: ExpressNextFunction\n ): Promise<void> {\n try {\n await handler(req, res, next);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n sdk.captureException(error, {\n level: 'error',\n tags: { endpoint: (req as ExpressRequest).path ?? 'unknown' },\n extra: {\n request: {\n method: (req as ExpressRequest).method,\n url: (req as ExpressRequest).url,\n },\n },\n });\n next(err);\n }\n };\n}\n"],"mappings":";AA4CO,SAAS,oBACZ,KACA,UAAsC,CAAC,GACzC;AACE,SAAO,SAAS,aACZ,KACA,KACA,KACA,MACI;AAEJ,UAAM,QAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAGhE,UAAM,gBACF,OAAO,QAAQ,kBAAkB,aAC3B,QAAQ,cAAc,OAAO,GAAG,IAChC;AAEV,QAAI,eAAe;AACf,YAAM,iBAAiB,oBAAoB,KAAK,OAAO;AACvD,UAAI,iBAAiB,OAAO,cAAc;AAAA,IAC9C;AAGA,SAAK,GAAG;AAAA,EACZ;AACJ;AAmBA,SAAS,oBACL,KACA,SACc;AACd,QAAM,QAAe;AAAA,IACjB,GAAG,QAAQ;AAAA,IACX,SAAS;AAAA,MACL,QAAQ,IAAI;AAAA,MACZ,KAAK,IAAI;AAAA,MACT,MAAM,IAAI;AAAA,MACV,IAAI,IAAI;AAAA,MACR,QAAQ,IAAI;AAAA,MACZ,OAAO,IAAI;AAAA,IACf;AAAA,EACJ;AAGA,MAAI,QAAQ,WAAW;AACnB,UAAM,SAAS,QAAQ,UAAU,GAAG;AACpC,QAAI,QAAQ;AACR,YAAM,SAAS,IAAI;AAAA,IACvB;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,OAAO;AAAA,IACP,MAAM;AAAA,MACF,GAAG,QAAQ;AAAA,MACX,UAAU,IAAI,QAAQ,IAAI,OAAO;AAAA,IACrC;AAAA,IACA;AAAA,EACJ;AACJ;AAeO,SAAS,YACZ,KACA,SACF;AACE,SAAO,eAAe,eAClB,KACA,KACA,MACa;AACb,QAAI;AACA,YAAM,QAAQ,KAAK,KAAK,IAAI;AAAA,IAChC,SAAS,KAAK;AACV,YAAM,QAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAChE,UAAI,iBAAiB,OAAO;AAAA,QACxB,OAAO;AAAA,QACP,MAAM,EAAE,UAAW,IAAuB,QAAQ,UAAU;AAAA,QAC5D,OAAO;AAAA,UACH,SAAS;AAAA,YACL,QAAS,IAAuB;AAAA,YAChC,KAAM,IAAuB;AAAA,UACjC;AAAA,QACJ;AAAA,MACJ,CAAC;AACD,WAAK,GAAG;AAAA,IACZ;AAAA,EACJ;AACJ;","names":[]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { a as TraceBeamSDK, c as Tags, d as Extra } from '../client-CaHega3m.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fastify types (minimal interface for error handling)
|
|
5
|
+
*/
|
|
6
|
+
interface FastifyRequest {
|
|
7
|
+
method?: string;
|
|
8
|
+
url?: string;
|
|
9
|
+
routerPath?: string;
|
|
10
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
11
|
+
ip?: string;
|
|
12
|
+
params?: Record<string, unknown>;
|
|
13
|
+
query?: Record<string, unknown>;
|
|
14
|
+
body?: unknown;
|
|
15
|
+
}
|
|
16
|
+
interface FastifyReply {
|
|
17
|
+
statusCode?: number;
|
|
18
|
+
sent?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface FastifyError extends Error {
|
|
21
|
+
statusCode?: number;
|
|
22
|
+
code?: string;
|
|
23
|
+
}
|
|
24
|
+
interface FastifyInstance {
|
|
25
|
+
setErrorHandler: (handler: (error: FastifyError, request: FastifyRequest, reply: FastifyReply) => Promise<void> | void) => FastifyInstance;
|
|
26
|
+
addHook: (hook: string, handler: (...args: unknown[]) => Promise<void> | void) => FastifyInstance;
|
|
27
|
+
}
|
|
28
|
+
type FastifyPluginCallback = (fastify: FastifyInstance, options: TraceBeamFastifyPluginOptions, done: (err?: Error) => void) => void;
|
|
29
|
+
/**
|
|
30
|
+
* TraceBeam Fastify plugin options
|
|
31
|
+
*/
|
|
32
|
+
interface TraceBeamFastifyPluginOptions {
|
|
33
|
+
/** TraceBeamSDK instance */
|
|
34
|
+
sdk: TraceBeamSDK;
|
|
35
|
+
/** Additional tags to include with every error */
|
|
36
|
+
tags?: Tags;
|
|
37
|
+
/** Additional context to include with every error */
|
|
38
|
+
extra?: Extra;
|
|
39
|
+
/** Extract user ID from request */
|
|
40
|
+
getUserId?: (request: FastifyRequest) => string | undefined;
|
|
41
|
+
/** Filter which errors to capture */
|
|
42
|
+
shouldCapture?: (error: FastifyError, request: FastifyRequest) => boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Fastify plugin for TraceBeam SDK
|
|
46
|
+
*
|
|
47
|
+
* Automatically captures unhandled errors from routes.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import Fastify from 'fastify';
|
|
52
|
+
* import { TraceBeamSDK } from 'tracebeam-sdk';
|
|
53
|
+
* import { tracebeamPlugin } from 'tracebeam-sdk/integrations/fastify';
|
|
54
|
+
*
|
|
55
|
+
* const fastify = Fastify();
|
|
56
|
+
* const sdk = TraceBeamSDK.fromEnv();
|
|
57
|
+
*
|
|
58
|
+
* fastify.register(tracebeamPlugin, { sdk });
|
|
59
|
+
*
|
|
60
|
+
* // Your routes here...
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare const tracebeamPlugin: FastifyPluginCallback;
|
|
64
|
+
/**
|
|
65
|
+
* Create error handler hook for Fastify
|
|
66
|
+
*
|
|
67
|
+
* Alternative to plugin - use as onError hook
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* fastify.addHook('onError', createOnErrorHook(sdk));
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function createOnErrorHook(sdk: TraceBeamSDK, options?: Omit<TraceBeamFastifyPluginOptions, 'sdk'>): (request: FastifyRequest, reply: FastifyReply, error: FastifyError) => Promise<void>;
|
|
75
|
+
|
|
76
|
+
export { type TraceBeamFastifyPluginOptions, createOnErrorHook, tracebeamPlugin };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { a as TraceBeamSDK, c as Tags, d as Extra } from '../client-CaHega3m.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fastify types (minimal interface for error handling)
|
|
5
|
+
*/
|
|
6
|
+
interface FastifyRequest {
|
|
7
|
+
method?: string;
|
|
8
|
+
url?: string;
|
|
9
|
+
routerPath?: string;
|
|
10
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
11
|
+
ip?: string;
|
|
12
|
+
params?: Record<string, unknown>;
|
|
13
|
+
query?: Record<string, unknown>;
|
|
14
|
+
body?: unknown;
|
|
15
|
+
}
|
|
16
|
+
interface FastifyReply {
|
|
17
|
+
statusCode?: number;
|
|
18
|
+
sent?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface FastifyError extends Error {
|
|
21
|
+
statusCode?: number;
|
|
22
|
+
code?: string;
|
|
23
|
+
}
|
|
24
|
+
interface FastifyInstance {
|
|
25
|
+
setErrorHandler: (handler: (error: FastifyError, request: FastifyRequest, reply: FastifyReply) => Promise<void> | void) => FastifyInstance;
|
|
26
|
+
addHook: (hook: string, handler: (...args: unknown[]) => Promise<void> | void) => FastifyInstance;
|
|
27
|
+
}
|
|
28
|
+
type FastifyPluginCallback = (fastify: FastifyInstance, options: TraceBeamFastifyPluginOptions, done: (err?: Error) => void) => void;
|
|
29
|
+
/**
|
|
30
|
+
* TraceBeam Fastify plugin options
|
|
31
|
+
*/
|
|
32
|
+
interface TraceBeamFastifyPluginOptions {
|
|
33
|
+
/** TraceBeamSDK instance */
|
|
34
|
+
sdk: TraceBeamSDK;
|
|
35
|
+
/** Additional tags to include with every error */
|
|
36
|
+
tags?: Tags;
|
|
37
|
+
/** Additional context to include with every error */
|
|
38
|
+
extra?: Extra;
|
|
39
|
+
/** Extract user ID from request */
|
|
40
|
+
getUserId?: (request: FastifyRequest) => string | undefined;
|
|
41
|
+
/** Filter which errors to capture */
|
|
42
|
+
shouldCapture?: (error: FastifyError, request: FastifyRequest) => boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Fastify plugin for TraceBeam SDK
|
|
46
|
+
*
|
|
47
|
+
* Automatically captures unhandled errors from routes.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import Fastify from 'fastify';
|
|
52
|
+
* import { TraceBeamSDK } from 'tracebeam-sdk';
|
|
53
|
+
* import { tracebeamPlugin } from 'tracebeam-sdk/integrations/fastify';
|
|
54
|
+
*
|
|
55
|
+
* const fastify = Fastify();
|
|
56
|
+
* const sdk = TraceBeamSDK.fromEnv();
|
|
57
|
+
*
|
|
58
|
+
* fastify.register(tracebeamPlugin, { sdk });
|
|
59
|
+
*
|
|
60
|
+
* // Your routes here...
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare const tracebeamPlugin: FastifyPluginCallback;
|
|
64
|
+
/**
|
|
65
|
+
* Create error handler hook for Fastify
|
|
66
|
+
*
|
|
67
|
+
* Alternative to plugin - use as onError hook
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* fastify.addHook('onError', createOnErrorHook(sdk));
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function createOnErrorHook(sdk: TraceBeamSDK, options?: Omit<TraceBeamFastifyPluginOptions, 'sdk'>): (request: FastifyRequest, reply: FastifyReply, error: FastifyError) => Promise<void>;
|
|
75
|
+
|
|
76
|
+
export { type TraceBeamFastifyPluginOptions, createOnErrorHook, tracebeamPlugin };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/integrations/fastify.ts
|
|
21
|
+
var fastify_exports = {};
|
|
22
|
+
__export(fastify_exports, {
|
|
23
|
+
createOnErrorHook: () => createOnErrorHook,
|
|
24
|
+
tracebeamPlugin: () => tracebeamPlugin
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(fastify_exports);
|
|
27
|
+
var tracebeamPlugin = function tracebeamPlugin2(fastify, options, done) {
|
|
28
|
+
const { sdk, tags, extra, getUserId, shouldCapture } = options;
|
|
29
|
+
fastify.setErrorHandler(async (error, request, reply) => {
|
|
30
|
+
if (shouldCapture && !shouldCapture(error, request)) {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });
|
|
34
|
+
sdk.captureException(error, captureOptions);
|
|
35
|
+
throw error;
|
|
36
|
+
});
|
|
37
|
+
done();
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(tracebeamPlugin, /* @__PURE__ */ Symbol.for("fastify.display-name"), {
|
|
40
|
+
value: "tracebeam-sdk"
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(tracebeamPlugin, /* @__PURE__ */ Symbol.for("skip-override"), {
|
|
43
|
+
value: true
|
|
44
|
+
});
|
|
45
|
+
function buildCaptureOptions(request, options) {
|
|
46
|
+
const extra = {
|
|
47
|
+
...options.extra,
|
|
48
|
+
request: {
|
|
49
|
+
method: request.method,
|
|
50
|
+
url: request.url,
|
|
51
|
+
routerPath: request.routerPath,
|
|
52
|
+
ip: request.ip,
|
|
53
|
+
params: request.params,
|
|
54
|
+
query: request.query
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
if (options.getUserId) {
|
|
58
|
+
const userId = options.getUserId(request);
|
|
59
|
+
if (userId) {
|
|
60
|
+
extra["user_id"] = userId;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
level: "error",
|
|
65
|
+
tags: {
|
|
66
|
+
...options.tags,
|
|
67
|
+
endpoint: request.routerPath ?? request.url ?? "unknown"
|
|
68
|
+
},
|
|
69
|
+
extra
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function createOnErrorHook(sdk, options = {}) {
|
|
73
|
+
return async function onError(request, reply, error) {
|
|
74
|
+
const { tags, extra, getUserId, shouldCapture } = options;
|
|
75
|
+
if (shouldCapture && !shouldCapture(error, request)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });
|
|
79
|
+
sdk.captureException(error, captureOptions);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
createOnErrorHook,
|
|
85
|
+
tracebeamPlugin
|
|
86
|
+
});
|
|
87
|
+
//# sourceMappingURL=fastify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/fastify.ts"],"sourcesContent":["import type { TraceBeamSDK } from '../client.js';\nimport type { CaptureOptions, Tags, Extra } from '../types.js';\n\n/**\n * Fastify types (minimal interface for error handling)\n */\ninterface FastifyRequest {\n method?: string;\n url?: string;\n routerPath?: string;\n headers?: Record<string, string | string[] | undefined>;\n ip?: string;\n params?: Record<string, unknown>;\n query?: Record<string, unknown>;\n body?: unknown;\n}\n\ninterface FastifyReply {\n statusCode?: number;\n sent?: boolean;\n}\n\ninterface FastifyError extends Error {\n statusCode?: number;\n code?: string;\n}\n\ninterface FastifyInstance {\n setErrorHandler: (\n handler: (\n error: FastifyError,\n request: FastifyRequest,\n reply: FastifyReply\n ) => Promise<void> | void\n ) => FastifyInstance;\n addHook: (\n hook: string,\n handler: (...args: unknown[]) => Promise<void> | void\n ) => FastifyInstance;\n}\n\ntype FastifyPluginCallback = (\n fastify: FastifyInstance,\n options: TraceBeamFastifyPluginOptions,\n done: (err?: Error) => void\n) => void;\n\n/**\n * TraceBeam Fastify plugin options\n */\nexport interface TraceBeamFastifyPluginOptions {\n /** TraceBeamSDK instance */\n sdk: TraceBeamSDK;\n /** Additional tags to include with every error */\n tags?: Tags;\n /** Additional context to include with every error */\n extra?: Extra;\n /** Extract user ID from request */\n getUserId?: (request: FastifyRequest) => string | undefined;\n /** Filter which errors to capture */\n shouldCapture?: (error: FastifyError, request: FastifyRequest) => boolean;\n}\n\n/**\n * Fastify plugin for TraceBeam SDK\n * \n * Automatically captures unhandled errors from routes.\n * \n * @example\n * ```typescript\n * import Fastify from 'fastify';\n * import { TraceBeamSDK } from 'tracebeam-sdk';\n * import { tracebeamPlugin } from 'tracebeam-sdk/integrations/fastify';\n * \n * const fastify = Fastify();\n * const sdk = TraceBeamSDK.fromEnv();\n * \n * fastify.register(tracebeamPlugin, { sdk });\n * \n * // Your routes here...\n * ```\n */\nexport const tracebeamPlugin: FastifyPluginCallback = function tracebeamPlugin(\n fastify,\n options,\n done\n) {\n const { sdk, tags, extra, getUserId, shouldCapture } = options;\n\n fastify.setErrorHandler(async (error, request, reply) => {\n // Check if we should capture this error\n if (shouldCapture && !shouldCapture(error, request)) {\n throw error; // Re-throw to let other error handlers process it\n }\n\n // Build capture options\n const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });\n\n // Capture the error\n sdk.captureException(error, captureOptions);\n\n // Re-throw so Fastify's default error handling continues\n throw error;\n });\n\n done();\n};\n\n// Fastify plugin metadata using Object.defineProperty to avoid type issues\nObject.defineProperty(tracebeamPlugin, Symbol.for('fastify.display-name'), {\n value: 'tracebeam-sdk',\n});\nObject.defineProperty(tracebeamPlugin, Symbol.for('skip-override'), {\n value: true,\n});\n\n/**\n * Build capture options from Fastify request\n */\nfunction buildCaptureOptions(\n request: FastifyRequest,\n options: Pick<TraceBeamFastifyPluginOptions, 'tags' | 'extra' | 'getUserId'>\n): CaptureOptions {\n const extra: Extra = {\n ...options.extra,\n request: {\n method: request.method,\n url: request.url,\n routerPath: request.routerPath,\n ip: request.ip,\n params: request.params,\n query: request.query,\n },\n };\n\n // Add user ID if extractor provided\n if (options.getUserId) {\n const userId = options.getUserId(request);\n if (userId) {\n extra['user_id'] = userId;\n }\n }\n\n return {\n level: 'error',\n tags: {\n ...options.tags,\n endpoint: request.routerPath ?? request.url ?? 'unknown',\n },\n extra,\n };\n}\n\n/**\n * Create error handler hook for Fastify\n * \n * Alternative to plugin - use as onError hook\n * \n * @example\n * ```typescript\n * fastify.addHook('onError', createOnErrorHook(sdk));\n * ```\n */\nexport function createOnErrorHook(\n sdk: TraceBeamSDK,\n options: Omit<TraceBeamFastifyPluginOptions, 'sdk'> = {}\n) {\n return async function onError(\n request: FastifyRequest,\n reply: FastifyReply,\n error: FastifyError\n ): Promise<void> {\n const { tags, extra, getUserId, shouldCapture } = options;\n\n if (shouldCapture && !shouldCapture(error, request)) {\n return;\n }\n\n const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });\n sdk.captureException(error, captureOptions);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkFO,IAAM,kBAAyC,SAASA,iBAC3D,SACA,SACA,MACF;AACE,QAAM,EAAE,KAAK,MAAM,OAAO,WAAW,cAAc,IAAI;AAEvD,UAAQ,gBAAgB,OAAO,OAAO,SAAS,UAAU;AAErD,QAAI,iBAAiB,CAAC,cAAc,OAAO,OAAO,GAAG;AACjD,YAAM;AAAA,IACV;AAGA,UAAM,iBAAiB,oBAAoB,SAAS,EAAE,MAAM,OAAO,UAAU,CAAC;AAG9E,QAAI,iBAAiB,OAAO,cAAc;AAG1C,UAAM;AAAA,EACV,CAAC;AAED,OAAK;AACT;AAGA,OAAO,eAAe,iBAAiB,uBAAO,IAAI,sBAAsB,GAAG;AAAA,EACvE,OAAO;AACX,CAAC;AACD,OAAO,eAAe,iBAAiB,uBAAO,IAAI,eAAe,GAAG;AAAA,EAChE,OAAO;AACX,CAAC;AAKD,SAAS,oBACL,SACA,SACc;AACd,QAAM,QAAe;AAAA,IACjB,GAAG,QAAQ;AAAA,IACX,SAAS;AAAA,MACL,QAAQ,QAAQ;AAAA,MAChB,KAAK,QAAQ;AAAA,MACb,YAAY,QAAQ;AAAA,MACpB,IAAI,QAAQ;AAAA,MACZ,QAAQ,QAAQ;AAAA,MAChB,OAAO,QAAQ;AAAA,IACnB;AAAA,EACJ;AAGA,MAAI,QAAQ,WAAW;AACnB,UAAM,SAAS,QAAQ,UAAU,OAAO;AACxC,QAAI,QAAQ;AACR,YAAM,SAAS,IAAI;AAAA,IACvB;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,OAAO;AAAA,IACP,MAAM;AAAA,MACF,GAAG,QAAQ;AAAA,MACX,UAAU,QAAQ,cAAc,QAAQ,OAAO;AAAA,IACnD;AAAA,IACA;AAAA,EACJ;AACJ;AAYO,SAAS,kBACZ,KACA,UAAsD,CAAC,GACzD;AACE,SAAO,eAAe,QAClB,SACA,OACA,OACa;AACb,UAAM,EAAE,MAAM,OAAO,WAAW,cAAc,IAAI;AAElD,QAAI,iBAAiB,CAAC,cAAc,OAAO,OAAO,GAAG;AACjD;AAAA,IACJ;AAEA,UAAM,iBAAiB,oBAAoB,SAAS,EAAE,MAAM,OAAO,UAAU,CAAC;AAC9E,QAAI,iBAAiB,OAAO,cAAc;AAAA,EAC9C;AACJ;","names":["tracebeamPlugin"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/integrations/fastify.ts
|
|
2
|
+
var tracebeamPlugin = function tracebeamPlugin2(fastify, options, done) {
|
|
3
|
+
const { sdk, tags, extra, getUserId, shouldCapture } = options;
|
|
4
|
+
fastify.setErrorHandler(async (error, request, reply) => {
|
|
5
|
+
if (shouldCapture && !shouldCapture(error, request)) {
|
|
6
|
+
throw error;
|
|
7
|
+
}
|
|
8
|
+
const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });
|
|
9
|
+
sdk.captureException(error, captureOptions);
|
|
10
|
+
throw error;
|
|
11
|
+
});
|
|
12
|
+
done();
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(tracebeamPlugin, /* @__PURE__ */ Symbol.for("fastify.display-name"), {
|
|
15
|
+
value: "tracebeam-sdk"
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(tracebeamPlugin, /* @__PURE__ */ Symbol.for("skip-override"), {
|
|
18
|
+
value: true
|
|
19
|
+
});
|
|
20
|
+
function buildCaptureOptions(request, options) {
|
|
21
|
+
const extra = {
|
|
22
|
+
...options.extra,
|
|
23
|
+
request: {
|
|
24
|
+
method: request.method,
|
|
25
|
+
url: request.url,
|
|
26
|
+
routerPath: request.routerPath,
|
|
27
|
+
ip: request.ip,
|
|
28
|
+
params: request.params,
|
|
29
|
+
query: request.query
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
if (options.getUserId) {
|
|
33
|
+
const userId = options.getUserId(request);
|
|
34
|
+
if (userId) {
|
|
35
|
+
extra["user_id"] = userId;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
level: "error",
|
|
40
|
+
tags: {
|
|
41
|
+
...options.tags,
|
|
42
|
+
endpoint: request.routerPath ?? request.url ?? "unknown"
|
|
43
|
+
},
|
|
44
|
+
extra
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function createOnErrorHook(sdk, options = {}) {
|
|
48
|
+
return async function onError(request, reply, error) {
|
|
49
|
+
const { tags, extra, getUserId, shouldCapture } = options;
|
|
50
|
+
if (shouldCapture && !shouldCapture(error, request)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });
|
|
54
|
+
sdk.captureException(error, captureOptions);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
createOnErrorHook,
|
|
59
|
+
tracebeamPlugin
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=fastify.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/fastify.ts"],"sourcesContent":["import type { TraceBeamSDK } from '../client.js';\nimport type { CaptureOptions, Tags, Extra } from '../types.js';\n\n/**\n * Fastify types (minimal interface for error handling)\n */\ninterface FastifyRequest {\n method?: string;\n url?: string;\n routerPath?: string;\n headers?: Record<string, string | string[] | undefined>;\n ip?: string;\n params?: Record<string, unknown>;\n query?: Record<string, unknown>;\n body?: unknown;\n}\n\ninterface FastifyReply {\n statusCode?: number;\n sent?: boolean;\n}\n\ninterface FastifyError extends Error {\n statusCode?: number;\n code?: string;\n}\n\ninterface FastifyInstance {\n setErrorHandler: (\n handler: (\n error: FastifyError,\n request: FastifyRequest,\n reply: FastifyReply\n ) => Promise<void> | void\n ) => FastifyInstance;\n addHook: (\n hook: string,\n handler: (...args: unknown[]) => Promise<void> | void\n ) => FastifyInstance;\n}\n\ntype FastifyPluginCallback = (\n fastify: FastifyInstance,\n options: TraceBeamFastifyPluginOptions,\n done: (err?: Error) => void\n) => void;\n\n/**\n * TraceBeam Fastify plugin options\n */\nexport interface TraceBeamFastifyPluginOptions {\n /** TraceBeamSDK instance */\n sdk: TraceBeamSDK;\n /** Additional tags to include with every error */\n tags?: Tags;\n /** Additional context to include with every error */\n extra?: Extra;\n /** Extract user ID from request */\n getUserId?: (request: FastifyRequest) => string | undefined;\n /** Filter which errors to capture */\n shouldCapture?: (error: FastifyError, request: FastifyRequest) => boolean;\n}\n\n/**\n * Fastify plugin for TraceBeam SDK\n * \n * Automatically captures unhandled errors from routes.\n * \n * @example\n * ```typescript\n * import Fastify from 'fastify';\n * import { TraceBeamSDK } from 'tracebeam-sdk';\n * import { tracebeamPlugin } from 'tracebeam-sdk/integrations/fastify';\n * \n * const fastify = Fastify();\n * const sdk = TraceBeamSDK.fromEnv();\n * \n * fastify.register(tracebeamPlugin, { sdk });\n * \n * // Your routes here...\n * ```\n */\nexport const tracebeamPlugin: FastifyPluginCallback = function tracebeamPlugin(\n fastify,\n options,\n done\n) {\n const { sdk, tags, extra, getUserId, shouldCapture } = options;\n\n fastify.setErrorHandler(async (error, request, reply) => {\n // Check if we should capture this error\n if (shouldCapture && !shouldCapture(error, request)) {\n throw error; // Re-throw to let other error handlers process it\n }\n\n // Build capture options\n const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });\n\n // Capture the error\n sdk.captureException(error, captureOptions);\n\n // Re-throw so Fastify's default error handling continues\n throw error;\n });\n\n done();\n};\n\n// Fastify plugin metadata using Object.defineProperty to avoid type issues\nObject.defineProperty(tracebeamPlugin, Symbol.for('fastify.display-name'), {\n value: 'tracebeam-sdk',\n});\nObject.defineProperty(tracebeamPlugin, Symbol.for('skip-override'), {\n value: true,\n});\n\n/**\n * Build capture options from Fastify request\n */\nfunction buildCaptureOptions(\n request: FastifyRequest,\n options: Pick<TraceBeamFastifyPluginOptions, 'tags' | 'extra' | 'getUserId'>\n): CaptureOptions {\n const extra: Extra = {\n ...options.extra,\n request: {\n method: request.method,\n url: request.url,\n routerPath: request.routerPath,\n ip: request.ip,\n params: request.params,\n query: request.query,\n },\n };\n\n // Add user ID if extractor provided\n if (options.getUserId) {\n const userId = options.getUserId(request);\n if (userId) {\n extra['user_id'] = userId;\n }\n }\n\n return {\n level: 'error',\n tags: {\n ...options.tags,\n endpoint: request.routerPath ?? request.url ?? 'unknown',\n },\n extra,\n };\n}\n\n/**\n * Create error handler hook for Fastify\n * \n * Alternative to plugin - use as onError hook\n * \n * @example\n * ```typescript\n * fastify.addHook('onError', createOnErrorHook(sdk));\n * ```\n */\nexport function createOnErrorHook(\n sdk: TraceBeamSDK,\n options: Omit<TraceBeamFastifyPluginOptions, 'sdk'> = {}\n) {\n return async function onError(\n request: FastifyRequest,\n reply: FastifyReply,\n error: FastifyError\n ): Promise<void> {\n const { tags, extra, getUserId, shouldCapture } = options;\n\n if (shouldCapture && !shouldCapture(error, request)) {\n return;\n }\n\n const captureOptions = buildCaptureOptions(request, { tags, extra, getUserId });\n sdk.captureException(error, captureOptions);\n };\n}\n"],"mappings":";AAkFO,IAAM,kBAAyC,SAASA,iBAC3D,SACA,SACA,MACF;AACE,QAAM,EAAE,KAAK,MAAM,OAAO,WAAW,cAAc,IAAI;AAEvD,UAAQ,gBAAgB,OAAO,OAAO,SAAS,UAAU;AAErD,QAAI,iBAAiB,CAAC,cAAc,OAAO,OAAO,GAAG;AACjD,YAAM;AAAA,IACV;AAGA,UAAM,iBAAiB,oBAAoB,SAAS,EAAE,MAAM,OAAO,UAAU,CAAC;AAG9E,QAAI,iBAAiB,OAAO,cAAc;AAG1C,UAAM;AAAA,EACV,CAAC;AAED,OAAK;AACT;AAGA,OAAO,eAAe,iBAAiB,uBAAO,IAAI,sBAAsB,GAAG;AAAA,EACvE,OAAO;AACX,CAAC;AACD,OAAO,eAAe,iBAAiB,uBAAO,IAAI,eAAe,GAAG;AAAA,EAChE,OAAO;AACX,CAAC;AAKD,SAAS,oBACL,SACA,SACc;AACd,QAAM,QAAe;AAAA,IACjB,GAAG,QAAQ;AAAA,IACX,SAAS;AAAA,MACL,QAAQ,QAAQ;AAAA,MAChB,KAAK,QAAQ;AAAA,MACb,YAAY,QAAQ;AAAA,MACpB,IAAI,QAAQ;AAAA,MACZ,QAAQ,QAAQ;AAAA,MAChB,OAAO,QAAQ;AAAA,IACnB;AAAA,EACJ;AAGA,MAAI,QAAQ,WAAW;AACnB,UAAM,SAAS,QAAQ,UAAU,OAAO;AACxC,QAAI,QAAQ;AACR,YAAM,SAAS,IAAI;AAAA,IACvB;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,OAAO;AAAA,IACP,MAAM;AAAA,MACF,GAAG,QAAQ;AAAA,MACX,UAAU,QAAQ,cAAc,QAAQ,OAAO;AAAA,IACnD;AAAA,IACA;AAAA,EACJ;AACJ;AAYO,SAAS,kBACZ,KACA,UAAsD,CAAC,GACzD;AACE,SAAO,eAAe,QAClB,SACA,OACA,OACa;AACb,UAAM,EAAE,MAAM,OAAO,WAAW,cAAc,IAAI;AAElD,QAAI,iBAAiB,CAAC,cAAc,OAAO,OAAO,GAAG;AACjD;AAAA,IACJ;AAEA,UAAM,iBAAiB,oBAAoB,SAAS,EAAE,MAAM,OAAO,UAAU,CAAC;AAC9E,QAAI,iBAAiB,OAAO,cAAc;AAAA,EAC9C;AACJ;","names":["tracebeamPlugin"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tracebeam-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js SDK for TraceBeam API Observability Platform",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./integrations/express": {
|
|
15
|
+
"types": "./dist/integrations/express.d.ts",
|
|
16
|
+
"import": "./dist/integrations/express.js",
|
|
17
|
+
"require": "./dist/integrations/express.cjs"
|
|
18
|
+
},
|
|
19
|
+
"./integrations/fastify": {
|
|
20
|
+
"types": "./dist/integrations/fastify.d.ts",
|
|
21
|
+
"import": "./dist/integrations/fastify.js",
|
|
22
|
+
"require": "./dist/integrations/fastify.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"dev": "tsup --watch",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"test:watch": "vitest",
|
|
33
|
+
"lint": "tsc --noEmit",
|
|
34
|
+
"prepublishOnly": "npm run build"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"tracebeam",
|
|
38
|
+
"error-tracking",
|
|
39
|
+
"observability",
|
|
40
|
+
"monitoring",
|
|
41
|
+
"sdk",
|
|
42
|
+
"apm"
|
|
43
|
+
],
|
|
44
|
+
"author": "",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.0.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^20.10.0",
|
|
51
|
+
"tsup": "^8.0.0",
|
|
52
|
+
"typescript": "^5.3.0",
|
|
53
|
+
"vitest": "^2.0.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"express": "^4.0.0 || ^5.0.0",
|
|
57
|
+
"fastify": "^4.0.0 || ^5.0.0"
|
|
58
|
+
},
|
|
59
|
+
"peerDependenciesMeta": {
|
|
60
|
+
"express": {
|
|
61
|
+
"optional": true
|
|
62
|
+
},
|
|
63
|
+
"fastify": {
|
|
64
|
+
"optional": true
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|