@alanszp/express 14.0.5 → 14.1.1
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/endpoints/BaseApi.d.ts +21 -0
- package/dist/endpoints/BaseApi.js +37 -0
- package/dist/endpoints/BaseApi.js.map +1 -0
- package/dist/errors/AuthMethodFailureError.d.ts +7 -0
- package/dist/errors/AuthMethodFailureError.js +20 -0
- package/dist/errors/AuthMethodFailureError.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/middlewares/authenticateUser.d.ts +7 -1
- package/dist/middlewares/authenticateUser.js +52 -26
- package/dist/middlewares/authenticateUser.js.map +1 -1
- package/dist/middlewares/errorRequestHandlerMiddleware.js +14 -1
- package/dist/middlewares/errorRequestHandlerMiddleware.js.map +1 -1
- package/dist/types/docs/examples.d.ts +26 -0
- package/dist/types/docs/examples.js +34 -0
- package/dist/types/docs/examples.js.map +1 -0
- package/package.json +7 -4
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { JWTUser } from "@alanszp/jwt";
|
|
2
|
+
import { BaseModel } from "@alanszp/validations";
|
|
3
|
+
import { Controller } from "tsoa";
|
|
4
|
+
import { Request } from "express";
|
|
5
|
+
import { ILogger } from "@alanszp/logger";
|
|
6
|
+
export declare type AuthRequest = Request & {
|
|
7
|
+
user: JWTUser;
|
|
8
|
+
};
|
|
9
|
+
export declare type BuildAuthEndpointOptions<Input extends BaseModel, CommandReturnType, ViewReturnType> = {
|
|
10
|
+
request: AuthRequest;
|
|
11
|
+
inputConstructor: (jwtUser: JWTUser) => Input;
|
|
12
|
+
command: (input: Input) => Promise<CommandReturnType & {
|
|
13
|
+
toView?: () => ViewReturnType;
|
|
14
|
+
}>;
|
|
15
|
+
returnCode?: number;
|
|
16
|
+
view?: (commandReturn: CommandReturnType) => ViewReturnType;
|
|
17
|
+
getLogger: () => ILogger;
|
|
18
|
+
};
|
|
19
|
+
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<ViewReturnType | CommandReturnType>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseApi = void 0;
|
|
13
|
+
const tsoa_1 = require("tsoa");
|
|
14
|
+
const lodash_1 = require("lodash");
|
|
15
|
+
class BaseApi extends tsoa_1.Controller {
|
|
16
|
+
buildAuthEndpoint({ request, inputConstructor, command, returnCode = 200, view, getLogger, }) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const { path, method, user } = request;
|
|
19
|
+
const baseLog = `${(0, lodash_1.snakeCase)(path)}.${(0, lodash_1.snakeCase)(method)}`;
|
|
20
|
+
const logger = getLogger();
|
|
21
|
+
const input = inputConstructor(user);
|
|
22
|
+
logger.info(`${baseLog}.controller.starting`, { input });
|
|
23
|
+
const viewResponse = yield command(input);
|
|
24
|
+
this.setStatus(returnCode);
|
|
25
|
+
logger.info(`${baseLog}.controller.succeed`, { returnCode });
|
|
26
|
+
return view
|
|
27
|
+
? view(viewResponse)
|
|
28
|
+
: (viewResponse === null || viewResponse === void 0 ? void 0 : viewResponse.toView) &&
|
|
29
|
+
typeof viewResponse.toView === "function" &&
|
|
30
|
+
viewResponse.toView.arguments === 0
|
|
31
|
+
? viewResponse.toView()
|
|
32
|
+
: viewResponse;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.BaseApi = BaseApi;
|
|
37
|
+
//# sourceMappingURL=BaseApi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseApi.js","sourceRoot":"","sources":["../../src/endpoints/BaseApi.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,+BAAkC;AAElC,mCAAmC;AAoBnC,MAAa,OAAQ,SAAQ,iBAAU;IACrB,iBAAiB,CAI/B,EACA,OAAO,EACP,gBAAgB,EAChB,OAAO,EACP,UAAU,GAAG,GAAG,EAChB,IAAI,EACJ,SAAS,GAKV;;YACC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;YACvC,MAAM,OAAO,GAAG,GAAG,IAAA,kBAAS,EAAC,IAAI,CAAC,IAAI,IAAA,kBAAS,EAAC,MAAM,CAAC,EAAE,CAAC;YAC1D,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;YAE3B,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;YAErC,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,sBAAsB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;YAEzD,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC;YAE1C,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;YAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,qBAAqB,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;YAE7D,OAAO,IAAI;gBACT,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC;gBACpB,CAAC,CAAC,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,MAAM;oBAClB,OAAO,YAAY,CAAC,MAAM,KAAK,UAAU;oBACzC,YAAY,CAAC,MAAM,CAAC,SAAS,KAAK,CAAC;oBACrC,CAAC,CAAC,YAAY,CAAC,MAAM,EAAE;oBACvB,CAAC,CAAC,YAAY,CAAC;QACrB,CAAC;KAAA;CACF;AAtCD,0BAsCC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HttpRenderableError, RenderableContext } from "@alanszp/errors";
|
|
2
|
+
export declare class AuthenticationMethodError extends HttpRenderableError {
|
|
3
|
+
requiredChecks: string[];
|
|
4
|
+
constructor(requiredChecks: string[]);
|
|
5
|
+
httpCode(): number;
|
|
6
|
+
context(): RenderableContext;
|
|
7
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthenticationMethodError = void 0;
|
|
4
|
+
const errors_1 = require("@alanszp/errors");
|
|
5
|
+
class AuthenticationMethodError extends errors_1.HttpRenderableError {
|
|
6
|
+
constructor(requiredChecks) {
|
|
7
|
+
super("Authentication Method Error");
|
|
8
|
+
this.requiredChecks = requiredChecks;
|
|
9
|
+
}
|
|
10
|
+
httpCode() {
|
|
11
|
+
return 401;
|
|
12
|
+
}
|
|
13
|
+
context() {
|
|
14
|
+
return {
|
|
15
|
+
requiredChecks: this.requiredChecks,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.AuthenticationMethodError = AuthenticationMethodError;
|
|
20
|
+
//# sourceMappingURL=AuthMethodFailureError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthMethodFailureError.js","sourceRoot":"","sources":["../../src/errors/AuthMethodFailureError.ts"],"names":[],"mappings":";;;AAAA,4CAAyE;AAEzE,MAAa,yBAA0B,SAAQ,4BAAmB;IAGhE,YAAY,cAAwB;QAClC,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACrC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;IAED,QAAQ;QACN,OAAO,GAAG,CAAC;IACb,CAAC;IAEM,OAAO;QACZ,OAAO;YACL,cAAc,EAAE,IAAI,CAAC,cAAc;SACpC,CAAC;IACJ,CAAC;CACF;AAjBD,8DAiBC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from "./types/AuthMethod";
|
|
2
|
+
export * from "./types/docs/examples";
|
|
2
3
|
export * from "./middlewares/auditLog";
|
|
3
4
|
export * from "./middlewares/accessLogger";
|
|
4
5
|
export * from "./middlewares/authedForOrg";
|
|
@@ -10,3 +11,5 @@ export * from "./middlewares/jsonBodyParser";
|
|
|
10
11
|
export * from "./middlewares/hasRoles";
|
|
11
12
|
export * from "./middlewares/hasPermissions";
|
|
12
13
|
export * from "./helpers/getRequestBaseLog";
|
|
14
|
+
export * from "./endpoints/BaseApi";
|
|
15
|
+
export * from "./errors/AuthMethodFailureError";
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./types/AuthMethod"), exports);
|
|
14
|
+
__exportStar(require("./types/docs/examples"), exports);
|
|
14
15
|
__exportStar(require("./middlewares/auditLog"), exports);
|
|
15
16
|
__exportStar(require("./middlewares/accessLogger"), exports);
|
|
16
17
|
__exportStar(require("./middlewares/authedForOrg"), exports);
|
|
@@ -22,4 +23,6 @@ __exportStar(require("./middlewares/jsonBodyParser"), exports);
|
|
|
22
23
|
__exportStar(require("./middlewares/hasRoles"), exports);
|
|
23
24
|
__exportStar(require("./middlewares/hasPermissions"), exports);
|
|
24
25
|
__exportStar(require("./helpers/getRequestBaseLog"), exports);
|
|
26
|
+
__exportStar(require("./endpoints/BaseApi"), exports);
|
|
27
|
+
__exportStar(require("./errors/AuthMethodFailureError"), exports);
|
|
25
28
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAmC;AACnC,yDAAuC;AACvC,6DAA2C;AAC3C,6DAA2C;AAC3C,8DAA4C;AAC5C,iEAA+C;AAC/C,8EAA4D;AAC5D,+DAA6C;AAC7C,+DAA6C;AAC7C,yDAAuC;AACvC,+DAA6C;AAC7C,8DAA4C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAmC;AACnC,wDAAsC;AACtC,yDAAuC;AACvC,6DAA2C;AAC3C,6DAA2C;AAC3C,8DAA4C;AAC5C,iEAA+C;AAC/C,8EAA4D;AAC5D,+DAA6C;AAC7C,+DAA6C;AAC7C,yDAAuC;AACvC,+DAA6C;AAC7C,8DAA4C;AAC5C,sDAAoC;AACpC,kEAAgD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { VerifyOptions } from "@alanszp/jwt";
|
|
1
|
+
import { JWTUser, VerifyOptions } from "@alanszp/jwt";
|
|
2
2
|
import { NextFunction, Response } from "express";
|
|
3
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
4
4
|
export declare enum AuthMethods {
|
|
@@ -23,3 +23,9 @@ export interface BothMethodsOptions {
|
|
|
23
23
|
}
|
|
24
24
|
export declare type AuthOptions = JWTOptions | ApiKeyOptions | BothMethodsOptions;
|
|
25
25
|
export declare function createAuthContext<Options extends AuthOptions>(options: Options): (authMethods: Options["types"][number][]) => (req: GenericRequest, res: Response, next: NextFunction) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Attempt to authenticate a user using the authentication methods sent by parameter
|
|
28
|
+
* Used in authenticationModule for TSOA
|
|
29
|
+
* https://tsoa-community.github.io/docs/authentication.html
|
|
30
|
+
*/
|
|
31
|
+
export declare function tsoaAuthProvider<Options extends AuthOptions>(req: GenericRequest, options: Options, authMethods: AuthMethods[]): Promise<JWTUser>;
|
|
@@ -9,10 +9,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.createAuthContext = exports.AuthMethods = void 0;
|
|
12
|
+
exports.tsoaAuthProvider = exports.createAuthContext = exports.AuthMethods = void 0;
|
|
13
13
|
const jwt_1 = require("@alanszp/jwt");
|
|
14
14
|
const getRequestLogger_1 = require("../helpers/getRequestLogger");
|
|
15
15
|
const lodash_1 = require("lodash");
|
|
16
|
+
const AuthMethodFailureError_1 = require("../errors/AuthMethodFailureError");
|
|
16
17
|
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
17
18
|
function parseAuthorizationHeader(authorization) {
|
|
18
19
|
if (!authorization)
|
|
@@ -40,7 +41,7 @@ const middlewareGetterByAuthType = {
|
|
|
40
41
|
return jwtUser;
|
|
41
42
|
}
|
|
42
43
|
catch (error) {
|
|
43
|
-
logger.info("auth.authWithJwt.invalidJwt", {
|
|
44
|
+
logger.info("auth.authWithJwt.invalidJwt", { error });
|
|
44
45
|
return null;
|
|
45
46
|
}
|
|
46
47
|
}),
|
|
@@ -77,34 +78,17 @@ function createAuthContext(options) {
|
|
|
77
78
|
return function getMiddlewareForMethods(authMethods) {
|
|
78
79
|
return function authWithGivenMethods(req, res, next) {
|
|
79
80
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
const logger = (0, getRequestLogger_1.getRequestLogger)(req);
|
|
81
|
-
const cookies = req.cookies || {};
|
|
82
|
-
const jwt = cookies.jwt || parseAuthorizationHeader(req.headers.authorization);
|
|
83
81
|
try {
|
|
84
|
-
|
|
85
|
-
const successfulAuthAttempts = (0, lodash_1.compact)(authAttempts);
|
|
86
|
-
if ((0, lodash_1.isEmpty)(successfulAuthAttempts)) {
|
|
87
|
-
res
|
|
88
|
-
.status(401)
|
|
89
|
-
.json((0, renderErrorJson_1.render401Error)([
|
|
90
|
-
authAttempts.includes(null)
|
|
91
|
-
? `Token invalid for methods ${authMethods}`
|
|
92
|
-
: `Token not set for methods ${authMethods}`,
|
|
93
|
-
]));
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const jwtUser = successfulAuthAttempts[0];
|
|
97
|
-
req.context.jwtUser = jwtUser;
|
|
98
|
-
req.context.authenticated.push(jwtUser.employeeReference !== "0" ? "jwt" : "api_key");
|
|
82
|
+
yield tsoaAuthProvider(req, options, authMethods);
|
|
99
83
|
next();
|
|
100
84
|
}
|
|
101
85
|
catch (error) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
86
|
+
if (error instanceof AuthMethodFailureError_1.AuthenticationMethodError) {
|
|
87
|
+
res
|
|
88
|
+
.status(error.httpCode())
|
|
89
|
+
.json((0, renderErrorJson_1.render401Error)(error.requiredChecks));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
108
92
|
res.status(401).json((0, renderErrorJson_1.render401Error)(authMethods));
|
|
109
93
|
}
|
|
110
94
|
});
|
|
@@ -112,4 +96,46 @@ function createAuthContext(options) {
|
|
|
112
96
|
};
|
|
113
97
|
}
|
|
114
98
|
exports.createAuthContext = createAuthContext;
|
|
99
|
+
/**
|
|
100
|
+
* Attempt to authenticate a user using the authentication methods sent by parameter
|
|
101
|
+
* Used in authenticationModule for TSOA
|
|
102
|
+
* https://tsoa-community.github.io/docs/authentication.html
|
|
103
|
+
*/
|
|
104
|
+
function tsoaAuthProvider(req, options, authMethods) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
const logger = (0, getRequestLogger_1.getRequestLogger)(req);
|
|
107
|
+
const cookies = req.cookies || {};
|
|
108
|
+
const jwt = cookies.jwt || parseAuthorizationHeader(req.headers.authorization);
|
|
109
|
+
try {
|
|
110
|
+
const authAttempts = yield Promise.all(authMethods.map((method) => middlewareGetterByAuthType[method](method === AuthMethods.JWT ? jwt : req.headers.authorization, options, logger)));
|
|
111
|
+
const successfulAuthAttempts = (0, lodash_1.compact)(authAttempts);
|
|
112
|
+
if ((0, lodash_1.isEmpty)(successfulAuthAttempts)) {
|
|
113
|
+
throw new AuthMethodFailureError_1.AuthenticationMethodError([
|
|
114
|
+
authAttempts.includes(null)
|
|
115
|
+
? `Token invalid for methods ${authMethods}`
|
|
116
|
+
: `Token not set for methods ${authMethods}`,
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
119
|
+
const jwtUser = successfulAuthAttempts[0];
|
|
120
|
+
req.context.jwtUser = jwtUser;
|
|
121
|
+
req.context.authenticated.push(jwtUser.employeeReference !== "0" ? "jwt" : "api_key");
|
|
122
|
+
return jwtUser;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (error instanceof AuthMethodFailureError_1.AuthenticationMethodError) {
|
|
126
|
+
logger.info("auth.authenticate_with_methods.authentication_user_fail", {
|
|
127
|
+
methods: authMethods,
|
|
128
|
+
error,
|
|
129
|
+
});
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
logger.error("auth.authenticate_with_methods.error", {
|
|
133
|
+
error,
|
|
134
|
+
authMethods,
|
|
135
|
+
});
|
|
136
|
+
throw new AuthMethodFailureError_1.AuthenticationMethodError(authMethods);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
exports.tsoaAuthProvider = tsoaAuthProvider;
|
|
115
141
|
//# sourceMappingURL=authenticateUser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticateUser.js","sourceRoot":"","sources":["../../src/middlewares/authenticateUser.ts"],"names":[],"mappings":";;;;;;;;;;;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AAI,CAC5B,OAAO,CAAC,iBAAiB,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CACtD,CAAC;YACF,OAAO,OAAO,CAAC;SAChB;QAAC,OAAO,KAAc,EAAE;YACvB,IAAI,KAAK,YAAY,kDAAyB,EAAE;gBAC9C,MAAM,CAAC,IAAI,CAAC,yDAAyD,EAAE;oBACrE,OAAO,EAAE,WAAW;oBACpB,KAAK;iBACN,CAAC,CAAC;gBACH,MAAM,KAAK,CAAC;aACb;YACD,MAAM,CAAC,KAAK,CAAC,sCAAsC,EAAE;gBACnD,KAAK;gBACL,WAAW;aACZ,CAAC,CAAC;YACH,MAAM,IAAI,kDAAyB,CAAC,WAAW,CAAC,CAAC;SAClD;IACH,CAAC;CAAA;AAnDD,4CAmDC"}
|
|
@@ -2,13 +2,26 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.errorRequestHandlerMiddleware = void 0;
|
|
4
4
|
const errors_1 = require("@alanszp/errors");
|
|
5
|
+
const core_1 = require("@alanszp/core");
|
|
6
|
+
const tsoa_1 = require("tsoa");
|
|
5
7
|
const getRequestBaseLog_1 = require("../helpers/getRequestBaseLog");
|
|
6
8
|
const typeorm_1 = require("typeorm");
|
|
7
9
|
const renderErrorJson_1 = require("../helpers/renderErrorJson");
|
|
8
|
-
const errorRequestHandlerMiddleware = (getLogger) => (error, req, res,
|
|
10
|
+
const errorRequestHandlerMiddleware = (getLogger) => (error, req, res, next) => {
|
|
9
11
|
const logger = getLogger();
|
|
10
12
|
const baseLog = (0, getRequestBaseLog_1.getRequestBaseLog)(req);
|
|
11
13
|
try {
|
|
14
|
+
if (error instanceof tsoa_1.ValidateError) {
|
|
15
|
+
const { body } = error.fields;
|
|
16
|
+
return res.status(400).json({
|
|
17
|
+
code: "json_schema_error",
|
|
18
|
+
message: "Error to validate JSON Schema",
|
|
19
|
+
context: {
|
|
20
|
+
errors: body === null || body === void 0 ? void 0 : body.message,
|
|
21
|
+
},
|
|
22
|
+
origin: (0, core_1.appIdentifier)(),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
12
25
|
if (error instanceof errors_1.RenderableError) {
|
|
13
26
|
const statusCode = error instanceof errors_1.HttpRenderableError ? error.httpCode() : 500;
|
|
14
27
|
if (statusCode >= 500) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorRequestHandlerMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/errorRequestHandlerMiddleware.ts"],"names":[],"mappings":";;;AACA,4CAIyB;
|
|
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;AAMrE,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,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC;YAC9B,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBAC1B,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,+BAA+B;gBACxC,OAAO,EAAE;oBACP,MAAM,EAAE,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,OAAO;iBACtB;gBACD,MAAM,EAAE,IAAA,oBAAa,GAAE;aACxB,CAAC,CAAC;SACJ;QAED,IAAI,KAAK,YAAY,wBAAe,EAAE;YACpC,MAAM,UAAU,GACd,KAAK,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,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;AAlFS,QAAA,6BAA6B,iCAkFtC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RenderableView } from "@alanszp/errors";
|
|
2
|
+
/**
|
|
3
|
+
* Internal server error
|
|
4
|
+
* Error 500
|
|
5
|
+
*/
|
|
6
|
+
export declare const InternalServerErrorExample: RenderableView;
|
|
7
|
+
/**
|
|
8
|
+
* Bad request
|
|
9
|
+
* Error 400
|
|
10
|
+
*/
|
|
11
|
+
export declare const MalformedJSONExample: {
|
|
12
|
+
code: string;
|
|
13
|
+
message: string;
|
|
14
|
+
context: {};
|
|
15
|
+
origin: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Not Found
|
|
19
|
+
* Error 404
|
|
20
|
+
*/
|
|
21
|
+
export declare const NotFoundExample: {
|
|
22
|
+
code: string;
|
|
23
|
+
message: string;
|
|
24
|
+
context: {};
|
|
25
|
+
origin: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundExample = exports.MalformedJSONExample = exports.InternalServerErrorExample = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Internal server error
|
|
6
|
+
* Error 500
|
|
7
|
+
*/
|
|
8
|
+
exports.InternalServerErrorExample = {
|
|
9
|
+
code: "internal_server_error",
|
|
10
|
+
message: "Internal Server Error",
|
|
11
|
+
context: {},
|
|
12
|
+
origin: "example-service:dev",
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Bad request
|
|
16
|
+
* Error 400
|
|
17
|
+
*/
|
|
18
|
+
exports.MalformedJSONExample = {
|
|
19
|
+
code: "bad_request",
|
|
20
|
+
message: "Malformed JSON",
|
|
21
|
+
context: {},
|
|
22
|
+
origin: "example-service:dev",
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Not Found
|
|
26
|
+
* Error 404
|
|
27
|
+
*/
|
|
28
|
+
exports.NotFoundExample = {
|
|
29
|
+
code: "not_found",
|
|
30
|
+
message: "Not Found",
|
|
31
|
+
context: {},
|
|
32
|
+
origin: "example-service:dev",
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=examples.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"examples.js","sourceRoot":"","sources":["../../../src/types/docs/examples.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACU,QAAA,0BAA0B,GAAmB;IACxD,IAAI,EAAE,uBAAuB;IAC7B,OAAO,EAAE,uBAAuB;IAChC,OAAO,EAAE,EAAE;IACX,MAAM,EAAE,qBAAqB;CAC9B,CAAC;AAEF;;;GAGG;AACU,QAAA,oBAAoB,GAAG;IAClC,IAAI,EAAE,aAAa;IACnB,OAAO,EAAE,gBAAgB;IACzB,OAAO,EAAE,EAAE;IACX,MAAM,EAAE,qBAAqB;CAC9B,CAAC;AAEF;;;GAGG;AACU,QAAA,eAAe,GAAG;IAC7B,IAAI,EAAE,WAAW;IACjB,OAAO,EAAE,WAAW;IACpB,OAAO,EAAE,EAAE;IACX,MAAM,EAAE,qBAAqB;CAC9B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.1.1",
|
|
4
4
|
"description": "Alan's express utils and middlewares.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -18,25 +18,28 @@
|
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@alanszp/errors": ">=12.0.0",
|
|
21
|
-
"express": "^4.
|
|
21
|
+
"express": "^4.19.2",
|
|
22
|
+
"tsoa": "^6.2.0",
|
|
22
23
|
"typeorm": "^0.2.41"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@types/body-parser": "^1.19.1",
|
|
26
27
|
"@types/express": "^4.17.12",
|
|
27
28
|
"@types/jest": "^28.1.8",
|
|
29
|
+
"@types/joi": "^17.2.3",
|
|
28
30
|
"@types/lodash": "^4.14.170",
|
|
29
31
|
"@types/node": "^20.11.17",
|
|
30
32
|
"babel-jest": "^29.7.0",
|
|
31
33
|
"jest": "^29.7.0",
|
|
32
34
|
"ts-node": "^10.0.0",
|
|
33
35
|
"tslint": "^6.1.3",
|
|
36
|
+
"tsoa": "^6.2.0",
|
|
34
37
|
"typeorm": "^0.2.41",
|
|
35
38
|
"typescript": "^4.3.4"
|
|
36
39
|
},
|
|
37
40
|
"dependencies": {
|
|
38
41
|
"@alanszp/audit": "^14.0.4",
|
|
39
|
-
"@alanszp/jwt": "^14.
|
|
42
|
+
"@alanszp/jwt": "^14.1.1",
|
|
40
43
|
"@alanszp/logger": "^14.0.4",
|
|
41
44
|
"@alanszp/shared-context": "^14.0.4",
|
|
42
45
|
"@babel/core": "^7.23.9",
|
|
@@ -44,5 +47,5 @@
|
|
|
44
47
|
"body-parser": "^1.20.2",
|
|
45
48
|
"lodash": "^4.17.21"
|
|
46
49
|
},
|
|
47
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "12c31d7d34405552a249f95ac261333b33cf21b2"
|
|
48
51
|
}
|