barehttp 0.4.0 → 0.5.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 +45 -12
- package/lib/context/execution.js +1 -1
- package/lib/logger/index.js +15 -29
- package/lib/logger/serializers.js +1 -1
- package/lib/middlewares/cookies/cookie-manager.js +1 -1
- package/lib/request.d.ts +6 -0
- package/lib/request.js +53 -36
- 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 +32 -12
- package/lib/server.js +107 -66
- package/lib/websocket.js +12 -10
- package/package.json +33 -27
- package/lib/report.d.ts +0 -2
- package/lib/report.js +0 -20
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const ts_morph_1 = require("ts-morph");
|
|
4
|
+
const custom_schema_1 = require("./custom-schema");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
|
+
const project = new ts_morph_1.Project({ tsConfigFilePath: 'tsconfig.json' });
|
|
7
|
+
project.enableLogging();
|
|
8
|
+
const sourceFile = project.getSourceFile('server.ts');
|
|
9
|
+
const tp = sourceFile?.getClass('BareServer')?.getMember('route');
|
|
10
|
+
const isHandler = (c) => c.getSymbol()?.getName() === 'handler';
|
|
11
|
+
const isRoute = (c) => c.getSymbol()?.getName() === 'route';
|
|
12
|
+
function returnFinder(route, base) {
|
|
13
|
+
if (!base) {
|
|
14
|
+
throw new Error('No project been allocated, theres some issue');
|
|
15
|
+
}
|
|
16
|
+
const refsAcrossProject = base
|
|
17
|
+
.getChildrenOfKind(ts_morph_1.ts.SyntaxKind.Identifier)[0]
|
|
18
|
+
.findReferences()[0]
|
|
19
|
+
.getReferences()
|
|
20
|
+
?.filter((re) => re.compilerObject.fileName.includes(route));
|
|
21
|
+
if (!refsAcrossProject?.length) {
|
|
22
|
+
console.log('There are no routes declarations across the project');
|
|
23
|
+
process.exit(0);
|
|
24
|
+
}
|
|
25
|
+
const extractedReturns = refsAcrossProject.map((ref) => {
|
|
26
|
+
return ref
|
|
27
|
+
.getNode()
|
|
28
|
+
.getAncestors()
|
|
29
|
+
.find((n) => n.getKind() === ts_morph_1.ts.SyntaxKind.CallExpression)
|
|
30
|
+
?.getChildren()
|
|
31
|
+
.find((n) => n.getKind() === ts_morph_1.ts.SyntaxKind.SyntaxList)
|
|
32
|
+
?.getFirstChild()
|
|
33
|
+
?.getChildSyntaxList()
|
|
34
|
+
?.getChildren()
|
|
35
|
+
.filter((c) => {
|
|
36
|
+
return c.getKind() === ts_morph_1.ts.SyntaxKind.PropertyAssignment && (isHandler(c) || isRoute(c));
|
|
37
|
+
})
|
|
38
|
+
.map((c) => {
|
|
39
|
+
if (isHandler(c)) {
|
|
40
|
+
return {
|
|
41
|
+
type: 'handler',
|
|
42
|
+
syntaxList: c
|
|
43
|
+
.getChildren()
|
|
44
|
+
.find((n) => n.getKind() === ts_morph_1.ts.SyntaxKind.ArrowFunction ||
|
|
45
|
+
n.getKind() === ts_morph_1.ts.SyntaxKind.FunctionExpression)
|
|
46
|
+
?.getLastChild()
|
|
47
|
+
?.getChildSyntaxList(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
type: 'route',
|
|
52
|
+
value: c
|
|
53
|
+
.getNodeProperty('initializer')
|
|
54
|
+
.getText()
|
|
55
|
+
?.replaceAll("'", ''),
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
const perRoute = extractedReturns
|
|
60
|
+
.map((routeCombination) => {
|
|
61
|
+
return routeCombination.reduce((acc, curr) => {
|
|
62
|
+
if (curr.type === 'handler') {
|
|
63
|
+
return {
|
|
64
|
+
...acc,
|
|
65
|
+
handler: curr.syntaxList,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
return {
|
|
70
|
+
...acc,
|
|
71
|
+
route: curr.value,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}, {});
|
|
75
|
+
})
|
|
76
|
+
.map((routeCombination) => ({
|
|
77
|
+
...routeCombination,
|
|
78
|
+
handler: getReturnStatements(routeCombination.handler),
|
|
79
|
+
}));
|
|
80
|
+
const schemas = perRoute.map(({ handler, route }) => {
|
|
81
|
+
const schemas = handler.map((t) => (0, custom_schema_1.generateCustomSchema)(t));
|
|
82
|
+
let finalSchema = schemas[0];
|
|
83
|
+
if (schemas.length > 1) {
|
|
84
|
+
finalSchema = {
|
|
85
|
+
type: 'union',
|
|
86
|
+
nullable: false,
|
|
87
|
+
anyOf: schemas,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
route,
|
|
92
|
+
schemas,
|
|
93
|
+
finalSchema,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
return schemas;
|
|
97
|
+
}
|
|
98
|
+
// const res = tp
|
|
99
|
+
// ?.getChildrenOfKind(ts.SyntaxKind.Identifier)[0]
|
|
100
|
+
// .findReferences()[0]
|
|
101
|
+
// .getReferences()
|
|
102
|
+
// .filter((ref) => !ref.compilerObject.fileName.includes('server.ts'))[0]
|
|
103
|
+
// .getNode()
|
|
104
|
+
// .getAncestors()
|
|
105
|
+
// .filter((n) => n.getKind() === ts.SyntaxKind.CallExpression)[0]
|
|
106
|
+
// .getAncestors()[0]
|
|
107
|
+
// .getChildrenOfKind(ts.SyntaxKind.CallExpression)[0]
|
|
108
|
+
// .getChildrenOfKind(ts.SyntaxKind.SyntaxList)[0]
|
|
109
|
+
// .getChildren()[0]
|
|
110
|
+
// .getChildrenOfKind(ts.SyntaxKind.SyntaxList)[0]
|
|
111
|
+
// .getChildrenOfKind(ts.SyntaxKind.PropertyAssignment)
|
|
112
|
+
// .find((node) =>
|
|
113
|
+
// node
|
|
114
|
+
// .getChildren()
|
|
115
|
+
// .find(
|
|
116
|
+
// (n) =>
|
|
117
|
+
// n.getKind() === ts.SyntaxKind.ArrowFunction ||
|
|
118
|
+
// n.getKind() === ts.SyntaxKind.FunctionExpression,
|
|
119
|
+
// ),
|
|
120
|
+
// )
|
|
121
|
+
// ?.getChildren()
|
|
122
|
+
// ?.find((c) => c.getKind() === ts.SyntaxKind.FunctionExpression)
|
|
123
|
+
// ?.getLastChild()
|
|
124
|
+
// ?.getChildSyntaxList()
|
|
125
|
+
// ?.getChildren()
|
|
126
|
+
// .filter((c) => c.getKind() === ts.SyntaxKind.IfStatement)[0]
|
|
127
|
+
// .getChildren()
|
|
128
|
+
// .find((c) => c.getKind() === ts.SyntaxKind.Block)
|
|
129
|
+
// ?.getChildSyntaxList();
|
|
130
|
+
const extractReturnStatements = (accumulator, n) => {
|
|
131
|
+
if (!n)
|
|
132
|
+
return;
|
|
133
|
+
if (ts_morph_1.ts.SyntaxKind.IfStatement === n.getKind()) {
|
|
134
|
+
const thenProp = n.getNodeProperty('thenStatement');
|
|
135
|
+
const elseProp = n.getNodeProperty('elseStatement');
|
|
136
|
+
const thenSyntax = thenProp?.getChildSyntaxList();
|
|
137
|
+
const elseSyntax = elseProp?.getChildSyntaxList();
|
|
138
|
+
extractReturnStatements(accumulator, thenSyntax);
|
|
139
|
+
extractReturnStatements(accumulator, elseSyntax);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (n.getChildren().length) {
|
|
143
|
+
const cleanChildren = n.getChildren().filter((c) => typeof c.getKind === 'function');
|
|
144
|
+
const findReturn = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.ReturnStatement);
|
|
145
|
+
const thereIf = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.IfStatement);
|
|
146
|
+
const thereWhile = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.WhileKeyword);
|
|
147
|
+
const thereFor = cleanChildren.find((c) => c.getKind() === ts_morph_1.ts.SyntaxKind.ForStatement);
|
|
148
|
+
const syntaxList = n.getChildSyntaxList();
|
|
149
|
+
if (findReturn) {
|
|
150
|
+
accumulator.push(findReturn);
|
|
151
|
+
}
|
|
152
|
+
extractReturnStatements(accumulator, thereIf);
|
|
153
|
+
extractReturnStatements(accumulator, thereWhile);
|
|
154
|
+
extractReturnStatements(accumulator, thereFor);
|
|
155
|
+
extractReturnStatements(accumulator, syntaxList);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const getReturnStatements = (n) => {
|
|
159
|
+
if (!n)
|
|
160
|
+
return [];
|
|
161
|
+
const accumulator = [];
|
|
162
|
+
extractReturnStatements(accumulator, n);
|
|
163
|
+
return accumulator
|
|
164
|
+
.map((r) => r.getChildren().find((c) => {
|
|
165
|
+
const type = c.getType();
|
|
166
|
+
return type.isObject() || (0, helpers_1.isFinalType)(type);
|
|
167
|
+
}))
|
|
168
|
+
.filter((n) => n)
|
|
169
|
+
.map((acc) => acc.getType());
|
|
170
|
+
// console.log({ accumulator });
|
|
171
|
+
// let baseChildren = n?.getChildren()?.filter((c) => typeof c.getKind === 'function') ?? [];
|
|
172
|
+
// baseChildren = baseChildren.flat(5).filter((c) => typeof c.getKind === 'function');
|
|
173
|
+
// if (thereIf || thereBlock || thereWhile || thereFor) {
|
|
174
|
+
// baseChildren?.push(
|
|
175
|
+
// getReturnStatements(thereIf) as any,
|
|
176
|
+
// getReturnStatements(thereWhile) as any,
|
|
177
|
+
// getReturnStatements(thereBlock) as any,
|
|
178
|
+
// getReturnStatements(thereFor) as any,
|
|
179
|
+
// );
|
|
180
|
+
// }
|
|
181
|
+
// baseChildren = baseChildren.flat(5).filter((c) => typeof c.getKind === 'function');
|
|
182
|
+
// return baseChildren
|
|
183
|
+
// .filter((c) => typeof c.getKind === 'function')
|
|
184
|
+
// .filter((c) => c.getKind() === ts.SyntaxKind.ReturnStatement)
|
|
185
|
+
// .map((r) =>
|
|
186
|
+
// r.getChildren().find((c) => {
|
|
187
|
+
// const type = c.getType();
|
|
188
|
+
// return type.isObject() || isFinalType(type);
|
|
189
|
+
// }),
|
|
190
|
+
// )
|
|
191
|
+
// .filter((v) => v)
|
|
192
|
+
// .map((v) => v!.getType());
|
|
193
|
+
};
|
|
194
|
+
// returnFinder('examples', tp);
|
|
195
|
+
(0, helpers_1.logInternals)(returnFinder('examples', tp));
|
|
196
|
+
// logInternals(returnFinder('examples', tp).map((s) => convertToJsonSchema(s)));
|
|
197
|
+
// console.log(tp);
|
|
198
|
+
// logInternals(getReturnStatements(res!)?.map((t) => regenerateTypeSchema(t!)));
|
|
199
|
+
// regenerateTypeSchema(res![0].getType());
|
|
200
|
+
// logInternals(regenerateTypeSchema(res![0].getType()));
|
|
201
|
+
// console.log(regenerateTypeSchema(res![0].getType()));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const generateRouteSchema: (fileRouteToDeclarations: string) => {
|
|
2
|
+
route: string;
|
|
3
|
+
methodName: "get" | "post" | "put" | "delete" | "patch" | "options" | "head";
|
|
4
|
+
schemas: import("./custom-schema").CustomSchema[];
|
|
5
|
+
finalSchema: import("./custom-schema").CustomSchema;
|
|
6
|
+
jsonSchema: any;
|
|
7
|
+
}[];
|
|
@@ -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";
|