@alanszp/express 5.0.0 → 6.0.4
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/middlewares/createContext.d.ts +6 -0
- package/dist/middlewares/createContext.js +36 -0
- package/dist/middlewares/createContext.js.map +1 -0
- package/package.json +7 -6
- package/src/index.ts +1 -1
- package/src/middlewares/createContext.ts +44 -0
- package/src/types/custom.d.ts +1 -0
- package/dist/middlewares/extraContext.d.ts +0 -17
- package/dist/middlewares/extraContext.js +0 -41
- package/dist/middlewares/extraContext.js.map +0 -1
- package/src/middlewares/extraContext.ts +0 -59
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from "./types/AuthMethod";
|
|
|
3
3
|
export * from "./middlewares/auditLog";
|
|
4
4
|
export * from "./middlewares/accessLogger";
|
|
5
5
|
export * from "./middlewares/authedForOrg";
|
|
6
|
-
export * from "./middlewares/
|
|
6
|
+
export * from "./middlewares/createContext";
|
|
7
7
|
export * from "./middlewares/authWithJWT";
|
|
8
8
|
export * from "./middlewares/returnInternalServerError";
|
|
9
9
|
export * from "./middlewares/returnNotFound";
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ __exportStar(require("./types/AuthMethod"), exports);
|
|
|
15
15
|
__exportStar(require("./middlewares/auditLog"), exports);
|
|
16
16
|
__exportStar(require("./middlewares/accessLogger"), exports);
|
|
17
17
|
__exportStar(require("./middlewares/authedForOrg"), exports);
|
|
18
|
-
__exportStar(require("./middlewares/
|
|
18
|
+
__exportStar(require("./middlewares/createContext"), exports);
|
|
19
19
|
__exportStar(require("./middlewares/authWithJWT"), exports);
|
|
20
20
|
__exportStar(require("./middlewares/returnInternalServerError"), exports);
|
|
21
21
|
__exportStar(require("./middlewares/returnNotFound"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAAkC;AAClC,qDAAmC;AACnC,yDAAuC;AACvC,6DAA2C;AAC3C,6DAA2C;AAC3C,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAAkC;AAClC,qDAAmC;AACnC,yDAAuC;AACvC,6DAA2C;AAC3C,6DAA2C;AAC3C,8DAA4C;AAC5C,4DAA0C;AAC1C,0EAAwD;AACxD,+DAA6C;AAC7C,+DAA6C;AAC7C,yDAAuC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NextFunction, Response } from "express";
|
|
2
|
+
import { ILogger } from "@alanszp/logger";
|
|
3
|
+
import { Audit } from "@alanszp/audit";
|
|
4
|
+
import { GenericRequest } from "../types/GenericRequest";
|
|
5
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
6
|
+
export declare function createContext(sharedContext: SharedContext, baseLogger: ILogger, audit: Audit): (req: GenericRequest, _res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createContext = void 0;
|
|
7
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
8
|
+
const appIdentifier_1 = require("../helpers/appIdentifier");
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
10
|
+
function createContext(sharedContext, baseLogger, audit) {
|
|
11
|
+
return (req, _res, next) => {
|
|
12
|
+
var _a;
|
|
13
|
+
req.context = req.context || {};
|
|
14
|
+
const receivedChain = req.header("x-lifecycle-chain");
|
|
15
|
+
const lifecycleChain = (0, lodash_1.compact)([receivedChain, (0, appIdentifier_1.appIdentifier)()]).join(",");
|
|
16
|
+
const lifecycleId = ((_a = req.headers["x-lifecycle-id"]) === null || _a === void 0 ? void 0 : _a.toString()) || (0, cuid_1.default)();
|
|
17
|
+
const contextId = (0, cuid_1.default)();
|
|
18
|
+
sharedContext.run((context) => {
|
|
19
|
+
req.context.authenticated = [];
|
|
20
|
+
req.context.lifecycleId = context.lifecycleId;
|
|
21
|
+
req.context.lifecycleChain = context.lifecycleChain;
|
|
22
|
+
req.context.contextId = context.contextId;
|
|
23
|
+
req.context.log = context.logger;
|
|
24
|
+
req.context.audit = context.audit;
|
|
25
|
+
next();
|
|
26
|
+
}, {
|
|
27
|
+
logger: baseLogger,
|
|
28
|
+
audit: audit.withState(),
|
|
29
|
+
lifecycleId,
|
|
30
|
+
lifecycleChain,
|
|
31
|
+
contextId,
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
exports.createContext = createContext;
|
|
36
|
+
//# sourceMappingURL=createContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createContext.js","sourceRoot":"","sources":["../../src/middlewares/createContext.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AAIxB,4DAAyD;AAGzD,mCAAiC;AAEjC,SAAgB,aAAa,CAC3B,aAA4B,EAC5B,UAAmB,EACnB,KAAY;IAEZ,OAAO,CAAC,GAAmB,EAAE,IAAc,EAAE,IAAkB,EAAQ,EAAE;;QACvE,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;QAEhC,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QACtD,MAAM,cAAc,GAAG,IAAA,gBAAO,EAAC,CAAC,aAAa,EAAE,IAAA,6BAAa,GAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAE3E,MAAM,WAAW,GAAG,CAAA,MAAA,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,0CAAE,QAAQ,EAAE,KAAI,IAAA,cAAI,GAAE,CAAC;QAExE,MAAM,SAAS,GAAG,IAAA,cAAI,GAAE,CAAC;QAEzB,aAAa,CAAC,GAAG,CACf,CAAC,OAAO,EAAE,EAAE;YACV,GAAG,CAAC,OAAO,CAAC,aAAa,GAAG,EAAE,CAAC;YAC/B,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;YAC9C,GAAG,CAAC,OAAO,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;YACpD,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;YAC1C,GAAG,CAAC,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;YACjC,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;YAClC,IAAI,EAAE,CAAC;QACT,CAAC,EACD;YACE,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,KAAK,CAAC,SAAS,EAAE;YACxB,WAAW;YACX,cAAc;YACd,SAAS;SACV,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAlCD,sCAkCC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.4",
|
|
4
4
|
"description": "Alan's express utils and middlewares.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -31,13 +31,14 @@
|
|
|
31
31
|
"typescript": "^4.3.4"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@alanszp/audit": "^
|
|
35
|
-
"@alanszp/errors": "^
|
|
36
|
-
"@alanszp/jwt": "^
|
|
37
|
-
"@alanszp/logger": "^
|
|
34
|
+
"@alanszp/audit": "^6.0.0",
|
|
35
|
+
"@alanszp/errors": "^6.0.0",
|
|
36
|
+
"@alanszp/jwt": "^6.0.0",
|
|
37
|
+
"@alanszp/logger": "^6.0.0",
|
|
38
|
+
"@alanszp/shared-context": "^6.0.4",
|
|
38
39
|
"body-parser": "^1.19.0",
|
|
39
40
|
"cuid": "^2.1.8",
|
|
40
41
|
"lodash": "^4.17.21"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "f11faa4b3583d683efc45e30d0817e4d3a9786a2"
|
|
43
44
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from "./types/AuthMethod";
|
|
|
3
3
|
export * from "./middlewares/auditLog";
|
|
4
4
|
export * from "./middlewares/accessLogger";
|
|
5
5
|
export * from "./middlewares/authedForOrg";
|
|
6
|
-
export * from "./middlewares/
|
|
6
|
+
export * from "./middlewares/createContext";
|
|
7
7
|
export * from "./middlewares/authWithJWT";
|
|
8
8
|
export * from "./middlewares/returnInternalServerError";
|
|
9
9
|
export * from "./middlewares/returnNotFound";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import cuid from "cuid";
|
|
2
|
+
import { NextFunction, Response } from "express";
|
|
3
|
+
import { ILogger } from "@alanszp/logger";
|
|
4
|
+
import { Audit } from "@alanszp/audit";
|
|
5
|
+
import { appIdentifier } from "../helpers/appIdentifier";
|
|
6
|
+
import { GenericRequest } from "../types/GenericRequest";
|
|
7
|
+
import { SharedContext } from "@alanszp/shared-context";
|
|
8
|
+
import { compact } from "lodash";
|
|
9
|
+
|
|
10
|
+
export function createContext(
|
|
11
|
+
sharedContext: SharedContext,
|
|
12
|
+
baseLogger: ILogger,
|
|
13
|
+
audit: Audit
|
|
14
|
+
) {
|
|
15
|
+
return (req: GenericRequest, _res: Response, next: NextFunction): void => {
|
|
16
|
+
req.context = req.context || {};
|
|
17
|
+
|
|
18
|
+
const receivedChain = req.header("x-lifecycle-chain");
|
|
19
|
+
const lifecycleChain = compact([receivedChain, appIdentifier()]).join(",");
|
|
20
|
+
|
|
21
|
+
const lifecycleId = req.headers["x-lifecycle-id"]?.toString() || cuid();
|
|
22
|
+
|
|
23
|
+
const contextId = cuid();
|
|
24
|
+
|
|
25
|
+
sharedContext.run(
|
|
26
|
+
(context) => {
|
|
27
|
+
req.context.authenticated = [];
|
|
28
|
+
req.context.lifecycleId = context.lifecycleId;
|
|
29
|
+
req.context.lifecycleChain = context.lifecycleChain;
|
|
30
|
+
req.context.contextId = context.contextId;
|
|
31
|
+
req.context.log = context.logger;
|
|
32
|
+
req.context.audit = context.audit;
|
|
33
|
+
next();
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
logger: baseLogger,
|
|
37
|
+
audit: audit.withState(),
|
|
38
|
+
lifecycleId,
|
|
39
|
+
lifecycleChain,
|
|
40
|
+
contextId,
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
}
|
package/src/types/custom.d.ts
CHANGED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { NextFunction, Response } from "express";
|
|
3
|
-
import { ILogger } from "@alanszp/logger";
|
|
4
|
-
import { Audit } from "@alanszp/audit";
|
|
5
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
6
|
-
import { AuditWithState } from "@alanszp/audit/dist/auditWithState";
|
|
7
|
-
import { GenericRequest } from "../types/GenericRequest";
|
|
8
|
-
export interface RequestSharedContext {
|
|
9
|
-
audit: AuditWithState;
|
|
10
|
-
logger: ILogger;
|
|
11
|
-
lifecycleId: string;
|
|
12
|
-
lifecycleChain: string;
|
|
13
|
-
}
|
|
14
|
-
export declare function createExtraContext(baseLogger: ILogger, audit: Audit): {
|
|
15
|
-
requestSharedContext: AsyncLocalStorage<RequestSharedContext>;
|
|
16
|
-
extraContext: (req: GenericRequest, _res: Response, next: NextFunction) => void;
|
|
17
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createExtraContext = void 0;
|
|
7
|
-
const cuid_1 = __importDefault(require("cuid"));
|
|
8
|
-
const async_hooks_1 = require("async_hooks");
|
|
9
|
-
const appIdentifier_1 = require("../helpers/appIdentifier");
|
|
10
|
-
function createExtraContext(baseLogger, audit) {
|
|
11
|
-
const requestSharedContext = new async_hooks_1.AsyncLocalStorage();
|
|
12
|
-
return {
|
|
13
|
-
requestSharedContext,
|
|
14
|
-
extraContext: function extraContext(req, _res, next) {
|
|
15
|
-
var _a;
|
|
16
|
-
req.context = req.context || {};
|
|
17
|
-
const receivedChain = req.header("x-lifecycle-chain");
|
|
18
|
-
const separator = receivedChain ? "," : "";
|
|
19
|
-
const lifecycleChain = `${receivedChain || ""}${separator}${(0, appIdentifier_1.appIdentifier)()}`;
|
|
20
|
-
const lifecycleId = ((_a = req.headers["x-lifecycle-id"]) === null || _a === void 0 ? void 0 : _a.toString()) || (0, cuid_1.default)();
|
|
21
|
-
const logger = baseLogger.child({
|
|
22
|
-
lid: lifecycleId,
|
|
23
|
-
lch: lifecycleChain,
|
|
24
|
-
});
|
|
25
|
-
const auditWithState = audit.withState();
|
|
26
|
-
req.context.authenticated = [];
|
|
27
|
-
req.context.lifecycleId = lifecycleId;
|
|
28
|
-
req.context.lifecycleChain = lifecycleChain;
|
|
29
|
-
req.context.log = logger;
|
|
30
|
-
req.context.audit = auditWithState;
|
|
31
|
-
requestSharedContext.run({
|
|
32
|
-
audit: auditWithState,
|
|
33
|
-
logger,
|
|
34
|
-
lifecycleId,
|
|
35
|
-
lifecycleChain,
|
|
36
|
-
}, () => next());
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
exports.createExtraContext = createExtraContext;
|
|
41
|
-
//# sourceMappingURL=extraContext.js.map
|
|
@@ -1 +0,0 @@
|
|
|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,CAAC,GAAG,CACtB;gBACE,KAAK,EAAE,cAAc;gBACrB,MAAM;gBACN,WAAW;gBACX,cAAc;aACf,EACD,GAAG,EAAE,CAAC,IAAI,EAAE,CACb,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AA1CD,gDA0CC"}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import cuid from "cuid";
|
|
2
|
-
import { NextFunction, Response } from "express";
|
|
3
|
-
import { ILogger } from "@alanszp/logger";
|
|
4
|
-
import { Audit } from "@alanszp/audit";
|
|
5
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
6
|
-
import { appIdentifier } from "../helpers/appIdentifier";
|
|
7
|
-
import { AuditWithState } from "@alanszp/audit/dist/auditWithState";
|
|
8
|
-
import { GenericRequest } from "../types/GenericRequest";
|
|
9
|
-
|
|
10
|
-
export interface RequestSharedContext {
|
|
11
|
-
audit: AuditWithState;
|
|
12
|
-
logger: ILogger;
|
|
13
|
-
lifecycleId: string;
|
|
14
|
-
lifecycleChain: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function createExtraContext(baseLogger: ILogger, audit: Audit) {
|
|
18
|
-
const requestSharedContext = new AsyncLocalStorage<RequestSharedContext>();
|
|
19
|
-
return {
|
|
20
|
-
requestSharedContext,
|
|
21
|
-
extraContext: function extraContext(
|
|
22
|
-
req: GenericRequest,
|
|
23
|
-
_res: Response,
|
|
24
|
-
next: NextFunction
|
|
25
|
-
): void {
|
|
26
|
-
req.context = req.context || {};
|
|
27
|
-
|
|
28
|
-
const receivedChain = req.header("x-lifecycle-chain");
|
|
29
|
-
const separator = receivedChain ? "," : "";
|
|
30
|
-
const lifecycleChain = `${
|
|
31
|
-
receivedChain || ""
|
|
32
|
-
}${separator}${appIdentifier()}`;
|
|
33
|
-
const lifecycleId = req.headers["x-lifecycle-id"]?.toString() || cuid();
|
|
34
|
-
|
|
35
|
-
const logger = baseLogger.child({
|
|
36
|
-
lid: lifecycleId,
|
|
37
|
-
lch: lifecycleChain,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const auditWithState = audit.withState();
|
|
41
|
-
|
|
42
|
-
req.context.authenticated = [];
|
|
43
|
-
req.context.lifecycleId = lifecycleId;
|
|
44
|
-
req.context.lifecycleChain = lifecycleChain;
|
|
45
|
-
req.context.log = logger;
|
|
46
|
-
req.context.audit = auditWithState;
|
|
47
|
-
|
|
48
|
-
requestSharedContext.run(
|
|
49
|
-
{
|
|
50
|
-
audit: auditWithState,
|
|
51
|
-
logger,
|
|
52
|
-
lifecycleId,
|
|
53
|
-
lifecycleChain,
|
|
54
|
-
},
|
|
55
|
-
() => next()
|
|
56
|
-
);
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
}
|