barehttp 0.4.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -10
- package/lib/logger/index.js +6 -2
- package/lib/request.d.ts +8 -4
- package/lib/request.js +41 -32
- package/lib/schemas/custom-schema.d.ts +32 -0
- package/lib/schemas/custom-schema.js +69 -0
- package/lib/schemas/dirty-tsm.d.ts +1 -0
- package/lib/schemas/dirty-tsm.js +201 -0
- package/lib/schemas/generator.d.ts +7 -0
- package/lib/schemas/generator.js +186 -0
- package/lib/schemas/helpers.d.ts +27 -0
- package/lib/schemas/helpers.js +50 -0
- package/lib/schemas/json-schema.d.ts +2 -0
- package/lib/schemas/json-schema.js +52 -0
- package/lib/schemas/openami-schema.d.ts +2 -0
- package/lib/schemas/openami-schema.js +63 -0
- package/lib/schemas/project.d.ts +0 -0
- package/lib/schemas/project.js +1 -0
- package/lib/server.d.ts +44 -15
- package/lib/server.js +103 -62
- package/lib/websocket.js +3 -1
- package/package.json +14 -10
- package/lib/report.d.ts +0 -2
- package/lib/report.js +0 -20
|
@@ -0,0 +1,186 @@
|
|
|
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.generateRouteSchema = void 0;
|
|
7
|
+
const ts_morph_1 = require("ts-morph");
|
|
8
|
+
const custom_schema_1 = require("./custom-schema");
|
|
9
|
+
const helpers_1 = require("./helpers");
|
|
10
|
+
const json_schema_1 = require("./json-schema");
|
|
11
|
+
const path_1 = __importDefault(require("path"));
|
|
12
|
+
const fs_1 = require("fs");
|
|
13
|
+
const project = new ts_morph_1.Project({ tsConfigFilePath: path_1.default.join(process.cwd(), '/tsconfig.json') });
|
|
14
|
+
const nodeModulesFile = path_1.default.join(process.cwd(), 'node_modules', 'barehttp');
|
|
15
|
+
const isInstalledPackage = (0, fs_1.existsSync)(nodeModulesFile);
|
|
16
|
+
if (isInstalledPackage)
|
|
17
|
+
project.addSourceFileAtPath(nodeModulesFile + '/lib/server.d.ts');
|
|
18
|
+
const serverSourceFile = project.getSourceFile('server.d.ts');
|
|
19
|
+
const routes = serverSourceFile?.getClass('BareServer')?.getMember('route');
|
|
20
|
+
const runtimeRoutes = serverSourceFile?.getClass('BareServer')?.getMember('runtimeRoute');
|
|
21
|
+
// const requestSourceFile = project.getSourceFile('request.ts');
|
|
22
|
+
// const flowJson = requestSourceFile?.getClass('BareRequest')?.getMember('json');
|
|
23
|
+
// const flowSend = requestSourceFile?.getClass('BareRequest')?.getMember('send');
|
|
24
|
+
const acceptedPropertyNames = ['get', 'post', 'put', 'delete', 'options', 'head', 'patch'];
|
|
25
|
+
const getReferences = (fileRoute, target) => {
|
|
26
|
+
if (!target)
|
|
27
|
+
return [];
|
|
28
|
+
return target
|
|
29
|
+
?.getChildrenOfKind(ts_morph_1.ts.SyntaxKind.Identifier)[0]
|
|
30
|
+
.findReferences()[0]
|
|
31
|
+
.getReferences()
|
|
32
|
+
?.filter((re) => {
|
|
33
|
+
return re.compilerObject.fileName.includes(fileRoute);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
// needed for future (extract call functions)
|
|
37
|
+
// const getFlowNodes = (n?: ClassMemberTypes) => {
|
|
38
|
+
// if (!n) return [];
|
|
39
|
+
// return n
|
|
40
|
+
// .getChildrenOfKind(ts.SyntaxKind.Identifier)[0]
|
|
41
|
+
// .findReferences()[0]
|
|
42
|
+
// .getReferences()
|
|
43
|
+
// .map((r) => r.getNode().getParent()?.getParent())
|
|
44
|
+
// .filter((p) => p?.getKind() === ts.SyntaxKind.CallExpression)
|
|
45
|
+
// .map((p) => p?.getNodeProperty('arguments' as any));
|
|
46
|
+
// };
|
|
47
|
+
const generateRouteSchema = (fileRouteToDeclarations) => {
|
|
48
|
+
if (!routes && !runtimeRoutes) {
|
|
49
|
+
throw new Error('No project been allocated, theres some issue');
|
|
50
|
+
}
|
|
51
|
+
const allReferences = [
|
|
52
|
+
...getReferences(fileRouteToDeclarations, routes),
|
|
53
|
+
...getReferences(fileRouteToDeclarations, runtimeRoutes),
|
|
54
|
+
];
|
|
55
|
+
const extractedReturns = allReferences.map((ref) => {
|
|
56
|
+
const methodName = ref
|
|
57
|
+
.getNode()
|
|
58
|
+
.getAncestors()
|
|
59
|
+
.map((n) => n.getSymbol()?.getName())
|
|
60
|
+
.filter((param) => acceptedPropertyNames.includes(param))
|
|
61
|
+
.pop();
|
|
62
|
+
if (!methodName) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
return ref
|
|
66
|
+
.getNode()
|
|
67
|
+
.getAncestors()
|
|
68
|
+
.find((n) => n.getKind() === ts_morph_1.ts.SyntaxKind.CallExpression)
|
|
69
|
+
?.getChildren()
|
|
70
|
+
.find((n) => n.getKind() === ts_morph_1.ts.SyntaxKind.SyntaxList)
|
|
71
|
+
?.getFirstChild()
|
|
72
|
+
?.getChildSyntaxList()
|
|
73
|
+
?.getChildren()
|
|
74
|
+
.filter((c) => {
|
|
75
|
+
return c.getKind() === ts_morph_1.ts.SyntaxKind.PropertyAssignment && ((0, helpers_1.isHandler)(c) || (0, helpers_1.isRoute)(c));
|
|
76
|
+
})
|
|
77
|
+
.map((c) => {
|
|
78
|
+
if ((0, helpers_1.isHandler)(c)) {
|
|
79
|
+
return {
|
|
80
|
+
type: 'handler',
|
|
81
|
+
methodName,
|
|
82
|
+
syntaxList: c
|
|
83
|
+
.getChildren()
|
|
84
|
+
.find((n) => n.getKind() === ts_morph_1.ts.SyntaxKind.ArrowFunction ||
|
|
85
|
+
n.getKind() === ts_morph_1.ts.SyntaxKind.FunctionExpression)
|
|
86
|
+
?.getLastChild()
|
|
87
|
+
?.getChildSyntaxList(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
type: 'route',
|
|
92
|
+
methodName,
|
|
93
|
+
value: c
|
|
94
|
+
.getNodeProperty('initializer')
|
|
95
|
+
.getText()
|
|
96
|
+
?.replaceAll("'", ''),
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
const perRoute = extractedReturns
|
|
101
|
+
.map((routeCombination) => {
|
|
102
|
+
return routeCombination.reduce((acc, curr) => {
|
|
103
|
+
acc.methodName = curr.methodName;
|
|
104
|
+
if (curr.type === 'handler') {
|
|
105
|
+
return {
|
|
106
|
+
...acc,
|
|
107
|
+
handler: curr.syntaxList,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
return {
|
|
112
|
+
...acc,
|
|
113
|
+
route: curr.value,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}, {});
|
|
117
|
+
})
|
|
118
|
+
.map((routeCombination) => ({
|
|
119
|
+
...routeCombination,
|
|
120
|
+
handler: getReturnStatements(routeCombination.handler),
|
|
121
|
+
}));
|
|
122
|
+
const schemas = perRoute
|
|
123
|
+
.filter((pr) => pr.route && pr.handler.length)
|
|
124
|
+
.map(({ handler, route, methodName }) => {
|
|
125
|
+
const schemas = handler.map((t) => (0, custom_schema_1.generateCustomSchema)(t));
|
|
126
|
+
let finalSchema = schemas[0];
|
|
127
|
+
if (schemas.length > 1) {
|
|
128
|
+
finalSchema = {
|
|
129
|
+
type: 'union',
|
|
130
|
+
nullable: false,
|
|
131
|
+
anyOf: schemas,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
route,
|
|
136
|
+
methodName,
|
|
137
|
+
schemas,
|
|
138
|
+
finalSchema,
|
|
139
|
+
jsonSchema: (0, json_schema_1.convertToJsonSchema)(finalSchema),
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
return [...schemas];
|
|
143
|
+
};
|
|
144
|
+
exports.generateRouteSchema = generateRouteSchema;
|
|
145
|
+
const extractReturnStatements = (accumulator, n) => {
|
|
146
|
+
if (!n)
|
|
147
|
+
return;
|
|
148
|
+
if (ts_morph_1.ts.SyntaxKind.IfStatement === n.getKind()) {
|
|
149
|
+
const thenProp = n.getNodeProperty('thenStatement');
|
|
150
|
+
const elseProp = n.getNodeProperty('elseStatement');
|
|
151
|
+
const thenSyntax = thenProp?.getChildSyntaxList();
|
|
152
|
+
const elseSyntax = elseProp?.getChildSyntaxList();
|
|
153
|
+
extractReturnStatements(accumulator, thenSyntax);
|
|
154
|
+
extractReturnStatements(accumulator, elseSyntax);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (n.getChildren().length) {
|
|
158
|
+
const cleanChildren = n.getChildren().filter((c) => typeof c.getKind === 'function');
|
|
159
|
+
const findReturn = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.ReturnStatement);
|
|
160
|
+
const thereIf = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.IfStatement);
|
|
161
|
+
const thereWhile = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.WhileKeyword);
|
|
162
|
+
const thereFor = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.ForStatement);
|
|
163
|
+
const syntaxList = n.getChildSyntaxList();
|
|
164
|
+
if (findReturn) {
|
|
165
|
+
accumulator.push(findReturn);
|
|
166
|
+
}
|
|
167
|
+
extractReturnStatements(accumulator, thereIf);
|
|
168
|
+
extractReturnStatements(accumulator, thereWhile);
|
|
169
|
+
extractReturnStatements(accumulator, thereFor);
|
|
170
|
+
extractReturnStatements(accumulator, syntaxList);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const getTypes = (nodes) => nodes
|
|
174
|
+
.map((r) => r.getChildren().find((c) => {
|
|
175
|
+
const type = c.getType();
|
|
176
|
+
return type.isObject() || (0, helpers_1.isFinalType)(type);
|
|
177
|
+
}))
|
|
178
|
+
.filter((n) => n && typeof n.getType === 'function')
|
|
179
|
+
.map((acc) => acc.getType());
|
|
180
|
+
const getReturnStatements = (n) => {
|
|
181
|
+
if (!n)
|
|
182
|
+
return [];
|
|
183
|
+
const accumulator = [];
|
|
184
|
+
extractReturnStatements(accumulator, n);
|
|
185
|
+
return getTypes(accumulator);
|
|
186
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Node, PropertyAssignment, ts, Type } from 'ts-morph';
|
|
2
|
+
export declare const helpers: {
|
|
3
|
+
findCallExpressionFromChildren: (property: PropertyAssignment) => Node<ts.Node>[];
|
|
4
|
+
findCallExpression: (n: Node<ts.Node>[]) => Node<ts.Node> | undefined;
|
|
5
|
+
findSyntaxList: (property: PropertyAssignment | Node<ts.Node>) => Node<ts.Node> | undefined;
|
|
6
|
+
findFunction: (property: PropertyAssignment) => Node<ts.Node> | undefined;
|
|
7
|
+
findReturnStatement: (property: PropertyAssignment) => Node<ts.Node> | undefined;
|
|
8
|
+
findIdentifier: (property: PropertyAssignment) => Node<ts.Node> | undefined;
|
|
9
|
+
findObjectLiteralExpressionFromChildren: (property: PropertyAssignment) => Node<ts.Node> | undefined;
|
|
10
|
+
findObjectLiteralExpression: (n: Node<ts.Node>[]) => Node<ts.Node> | undefined;
|
|
11
|
+
filterPropertyAssignmentFromChildren: (property: PropertyAssignment) => Node<ts.Node>[];
|
|
12
|
+
findPropertyAssignmentFromChildren: (property: PropertyAssignment) => Node<ts.Node> | undefined;
|
|
13
|
+
findPropertyAssignment: (n: Node<ts.Node>[]) => Node<ts.Node> | undefined;
|
|
14
|
+
findUnionTypeNodeFromChildren: (n: Node<ts.Node>) => Node<ts.Node> | undefined;
|
|
15
|
+
findNullableTypeFromChildren: (n: Node<ts.Node>) => Node<ts.Node> | undefined;
|
|
16
|
+
filterNullableTypeFromChildren: (n: Node<ts.Node>) => Node<ts.Node>[];
|
|
17
|
+
cleanNullableTypes: (t: Type<ts.Type>[]) => Type<ts.Type>[];
|
|
18
|
+
};
|
|
19
|
+
export declare const isFinalType: (t: Type<ts.Type>) => boolean;
|
|
20
|
+
export declare const isNullType: (t: Type<ts.Type>) => boolean;
|
|
21
|
+
declare type ResolvedBasicTypes = 'string' | 'number' | 'boolean' | 'array' | 'object';
|
|
22
|
+
export declare const isHandler: (c: Node<ts.Node>) => boolean;
|
|
23
|
+
export declare const isRoute: (c: Node<ts.Node>) => boolean;
|
|
24
|
+
export declare const getTypeGenericText: (t: Type<ts.Type>) => ResolvedBasicTypes;
|
|
25
|
+
export declare const getApparentTypeName: (t: Type<ts.Type>) => string;
|
|
26
|
+
export declare function logInternals(data: any): void;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logInternals = exports.getApparentTypeName = exports.getTypeGenericText = exports.isRoute = exports.isHandler = exports.isNullType = exports.isFinalType = exports.helpers = void 0;
|
|
4
|
+
const ts_morph_1 = require("ts-morph");
|
|
5
|
+
const util_1 = require("util");
|
|
6
|
+
exports.helpers = {
|
|
7
|
+
findCallExpressionFromChildren: (property) => property.getChildren(),
|
|
8
|
+
findCallExpression: (n) => n.find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.CallExpression),
|
|
9
|
+
findSyntaxList: (property) => property.getChildren().find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.SyntaxList),
|
|
10
|
+
findFunction: (property) => property
|
|
11
|
+
.getChildren()
|
|
12
|
+
.find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.ArrowFunction ||
|
|
13
|
+
x.getKind() === ts_morph_1.ts.SyntaxKind.FunctionExpression),
|
|
14
|
+
findReturnStatement: (property) => property.getChildren().find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.ReturnStatement),
|
|
15
|
+
findIdentifier: (property) => property.getChildren().find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.ReturnStatement),
|
|
16
|
+
findObjectLiteralExpressionFromChildren: (property) => property.getChildren().find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.ObjectLiteralExpression),
|
|
17
|
+
findObjectLiteralExpression: (n) => n.find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.ObjectLiteralExpression),
|
|
18
|
+
filterPropertyAssignmentFromChildren: (property) => property.getChildren().filter((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.PropertyAssignment),
|
|
19
|
+
findPropertyAssignmentFromChildren: (property) => property.getChildren().find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.PropertyAssignment),
|
|
20
|
+
findPropertyAssignment: (n) => n.find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.PropertyAssignment),
|
|
21
|
+
findUnionTypeNodeFromChildren: (n) => n.getChildren().find((x) => x.getKind() === ts_morph_1.ts.SyntaxKind.UnionType),
|
|
22
|
+
findNullableTypeFromChildren: (n) => n.getChildren().find((x) => (0, exports.isNullType)(x.getType())),
|
|
23
|
+
filterNullableTypeFromChildren: (n) => n.getChildren().filter((x) => !(0, exports.isNullType)(x.getType())),
|
|
24
|
+
cleanNullableTypes: (t) => t.filter((x) => !(0, exports.isNullType)(x)),
|
|
25
|
+
}; //Prop
|
|
26
|
+
const isFinalType = (t) => t.isNumber() || t.isString() || t.isBoolean() || t.isLiteral();
|
|
27
|
+
exports.isFinalType = isFinalType;
|
|
28
|
+
const isNullType = (t) => t.isNull() || t.isUndefined();
|
|
29
|
+
exports.isNullType = isNullType;
|
|
30
|
+
const isHandler = (c) => c.getSymbol()?.getName() === 'handler';
|
|
31
|
+
exports.isHandler = isHandler;
|
|
32
|
+
const isRoute = (c) => c.getSymbol()?.getName() === 'route';
|
|
33
|
+
exports.isRoute = isRoute;
|
|
34
|
+
const getTypeGenericText = (t) => {
|
|
35
|
+
if (t.isStringLiteral() || t.isNumberLiteral() || t.isBooleanLiteral()) {
|
|
36
|
+
return t.getBaseTypeOfLiteralType().getText();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return t.getText();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.getTypeGenericText = getTypeGenericText;
|
|
43
|
+
const getApparentTypeName = (t) => {
|
|
44
|
+
return t.getApparentType().getText().toLowerCase();
|
|
45
|
+
};
|
|
46
|
+
exports.getApparentTypeName = getApparentTypeName;
|
|
47
|
+
function logInternals(data) {
|
|
48
|
+
console.log((0, util_1.inspect)(data, false, null, true));
|
|
49
|
+
}
|
|
50
|
+
exports.logInternals = logInternals;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertToJsonSchema = void 0;
|
|
4
|
+
const convertToJsonSchema = (schema) => {
|
|
5
|
+
if (schema.type === 'string') {
|
|
6
|
+
return {
|
|
7
|
+
type: 'string',
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
if (schema.type === 'number') {
|
|
11
|
+
return {
|
|
12
|
+
type: 'number',
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (schema.type === 'boolean') {
|
|
16
|
+
return {
|
|
17
|
+
type: 'boolean',
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (schema.type === 'array') {
|
|
21
|
+
const reSchema = schema;
|
|
22
|
+
return {
|
|
23
|
+
type: 'array',
|
|
24
|
+
items: (0, exports.convertToJsonSchema)(reSchema.items),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (schema.type === 'object') {
|
|
28
|
+
const reSchema = schema;
|
|
29
|
+
const objectJsonedProperties = Object.keys(reSchema.properties).reduce((acc, key) => {
|
|
30
|
+
acc[key] = (0, exports.convertToJsonSchema)(reSchema.properties[key]);
|
|
31
|
+
return acc;
|
|
32
|
+
}, {});
|
|
33
|
+
const required = Object.entries(reSchema.properties).reduce((acc, [key, value]) => {
|
|
34
|
+
if (!value.nullable) {
|
|
35
|
+
acc.push(key);
|
|
36
|
+
}
|
|
37
|
+
return acc;
|
|
38
|
+
}, []);
|
|
39
|
+
return {
|
|
40
|
+
required,
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: objectJsonedProperties,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (schema.type === 'union') {
|
|
46
|
+
const reSchema = schema;
|
|
47
|
+
return {
|
|
48
|
+
anyOf: reSchema.anyOf.map((item) => (0, exports.convertToJsonSchema)(item)),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
exports.convertToJsonSchema = convertToJsonSchema;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertToOpenApiSchema = void 0;
|
|
4
|
+
const createRouteSchema = (route, method, openApiSchema) => ({
|
|
5
|
+
[route]: {
|
|
6
|
+
[method]: {
|
|
7
|
+
description: 'Autogenerated',
|
|
8
|
+
responses: {
|
|
9
|
+
'200': openApiSchema,
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
const convertToOpenApiSchema = (schema, route, method) => {
|
|
15
|
+
if (schema.type === 'string') {
|
|
16
|
+
return {
|
|
17
|
+
type: 'string',
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (schema.type === 'number') {
|
|
21
|
+
return {
|
|
22
|
+
type: 'number',
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (schema.type === 'boolean') {
|
|
26
|
+
return {
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (schema.type === 'array') {
|
|
31
|
+
const reSchema = schema;
|
|
32
|
+
return {
|
|
33
|
+
type: 'array',
|
|
34
|
+
items: (0, exports.convertToOpenApiSchema)(reSchema.items),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (schema.type === 'object') {
|
|
38
|
+
const reSchema = schema;
|
|
39
|
+
const objectJsonedProperties = Object.keys(reSchema.properties).reduce((acc, key) => {
|
|
40
|
+
acc[key] = (0, exports.convertToOpenApiSchema)(reSchema.properties[key]);
|
|
41
|
+
return acc;
|
|
42
|
+
}, {});
|
|
43
|
+
const required = Object.entries(reSchema.properties).reduce((acc, [key, value]) => {
|
|
44
|
+
if (!value.nullable) {
|
|
45
|
+
acc.push(key);
|
|
46
|
+
}
|
|
47
|
+
return acc;
|
|
48
|
+
}, []);
|
|
49
|
+
return {
|
|
50
|
+
required,
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties: objectJsonedProperties,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (schema.type === 'union') {
|
|
56
|
+
const reSchema = schema;
|
|
57
|
+
return {
|
|
58
|
+
anyOf: reSchema.anyOf.map((item) => (0, exports.convertToOpenApiSchema)(item)),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return createRouteSchema(route, method, schema);
|
|
62
|
+
};
|
|
63
|
+
exports.convertToOpenApiSchema = convertToOpenApiSchema;
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/lib/server.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { ServerOptions } from 'ws';
|
|
3
|
+
import Ajv from 'ajv';
|
|
3
4
|
import { BareRequest, CacheOpts } from './request';
|
|
4
5
|
import { CookiesManagerOptions } from './middlewares/cookies/cookie-manager';
|
|
5
6
|
import { HttpMethodsUnion, StatusCodesUnion } from './utils';
|
|
@@ -7,7 +8,9 @@ import { CorsOptions } from './middlewares/cors/cors';
|
|
|
7
8
|
import { WebSocketServer } from './websocket';
|
|
8
9
|
import { Server } from 'http';
|
|
9
10
|
declare type Middleware = (flow: BareRequest) => Promise<void> | void;
|
|
10
|
-
declare type Handler
|
|
11
|
+
declare type Handler<H extends {
|
|
12
|
+
[key: string]: string | undefined;
|
|
13
|
+
}> = (flow: BareRequest<H>) => any;
|
|
11
14
|
declare type ErrorHandler = (err: any, flow: BareRequest, status?: StatusCodesUnion) => void;
|
|
12
15
|
declare type IP = `${number}.${number}.${number}.${number}`;
|
|
13
16
|
declare type RouteOpts<C> = {
|
|
@@ -17,15 +20,39 @@ declare type RouteOpts<C> = {
|
|
|
17
20
|
* Request timeout handler in `ms`
|
|
18
21
|
*/
|
|
19
22
|
timeout?: number;
|
|
23
|
+
builtInRuntime?: {
|
|
24
|
+
output?: boolean;
|
|
25
|
+
};
|
|
26
|
+
middlewares?: Array<Middleware>;
|
|
20
27
|
};
|
|
21
28
|
declare type BareOptions<A extends IP> = {
|
|
29
|
+
/**
|
|
30
|
+
* Declare a global middlewares array
|
|
31
|
+
* Default: []
|
|
32
|
+
*/
|
|
22
33
|
middlewares?: Array<Middleware>;
|
|
34
|
+
/**
|
|
35
|
+
* Opt-out request body parsing (de-serialization)
|
|
36
|
+
* Default `false`
|
|
37
|
+
*/
|
|
38
|
+
doNotParseBody?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Opt-in to have a custom swagger per route generation
|
|
41
|
+
* Default `false`
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* Opt-in to have a custom runtime JSON Schema checker per routes
|
|
45
|
+
* Default `false`
|
|
46
|
+
*/
|
|
47
|
+
enableSchemaValidation?: boolean;
|
|
23
48
|
serverPort?: number;
|
|
49
|
+
declaredRoutesPaths?: Array<string>;
|
|
24
50
|
/**
|
|
25
51
|
* Address to bind the web server to
|
|
26
52
|
* Default '0.0.0.0'
|
|
27
53
|
*/
|
|
28
54
|
serverAddress?: A | 'localhost';
|
|
55
|
+
setRandomPort?: boolean;
|
|
29
56
|
/**
|
|
30
57
|
* Enable request context storage
|
|
31
58
|
* Default `false`
|
|
@@ -52,11 +79,6 @@ declare type BareOptions<A extends IP> = {
|
|
|
52
79
|
* Log the resolved reverse DNS first hop for remote ip of the client (first proxy)
|
|
53
80
|
*/
|
|
54
81
|
reverseDns?: boolean;
|
|
55
|
-
/**
|
|
56
|
-
* Exposes a report with the routes usage.
|
|
57
|
-
* Default `false`
|
|
58
|
-
*/
|
|
59
|
-
statisticsReport?: boolean;
|
|
60
82
|
/**
|
|
61
83
|
* WebSocket server exposure
|
|
62
84
|
*/
|
|
@@ -69,16 +91,23 @@ declare type BareOptions<A extends IP> = {
|
|
|
69
91
|
*/
|
|
70
92
|
cors?: boolean | CorsOptions;
|
|
71
93
|
};
|
|
94
|
+
declare type ExtractRouteParams<T extends string> = T extends `${infer Start}:${infer Param}/${infer Rest}` ? {
|
|
95
|
+
[K in Param | keyof ExtractRouteParams<Rest>]: string;
|
|
96
|
+
} : T extends `${infer Start}:${infer Param}` ? {
|
|
97
|
+
[K in Param]: string;
|
|
98
|
+
} : {
|
|
99
|
+
[k: string]: string;
|
|
100
|
+
};
|
|
72
101
|
interface HandlerExposed<K> {
|
|
73
102
|
<R extends `/${string}`, C>(setUp: K extends 'declare' ? {
|
|
74
103
|
route: R;
|
|
75
104
|
options?: RouteOpts<C>;
|
|
76
|
-
handler: Handler
|
|
105
|
+
handler: Handler<ExtractRouteParams<R>>;
|
|
77
106
|
methods: Array<HttpMethodsUnion>;
|
|
78
107
|
} : {
|
|
79
108
|
route: R;
|
|
80
109
|
options?: RouteOpts<C>;
|
|
81
|
-
handler: Handler
|
|
110
|
+
handler: Handler<ExtractRouteParams<R>>;
|
|
82
111
|
}): BareServer<any> & Routes;
|
|
83
112
|
}
|
|
84
113
|
export declare type RouteReport = {
|
|
@@ -90,24 +119,23 @@ export declare type Routes = {
|
|
|
90
119
|
[K in HttpMethodsUnion | 'declare']: HandlerExposed<K>;
|
|
91
120
|
};
|
|
92
121
|
export declare type BareHttpType<A extends IP = any> = BareServer<A> & Routes;
|
|
93
|
-
export declare type ServerMergedType = {
|
|
94
|
-
new <A extends IP>(args?: BareOptions<A>): BareHttpType<A>;
|
|
95
|
-
};
|
|
96
122
|
export declare class BareServer<A extends IP> {
|
|
97
123
|
#private;
|
|
98
124
|
private bareOptions;
|
|
99
125
|
server: Server;
|
|
100
126
|
ws?: WebSocketServer;
|
|
127
|
+
ajv?: Ajv;
|
|
128
|
+
route: Readonly<Routes>;
|
|
101
129
|
constructor(bareOptions?: BareOptions<A>);
|
|
102
|
-
private
|
|
130
|
+
private applyLaunchOptions;
|
|
103
131
|
private applyMiddlewares;
|
|
104
132
|
/**
|
|
105
133
|
* This handler is used in async generated middlewares runtime function
|
|
106
134
|
*/
|
|
107
135
|
private resolveMiddleware;
|
|
108
136
|
private setRoute;
|
|
109
|
-
private registerReport;
|
|
110
137
|
private handleRoute;
|
|
138
|
+
private resolveResponse;
|
|
111
139
|
private encodeRoute;
|
|
112
140
|
private explodeRoute;
|
|
113
141
|
private basicErrorHandler;
|
|
@@ -117,10 +145,11 @@ export declare class BareServer<A extends IP> {
|
|
|
117
145
|
get runtimeRoute(): Readonly<Routes>;
|
|
118
146
|
start(cb?: (address: string) => void): Promise<void>;
|
|
119
147
|
stop(cb?: (e?: Error) => void): Promise<void>;
|
|
148
|
+
loadRoutesSchemas(): void;
|
|
120
149
|
use(middleware: Middleware): this;
|
|
121
150
|
getMiddlewares(): Middleware[];
|
|
122
151
|
setCustomErrorHandler(eh: ErrorHandler): void;
|
|
152
|
+
getServerPort(): number;
|
|
123
153
|
getRoutes(): string[];
|
|
124
154
|
}
|
|
125
|
-
|
|
126
|
-
export { BareHttp };
|
|
155
|
+
export { BareServer as BareHttp };
|