@alanszp/express 14.1.1 → 14.2.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.
|
@@ -6,16 +6,19 @@ import { ILogger } from "@alanszp/logger";
|
|
|
6
6
|
export declare type AuthRequest = Request & {
|
|
7
7
|
user: JWTUser;
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
interface Viewable<T> {
|
|
10
|
+
toView: () => T;
|
|
11
|
+
}
|
|
12
|
+
export declare type BuildAuthEndpointOptions<Input extends BaseModel, CommandReturnType, ViewReturnType, ViewFunction extends ((crt: CommandReturnType, input: Input) => ViewReturnType) | undefined> = {
|
|
10
13
|
request: AuthRequest;
|
|
11
14
|
inputConstructor: (jwtUser: JWTUser) => Input;
|
|
12
|
-
command: (input: Input) => Promise<CommandReturnType &
|
|
13
|
-
toView?: () => ViewReturnType;
|
|
14
|
-
}>;
|
|
15
|
+
command: (input: Input) => Promise<CommandReturnType & Partial<Viewable<ViewReturnType>>>;
|
|
15
16
|
returnCode?: number;
|
|
16
|
-
view?:
|
|
17
|
+
view?: ViewFunction;
|
|
17
18
|
getLogger: () => ILogger;
|
|
18
19
|
};
|
|
20
|
+
declare type InferReturnType<ViewFunction, CommandReturnType, ViewReturnType> = ViewFunction extends undefined ? CommandReturnType extends Viewable<ViewReturnType> ? ViewReturnType : CommandReturnType : ViewReturnType;
|
|
19
21
|
export declare class BaseApi extends Controller {
|
|
20
|
-
protected buildAuthEndpoint<Input extends BaseModel, CommandReturnType, ViewReturnType>({ request, inputConstructor, command, returnCode, view, getLogger, }: BuildAuthEndpointOptions<Input, CommandReturnType, ViewReturnType>): Promise<
|
|
22
|
+
protected buildAuthEndpoint<Input extends BaseModel, CommandReturnType, ViewReturnType, ViewFunction extends ((crt: CommandReturnType, input: Input) => ViewReturnType) | undefined>({ request, inputConstructor, command, returnCode, view, getLogger, }: BuildAuthEndpointOptions<Input, CommandReturnType, ViewReturnType, ViewFunction>): Promise<InferReturnType<ViewFunction, CommandReturnType, ViewReturnType>>;
|
|
21
23
|
}
|
|
24
|
+
export {};
|
|
@@ -12,6 +12,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.BaseApi = void 0;
|
|
13
13
|
const tsoa_1 = require("tsoa");
|
|
14
14
|
const lodash_1 = require("lodash");
|
|
15
|
+
function isViewable(object) {
|
|
16
|
+
return (typeof object === "object" &&
|
|
17
|
+
object !== null &&
|
|
18
|
+
typeof object.toView === "function");
|
|
19
|
+
}
|
|
15
20
|
class BaseApi extends tsoa_1.Controller {
|
|
16
21
|
buildAuthEndpoint({ request, inputConstructor, command, returnCode = 200, view, getLogger, }) {
|
|
17
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -20,16 +25,19 @@ class BaseApi extends tsoa_1.Controller {
|
|
|
20
25
|
const logger = getLogger();
|
|
21
26
|
const input = inputConstructor(user);
|
|
22
27
|
logger.info(`${baseLog}.controller.starting`, { input });
|
|
23
|
-
const
|
|
28
|
+
const commandResponse = yield command(input);
|
|
24
29
|
this.setStatus(returnCode);
|
|
25
30
|
logger.info(`${baseLog}.controller.succeed`, { returnCode });
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
// If a view function is provided, use it to transform the command response
|
|
32
|
+
if (view) {
|
|
33
|
+
return view(commandResponse, input);
|
|
34
|
+
}
|
|
35
|
+
// If the command response is viewable, use it to transform the command response
|
|
36
|
+
if (isViewable(commandResponse)) {
|
|
37
|
+
return commandResponse.toView();
|
|
38
|
+
}
|
|
39
|
+
// Otherwise, return the command response as is
|
|
40
|
+
return commandResponse;
|
|
33
41
|
});
|
|
34
42
|
}
|
|
35
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseApi.js","sourceRoot":"","sources":["../../src/endpoints/BaseApi.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,+BAA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ACH;YAED,+CAA+C;YAC/C,OAAO,eAIN,CAAC;QACJ,CAAC;KAAA;CACF;AA7DD,0BA6DC"}
|
|
@@ -7,17 +7,35 @@ const tsoa_1 = require("tsoa");
|
|
|
7
7
|
const getRequestBaseLog_1 = require("../helpers/getRequestBaseLog");
|
|
8
8
|
const typeorm_1 = require("typeorm");
|
|
9
9
|
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
10
11
|
const errorRequestHandlerMiddleware = (getLogger) => (error, req, res, next) => {
|
|
11
12
|
const logger = getLogger();
|
|
12
13
|
const baseLog = (0, getRequestBaseLog_1.getRequestBaseLog)(req);
|
|
13
14
|
try {
|
|
14
15
|
if (error instanceof tsoa_1.ValidateError) {
|
|
15
|
-
const {
|
|
16
|
+
const errors = (0, lodash_1.mapValues)(error.fields, ({ message: rawMessage }) => {
|
|
17
|
+
// TSOA Error message format: "Something went wrong. Issues: A JSON with the errors."
|
|
18
|
+
if (rawMessage.includes(" Issues: ")) {
|
|
19
|
+
const [message, issues] = rawMessage.split(" Issues: ");
|
|
20
|
+
try {
|
|
21
|
+
// Try to parse issues as JSON
|
|
22
|
+
return {
|
|
23
|
+
message,
|
|
24
|
+
issues: JSON.parse(issues),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
// Send unparsable issues as string
|
|
29
|
+
return { message: rawMessage, issues };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return { message: rawMessage };
|
|
33
|
+
});
|
|
16
34
|
return res.status(400).json({
|
|
17
35
|
code: "json_schema_error",
|
|
18
36
|
message: "Error to validate JSON Schema",
|
|
19
37
|
context: {
|
|
20
|
-
errors
|
|
38
|
+
errors,
|
|
21
39
|
},
|
|
22
40
|
origin: (0, core_1.appIdentifier)(),
|
|
23
41
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorRequestHandlerMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/errorRequestHandlerMiddleware.ts"],"names":[],"mappings":";;;AACA,4CAIyB;AACzB,wCAA8C;AAC9C,+BAAqC;AAGrC,oEAAiE;AACjE,qCAAgE;AAChE,gEAA4E;
|
|
1
|
+
{"version":3,"file":"errorRequestHandlerMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/errorRequestHandlerMiddleware.ts"],"names":[],"mappings":";;;AACA,4CAIyB;AACzB,wCAA8C;AAC9C,+BAAqC;AAGrC,oEAAiE;AACjE,qCAAgE;AAChE,gEAA4E;AAC5E,mCAAmC;AAM5B,MAAM,6BAA6B,GACxC,CAAC,SAAwB,EAAE,EAAE,CAC7B,CAAC,KAAc,EAAE,GAAmB,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;IACzE,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,OAAO,GAAG,IAAA,qCAAiB,EAAC,GAAG,CAAC,CAAC;IAEvC,IAAI;QACF,IAAI,KAAK,YAAY,oBAAa,EAAE;YAClC,MAAM,MAAM,GAAG,IAAA,kBAAS,EAAC,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE;gBACjE,qFAAqF;gBACrF,IAAI,UAAU,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE;oBACpC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;oBACxD,IAAI;wBACF,8BAA8B;wBAC9B,OAAO;4BACL,OAAO;4BACP,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;yBAC3B,CAAC;qBACH;oBAAC,OAAO,KAAK,EAAE;wBACd,mCAAmC;wBACnC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;qBACxC;iBACF;gBAED,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;YACjC,CAAC,CAAC,CAAC;YACH,OAA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GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,gCAAc,EAAC,uBAAuB,CAAC,CAAC,CAAC;aAC/D;iBAAM;gBACL,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,oCAAoC,EAAE;oBAC3D,KAAK;iBACN,CAAC,CAAC;gBACH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,4BAAmB,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;aAC/D;YACD,OAAO;SACR;QAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,4BAAmB,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9D,MAAM,CAAC,KAAK,CACV,GAAG,OAAO,qDAAqD,EAC/D,EAAE,KAAK,EAAE,CACV,CAAC;KACH;IAAC,OAAO,YAAqB,EAAE;QAC9B,IAAI;YACF,qCAAqC;YACrC,MAAM,CAAC,KAAK,CACV,GAAG,OAAO,qEAAqE,EAC/E,EAAE,KAAK,EAAE,YAAY,EAAE,CACxB,CAAC;SACH;QAAC,OAAO,MAAe,EAAE,GAAE;QAE5B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,IAAI,EAAE,uBAAuB;YAC7B,OAAO,EAAE,uBAAuB;YAChC,OAAO,EAAE,EAAE;SACZ,CAAC,CAAC;KACJ;AACH,CAAC,CAAC;AAnGS,QAAA,6BAA6B,iCAmGtC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.2.0",
|
|
4
4
|
"description": "Alan's express utils and middlewares.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@alanszp/audit": "^14.0.4",
|
|
42
|
-
"@alanszp/jwt": "^14.1.
|
|
42
|
+
"@alanszp/jwt": "^14.1.2",
|
|
43
43
|
"@alanszp/logger": "^14.0.4",
|
|
44
44
|
"@alanszp/shared-context": "^14.0.4",
|
|
45
45
|
"@babel/core": "^7.23.9",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"body-parser": "^1.20.2",
|
|
48
48
|
"lodash": "^4.17.21"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "e59d2543abacfec6b6db1f39cb8002b5a082a179"
|
|
51
51
|
}
|