@dispatchitapp/express 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 +21 -0
- package/README.md +29 -0
- package/dist/index.cjs +57 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
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,29 @@
|
|
|
1
|
+
# @dispatchitapp/express
|
|
2
|
+
|
|
3
|
+
Express error-handler middleware for [Dispatch](https://dispatchit.app). Auto-captures
|
|
4
|
+
unhandled route errors with request context, then re-propagates — it never swallows the error.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import express from "express";
|
|
8
|
+
import { init } from "@dispatchitapp/node";
|
|
9
|
+
import { errorHandler } from "@dispatchitapp/express";
|
|
10
|
+
|
|
11
|
+
init({ apiKey: process.env.DISPATCH_API_KEY!, environment: process.env.NODE_ENV });
|
|
12
|
+
|
|
13
|
+
const app = express();
|
|
14
|
+
app.get("/orders/:id", (req, res) => { /* ... */ });
|
|
15
|
+
|
|
16
|
+
// Register LAST — after routes and any other error handlers.
|
|
17
|
+
app.use(errorHandler({
|
|
18
|
+
user: (req) => req.user && { external_id: req.user.id, email: req.user.email },
|
|
19
|
+
}));
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Each captured event carries `handled: false`, the request (`url`, `method`, `query_string`,
|
|
23
|
+
allow-listed headers, client IP), the `transaction` (`"POST /orders/:id"` — the matched route
|
|
24
|
+
pattern, low cardinality), and the resolved user. The error then continues to Express's default
|
|
25
|
+
handler (or yours), so behaviour is unchanged.
|
|
26
|
+
|
|
27
|
+
Options: `client` (defaults to the `init()` client), `user(req)`, `shouldHandle(err, req)`.
|
|
28
|
+
Pairs with [`@dispatchitapp/node`](../node)'s global handlers, which catch anything that escapes the
|
|
29
|
+
request lifecycle.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
errorHandler: () => errorHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_node = require("@dispatchitapp/node");
|
|
27
|
+
function transactionOf(req) {
|
|
28
|
+
const routePath = req.route?.path;
|
|
29
|
+
const base = req.baseUrl ?? "";
|
|
30
|
+
const path = routePath ? base + routePath : req.path ?? req.originalUrl ?? req.url;
|
|
31
|
+
if (!path) return void 0;
|
|
32
|
+
return req.method ? `${req.method} ${path}` : path;
|
|
33
|
+
}
|
|
34
|
+
function errorHandler(options = {}) {
|
|
35
|
+
return function dispatchErrorHandler(err, req, _res, next) {
|
|
36
|
+
try {
|
|
37
|
+
const client = options.client ?? (0, import_node.getClient)();
|
|
38
|
+
const handle = options.shouldHandle ? options.shouldHandle(err, req) : true;
|
|
39
|
+
if (client && handle) {
|
|
40
|
+
const resolvedUser = options.user ? options.user(req) : req.user;
|
|
41
|
+
client.captureException(err, {
|
|
42
|
+
handled: false,
|
|
43
|
+
request: (0, import_node.extractRequest)(req),
|
|
44
|
+
transaction: transactionOf(req),
|
|
45
|
+
user: (0, import_node.normalizeUser)(resolvedUser)
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
next(err);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
54
|
+
0 && (module.exports = {
|
|
55
|
+
errorHandler
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// @dispatchitapp/express — Express error-handler middleware.\n//\n// Register it LAST (after your routes and any other error handlers) so it sees unhandled route\n// errors with full request context, captures them (handled:false), and then re-propagates via\n// next(err) — it never swallows the error. The innermost-and-re-raise shape mirrors the gem's\n// 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 an Express request — we depend on no Express types.\nexport interface ExpressRequestLike extends HttpRequestLike {\n baseUrl?: string;\n path?: string;\n route?: { path?: string };\n user?: unknown;\n}\n\ntype NextFunction = (err?: unknown) => void;\n\nexport interface ErrorHandlerOptions {\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 req.user. */\n user?: (req: ExpressRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;\n /** Decide whether a given error should be captured. Default: always. */\n shouldHandle?: (err: unknown, req: ExpressRequestLike) => boolean;\n}\n\n// \"POST /orders/:id\" — prefer the matched route pattern (low cardinality), else the path.\nfunction transactionOf(req: ExpressRequestLike): string | undefined {\n const routePath = req.route?.path;\n const base = req.baseUrl ?? \"\";\n const path = routePath ? base + routePath : (req.path ?? req.originalUrl ?? req.url);\n if (!path) return undefined;\n return req.method ? `${req.method} ${path}` : path;\n}\n\nexport function errorHandler(options: ErrorHandlerOptions = {}) {\n // Arity 4 is required for Express to treat this as an error handler.\n return function dispatchErrorHandler(\n err: unknown,\n req: ExpressRequestLike,\n _res: unknown,\n next: NextFunction,\n ): void {\n try {\n const client = options.client ?? getClient();\n const handle = options.shouldHandle ? options.shouldHandle(err, req) : true;\n if (client && handle) {\n const resolvedUser = options.user ? options.user(req) : req.user;\n client.captureException(err, {\n handled: false,\n request: extractRequest(req),\n transaction: transactionOf(req),\n user: normalizeUser(resolvedUser),\n });\n }\n } catch {\n // Telemetry must never break the error pipeline.\n }\n next(err);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,kBAOO;AAsBP,SAAS,cAAc,KAA6C;AAClE,QAAM,YAAY,IAAI,OAAO;AAC7B,QAAM,OAAO,IAAI,WAAW;AAC5B,QAAM,OAAO,YAAY,OAAO,YAAa,IAAI,QAAQ,IAAI,eAAe,IAAI;AAChF,MAAI,CAAC,KAAM,QAAO;AAClB,SAAO,IAAI,SAAS,GAAG,IAAI,MAAM,IAAI,IAAI,KAAK;AAChD;AAEO,SAAS,aAAa,UAA+B,CAAC,GAAG;AAE9D,SAAO,SAAS,qBACd,KACA,KACA,MACA,MACM;AACN,QAAI;AACF,YAAM,SAAS,QAAQ,cAAU,uBAAU;AAC3C,YAAM,SAAS,QAAQ,eAAe,QAAQ,aAAa,KAAK,GAAG,IAAI;AACvE,UAAI,UAAU,QAAQ;AACpB,cAAM,eAAe,QAAQ,OAAO,QAAQ,KAAK,GAAG,IAAI,IAAI;AAC5D,eAAO,iBAAiB,KAAK;AAAA,UAC3B,SAAS;AAAA,UACT,aAAS,4BAAe,GAAG;AAAA,UAC3B,aAAa,cAAc,GAAG;AAAA,UAC9B,UAAM,2BAAc,YAAY;AAAA,QAClC,CAAC;AAAA,MACH;AAAA,IACF,QAAQ;AAAA,IAER;AACA,SAAK,GAAG;AAAA,EACV;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Client, HttpRequestLike, DispatchUser } from '@dispatchitapp/node';
|
|
2
|
+
|
|
3
|
+
interface ExpressRequestLike extends HttpRequestLike {
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
route?: {
|
|
7
|
+
path?: string;
|
|
8
|
+
};
|
|
9
|
+
user?: unknown;
|
|
10
|
+
}
|
|
11
|
+
type NextFunction = (err?: unknown) => void;
|
|
12
|
+
interface ErrorHandlerOptions {
|
|
13
|
+
/** Client to report through. Defaults to the module-level client from init(). */
|
|
14
|
+
client?: Client;
|
|
15
|
+
/** Resolve the affected user from the request. Falls back to req.user. */
|
|
16
|
+
user?: (req: ExpressRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;
|
|
17
|
+
/** Decide whether a given error should be captured. Default: always. */
|
|
18
|
+
shouldHandle?: (err: unknown, req: ExpressRequestLike) => boolean;
|
|
19
|
+
}
|
|
20
|
+
declare function errorHandler(options?: ErrorHandlerOptions): (err: unknown, req: ExpressRequestLike, _res: unknown, next: NextFunction) => void;
|
|
21
|
+
|
|
22
|
+
export { type ErrorHandlerOptions, type ExpressRequestLike, errorHandler };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Client, HttpRequestLike, DispatchUser } from '@dispatchitapp/node';
|
|
2
|
+
|
|
3
|
+
interface ExpressRequestLike extends HttpRequestLike {
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
route?: {
|
|
7
|
+
path?: string;
|
|
8
|
+
};
|
|
9
|
+
user?: unknown;
|
|
10
|
+
}
|
|
11
|
+
type NextFunction = (err?: unknown) => void;
|
|
12
|
+
interface ErrorHandlerOptions {
|
|
13
|
+
/** Client to report through. Defaults to the module-level client from init(). */
|
|
14
|
+
client?: Client;
|
|
15
|
+
/** Resolve the affected user from the request. Falls back to req.user. */
|
|
16
|
+
user?: (req: ExpressRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;
|
|
17
|
+
/** Decide whether a given error should be captured. Default: always. */
|
|
18
|
+
shouldHandle?: (err: unknown, req: ExpressRequestLike) => boolean;
|
|
19
|
+
}
|
|
20
|
+
declare function errorHandler(options?: ErrorHandlerOptions): (err: unknown, req: ExpressRequestLike, _res: unknown, next: NextFunction) => void;
|
|
21
|
+
|
|
22
|
+
export { type ErrorHandlerOptions, type ExpressRequestLike, errorHandler };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
extractRequest,
|
|
4
|
+
getClient,
|
|
5
|
+
normalizeUser
|
|
6
|
+
} from "@dispatchitapp/node";
|
|
7
|
+
function transactionOf(req) {
|
|
8
|
+
const routePath = req.route?.path;
|
|
9
|
+
const base = req.baseUrl ?? "";
|
|
10
|
+
const path = routePath ? base + routePath : req.path ?? req.originalUrl ?? req.url;
|
|
11
|
+
if (!path) return void 0;
|
|
12
|
+
return req.method ? `${req.method} ${path}` : path;
|
|
13
|
+
}
|
|
14
|
+
function errorHandler(options = {}) {
|
|
15
|
+
return function dispatchErrorHandler(err, req, _res, next) {
|
|
16
|
+
try {
|
|
17
|
+
const client = options.client ?? getClient();
|
|
18
|
+
const handle = options.shouldHandle ? options.shouldHandle(err, req) : true;
|
|
19
|
+
if (client && handle) {
|
|
20
|
+
const resolvedUser = options.user ? options.user(req) : req.user;
|
|
21
|
+
client.captureException(err, {
|
|
22
|
+
handled: false,
|
|
23
|
+
request: extractRequest(req),
|
|
24
|
+
transaction: transactionOf(req),
|
|
25
|
+
user: normalizeUser(resolvedUser)
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
} catch {
|
|
29
|
+
}
|
|
30
|
+
next(err);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
errorHandler
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// @dispatchitapp/express — Express error-handler middleware.\n//\n// Register it LAST (after your routes and any other error handlers) so it sees unhandled route\n// errors with full request context, captures them (handled:false), and then re-propagates via\n// next(err) — it never swallows the error. The innermost-and-re-raise shape mirrors the gem's\n// 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 an Express request — we depend on no Express types.\nexport interface ExpressRequestLike extends HttpRequestLike {\n baseUrl?: string;\n path?: string;\n route?: { path?: string };\n user?: unknown;\n}\n\ntype NextFunction = (err?: unknown) => void;\n\nexport interface ErrorHandlerOptions {\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 req.user. */\n user?: (req: ExpressRequestLike) => DispatchUser | Record<string, unknown> | null | undefined;\n /** Decide whether a given error should be captured. Default: always. */\n shouldHandle?: (err: unknown, req: ExpressRequestLike) => boolean;\n}\n\n// \"POST /orders/:id\" — prefer the matched route pattern (low cardinality), else the path.\nfunction transactionOf(req: ExpressRequestLike): string | undefined {\n const routePath = req.route?.path;\n const base = req.baseUrl ?? \"\";\n const path = routePath ? base + routePath : (req.path ?? req.originalUrl ?? req.url);\n if (!path) return undefined;\n return req.method ? `${req.method} ${path}` : path;\n}\n\nexport function errorHandler(options: ErrorHandlerOptions = {}) {\n // Arity 4 is required for Express to treat this as an error handler.\n return function dispatchErrorHandler(\n err: unknown,\n req: ExpressRequestLike,\n _res: unknown,\n next: NextFunction,\n ): void {\n try {\n const client = options.client ?? getClient();\n const handle = options.shouldHandle ? options.shouldHandle(err, req) : true;\n if (client && handle) {\n const resolvedUser = options.user ? options.user(req) : req.user;\n client.captureException(err, {\n handled: false,\n request: extractRequest(req),\n transaction: transactionOf(req),\n user: normalizeUser(resolvedUser),\n });\n }\n } catch {\n // Telemetry must never break the error pipeline.\n }\n next(err);\n };\n}\n"],"mappings":";AAOA;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAsBP,SAAS,cAAc,KAA6C;AAClE,QAAM,YAAY,IAAI,OAAO;AAC7B,QAAM,OAAO,IAAI,WAAW;AAC5B,QAAM,OAAO,YAAY,OAAO,YAAa,IAAI,QAAQ,IAAI,eAAe,IAAI;AAChF,MAAI,CAAC,KAAM,QAAO;AAClB,SAAO,IAAI,SAAS,GAAG,IAAI,MAAM,IAAI,IAAI,KAAK;AAChD;AAEO,SAAS,aAAa,UAA+B,CAAC,GAAG;AAE9D,SAAO,SAAS,qBACd,KACA,KACA,MACA,MACM;AACN,QAAI;AACF,YAAM,SAAS,QAAQ,UAAU,UAAU;AAC3C,YAAM,SAAS,QAAQ,eAAe,QAAQ,aAAa,KAAK,GAAG,IAAI;AACvE,UAAI,UAAU,QAAQ;AACpB,cAAM,eAAe,QAAQ,OAAO,QAAQ,KAAK,GAAG,IAAI,IAAI;AAC5D,eAAO,iBAAiB,KAAK;AAAA,UAC3B,SAAS;AAAA,UACT,SAAS,eAAe,GAAG;AAAA,UAC3B,aAAa,cAAc,GAAG;AAAA,UAC9B,MAAM,cAAc,YAAY;AAAA,QAClC,CAAC;AAAA,MACH;AAAA,IACF,QAAQ;AAAA,IAER;AACA,SAAK,GAAG;AAAA,EACV;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dispatchitapp/express",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Express error-handler middleware for Dispatch — auto-captures unhandled route errors with request 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/express"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/Recursivity-LLC/dispatch-sdks/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"error-tracking",
|
|
16
|
+
"express",
|
|
17
|
+
"middleware",
|
|
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
|
+
}
|