@dispatchitapp/fastify 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Recursivity LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @dispatchitapp/fastify
2
+
3
+ A Fastify `onError` hook for [Dispatch](https://dispatchit.app). Captures unhandled request
4
+ errors with route context — and only observes, so Fastify's own error handling and reply are
5
+ unchanged.
6
+
7
+ ```ts
8
+ import Fastify from "fastify";
9
+ import { init } from "@dispatchitapp/node";
10
+ import { dispatchFastify } from "@dispatchitapp/fastify";
11
+
12
+ init({ apiKey: process.env.DISPATCH_API_KEY!, environment: process.env.NODE_ENV });
13
+
14
+ const app = Fastify();
15
+ await app.register(dispatchFastify, {
16
+ user: (req) => req.user && { external_id: req.user.id, email: req.user.email },
17
+ });
18
+ ```
19
+
20
+ Or attach the hook directly:
21
+
22
+ ```ts
23
+ import { onErrorHook } from "@dispatchitapp/fastify";
24
+ app.addHook("onError", onErrorHook({ /* client, user, shouldHandle */ }));
25
+ ```
26
+
27
+ Each captured event carries `handled: false`, the request context, the resolved user, and the
28
+ `transaction` (`"POST /orders/:id"` — the route pattern from `routeOptions.url`). The plugin is
29
+ marked `skip-override` so the hook applies app-wide. Pairs with [`@dispatchitapp/node`](../node)'s
30
+ global handlers for anything outside the request lifecycle.
package/dist/index.cjs ADDED
@@ -0,0 +1,60 @@
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/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ dispatchFastify: () => dispatchFastify,
24
+ onErrorHook: () => onErrorHook
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_node = require("@dispatchitapp/node");
28
+ function transactionOf(req) {
29
+ const url = req.routeOptions?.url ?? req.routerPath;
30
+ if (!url) return req.method;
31
+ return req.method ? `${req.method} ${url}` : url;
32
+ }
33
+ function onErrorHook(options = {}) {
34
+ return async (request, _reply, error) => {
35
+ try {
36
+ const client = options.client ?? (0, import_node.getClient)();
37
+ const handle = options.shouldHandle ? options.shouldHandle(error, request) : true;
38
+ if (!client || !handle) return;
39
+ const resolvedUser = options.user ? options.user(request) : request.user;
40
+ client.captureException(error, {
41
+ handled: false,
42
+ request: (0, import_node.extractRequest)(request),
43
+ transaction: transactionOf(request),
44
+ user: (0, import_node.normalizeUser)(resolvedUser)
45
+ });
46
+ } catch {
47
+ }
48
+ };
49
+ }
50
+ function dispatchFastify(fastify, options, done) {
51
+ fastify.addHook("onError", onErrorHook(options));
52
+ done();
53
+ }
54
+ dispatchFastify[/* @__PURE__ */ Symbol.for("skip-override")] = true;
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ dispatchFastify,
58
+ onErrorHook
59
+ });
60
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// @dispatchitapp/fastify — a Fastify onError hook.\n//\n// The onError hook OBSERVES errors thrown in the request lifecycle (it does not replace\n// Fastify's error handler or change the reply), captures them with handled:false + route\n// context, and lets Fastify proceed as usual — the same \"capture, don't swallow\" shape as the\n// gem's Rack middleware.\n\nimport {\n type Client,\n type DispatchUser,\n type HttpRequestLike,\n extractRequest,\n getClient,\n normalizeUser,\n} from \"@dispatchitapp/node\";\n\n// Structural subset of a Fastify request — no Fastify types required.\nexport interface FastifyRequestLike extends HttpRequestLike {\n routeOptions?: { url?: string };\n routerPath?: string;\n user?: unknown;\n}\n\nexport type OnErrorHook = (\n request: FastifyRequestLike,\n reply: unknown,\n error: unknown,\n) => Promise<void>;\n\nexport interface DispatchFastifyOptions {\n /** Client to report through. Defaults to the module-level client from init(). */\n client?: Client;\n /** Resolve the affected user from the request. Falls back to request.user. */\n user?: (req: FastifyRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;\n /** Decide whether a given error should be captured. Default: always. */\n shouldHandle?: (err: unknown, req: FastifyRequestLike) => boolean;\n}\n\ninterface FastifyLike {\n addHook(name: \"onError\", hook: OnErrorHook): unknown;\n}\ntype Done = (err?: Error) => void;\n\n// \"POST /orders/:id\" — the route pattern (low cardinality), not the concrete URL.\nfunction transactionOf(req: FastifyRequestLike): string | undefined {\n const url = req.routeOptions?.url ?? req.routerPath;\n if (!url) return req.method;\n return req.method ? `${req.method} ${url}` : url;\n}\n\n// The hook factory — use directly via `fastify.addHook(\"onError\", onErrorHook({ ... }))`.\nexport function onErrorHook(options: DispatchFastifyOptions = {}): OnErrorHook {\n return async (request, _reply, error) => {\n try {\n const client = options.client ?? getClient();\n const handle = options.shouldHandle ? options.shouldHandle(error, request) : true;\n if (!client || !handle) return;\n const resolvedUser = options.user ? options.user(request) : request.user;\n client.captureException(error, {\n handled: false,\n request: extractRequest(request),\n transaction: transactionOf(request),\n user: normalizeUser(resolvedUser),\n });\n } catch {\n // never interfere with Fastify's error flow\n }\n };\n}\n\n// Plugin form: `fastify.register(dispatchFastify, { client, user })`. Marked skip-override so the\n// hook applies app-wide rather than only within this plugin's encapsulation context.\nexport function dispatchFastify(\n fastify: FastifyLike,\n options: DispatchFastifyOptions,\n done: Done,\n): void {\n fastify.addHook(\"onError\", onErrorHook(options));\n done();\n}\n(dispatchFastify as unknown as Record<symbol, unknown>)[Symbol.for(\"skip-override\")] = true;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,kBAOO;AA8BP,SAAS,cAAc,KAA6C;AAClE,QAAM,MAAM,IAAI,cAAc,OAAO,IAAI;AACzC,MAAI,CAAC,IAAK,QAAO,IAAI;AACrB,SAAO,IAAI,SAAS,GAAG,IAAI,MAAM,IAAI,GAAG,KAAK;AAC/C;AAGO,SAAS,YAAY,UAAkC,CAAC,GAAgB;AAC7E,SAAO,OAAO,SAAS,QAAQ,UAAU;AACvC,QAAI;AACF,YAAM,SAAS,QAAQ,cAAU,uBAAU;AAC3C,YAAM,SAAS,QAAQ,eAAe,QAAQ,aAAa,OAAO,OAAO,IAAI;AAC7E,UAAI,CAAC,UAAU,CAAC,OAAQ;AACxB,YAAM,eAAe,QAAQ,OAAO,QAAQ,KAAK,OAAO,IAAI,QAAQ;AACpE,aAAO,iBAAiB,OAAO;AAAA,QAC7B,SAAS;AAAA,QACT,aAAS,4BAAe,OAAO;AAAA,QAC/B,aAAa,cAAc,OAAO;AAAA,QAClC,UAAM,2BAAc,YAAY;AAAA,MAClC,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AAIO,SAAS,gBACd,SACA,SACA,MACM;AACN,UAAQ,QAAQ,WAAW,YAAY,OAAO,CAAC;AAC/C,OAAK;AACP;AACC,gBAAuD,uBAAO,IAAI,eAAe,CAAC,IAAI;","names":[]}
@@ -0,0 +1,26 @@
1
+ import { Client, HttpRequestLike, DispatchUser } from '@dispatchitapp/node';
2
+
3
+ interface FastifyRequestLike extends HttpRequestLike {
4
+ routeOptions?: {
5
+ url?: string;
6
+ };
7
+ routerPath?: string;
8
+ user?: unknown;
9
+ }
10
+ type OnErrorHook = (request: FastifyRequestLike, reply: unknown, error: unknown) => Promise<void>;
11
+ interface DispatchFastifyOptions {
12
+ /** Client to report through. Defaults to the module-level client from init(). */
13
+ client?: Client;
14
+ /** Resolve the affected user from the request. Falls back to request.user. */
15
+ user?: (req: FastifyRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;
16
+ /** Decide whether a given error should be captured. Default: always. */
17
+ shouldHandle?: (err: unknown, req: FastifyRequestLike) => boolean;
18
+ }
19
+ interface FastifyLike {
20
+ addHook(name: "onError", hook: OnErrorHook): unknown;
21
+ }
22
+ type Done = (err?: Error) => void;
23
+ declare function onErrorHook(options?: DispatchFastifyOptions): OnErrorHook;
24
+ declare function dispatchFastify(fastify: FastifyLike, options: DispatchFastifyOptions, done: Done): void;
25
+
26
+ export { type DispatchFastifyOptions, type FastifyRequestLike, type OnErrorHook, dispatchFastify, onErrorHook };
@@ -0,0 +1,26 @@
1
+ import { Client, HttpRequestLike, DispatchUser } from '@dispatchitapp/node';
2
+
3
+ interface FastifyRequestLike extends HttpRequestLike {
4
+ routeOptions?: {
5
+ url?: string;
6
+ };
7
+ routerPath?: string;
8
+ user?: unknown;
9
+ }
10
+ type OnErrorHook = (request: FastifyRequestLike, reply: unknown, error: unknown) => Promise<void>;
11
+ interface DispatchFastifyOptions {
12
+ /** Client to report through. Defaults to the module-level client from init(). */
13
+ client?: Client;
14
+ /** Resolve the affected user from the request. Falls back to request.user. */
15
+ user?: (req: FastifyRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;
16
+ /** Decide whether a given error should be captured. Default: always. */
17
+ shouldHandle?: (err: unknown, req: FastifyRequestLike) => boolean;
18
+ }
19
+ interface FastifyLike {
20
+ addHook(name: "onError", hook: OnErrorHook): unknown;
21
+ }
22
+ type Done = (err?: Error) => void;
23
+ declare function onErrorHook(options?: DispatchFastifyOptions): OnErrorHook;
24
+ declare function dispatchFastify(fastify: FastifyLike, options: DispatchFastifyOptions, done: Done): void;
25
+
26
+ export { type DispatchFastifyOptions, type FastifyRequestLike, type OnErrorHook, dispatchFastify, onErrorHook };
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ // src/index.ts
2
+ import {
3
+ extractRequest,
4
+ getClient,
5
+ normalizeUser
6
+ } from "@dispatchitapp/node";
7
+ function transactionOf(req) {
8
+ const url = req.routeOptions?.url ?? req.routerPath;
9
+ if (!url) return req.method;
10
+ return req.method ? `${req.method} ${url}` : url;
11
+ }
12
+ function onErrorHook(options = {}) {
13
+ return async (request, _reply, error) => {
14
+ try {
15
+ const client = options.client ?? getClient();
16
+ const handle = options.shouldHandle ? options.shouldHandle(error, request) : true;
17
+ if (!client || !handle) return;
18
+ const resolvedUser = options.user ? options.user(request) : request.user;
19
+ client.captureException(error, {
20
+ handled: false,
21
+ request: extractRequest(request),
22
+ transaction: transactionOf(request),
23
+ user: normalizeUser(resolvedUser)
24
+ });
25
+ } catch {
26
+ }
27
+ };
28
+ }
29
+ function dispatchFastify(fastify, options, done) {
30
+ fastify.addHook("onError", onErrorHook(options));
31
+ done();
32
+ }
33
+ dispatchFastify[/* @__PURE__ */ Symbol.for("skip-override")] = true;
34
+ export {
35
+ dispatchFastify,
36
+ onErrorHook
37
+ };
38
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// @dispatchitapp/fastify — a Fastify onError hook.\n//\n// The onError hook OBSERVES errors thrown in the request lifecycle (it does not replace\n// Fastify's error handler or change the reply), captures them with handled:false + route\n// context, and lets Fastify proceed as usual — the same \"capture, don't swallow\" shape as the\n// gem's Rack middleware.\n\nimport {\n type Client,\n type DispatchUser,\n type HttpRequestLike,\n extractRequest,\n getClient,\n normalizeUser,\n} from \"@dispatchitapp/node\";\n\n// Structural subset of a Fastify request — no Fastify types required.\nexport interface FastifyRequestLike extends HttpRequestLike {\n routeOptions?: { url?: string };\n routerPath?: string;\n user?: unknown;\n}\n\nexport type OnErrorHook = (\n request: FastifyRequestLike,\n reply: unknown,\n error: unknown,\n) => Promise<void>;\n\nexport interface DispatchFastifyOptions {\n /** Client to report through. Defaults to the module-level client from init(). */\n client?: Client;\n /** Resolve the affected user from the request. Falls back to request.user. */\n user?: (req: FastifyRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;\n /** Decide whether a given error should be captured. Default: always. */\n shouldHandle?: (err: unknown, req: FastifyRequestLike) => boolean;\n}\n\ninterface FastifyLike {\n addHook(name: \"onError\", hook: OnErrorHook): unknown;\n}\ntype Done = (err?: Error) => void;\n\n// \"POST /orders/:id\" — the route pattern (low cardinality), not the concrete URL.\nfunction transactionOf(req: FastifyRequestLike): string | undefined {\n const url = req.routeOptions?.url ?? req.routerPath;\n if (!url) return req.method;\n return req.method ? `${req.method} ${url}` : url;\n}\n\n// The hook factory — use directly via `fastify.addHook(\"onError\", onErrorHook({ ... }))`.\nexport function onErrorHook(options: DispatchFastifyOptions = {}): OnErrorHook {\n return async (request, _reply, error) => {\n try {\n const client = options.client ?? getClient();\n const handle = options.shouldHandle ? options.shouldHandle(error, request) : true;\n if (!client || !handle) return;\n const resolvedUser = options.user ? options.user(request) : request.user;\n client.captureException(error, {\n handled: false,\n request: extractRequest(request),\n transaction: transactionOf(request),\n user: normalizeUser(resolvedUser),\n });\n } catch {\n // never interfere with Fastify's error flow\n }\n };\n}\n\n// Plugin form: `fastify.register(dispatchFastify, { client, user })`. Marked skip-override so the\n// hook applies app-wide rather than only within this plugin's encapsulation context.\nexport function dispatchFastify(\n fastify: FastifyLike,\n options: DispatchFastifyOptions,\n done: Done,\n): void {\n fastify.addHook(\"onError\", onErrorHook(options));\n done();\n}\n(dispatchFastify as unknown as Record<symbol, unknown>)[Symbol.for(\"skip-override\")] = true;\n"],"mappings":";AAOA;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA8BP,SAAS,cAAc,KAA6C;AAClE,QAAM,MAAM,IAAI,cAAc,OAAO,IAAI;AACzC,MAAI,CAAC,IAAK,QAAO,IAAI;AACrB,SAAO,IAAI,SAAS,GAAG,IAAI,MAAM,IAAI,GAAG,KAAK;AAC/C;AAGO,SAAS,YAAY,UAAkC,CAAC,GAAgB;AAC7E,SAAO,OAAO,SAAS,QAAQ,UAAU;AACvC,QAAI;AACF,YAAM,SAAS,QAAQ,UAAU,UAAU;AAC3C,YAAM,SAAS,QAAQ,eAAe,QAAQ,aAAa,OAAO,OAAO,IAAI;AAC7E,UAAI,CAAC,UAAU,CAAC,OAAQ;AACxB,YAAM,eAAe,QAAQ,OAAO,QAAQ,KAAK,OAAO,IAAI,QAAQ;AACpE,aAAO,iBAAiB,OAAO;AAAA,QAC7B,SAAS;AAAA,QACT,SAAS,eAAe,OAAO;AAAA,QAC/B,aAAa,cAAc,OAAO;AAAA,QAClC,MAAM,cAAc,YAAY;AAAA,MAClC,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AAIO,SAAS,gBACd,SACA,SACA,MACM;AACN,UAAQ,QAAQ,WAAW,YAAY,OAAO,CAAC;AAC/C,OAAK;AACP;AACC,gBAAuD,uBAAO,IAAI,eAAe,CAAC,IAAI;","names":[]}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@dispatchitapp/fastify",
3
+ "version": "1.0.0",
4
+ "description": "Fastify onError hook for Dispatch — auto-captures unhandled request errors with route context.",
5
+ "license": "MIT",
6
+ "author": "Dispatch Team <hello@dispatchit.app>",
7
+ "homepage": "https://dispatchit.app",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/Recursivity-LLC/dispatch-sdks.git",
11
+ "directory": "packages/fastify"
12
+ },
13
+ "bugs": "https://github.com/Recursivity-LLC/dispatch-sdks/issues",
14
+ "keywords": [
15
+ "error-tracking",
16
+ "fastify",
17
+ "plugin",
18
+ "dispatch",
19
+ "monitoring"
20
+ ],
21
+ "publishConfig": {
22
+ "access": "public",
23
+ "provenance": true
24
+ },
25
+ "type": "module",
26
+ "main": "./dist/index.cjs",
27
+ "module": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "require": "./dist/index.cjs"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "engines": {
40
+ "node": ">=18"
41
+ },
42
+ "dependencies": {
43
+ "@dispatchitapp/node": "1.0.0"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^20.14.0",
47
+ "tsup": "^8.0.0",
48
+ "typescript": "^5.5.0",
49
+ "vitest": "^2.0.0",
50
+ "@dispatchitapp/core": "1.0.0"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup",
54
+ "test": "vitest run",
55
+ "typecheck": "tsc --noEmit",
56
+ "lint": "tsc --noEmit"
57
+ }
58
+ }