@forklaunch/core 0.1.3 → 0.1.6
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/cache/redisTtlCache.d.ts +1 -0
- package/dist/cache/redisTtlCache.js +1 -0
- package/dist/cache/redisTtlCache.js.map +1 -1
- package/dist/entityMapper/interfaces/entityMapper.interface.d.ts +1 -0
- package/dist/entityMapper/models/baseEntityMapper.model.d.ts +7 -7
- package/dist/entityMapper/models/baseEntityMapper.model.js +7 -7
- package/dist/entityMapper/models/baseEntityMapper.model.js.map +1 -1
- package/dist/entityMapper/models/requestEntityMapper.model.d.ts +13 -9
- package/dist/entityMapper/models/requestEntityMapper.model.js +13 -9
- package/dist/entityMapper/models/requestEntityMapper.model.js.map +1 -1
- package/dist/entityMapper/models/responseEntityMapper.model.d.ts +14 -7
- package/dist/entityMapper/models/responseEntityMapper.model.js +12 -6
- package/dist/entityMapper/models/responseEntityMapper.model.js.map +1 -1
- package/dist/entityMapper/types/entityMapper.types.d.ts +2 -2
- package/dist/http/httpStatusCodes.d.ts +74 -0
- package/dist/http/httpStatusCodes.js +1050 -0
- package/dist/http/httpStatusCodes.js.map +1 -0
- package/dist/http/index.d.ts +4 -1
- package/dist/http/index.js +4 -1
- package/dist/http/index.js.map +1 -1
- package/dist/http/middleware/index.js.map +1 -0
- package/dist/http/middleware/request.middleware.d.ts +95 -0
- package/dist/http/{middlewares → middleware}/request.middleware.js +105 -2
- package/dist/http/middleware/request.middleware.js.map +1 -0
- package/dist/http/middleware/response.middleware.d.ts +14 -0
- package/dist/http/middleware/response.middleware.js +53 -0
- package/dist/http/middleware/response.middleware.js.map +1 -0
- package/dist/http/openApiV3Generator.d.ts +13 -0
- package/dist/http/openApiV3Generator.js +177 -0
- package/dist/http/openApiV3Generator.js.map +1 -0
- package/dist/http/regex.d.ts +8 -0
- package/dist/http/regex.js +115 -0
- package/dist/http/regex.js.map +1 -0
- package/dist/http/types/api.types.d.ts +71 -0
- package/dist/http/types/forklaunch.types.d.ts +30 -0
- package/dist/http/types/forklaunch.types.js +3 -0
- package/dist/http/types/forklaunch.types.js.map +1 -0
- package/dist/http/types/index.d.ts +1 -0
- package/dist/http/types/index.js +1 -0
- package/dist/http/types/index.js.map +1 -1
- package/dist/http/types/primitive.types.d.ts +73 -0
- package/dist/tests/http.middleware.test.js +1 -1
- package/dist/tests/http.middleware.test.js.map +1 -1
- package/dist/tests/openApiV3Generator.test.d.ts +1 -0
- package/dist/tests/openApiV3Generator.test.js +71 -0
- package/dist/tests/openApiV3Generator.test.js.map +1 -0
- package/package.json +3 -2
- package/dist/http/middlewares/index.js.map +0 -1
- package/dist/http/middlewares/request.middleware.d.ts +0 -11
- package/dist/http/middlewares/request.middleware.js.map +0 -1
- package/dist/http/middlewares/response.middleware.d.ts +0 -3
- package/dist/http/middlewares/response.middleware.js +0 -33
- package/dist/http/middlewares/response.middleware.js.map +0 -1
- /package/dist/http/{middlewares → middleware}/index.d.ts +0 -0
- /package/dist/http/{middlewares → middleware}/index.js +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"httpStatusCodes.js","sourceRoot":"","sources":["../../http/httpStatusCodes.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;GAUG;AACU,QAAA,YAAY,GAAwC;IAC/D;;;;;;;;;;;;;OAaG;IACH,GAAG,EAAE,UAAU;IAEf;;;;;;;;;;;;;;OAcG;IACH,GAAG,EAAE,qBAAqB;IAE1B;;;;;;;;;;;;;;;;OAgBG;IACH,GAAG,EAAE,YAAY;IAEjB;;;;;;;;;;;OAWG;IACH,GAAG,EAAE,IAAI;IAET;;;;;;;;;;;;OAYG;IACH,GAAG,EAAE,SAAS;IAEd;;;;;;;;;;;;;;;;OAgBG;IACH,GAAG,EAAE,UAAU;IAEf;;;;;;;;;;;;;;;;OAgBG;IACH,GAAG,EAAE,+BAA+B;IAEpC;;;;;;;;;;;;OAYG;IACH,GAAG,EAAE,YAAY;IAEjB;;;;;;;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AA3D,QAAA,aAAa,iBAA8C;AAExE;;;;;;;;;;GAUG;AACI,MAAM,gBAAgB,GAAG,CAAC,MAAc,EAAqB,EAAE;IACpE,MAAM,MAAM,GAAG,MAAM,CAAC,iBAAiB,EAAE,CAAC,IAAI,EAAE,CAAC;IACjD,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,oBAAY,CAAC,CAAC,IAAI,CAC7C,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,MAAM,CACzC,CAAC;IACF,IAAI,KAAK;QAAE,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAe,CAAC;IACnD,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAPW,QAAA,gBAAgB,oBAO3B;AAEF,kBAAe,oBAAY,CAAC"}
|
package/dist/http/index.d.ts
CHANGED
package/dist/http/index.js
CHANGED
@@ -14,6 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./
|
17
|
+
__exportStar(require("./httpStatusCodes"), exports);
|
18
|
+
__exportStar(require("./middleware"), exports);
|
19
|
+
__exportStar(require("./openApiV3Generator"), exports);
|
20
|
+
__exportStar(require("./regex"), exports);
|
18
21
|
__exportStar(require("./types"), exports);
|
19
22
|
//# sourceMappingURL=index.js.map
|
package/dist/http/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../http/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../http/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,+CAA6B;AAC7B,uDAAqC;AACrC,0CAAwB;AACxB,0CAAwB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../http/middleware/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,wDAAsC"}
|
@@ -0,0 +1,95 @@
|
|
1
|
+
import { AnySchemaValidator, SchemaValidator } from '@forklaunch/validator';
|
2
|
+
import { ForklaunchNextFunction, ForklaunchRequest, ForklaunchResponse } from '../types/api.types';
|
3
|
+
import { HttpContractDetails, PathParamHttpContractDetails, StringOnlyObject } from '../types/primitive.types';
|
4
|
+
/**
|
5
|
+
* Middleware to create and add a request context.
|
6
|
+
*
|
7
|
+
* @template SV - A type that extends AnySchemaValidator.
|
8
|
+
* @template Request - A type that extends ForklaunchRequest.
|
9
|
+
* @template Response - A type that extends ForklaunchResponse.
|
10
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
11
|
+
* @param {SV} schemaValidator - The schema validator.
|
12
|
+
* @returns {Function} - Middleware function to create request context.
|
13
|
+
*/
|
14
|
+
export declare function createRequestContext<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(schemaValidator: SV): (req: Request, res: Response, next?: NextFunction) => void;
|
15
|
+
/**
|
16
|
+
* Middleware to enrich the request details with contract details.
|
17
|
+
*
|
18
|
+
* @template SV - A type that extends AnySchemaValidator.
|
19
|
+
* @template Request - A type that extends ForklaunchRequest.
|
20
|
+
* @template Response - A type that extends ForklaunchResponse.
|
21
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
22
|
+
* @param {PathParamHttpContractDetails<SV> | HttpContractDetails<SV>} contractDetails - The contract details.
|
23
|
+
* @returns {Function} - Middleware function to enrich request details.
|
24
|
+
*/
|
25
|
+
export declare function enrichRequestDetails<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(contractDetails: PathParamHttpContractDetails<SV> | HttpContractDetails<SV>): (req: Request, _res: Response, next?: NextFunction) => void;
|
26
|
+
/**
|
27
|
+
* Pre-handler function to parse and validate input.
|
28
|
+
*
|
29
|
+
* @template SV - A type that extends AnySchemaValidator.
|
30
|
+
* @param {SchemaValidator} schemaValidator - The schema validator.
|
31
|
+
* @param {unknown} object - The object to validate.
|
32
|
+
* @param {StringOnlyObject<SV>} [schemaInput] - The schema input.
|
33
|
+
* @returns {number | void} - Returns 400 if validation fails.
|
34
|
+
*/
|
35
|
+
export declare function preHandlerParse<SV extends AnySchemaValidator>(schemaValidator: SchemaValidator, object: unknown, schemaInput?: StringOnlyObject<SV>): 400 | undefined;
|
36
|
+
/**
|
37
|
+
* Middleware to parse request parameters.
|
38
|
+
*
|
39
|
+
* @template SV - A type that extends AnySchemaValidator.
|
40
|
+
* @template Request - A type that extends ForklaunchRequest.
|
41
|
+
* @template Response - A type that extends ForklaunchResponse.
|
42
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
43
|
+
* @param {Request} req - The request object.
|
44
|
+
* @param {Response} res - The response object.
|
45
|
+
* @param {NextFunction} [next] - The next middleware function.
|
46
|
+
*/
|
47
|
+
export declare function parseRequestParams<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
48
|
+
/**
|
49
|
+
* Middleware to parse request body.
|
50
|
+
*
|
51
|
+
* @template SV - A type that extends AnySchemaValidator.
|
52
|
+
* @template Request - A type that extends ForklaunchRequest.
|
53
|
+
* @template Response - A type that extends ForklaunchResponse.
|
54
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
55
|
+
* @param {Request} req - The request object.
|
56
|
+
* @param {Response} res - The response object.
|
57
|
+
* @param {NextFunction} [next] - The next middleware function.
|
58
|
+
*/
|
59
|
+
export declare function parseRequestBody<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
60
|
+
/**
|
61
|
+
* Middleware to parse request headers.
|
62
|
+
*
|
63
|
+
* @template SV - A type that extends AnySchemaValidator.
|
64
|
+
* @template Request - A type that extends ForklaunchRequest.
|
65
|
+
* @template Response - A type that extends ForklaunchResponse.
|
66
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
67
|
+
* @param {Request} req - The request object.
|
68
|
+
* @param {Response} res - The response object.
|
69
|
+
* @param {NextFunction} [next] - The next middleware function.
|
70
|
+
*/
|
71
|
+
export declare function parseRequestHeaders<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
72
|
+
/**
|
73
|
+
* Middleware to parse request query.
|
74
|
+
*
|
75
|
+
* @template SV - A type that extends AnySchemaValidator.
|
76
|
+
* @template Request - A type that extends ForklaunchRequest.
|
77
|
+
* @template Response - A type that extends ForklaunchResponse.
|
78
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
79
|
+
* @param {Request} req - The request object.
|
80
|
+
* @param {Response} res - The response object.
|
81
|
+
* @param {NextFunction} [next] - The next middleware function.
|
82
|
+
*/
|
83
|
+
export declare function parseRequestQuery<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
84
|
+
/**
|
85
|
+
* Middleware to parse request authorization.
|
86
|
+
*
|
87
|
+
* @template SV - A type that extends AnySchemaValidator.
|
88
|
+
* @template Request - A type that extends ForklaunchRequest.
|
89
|
+
* @template Response - A type that extends ForklaunchResponse.
|
90
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
91
|
+
* @param {Request} req - The request object.
|
92
|
+
* @param {Response} res - The response object.
|
93
|
+
* @param {NextFunction} [next] - The next middleware function.
|
94
|
+
*/
|
95
|
+
export declare function parseRequestAuth<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): Promise<void>;
|
@@ -33,6 +33,16 @@ exports.parseRequestQuery = parseRequestQuery;
|
|
33
33
|
exports.parseRequestAuth = parseRequestAuth;
|
34
34
|
const jose = __importStar(require("jose"));
|
35
35
|
const uuid_1 = require("uuid");
|
36
|
+
/**
|
37
|
+
* Middleware to create and add a request context.
|
38
|
+
*
|
39
|
+
* @template SV - A type that extends AnySchemaValidator.
|
40
|
+
* @template Request - A type that extends ForklaunchRequest.
|
41
|
+
* @template Response - A type that extends ForklaunchResponse.
|
42
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
43
|
+
* @param {SV} schemaValidator - The schema validator.
|
44
|
+
* @returns {Function} - Middleware function to create request context.
|
45
|
+
*/
|
36
46
|
function createRequestContext(schemaValidator) {
|
37
47
|
return (req, res, next) => {
|
38
48
|
req.schemaValidator = schemaValidator;
|
@@ -49,6 +59,16 @@ function createRequestContext(schemaValidator) {
|
|
49
59
|
}
|
50
60
|
};
|
51
61
|
}
|
62
|
+
/**
|
63
|
+
* Middleware to enrich the request details with contract details.
|
64
|
+
*
|
65
|
+
* @template SV - A type that extends AnySchemaValidator.
|
66
|
+
* @template Request - A type that extends ForklaunchRequest.
|
67
|
+
* @template Response - A type that extends ForklaunchResponse.
|
68
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
69
|
+
* @param {PathParamHttpContractDetails<SV> | HttpContractDetails<SV>} contractDetails - The contract details.
|
70
|
+
* @returns {Function} - Middleware function to enrich request details.
|
71
|
+
*/
|
52
72
|
function enrichRequestDetails(contractDetails) {
|
53
73
|
return (req, _res, next) => {
|
54
74
|
req.contractDetails = contractDetails;
|
@@ -57,6 +77,15 @@ function enrichRequestDetails(contractDetails) {
|
|
57
77
|
}
|
58
78
|
};
|
59
79
|
}
|
80
|
+
/**
|
81
|
+
* Pre-handler function to parse and validate input.
|
82
|
+
*
|
83
|
+
* @template SV - A type that extends AnySchemaValidator.
|
84
|
+
* @param {SchemaValidator} schemaValidator - The schema validator.
|
85
|
+
* @param {unknown} object - The object to validate.
|
86
|
+
* @param {StringOnlyObject<SV>} [schemaInput] - The schema input.
|
87
|
+
* @returns {number | void} - Returns 400 if validation fails.
|
88
|
+
*/
|
60
89
|
function preHandlerParse(schemaValidator, object, schemaInput) {
|
61
90
|
if (!schemaInput) {
|
62
91
|
return;
|
@@ -66,6 +95,17 @@ function preHandlerParse(schemaValidator, object, schemaInput) {
|
|
66
95
|
return 400;
|
67
96
|
}
|
68
97
|
}
|
98
|
+
/**
|
99
|
+
* Middleware to parse request parameters.
|
100
|
+
*
|
101
|
+
* @template SV - A type that extends AnySchemaValidator.
|
102
|
+
* @template Request - A type that extends ForklaunchRequest.
|
103
|
+
* @template Response - A type that extends ForklaunchResponse.
|
104
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
105
|
+
* @param {Request} req - The request object.
|
106
|
+
* @param {Response} res - The response object.
|
107
|
+
* @param {NextFunction} [next] - The next middleware function.
|
108
|
+
*/
|
69
109
|
function parseRequestParams(req, res, next) {
|
70
110
|
const params = req.contractDetails.params;
|
71
111
|
if (preHandlerParse(req.schemaValidator, req.params, params) === 400) {
|
@@ -78,6 +118,17 @@ function parseRequestParams(req, res, next) {
|
|
78
118
|
next();
|
79
119
|
}
|
80
120
|
}
|
121
|
+
/**
|
122
|
+
* Middleware to parse request body.
|
123
|
+
*
|
124
|
+
* @template SV - A type that extends AnySchemaValidator.
|
125
|
+
* @template Request - A type that extends ForklaunchRequest.
|
126
|
+
* @template Response - A type that extends ForklaunchResponse.
|
127
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
128
|
+
* @param {Request} req - The request object.
|
129
|
+
* @param {Response} res - The response object.
|
130
|
+
* @param {NextFunction} [next] - The next middleware function.
|
131
|
+
*/
|
81
132
|
function parseRequestBody(req, res, next) {
|
82
133
|
if (req.headers['content-type'] === 'application/json') {
|
83
134
|
const body = (req.schemaValidator,
|
@@ -93,6 +144,17 @@ function parseRequestBody(req, res, next) {
|
|
93
144
|
next();
|
94
145
|
}
|
95
146
|
}
|
147
|
+
/**
|
148
|
+
* Middleware to parse request headers.
|
149
|
+
*
|
150
|
+
* @template SV - A type that extends AnySchemaValidator.
|
151
|
+
* @template Request - A type that extends ForklaunchRequest.
|
152
|
+
* @template Response - A type that extends ForklaunchResponse.
|
153
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
154
|
+
* @param {Request} req - The request object.
|
155
|
+
* @param {Response} res - The response object.
|
156
|
+
* @param {NextFunction} [next] - The next middleware function.
|
157
|
+
*/
|
96
158
|
function parseRequestHeaders(req, res, next) {
|
97
159
|
const headers = req.contractDetails.requestHeaders;
|
98
160
|
if (preHandlerParse(req.schemaValidator, req.headers, headers) === 400) {
|
@@ -105,6 +167,17 @@ function parseRequestHeaders(req, res, next) {
|
|
105
167
|
next();
|
106
168
|
}
|
107
169
|
}
|
170
|
+
/**
|
171
|
+
* Middleware to parse request query.
|
172
|
+
*
|
173
|
+
* @template SV - A type that extends AnySchemaValidator.
|
174
|
+
* @template Request - A type that extends ForklaunchRequest.
|
175
|
+
* @template Response - A type that extends ForklaunchResponse.
|
176
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
177
|
+
* @param {Request} req - The request object.
|
178
|
+
* @param {Response} res - The response object.
|
179
|
+
* @param {NextFunction} [next] - The next middleware function.
|
180
|
+
*/
|
108
181
|
function parseRequestQuery(req, res, next) {
|
109
182
|
const query = req.contractDetails.query;
|
110
183
|
if (preHandlerParse(req.schemaValidator, req.query, query) === 400) {
|
@@ -117,6 +190,13 @@ function parseRequestQuery(req, res, next) {
|
|
117
190
|
next();
|
118
191
|
}
|
119
192
|
}
|
193
|
+
/**
|
194
|
+
* Checks the authorization token for validity.
|
195
|
+
*
|
196
|
+
* @param {AuthMethod} [authorizationMethod] - The method of authorization.
|
197
|
+
* @param {string} [authorizationString] - The authorization string.
|
198
|
+
* @returns {Promise<[401 | 403, string] | string | undefined>} - The result of the authorization check.
|
199
|
+
*/
|
120
200
|
async function checkAuthorizationToken(authorizationMethod, authorizationString) {
|
121
201
|
if (!authorizationString) {
|
122
202
|
return [401, 'No Authorization token provided.'];
|
@@ -139,12 +219,37 @@ async function checkAuthorizationToken(authorizationMethod, authorizationString)
|
|
139
219
|
return [401, 'Invalid Authorization method.'];
|
140
220
|
}
|
141
221
|
}
|
222
|
+
/**
|
223
|
+
* Maps roles from authorization.
|
224
|
+
*
|
225
|
+
* @param {AuthMethod} [authorizationType] - The method of authorization.
|
226
|
+
* @param {string} [authorizationToken] - The authorization token.
|
227
|
+
* @returns {string[]} - The mapped roles.
|
228
|
+
*/
|
142
229
|
function mapRoles(authorizationType, authorizationToken) {
|
143
230
|
return [];
|
144
231
|
}
|
232
|
+
/**
|
233
|
+
* Maps permissions from authorization.
|
234
|
+
*
|
235
|
+
* @param {AuthMethod} [authorizationType] - The method of authorization.
|
236
|
+
* @param {string} [authorizationToken] - The authorization token.
|
237
|
+
* @returns {string[]} - The mapped permissions.
|
238
|
+
*/
|
145
239
|
function mapPermissions(authorizationType, authorizationToken) {
|
146
240
|
return [];
|
147
241
|
}
|
242
|
+
/**
|
243
|
+
* Middleware to parse request authorization.
|
244
|
+
*
|
245
|
+
* @template SV - A type that extends AnySchemaValidator.
|
246
|
+
* @template Request - A type that extends ForklaunchRequest.
|
247
|
+
* @template Response - A type that extends ForklaunchResponse.
|
248
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
249
|
+
* @param {Request} req - The request object.
|
250
|
+
* @param {Response} res - The response object.
|
251
|
+
* @param {NextFunction} [next] - The next middleware function.
|
252
|
+
*/
|
148
253
|
async function parseRequestAuth(req, res, next) {
|
149
254
|
const auth = req.contractDetails.auth;
|
150
255
|
if (auth) {
|
@@ -160,8 +265,6 @@ async function parseRequestAuth(req, res, next) {
|
|
160
265
|
const roles = mapRoles(auth.method, req.headers.authorization);
|
161
266
|
const permissionErrorMessage = 'User does not have sufficient permissions to perform action.';
|
162
267
|
const roleErrorMessage = 'User does not have correct role to perform action.';
|
163
|
-
// this is wrong, we need to check if any of the user's permissions are in the allowed permissions, while checking that any of the permissions is not in the forbidden slugs
|
164
|
-
// currently this is checking if any of the user's permissions are NOT in the allowed permissions
|
165
268
|
permissionSlugs.forEach((permissionSlug) => {
|
166
269
|
if (!req.contractDetails.auth?.allowedSlugs?.has(permissionSlug) ||
|
167
270
|
req.contractDetails.auth?.forbiddenSlugs?.has(permissionSlug)) {
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"request.middleware.js","sourceRoot":"","sources":["../../../http/middleware/request.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,oDAyBC;AAYD,oDAaC;AAWD,0CAaC;AAaD,gDAgBC;AAaD,4CAyBC;AAaD,kDAgBC;AAaD,8CAgBC;AA8ED,4CA0DC;AAvWD,2CAA6B;AAC7B,+BAA0B;AAa1B;;;;;;;;;GASG;AACH,SAAgB,oBAAoB,CAKlC,eAAmB;IACjB,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAmB,EAAE,EAAE;QACxD,GAAG,CAAC,eAAe,GAAG,eAAkC,CAAC;QAEzD,IAAI,aAAa,GAAG,IAAA,SAAE,GAAE,CAAC;QAEzB,IAAI,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC,EAAE,CAAC;YAClC,aAAa,GAAG,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAW,CAAC;QAC9D,CAAC;QAED,GAAG,CAAC,SAAS,CAAC,kBAAkB,EAAE,aAAa,CAAC,CAAC;QAEjD,GAAG,CAAC,OAAO,GAAG;YACV,aAAa,EAAE,aAAa;SAC/B,CAAC;QAEF,IAAI,IAAI,EAAE,CAAC;YACP,IAAI,EAAE,CAAC;QACX,CAAC;IACL,CAAC,CAAC;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5D,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,CAAC,cAAc,CAAC,EAC/D,CAAC;gBACC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;gBAC7C,IAAI,IAAI,EAAE,CAAC;oBACP,IAAI,CAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC;gBAC5C,CAAC;YACL,CAAC;QACL,CAAC,CAAC,CAAC;QACH,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACnB,IACI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,YAAY,EAAE,GAAG,CAAC,IAAI,CAAC;gBAClD,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,CAAC,IAAI,CAAC,EACrD,CAAC;gBACC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;gBACvC,IAAI,IAAI,EAAE,CAAC;oBACP,IAAI,CAAC,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC;gBACtC,CAAC;YACL,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IAED,cAAc;IACd,cAAc;IACd,IAAI;AACR,CAAC"}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { AnySchemaValidator } from '@forklaunch/validator';
|
2
|
+
import { ForklaunchNextFunction, ForklaunchRequest, ForklaunchResponse } from '../types/api.types';
|
3
|
+
/**
|
4
|
+
* Middleware to parse and validate the response.
|
5
|
+
*
|
6
|
+
* @template SV - A type that extends AnySchemaValidator.
|
7
|
+
* @template Request - A type that extends ForklaunchRequest.
|
8
|
+
* @template Response - A type that extends ForklaunchResponse.
|
9
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
10
|
+
* @param {Request} req - The request object.
|
11
|
+
* @param {Response} res - The response object.
|
12
|
+
* @param {NextFunction} [next] - The next middleware function.
|
13
|
+
*/
|
14
|
+
export declare function parseResponse<SV extends AnySchemaValidator, Request extends ForklaunchRequest<SV>, Response extends ForklaunchResponse, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
@@ -0,0 +1,53 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.parseResponse = parseResponse;
|
4
|
+
/**
|
5
|
+
* Checks if any validation is required for the given contract details.
|
6
|
+
*
|
7
|
+
* @template SV - A type that extends AnySchemaValidator.
|
8
|
+
* @param {HttpContractDetails<SV>} contractDetails - The contract details.
|
9
|
+
* @returns {boolean} - True if any validation is required, otherwise false.
|
10
|
+
*/
|
11
|
+
function checkAnyValidation(contractDetails) {
|
12
|
+
return (contractDetails.body ||
|
13
|
+
contractDetails.params ||
|
14
|
+
contractDetails.requestHeaders ||
|
15
|
+
contractDetails.query);
|
16
|
+
}
|
17
|
+
/**
|
18
|
+
* Middleware to parse and validate the response.
|
19
|
+
*
|
20
|
+
* @template SV - A type that extends AnySchemaValidator.
|
21
|
+
* @template Request - A type that extends ForklaunchRequest.
|
22
|
+
* @template Response - A type that extends ForklaunchResponse.
|
23
|
+
* @template NextFunction - A type that extends ForklaunchNextFunction.
|
24
|
+
* @param {Request} req - The request object.
|
25
|
+
* @param {Response} res - The response object.
|
26
|
+
* @param {NextFunction} [next] - The next middleware function.
|
27
|
+
*/
|
28
|
+
function parseResponse(req, res, next) {
|
29
|
+
if (req.contractDetails.responseHeaders) {
|
30
|
+
const schema = req.schemaValidator.schemify(req.contractDetails.responseHeaders);
|
31
|
+
req.schemaValidator.validate(schema, res.getHeaders());
|
32
|
+
}
|
33
|
+
if (res.statusCode === 500 ||
|
34
|
+
(checkAnyValidation(req.contractDetails) && res.statusCode === 400) ||
|
35
|
+
(req.contractDetails.auth &&
|
36
|
+
(res.statusCode === 401 || res.statusCode === 403))) {
|
37
|
+
req.schemaValidator.validate(req.schemaValidator.string, res.bodyData);
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
if (Object.prototype.hasOwnProperty.call(req.contractDetails.responses, res.statusCode)) {
|
41
|
+
const schema = req.schemaValidator.schemify(req.contractDetails.responses[res.statusCode]);
|
42
|
+
req.schemaValidator.validate(schema, res.bodyData);
|
43
|
+
}
|
44
|
+
else {
|
45
|
+
if (next) {
|
46
|
+
next(new Error(`Response code ${res.statusCode} not defined in contract.`));
|
47
|
+
}
|
48
|
+
}
|
49
|
+
if (next) {
|
50
|
+
next();
|
51
|
+
}
|
52
|
+
}
|
53
|
+
//# sourceMappingURL=response.middleware.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"response.middleware.js","sourceRoot":"","sources":["../../../http/middleware/response.middleware.ts"],"names":[],"mappings":";;AAqCA,sCA4CC;AAzED;;;;;;GAMG;AACH,SAAS,kBAAkB,CACzB,eAAwC;IAExC,OAAO,CACL,eAAe,CAAC,IAAI;QACpB,eAAe,CAAC,MAAM;QACtB,eAAe,CAAC,cAAc;QAC9B,eAAe,CAAC,KAAK,CACtB,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,aAAa,CAK3B,GAAY,EAAE,GAAa,EAAE,IAAmB;IAChD,IAAI,GAAG,CAAC,eAAe,CAAC,eAAe,EAAE,CAAC;QACxC,MAAM,MAAM,GAAG,GAAG,CAAC,eAAe,CAAC,QAAQ,CACzC,GAAG,CAAC,eAAe,CAAC,eAAe,CACpC,CAAC;QACF,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,IACE,GAAG,CAAC,UAAU,KAAK,GAAG;QACtB,CAAC,kBAAkB,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC;QACnE,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI;YACvB,CAAC,GAAG,CAAC,UAAU,KAAK,GAAG,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,CAAC,EACrD,CAAC;QACD,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;QACvE,OAAO;IACT,CAAC;IAED,IACE,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAClC,GAAG,CAAC,eAAe,CAAC,SAAS,EAC7B,GAAG,CAAC,UAAU,CACf,EACD,CAAC;QACD,MAAM,MAAM,GAAG,GAAG,CAAC,eAAe,CAAC,QAAQ,CACzC,GAAG,CAAC,eAAe,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAC9C,CAAC;QACF,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;IACrD,CAAC;SAAM,CAAC;QACN,IAAI,IAAI,EAAE,CAAC;YACT,IAAI,CACF,IAAI,KAAK,CAAC,iBAAiB,GAAG,CAAC,UAAU,2BAA2B,CAAC,CACtE,CAAC;QACJ,CAAC;IACH,CAAC;IAED,IAAI,IAAI,EAAE,CAAC;QACT,IAAI,EAAE,CAAC;IACT,CAAC;AACH,CAAC"}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { AnySchemaValidator } from '@forklaunch/validator';
|
2
|
+
import { OpenAPIObject } from 'openapi3-ts/oas31';
|
3
|
+
import { ForklaunchRouter } from './types/forklaunch.types';
|
4
|
+
/**
|
5
|
+
* Generates a Swagger document from given routers.
|
6
|
+
*
|
7
|
+
* @template SV - A type that extends AnySchemaValidator.
|
8
|
+
* @param {SV} schemaValidator - The schema validator.
|
9
|
+
* @param {string | number} port - The port on which the server is running.
|
10
|
+
* @param {ForklaunchRouter<SV>[]} routers - The routers to include in the Swagger document.
|
11
|
+
* @returns {OpenAPIObject} - The generated Swagger document.
|
12
|
+
*/
|
13
|
+
export declare function generateSwaggerDocument<SV extends AnySchemaValidator>(schemaValidator: SV, port: string | number, routers: ForklaunchRouter<SV>[]): OpenAPIObject;
|
@@ -0,0 +1,177 @@
|
|
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.generateSwaggerDocument = generateSwaggerDocument;
|
7
|
+
const httpStatusCodes_1 = __importDefault(require("./httpStatusCodes"));
|
8
|
+
/**
|
9
|
+
* Capitalizes the first letter of a string.
|
10
|
+
*
|
11
|
+
* @param {string} str - The string to transform.
|
12
|
+
* @returns {string} - The transformed string with the first letter capitalized.
|
13
|
+
*/
|
14
|
+
function toUpperCase(str) {
|
15
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
16
|
+
}
|
17
|
+
;
|
18
|
+
/**
|
19
|
+
* Transforms a base path by ensuring it starts with a slash and capitalizing the first letter.
|
20
|
+
*
|
21
|
+
* @param {string} basePath - The base path to transform.
|
22
|
+
* @returns {string} - The transformed base path.
|
23
|
+
*/
|
24
|
+
function transformBasePath(basePath) {
|
25
|
+
if (basePath.startsWith('/')) {
|
26
|
+
return toUpperCase(basePath.slice(1));
|
27
|
+
}
|
28
|
+
return `/${basePath}`;
|
29
|
+
}
|
30
|
+
;
|
31
|
+
/**
|
32
|
+
* Creates a Swagger document.
|
33
|
+
*
|
34
|
+
* @param {string | number} port - The port on which the server is running.
|
35
|
+
* @param {TagObject[]} tags - The tags for the Swagger document.
|
36
|
+
* @param {PathObject} paths - The paths for the Swagger document.
|
37
|
+
* @returns {OpenAPIObject} - The Swagger document.
|
38
|
+
*/
|
39
|
+
function swaggerDocument(port, tags, paths) {
|
40
|
+
return {
|
41
|
+
openapi: '3.1.0',
|
42
|
+
info: {
|
43
|
+
title: process.env.API_TITLE || '',
|
44
|
+
version: process.env.VERSION || '1.0.0'
|
45
|
+
},
|
46
|
+
components: {
|
47
|
+
securitySchemes: {
|
48
|
+
bearer: {
|
49
|
+
type: 'http',
|
50
|
+
scheme: 'bearer',
|
51
|
+
bearerFormat: 'JWT'
|
52
|
+
}
|
53
|
+
}
|
54
|
+
},
|
55
|
+
tags,
|
56
|
+
servers: [
|
57
|
+
{
|
58
|
+
url: `http://localhost:${port}`
|
59
|
+
}
|
60
|
+
],
|
61
|
+
paths
|
62
|
+
};
|
63
|
+
}
|
64
|
+
;
|
65
|
+
/**
|
66
|
+
* Resolves the content object for a given schema.
|
67
|
+
*
|
68
|
+
* @template SV - A type that extends AnySchemaValidator.
|
69
|
+
* @param {SV} schemaValidator - The schema validator.
|
70
|
+
* @param {IdiomaticSchema<SV>} body - The schema body.
|
71
|
+
* @returns {ContentObject} - The resolved content object.
|
72
|
+
*/
|
73
|
+
function contentResolver(schemaValidator, body) {
|
74
|
+
const bodySpec = schemaValidator.openapi(body);
|
75
|
+
return body === schemaValidator.string
|
76
|
+
? {
|
77
|
+
'plain/text': {
|
78
|
+
schema: bodySpec
|
79
|
+
}
|
80
|
+
}
|
81
|
+
: {
|
82
|
+
'application/json': {
|
83
|
+
schema: bodySpec
|
84
|
+
}
|
85
|
+
};
|
86
|
+
}
|
87
|
+
/**
|
88
|
+
* Generates a Swagger document from given routers.
|
89
|
+
*
|
90
|
+
* @template SV - A type that extends AnySchemaValidator.
|
91
|
+
* @param {SV} schemaValidator - The schema validator.
|
92
|
+
* @param {string | number} port - The port on which the server is running.
|
93
|
+
* @param {ForklaunchRouter<SV>[]} routers - The routers to include in the Swagger document.
|
94
|
+
* @returns {OpenAPIObject} - The generated Swagger document.
|
95
|
+
*/
|
96
|
+
function generateSwaggerDocument(schemaValidator, port, routers) {
|
97
|
+
const tags = [];
|
98
|
+
const paths = {};
|
99
|
+
routers.forEach((router) => {
|
100
|
+
const controllerName = transformBasePath(router.basePath);
|
101
|
+
tags.push({
|
102
|
+
name: controllerName,
|
103
|
+
description: `${controllerName} Operations`
|
104
|
+
});
|
105
|
+
router.routes.forEach((route) => {
|
106
|
+
const fullPath = `${router.basePath}${route.path === '/' ? '' : route.path}`.replace(/:(\w+)/g, '{$1}');
|
107
|
+
if (!paths[fullPath]) {
|
108
|
+
paths[fullPath] = {};
|
109
|
+
}
|
110
|
+
const { name, summary, query, requestHeaders } = route.contractDetails;
|
111
|
+
const responses = {};
|
112
|
+
for (const key in route.contractDetails.responses) {
|
113
|
+
responses[key] = {
|
114
|
+
description: httpStatusCodes_1.default[key],
|
115
|
+
content: contentResolver(schemaValidator, route.contractDetails.responses[key])
|
116
|
+
};
|
117
|
+
}
|
118
|
+
const pathItemObject = {
|
119
|
+
tags: [controllerName],
|
120
|
+
summary: `${name}: ${summary}`,
|
121
|
+
parameters: [],
|
122
|
+
responses
|
123
|
+
};
|
124
|
+
if (route.contractDetails.params) {
|
125
|
+
for (const key in route.contractDetails.params) {
|
126
|
+
pathItemObject.parameters?.push({
|
127
|
+
name: key,
|
128
|
+
in: 'path'
|
129
|
+
});
|
130
|
+
}
|
131
|
+
}
|
132
|
+
const body = route.contractDetails.body;
|
133
|
+
if (body) {
|
134
|
+
pathItemObject.requestBody = {
|
135
|
+
required: true,
|
136
|
+
content: contentResolver(schemaValidator, body)
|
137
|
+
};
|
138
|
+
}
|
139
|
+
if (requestHeaders) {
|
140
|
+
for (const key in requestHeaders) {
|
141
|
+
pathItemObject.parameters?.push({
|
142
|
+
name: key,
|
143
|
+
in: 'header'
|
144
|
+
});
|
145
|
+
}
|
146
|
+
}
|
147
|
+
if (query) {
|
148
|
+
for (const key in query) {
|
149
|
+
pathItemObject.parameters?.push({
|
150
|
+
name: key,
|
151
|
+
in: 'query'
|
152
|
+
});
|
153
|
+
}
|
154
|
+
}
|
155
|
+
if (route.contractDetails.auth) {
|
156
|
+
responses[401] = {
|
157
|
+
description: httpStatusCodes_1.default[401],
|
158
|
+
content: contentResolver(schemaValidator, schemaValidator.string)
|
159
|
+
};
|
160
|
+
responses[403] = {
|
161
|
+
description: httpStatusCodes_1.default[403],
|
162
|
+
content: contentResolver(schemaValidator, schemaValidator.string)
|
163
|
+
};
|
164
|
+
if (route.contractDetails.auth.method === 'jwt') {
|
165
|
+
pathItemObject.security = [
|
166
|
+
{
|
167
|
+
bearer: Array.from(route.contractDetails.auth.allowedSlugs?.values() || [])
|
168
|
+
}
|
169
|
+
];
|
170
|
+
}
|
171
|
+
}
|
172
|
+
paths[fullPath][route.method] = pathItemObject;
|
173
|
+
});
|
174
|
+
});
|
175
|
+
return swaggerDocument(port, tags, paths);
|
176
|
+
}
|
177
|
+
//# sourceMappingURL=openApiV3Generator.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"openApiV3Generator.js","sourceRoot":"","sources":["../../http/openApiV3Generator.ts"],"names":[],"mappings":";;;;;AAiHA,0DAyGC;AA7MD,wEAA6C;AAI7C;;;;;GAKG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC;AAAA,CAAC;AAEF;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,QAAgB;IACzC,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7B,OAAO,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACxC,CAAC;IACD,OAAO,IAAI,QAAQ,EAAE,CAAC;AACxB,CAAC;AAAA,CAAC;AAEF;;;;;;;GAOG;AACH,SAAS,eAAe,CACtB,IAAqB,EACrB,IAAiB,EACjB,KAAiB;IAEjB,OAAO;QACL,OAAO,EAAE,OAAO;QAChB,IAAI,EAAE;YACJ,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,EAAE;YAClC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,OAAO;SACxC;QACD,UAAU,EAAE;YACV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5C,CAAC"}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* Generates a string from a given regular expression or string representation of a regex.
|
3
|
+
*
|
4
|
+
* @param {RegExp | string} regex - The regular expression or string representation of a regex.
|
5
|
+
* @returns {string} - The generated string based on the regex pattern.
|
6
|
+
* @throws {Error} - Throws an error if there are unmatched brackets or groups.
|
7
|
+
*/
|
8
|
+
export declare function generateStringFromRegex(regex: RegExp | string): string;
|