@forklaunch/core 0.1.5 → 0.1.7
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/index.d.ts +1 -0
- package/dist/http/index.js +1 -0
- package/dist/http/index.js.map +1 -1
- package/dist/http/middleware/request.middleware.d.ts +84 -0
- package/dist/http/middleware/request.middleware.js +105 -2
- package/dist/http/middleware/request.middleware.js.map +1 -1
- package/dist/http/middleware/response.middleware.d.ts +11 -0
- package/dist/http/middleware/response.middleware.js +23 -3
- package/dist/http/middleware/response.middleware.js.map +1 -1
- package/dist/http/openApiV3Generator.d.ts +9 -0
- package/dist/http/openApiV3Generator.js +43 -6
- package/dist/http/openApiV3Generator.js.map +1 -1
- 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 +17 -0
- package/dist/http/types/primitive.types.d.ts +73 -0
- package/package.json +3 -2
@@ -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)) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"request.middleware.js","sourceRoot":"","sources":["../../../http/middleware/request.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;
|
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;IACnB,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAmB,EAAE,EAAE;QAC1D,GAAG,CAAC,eAAe,GAAG,eAAkC,CAAC;QAEzD,IAAI,aAAa,GAAG,IAAA,SAAE,GAAE,CAAC;QAEzB,IAAI,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC,EAAE,CAAC;YACpC,aAAa,GAAG,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAW,CAAC;QAC5D,CAAC;QAED,GAAG,CAAC,SAAS,CAAC,kBAAkB,EAAE,aAAa,CAAC,CAAC;QAEjD,GAAG,CAAC,OAAO,GAAG;YACZ,aAAa,EAAE,aAAa;SAC7B,CAAC;QAEF,IAAI,IAAI,EAAE,CAAC;YACT,IAAI,EAAE,CAAC;QACT,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,oBAAoB,CAKlC,eAA2E;IAC3E,OAAO,CAAC,GAAY,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;gBACD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;gBAC7C,IAAI,IAAI,EAAE,CAAC;oBACT,IAAI,CAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC;gBAC1C,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;QACH,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,IACE,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,EACnD,CAAC;gBACD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;gBACvC,IAAI,IAAI,EAAE,CAAC;oBACT,IAAI,CAAC,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC;gBACpC,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,cAAc;IACd,cAAc;IACd,IAAI;AACN,CAAC"}
|
@@ -1,3 +1,14 @@
|
|
1
1
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
2
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
|
+
*/
|
3
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;
|
@@ -1,12 +1,30 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
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
|
+
*/
|
4
11
|
function checkAnyValidation(contractDetails) {
|
5
12
|
return (contractDetails.body ||
|
6
13
|
contractDetails.params ||
|
7
14
|
contractDetails.requestHeaders ||
|
8
15
|
contractDetails.query);
|
9
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
|
+
*/
|
10
28
|
function parseResponse(req, res, next) {
|
11
29
|
if (req.contractDetails.responseHeaders) {
|
12
30
|
const schema = req.schemaValidator.schemify(req.contractDetails.responseHeaders);
|
@@ -19,13 +37,15 @@ function parseResponse(req, res, next) {
|
|
19
37
|
req.schemaValidator.validate(req.schemaValidator.string, res.bodyData);
|
20
38
|
return;
|
21
39
|
}
|
22
|
-
if (Object.prototype.hasOwnProperty.call(
|
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 {
|
23
45
|
if (next) {
|
24
46
|
next(new Error(`Response code ${res.statusCode} not defined in contract.`));
|
25
47
|
}
|
26
48
|
}
|
27
|
-
const schema = req.schemaValidator.schemify(req.contractDetails.responses[res.statusCode]);
|
28
|
-
req.schemaValidator.validate(schema, res.bodyData);
|
29
49
|
if (next) {
|
30
50
|
next();
|
31
51
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"response.middleware.js","sourceRoot":"","sources":["../../../http/middleware/response.middleware.ts"],"names":[],"mappings":";;
|
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"}
|
@@ -1,4 +1,13 @@
|
|
1
1
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
2
2
|
import { OpenAPIObject } from 'openapi3-ts/oas31';
|
3
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
|
+
*/
|
4
13
|
export declare function generateSwaggerDocument<SV extends AnySchemaValidator>(schemaValidator: SV, port: string | number, routers: ForklaunchRouter<SV>[]): OpenAPIObject;
|
@@ -5,16 +5,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.generateSwaggerDocument = generateSwaggerDocument;
|
7
7
|
const httpStatusCodes_1 = __importDefault(require("./httpStatusCodes"));
|
8
|
-
|
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) {
|
9
15
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
10
|
-
}
|
11
|
-
|
16
|
+
}
|
17
|
+
/**
|
18
|
+
* Transforms a base path by ensuring it starts with a slash and capitalizing the first letter.
|
19
|
+
*
|
20
|
+
* @param {string} basePath - The base path to transform.
|
21
|
+
* @returns {string} - The transformed base path.
|
22
|
+
*/
|
23
|
+
function transformBasePath(basePath) {
|
12
24
|
if (basePath.startsWith('/')) {
|
13
25
|
return toUpperCase(basePath.slice(1));
|
14
26
|
}
|
15
27
|
return `/${basePath}`;
|
16
|
-
}
|
17
|
-
|
28
|
+
}
|
29
|
+
/**
|
30
|
+
* Creates a Swagger document.
|
31
|
+
*
|
32
|
+
* @param {string | number} port - The port on which the server is running.
|
33
|
+
* @param {TagObject[]} tags - The tags for the Swagger document.
|
34
|
+
* @param {PathObject} paths - The paths for the Swagger document.
|
35
|
+
* @returns {OpenAPIObject} - The Swagger document.
|
36
|
+
*/
|
37
|
+
function swaggerDocument(port, tags, paths) {
|
18
38
|
return {
|
19
39
|
openapi: '3.1.0',
|
20
40
|
info: {
|
@@ -38,7 +58,15 @@ const swaggerDocument = (port, tags, paths) => {
|
|
38
58
|
],
|
39
59
|
paths
|
40
60
|
};
|
41
|
-
}
|
61
|
+
}
|
62
|
+
/**
|
63
|
+
* Resolves the content object for a given schema.
|
64
|
+
*
|
65
|
+
* @template SV - A type that extends AnySchemaValidator.
|
66
|
+
* @param {SV} schemaValidator - The schema validator.
|
67
|
+
* @param {IdiomaticSchema<SV>} body - The schema body.
|
68
|
+
* @returns {ContentObject} - The resolved content object.
|
69
|
+
*/
|
42
70
|
function contentResolver(schemaValidator, body) {
|
43
71
|
const bodySpec = schemaValidator.openapi(body);
|
44
72
|
return body === schemaValidator.string
|
@@ -53,6 +81,15 @@ function contentResolver(schemaValidator, body) {
|
|
53
81
|
}
|
54
82
|
};
|
55
83
|
}
|
84
|
+
/**
|
85
|
+
* Generates a Swagger document from given routers.
|
86
|
+
*
|
87
|
+
* @template SV - A type that extends AnySchemaValidator.
|
88
|
+
* @param {SV} schemaValidator - The schema validator.
|
89
|
+
* @param {string | number} port - The port on which the server is running.
|
90
|
+
* @param {ForklaunchRouter<SV>[]} routers - The routers to include in the Swagger document.
|
91
|
+
* @returns {OpenAPIObject} - The generated Swagger document.
|
92
|
+
*/
|
56
93
|
function generateSwaggerDocument(schemaValidator, port, routers) {
|
57
94
|
const tags = [];
|
58
95
|
const paths = {};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openApiV3Generator.js","sourceRoot":"","sources":["../../http/openApiV3Generator.ts"],"names":[],"mappings":";;;;;
|
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;AAED;;;;;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,OAA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AAe,CAAC,IAAI,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,CACxD;yBACF;qBACF,CAAC;gBACJ,CAAC;YACH,CAAC;YAED,KAAK,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC;QACjD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AAC5C,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;
|
@@ -0,0 +1,115 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.generateStringFromRegex = generateStringFromRegex;
|
4
|
+
/**
|
5
|
+
* Generates a string from a given regular expression or string representation of a regex.
|
6
|
+
*
|
7
|
+
* @param {RegExp | string} regex - The regular expression or string representation of a regex.
|
8
|
+
* @returns {string} - The generated string based on the regex pattern.
|
9
|
+
* @throws {Error} - Throws an error if there are unmatched brackets or groups.
|
10
|
+
*/
|
11
|
+
function generateStringFromRegex(regex) {
|
12
|
+
let regexStr = typeof regex === 'object' ? regex.source : regex;
|
13
|
+
// Remove leading and trailing slashes if present
|
14
|
+
if (regexStr.startsWith('/'))
|
15
|
+
regexStr = regexStr.slice(1);
|
16
|
+
if (regexStr.endsWith('/g'))
|
17
|
+
regexStr = regexStr.slice(0, -2); // Remove the global flag
|
18
|
+
if (regexStr.endsWith('/'))
|
19
|
+
regexStr = regexStr.slice(0, -1);
|
20
|
+
let result = '';
|
21
|
+
let i = 0;
|
22
|
+
while (i < regexStr.length) {
|
23
|
+
const char = regexStr[i];
|
24
|
+
switch (char) {
|
25
|
+
case '\\': {
|
26
|
+
// Handle escaped characters
|
27
|
+
const nextChar = regexStr[i + 1];
|
28
|
+
switch (nextChar) {
|
29
|
+
case 'b':
|
30
|
+
// Word boundary, ensure we start a new word
|
31
|
+
if (result.length > 0 && /\w/.test(result[result.length - 1])) {
|
32
|
+
result += ' ';
|
33
|
+
}
|
34
|
+
break;
|
35
|
+
case 'd':
|
36
|
+
result += '0'; // Match a digit
|
37
|
+
break;
|
38
|
+
case 'w':
|
39
|
+
result += 'a'; // Match a word character
|
40
|
+
break;
|
41
|
+
case 's':
|
42
|
+
result += ' '; // Match a whitespace character
|
43
|
+
break;
|
44
|
+
default:
|
45
|
+
result += nextChar;
|
46
|
+
}
|
47
|
+
i += 2;
|
48
|
+
break;
|
49
|
+
}
|
50
|
+
case '.':
|
51
|
+
// Match any character (using 'a' for simplicity)
|
52
|
+
result += 'a';
|
53
|
+
i++;
|
54
|
+
break;
|
55
|
+
case '[': {
|
56
|
+
// Handle character classes
|
57
|
+
const endIdx = regexStr.indexOf(']', i);
|
58
|
+
if (endIdx === -1) {
|
59
|
+
throw new Error('Unmatched [');
|
60
|
+
}
|
61
|
+
const charClass = regexStr.slice(i + 1, endIdx);
|
62
|
+
result += charClass[0]; // Use the first character in the class
|
63
|
+
i = endIdx + 1;
|
64
|
+
break;
|
65
|
+
}
|
66
|
+
case '(': {
|
67
|
+
// Handle groups (non-capturing groups (?:...) are simplified to normal groups)
|
68
|
+
const endGroupIdx = regexStr.indexOf(')', i);
|
69
|
+
if (endGroupIdx === -1) {
|
70
|
+
throw new Error('Unmatched (');
|
71
|
+
}
|
72
|
+
const groupContent = regexStr.slice(i + 1, endGroupIdx);
|
73
|
+
result += generateStringFromRegex(groupContent); // Recursively handle group content
|
74
|
+
i = endGroupIdx + 1;
|
75
|
+
break;
|
76
|
+
}
|
77
|
+
case '{': {
|
78
|
+
// Handle quantifiers {n} or {n,m}
|
79
|
+
const endQuantIdx = regexStr.indexOf('}', i);
|
80
|
+
if (endQuantIdx === -1) {
|
81
|
+
throw new Error('Unmatched {');
|
82
|
+
}
|
83
|
+
const quantifier = regexStr.slice(i + 1, endQuantIdx);
|
84
|
+
const min = parseInt(quantifier.split(',')[0], 10) || 1;
|
85
|
+
const lastChar = result[result.length - 1];
|
86
|
+
result += lastChar.repeat(min - 1);
|
87
|
+
i = endQuantIdx + 1;
|
88
|
+
break;
|
89
|
+
}
|
90
|
+
case '*':
|
91
|
+
case '+':
|
92
|
+
case '?': {
|
93
|
+
// Handle *, +, and ? quantifiers (simplified handling)
|
94
|
+
const prevChar = result[result.length - 1];
|
95
|
+
if (char === '*') {
|
96
|
+
// Match zero or more (using one for simplicity)
|
97
|
+
result += prevChar;
|
98
|
+
}
|
99
|
+
else if (char === '+') {
|
100
|
+
// Match one or more (already have one, so add one more)
|
101
|
+
result += prevChar;
|
102
|
+
}
|
103
|
+
i++;
|
104
|
+
break;
|
105
|
+
}
|
106
|
+
default:
|
107
|
+
// Default case: add character to result
|
108
|
+
result += char;
|
109
|
+
i++;
|
110
|
+
break;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
return result;
|
114
|
+
}
|
115
|
+
//# sourceMappingURL=regex.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"regex.js","sourceRoot":"","sources":["../../http/regex.ts"],"names":[],"mappings":";;AAOA,0DAwGC;AA/GD;;;;;;GAMG;AACH,SAAgB,uBAAuB,CAAC,KAAsB;IAC5D,IAAI,QAAQ,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;IAEhE,iDAAiD;IACjD,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC3D,IAAI,QAAQ,CAAC,QAAQ,CA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AAM,UAAU,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,WAAW,CAAC,CAAC;gBACtD,MAAM,GAAG,GAAG,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;gBACxD,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBAC3C,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;gBACnC,CAAC,GAAG,WAAW,GAAG,CAAC,CAAC;gBACpB,MAAM;YACR,CAAC;YACD,KAAK,GAAG,CAAC;YACT,KAAK,GAAG,CAAC;YACT,KAAK,GAAG,CAAC,CAAC,CAAC;gBACT,uDAAuD;gBACvD,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBAC3C,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;oBACjB,gDAAgD;oBAChD,MAAM,IAAI,QAAQ,CAAC;gBACrB,CAAC;qBAAM,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;oBACxB,wDAAwD;oBACxD,MAAM,IAAI,QAAQ,CAAC;gBACrB,CAAC;gBACD,CAAC,EAAE,CAAC;gBACJ,MAAM;YACR,CAAC;YACD;gBACE,wCAAwC;gBACxC,MAAM,IAAI,IAAI,CAAC;gBACf,CAAC,EAAE,CAAC;gBACJ,MAAM;QACV,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
@@ -4,50 +4,121 @@ import { IdiomaticSchema, SchemaValidator } from '@forklaunch/validator/types';
|
|
4
4
|
import { IncomingHttpHeaders, OutgoingHttpHeader } from 'http';
|
5
5
|
import { ParsedQs } from 'qs';
|
6
6
|
import { HttpContractDetails, ParamsDictionary, PathParamHttpContractDetails } from './primitive.types';
|
7
|
+
/**
|
8
|
+
* Interface representing the context of a request.
|
9
|
+
*/
|
7
10
|
export interface RequestContext {
|
11
|
+
/** Correlation ID for tracking requests */
|
8
12
|
correlationId: string;
|
13
|
+
/** Optional idempotency key for ensuring idempotent requests */
|
9
14
|
idempotencyKey?: string;
|
10
15
|
}
|
16
|
+
/**
|
17
|
+
* Interface representing a Forklaunch request.
|
18
|
+
*
|
19
|
+
* @template SV - A type that extends AnySchemaValidator.
|
20
|
+
* @template P - A type for request parameters, defaulting to ParamsDictionary.
|
21
|
+
* @template ReqBody - A type for the request body, defaulting to unknown.
|
22
|
+
* @template ReqQuery - A type for the request query, defaulting to ParsedQs.
|
23
|
+
* @template Headers - A type for the request headers, defaulting to IncomingHttpHeaders.
|
24
|
+
*/
|
11
25
|
export interface ForklaunchRequest<SV extends AnySchemaValidator, P = ParamsDictionary, ReqBody = unknown, ReqQuery = ParsedQs, Headers = IncomingHttpHeaders> {
|
26
|
+
/** Context of the request */
|
12
27
|
context: Prettify<RequestContext>;
|
28
|
+
/** Contract details for the request */
|
13
29
|
contractDetails: HttpContractDetails<SV> | PathParamHttpContractDetails<SV>;
|
30
|
+
/** Schema validator */
|
14
31
|
schemaValidator: SchemaValidator;
|
32
|
+
/** Request parameters */
|
15
33
|
params: P;
|
34
|
+
/** Request headers */
|
16
35
|
headers: Headers;
|
36
|
+
/** Request body */
|
17
37
|
body: ReqBody;
|
38
|
+
/** Request query */
|
18
39
|
query: ReqQuery;
|
19
40
|
}
|
41
|
+
/**
|
42
|
+
* Interface representing a Forklaunch response.
|
43
|
+
*
|
44
|
+
* @template ResBody - A type for the response body, defaulting to common status code responses.
|
45
|
+
* @template StatusCode - A type for the status code, defaulting to number.
|
46
|
+
*/
|
20
47
|
export interface ForklaunchResponse<ResBody = {
|
21
48
|
400: unknown;
|
22
49
|
401: unknown;
|
23
50
|
403: unknown;
|
24
51
|
500: unknown;
|
25
52
|
}, StatusCode = number> {
|
53
|
+
/** Data of the response body */
|
26
54
|
bodyData: unknown;
|
55
|
+
/** Status code of the response */
|
27
56
|
statusCode: StatusCode;
|
57
|
+
/** Whether the response is corked */
|
28
58
|
corked: boolean;
|
59
|
+
/**
|
60
|
+
* Gets the headers of the response.
|
61
|
+
* @returns {OutgoingHttpHeader} - The headers of the response.
|
62
|
+
*/
|
29
63
|
getHeaders: () => OutgoingHttpHeader;
|
64
|
+
/**
|
65
|
+
* Sets a header for the response.
|
66
|
+
* @param {string} key - The header key.
|
67
|
+
* @param {string} value - The header value.
|
68
|
+
*/
|
30
69
|
setHeader: (key: string, value: string) => void;
|
70
|
+
/**
|
71
|
+
* Sets the status of the response.
|
72
|
+
* @param {U} code - The status code.
|
73
|
+
* @param {string} [message] - Optional message.
|
74
|
+
* @returns {ForklaunchResponse<ResBody[U], U>} - The response with the given status.
|
75
|
+
*/
|
31
76
|
status: {
|
32
77
|
<U extends keyof ResBody>(code: U): ForklaunchResponse<ResBody[U], U>;
|
33
78
|
<U extends keyof ResBody>(code: U, message?: string): ForklaunchResponse<ResBody[U], U>;
|
34
79
|
<U extends 500>(code: U): ForklaunchResponse<string, U>;
|
35
80
|
<U extends 500>(code: U, message?: string): ForklaunchResponse<string, U>;
|
36
81
|
};
|
82
|
+
/**
|
83
|
+
* Sends the response.
|
84
|
+
* @param {ResBody} [body] - The response body.
|
85
|
+
* @param {boolean} [close_connection] - Whether to close the connection.
|
86
|
+
* @returns {T} - The sent response.
|
87
|
+
*/
|
37
88
|
send: {
|
38
89
|
<T>(body?: ResBody, close_connection?: boolean): T;
|
39
90
|
<T>(body?: ResBody): T;
|
40
91
|
};
|
92
|
+
/**
|
93
|
+
* Sends a JSON response.
|
94
|
+
* @param {ResBody} [body] - The response body.
|
95
|
+
* @returns {boolean|T} - The JSON response.
|
96
|
+
*/
|
41
97
|
json: {
|
42
98
|
(body?: ResBody): boolean;
|
43
99
|
<T>(body?: ResBody): T;
|
44
100
|
};
|
101
|
+
/**
|
102
|
+
* Sends a JSONP response.
|
103
|
+
* @param {ResBody} [body] - The response body.
|
104
|
+
* @returns {boolean|T} - The JSONP response.
|
105
|
+
*/
|
45
106
|
jsonp: {
|
46
107
|
(body?: ResBody): boolean;
|
47
108
|
<T>(body?: ResBody): T;
|
48
109
|
};
|
49
110
|
}
|
111
|
+
/**
|
112
|
+
* Type representing a mapped schema.
|
113
|
+
*
|
114
|
+
* @template SV - A type that extends AnySchemaValidator.
|
115
|
+
* @template T - A type that extends IdiomaticSchema or a valid schema object.
|
116
|
+
*/
|
50
117
|
export type MapSchema<SV extends AnySchemaValidator, T extends IdiomaticSchema<SV> | SV['_ValidSchemaObject']> = Schema<T, SV> extends infer U ? {
|
51
118
|
[key: string]: unknown;
|
52
119
|
} extends U ? never : U : never;
|
120
|
+
/**
|
121
|
+
* Type representing the next function in a middleware.
|
122
|
+
* @param {unknown} [err] - Optional error parameter.
|
123
|
+
*/
|
53
124
|
export type ForklaunchNextFunction = (err?: unknown) => void;
|