@flink-app/flink 0.3.11 → 0.3.12
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/bin/flink.js +2 -2
- package/dist/cli/build.js +3 -3
- package/dist/cli/clean.js +2 -2
- package/dist/cli/cli-utils.js +1 -1
- package/dist/cli/generate-schema.d.ts +2 -0
- package/{spec/mock-project/dist/src/handlers/PostLogin.js → dist/cli/generate-schema.js} +14 -18
- package/dist/cli/generate-schemas.js +16 -20
- package/dist/cli/generate.d.ts +2 -0
- package/{spec/mock-project/dist/src/handlers/GetCarWithOmitSchema.js → dist/cli/generate.js} +14 -19
- package/dist/cli/run.js +2 -2
- package/dist/src/FlinkApp.d.ts +7 -1
- package/dist/src/FlinkApp.js +46 -45
- package/dist/src/FlinkErrors.d.ts +1 -1
- package/dist/src/FlinkErrors.js +5 -5
- package/dist/src/FlinkHttpHandler.d.ts +7 -7
- package/dist/src/FlinkLog.js +5 -5
- package/dist/src/FlinkRepo.js +1 -1
- package/dist/src/FlinkResponse.d.ts +2 -2
- package/dist/src/FlinkTsParser.d.ts +2 -0
- package/dist/src/FlinkTsParser.js +219 -0
- package/dist/src/FlinkTsUtils.d.ts +16 -0
- package/dist/src/FlinkTsUtils.js +118 -0
- package/dist/src/FsUtils.js +4 -4
- package/dist/src/TypeScriptCompiler.js +63 -67
- package/dist/src/TypeScriptUtils.js +1 -1
- package/dist/src/index.js +1 -5
- package/dist/src/mock-data-generator.js +1 -1
- package/dist/src/utils.js +9 -9
- package/package.json +2 -2
- package/src/FlinkApp.ts +12 -2
- package/.vscode/notes.txt +0 -11
- package/spec/mock-project/dist/src/handlers/GetCar.js +0 -59
- package/spec/mock-project/dist/src/handlers/GetCar2.js +0 -61
- package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema.js +0 -55
- package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema2.js +0 -55
- package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema3.js +0 -55
- package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema.js +0 -57
- package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema2.js +0 -57
- package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile.js +0 -60
- package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile2.js +0 -60
- package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler.js +0 -56
- package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler2.js +0 -58
- package/spec/mock-project/dist/src/handlers/PostCar.js +0 -57
- package/spec/mock-project/dist/src/handlers/PutCar.js +0 -57
- package/spec/mock-project/dist/src/index.js +0 -79
- package/spec/mock-project/dist/src/repos/CarRepo.js +0 -26
- package/spec/mock-project/dist/src/schemas/Car.js +0 -2
- package/spec/mock-project/dist/src/schemas/DefaultExportSchema.js +0 -2
- package/spec/mock-project/dist/src/schemas/FileWithTwoSchemas.js +0 -2
package/dist/bin/flink.js
CHANGED
|
@@ -12,11 +12,11 @@ var commands = [
|
|
|
12
12
|
var argv = process.argv.slice(2);
|
|
13
13
|
var argCommand = argv[0];
|
|
14
14
|
if (!argCommand || argv[0] === "help") {
|
|
15
|
-
console.log("Usage: flink ["
|
|
15
|
+
console.log("Usage: flink [" + commands.join("|") + "]");
|
|
16
16
|
process.exit();
|
|
17
17
|
}
|
|
18
18
|
if (!commands.includes(argv[0])) {
|
|
19
|
-
console.log("Invalid command: "
|
|
19
|
+
console.log("Invalid command: " + argCommand);
|
|
20
20
|
process.exit(1);
|
|
21
21
|
}
|
|
22
22
|
if (argCommand === "generate") {
|
package/dist/cli/build.js
CHANGED
|
@@ -15,7 +15,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
15
15
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
16
|
function step(op) {
|
|
17
17
|
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
-
while (
|
|
18
|
+
while (_) try {
|
|
19
19
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
20
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
21
|
switch (op[0]) {
|
|
@@ -57,7 +57,7 @@ module.exports = function run(args) {
|
|
|
57
57
|
if (args[0] && !args[0].startsWith("--")) {
|
|
58
58
|
dir = args[0];
|
|
59
59
|
}
|
|
60
|
-
exclude =
|
|
60
|
+
exclude = cli_utils_1.getOption(args, "exclude", "/spec");
|
|
61
61
|
return [4 /*yield*/, TypeScriptCompiler_1.default.clean(dir)];
|
|
62
62
|
case 1:
|
|
63
63
|
_a.sent();
|
|
@@ -72,7 +72,7 @@ module.exports = function run(args) {
|
|
|
72
72
|
])];
|
|
73
73
|
case 2:
|
|
74
74
|
_a.sent();
|
|
75
|
-
console.log("Compilation done, took "
|
|
75
|
+
console.log("Compilation done, took " + (Date.now() - startTime) + "ms");
|
|
76
76
|
compiler.emit();
|
|
77
77
|
return [2 /*return*/];
|
|
78
78
|
}
|
package/dist/cli/clean.js
CHANGED
|
@@ -15,7 +15,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
15
15
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
16
|
function step(op) {
|
|
17
17
|
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
-
while (
|
|
18
|
+
while (_) try {
|
|
19
19
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
20
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
21
|
switch (op[0]) {
|
|
@@ -58,7 +58,7 @@ module.exports = function run(args) {
|
|
|
58
58
|
return [4 /*yield*/, TypeScriptCompiler_1.default.clean(dir)];
|
|
59
59
|
case 1:
|
|
60
60
|
cleanedFolder = _a.sent();
|
|
61
|
-
console.log("Cleaned directory "
|
|
61
|
+
console.log("Cleaned directory " + cleanedFolder);
|
|
62
62
|
return [2 /*return*/];
|
|
63
63
|
}
|
|
64
64
|
});
|
package/dist/cli/cli-utils.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getOption = void 0;
|
|
4
4
|
function getOption(args, name, defaultValue, opts) {
|
|
5
5
|
if (opts === void 0) { opts = {}; }
|
|
6
|
-
var option = "--"
|
|
6
|
+
var option = "--" + name;
|
|
7
7
|
if (args.includes(option)) {
|
|
8
8
|
if (opts.isBoolean && args[args.indexOf(option) + 1] !== "false") {
|
|
9
9
|
return true;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
"use strict";
|
|
2
3
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
4
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -36,23 +37,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
37
|
}
|
|
37
38
|
};
|
|
38
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"x-test": "hello world",
|
|
51
|
-
},
|
|
52
|
-
}];
|
|
40
|
+
exports.run = void 0;
|
|
41
|
+
var FlinkTsParser_1 = require("../src/FlinkTsParser");
|
|
42
|
+
function run() {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
44
|
+
return __generator(this, function (_a) {
|
|
45
|
+
switch (_a.label) {
|
|
46
|
+
case 0: return [4 /*yield*/, FlinkTsParser_1.parseAndWriteSchemas()];
|
|
47
|
+
case 1:
|
|
48
|
+
_a.sent();
|
|
49
|
+
return [2 /*return*/];
|
|
50
|
+
}
|
|
53
51
|
});
|
|
54
52
|
});
|
|
55
|
-
}
|
|
56
|
-
exports.
|
|
57
|
-
exports.__assumedHttpMethod = "post", exports.__file = "PostLogin.ts", exports.__query = [], exports.__params = [];
|
|
58
|
-
exports.__schemas = { reqSchema: undefined, resSchema: undefined };
|
|
53
|
+
}
|
|
54
|
+
exports.run = run;
|
|
@@ -26,7 +26,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
26
26
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
27
27
|
function step(op) {
|
|
28
28
|
if (f) throw new TypeError("Generator is already executing.");
|
|
29
|
-
while (
|
|
29
|
+
while (_) try {
|
|
30
30
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
31
31
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
32
32
|
switch (op[0]) {
|
|
@@ -47,14 +47,10 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
47
47
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
ar[i] = from[i];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
50
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
51
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
52
|
+
to[j] = from[i];
|
|
53
|
+
return to;
|
|
58
54
|
};
|
|
59
55
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
56
|
var path_1 = require("path");
|
|
@@ -76,19 +72,19 @@ module.exports = function run(args) {
|
|
|
76
72
|
if (args[0] && !args[0].startsWith("--")) {
|
|
77
73
|
dir = args[0];
|
|
78
74
|
}
|
|
79
|
-
verbose =
|
|
75
|
+
verbose = cli_utils_1.getOption(args, "verbose", false, {
|
|
80
76
|
isBoolean: true,
|
|
81
77
|
});
|
|
82
|
-
typesDir =
|
|
83
|
-
outFile =
|
|
78
|
+
typesDir = cli_utils_1.getOption(args, "types-dir", "./src/schemas");
|
|
79
|
+
outFile = cli_utils_1.getOption(args, "out-file", "./.flink/generated-schemas.json");
|
|
84
80
|
project = new ts_morph_1.Project({
|
|
85
|
-
tsConfigFilePath:
|
|
81
|
+
tsConfigFilePath: path_1.join(dir, "tsconfig.json"),
|
|
86
82
|
skipAddingFilesFromTsConfig: true,
|
|
87
83
|
compilerOptions: {
|
|
88
84
|
noEmit: true,
|
|
89
85
|
},
|
|
90
86
|
});
|
|
91
|
-
project.addSourceFilesAtPaths(
|
|
87
|
+
project.addSourceFilesAtPaths(path_1.join(dir, typesDir, "**/*.ts"));
|
|
92
88
|
console.log("Found", project.getSourceFiles().length, "files");
|
|
93
89
|
schemaDeclarations = [];
|
|
94
90
|
generator = initJsonSchemaGenerator(project);
|
|
@@ -96,11 +92,11 @@ module.exports = function run(args) {
|
|
|
96
92
|
for (_i = 0, _a = project.getSourceFiles(); _i < _a.length; _i++) {
|
|
97
93
|
sf = _a[_i];
|
|
98
94
|
if (sf.getDefaultExportSymbol()) {
|
|
99
|
-
console.warn("WARN: Schema file "
|
|
95
|
+
console.warn("WARN: Schema file " + sf.getBaseName() + " has default export, but only named exports are picked up by json schemas parser");
|
|
100
96
|
}
|
|
101
97
|
sourceFileInterfaceDeclarations = sf.getChildrenOfKind(ts_morph_1.SyntaxKind.InterfaceDeclaration);
|
|
102
98
|
sourceFileEnumDeclarations = sf.getChildrenOfKind(ts_morph_1.SyntaxKind.EnumDeclaration);
|
|
103
|
-
sourceFileDeclarations = __spreadArray(__spreadArray([], sourceFileEnumDeclarations
|
|
99
|
+
sourceFileDeclarations = __spreadArray(__spreadArray([], sourceFileEnumDeclarations), sourceFileInterfaceDeclarations);
|
|
104
100
|
schemaDeclarations.push.apply(schemaDeclarations, sourceFileDeclarations.map(function (d) { return d.compilerNode; }));
|
|
105
101
|
verbose &&
|
|
106
102
|
console.log("Found", sourceFileDeclarations.length, "schema(s) in file", sf.getBaseName());
|
|
@@ -124,8 +120,8 @@ module.exports = function run(args) {
|
|
|
124
120
|
$ref: "#/definitions/Schemas",
|
|
125
121
|
definitions: {},
|
|
126
122
|
});
|
|
127
|
-
file =
|
|
128
|
-
return [4 /*yield*/,
|
|
123
|
+
file = path_1.join(dir, outFile);
|
|
124
|
+
return [4 /*yield*/, FsUtils_1.writeJsonFile(file, mergedSchemas, {
|
|
129
125
|
ensureDir: true,
|
|
130
126
|
})];
|
|
131
127
|
case 1:
|
|
@@ -137,8 +133,8 @@ module.exports = function run(args) {
|
|
|
137
133
|
});
|
|
138
134
|
};
|
|
139
135
|
function initJsonSchemaGenerator(project) {
|
|
140
|
-
var formatter =
|
|
141
|
-
var parser =
|
|
136
|
+
var formatter = ts_json_schema_generator_1.createFormatter({});
|
|
137
|
+
var parser = ts_json_schema_generator_1.createParser(project.getProgram().compilerObject, {});
|
|
142
138
|
var generator = new ts_json_schema_generator_1.SchemaGenerator(project.getProgram().compilerObject, parser, formatter, { expose: "export" });
|
|
143
139
|
return generator;
|
|
144
140
|
}
|
package/{spec/mock-project/dist/src/handlers/GetCarWithOmitSchema.js → dist/cli/generate.js}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
"use strict";
|
|
2
3
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
4
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -36,24 +37,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
37
|
}
|
|
37
38
|
};
|
|
38
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
model: "Volvo",
|
|
51
|
-
foo: 1,
|
|
52
|
-
},
|
|
53
|
-
}];
|
|
40
|
+
exports.run = void 0;
|
|
41
|
+
var FlinkTsParser_1 = require("../src/FlinkTsParser");
|
|
42
|
+
function run() {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
44
|
+
return __generator(this, function (_a) {
|
|
45
|
+
switch (_a.label) {
|
|
46
|
+
case 0: return [4 /*yield*/, FlinkTsParser_1.parseSourceFiles()];
|
|
47
|
+
case 1:
|
|
48
|
+
_a.sent();
|
|
49
|
+
return [2 /*return*/];
|
|
50
|
+
}
|
|
54
51
|
});
|
|
55
52
|
});
|
|
56
|
-
}
|
|
57
|
-
exports.
|
|
58
|
-
exports.__assumedHttpMethod = "get", exports.__file = "GetCarWithOmitSchema.ts", exports.__query = [], exports.__params = [];
|
|
59
|
-
exports.__schemas = { reqSchema: undefined, resSchema: undefined };
|
|
53
|
+
}
|
|
54
|
+
exports.run = run;
|
package/dist/cli/run.js
CHANGED
|
@@ -15,7 +15,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
15
15
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
16
|
function step(op) {
|
|
17
17
|
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
-
while (
|
|
18
|
+
while (_) try {
|
|
19
19
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
20
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
21
|
switch (op[0]) {
|
|
@@ -75,7 +75,7 @@ module.exports = function run(args) {
|
|
|
75
75
|
])];
|
|
76
76
|
case 2:
|
|
77
77
|
_a.sent();
|
|
78
|
-
console.log("Compilation done, took "
|
|
78
|
+
console.log("Compilation done, took " + (Date.now() - startTime) + "ms");
|
|
79
79
|
compiler.emit();
|
|
80
80
|
require("child_process").fork(dir + "/dist/.flink/start.js");
|
|
81
81
|
return [2 /*return*/];
|
package/dist/src/FlinkApp.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OptionsJson } from "body-parser";
|
|
1
2
|
import { Express } from "express";
|
|
2
3
|
import { JSONSchema7 } from "json-schema";
|
|
3
4
|
import { Db } from "mongodb";
|
|
@@ -6,7 +7,7 @@ import { FlinkContext } from "./FlinkContext";
|
|
|
6
7
|
import { HandlerFile, HttpMethod, QueryParamMetadata, RouteProps } from "./FlinkHttpHandler";
|
|
7
8
|
import { FlinkPlugin } from "./FlinkPlugin";
|
|
8
9
|
import { FlinkRepo } from "./FlinkRepo";
|
|
9
|
-
export type JSONSchema = JSONSchema7;
|
|
10
|
+
export declare type JSONSchema = JSONSchema7;
|
|
10
11
|
/**
|
|
11
12
|
* This will be populated at compile time when the apps handlers
|
|
12
13
|
* are picked up by typescript compiler
|
|
@@ -88,6 +89,10 @@ export interface FlinkOptions {
|
|
|
88
89
|
* Optional root folder of app. Defaults to `./`
|
|
89
90
|
*/
|
|
90
91
|
appRoot?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Options for json body parser
|
|
94
|
+
*/
|
|
95
|
+
jsonOptions?: OptionsJson;
|
|
91
96
|
}
|
|
92
97
|
export interface HandlerConfig {
|
|
93
98
|
schema?: {
|
|
@@ -124,6 +129,7 @@ export declare class FlinkApp<C extends FlinkContext> {
|
|
|
124
129
|
private auth?;
|
|
125
130
|
private corsOpts;
|
|
126
131
|
private routingConfigured;
|
|
132
|
+
private jsonOptions?;
|
|
127
133
|
private repos;
|
|
128
134
|
/**
|
|
129
135
|
* Internal cache used to track registered handlers and potentially any overlapping routes
|
package/dist/src/FlinkApp.js
CHANGED
|
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
25
25
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
26
|
function step(op) {
|
|
27
27
|
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (
|
|
28
|
+
while (_) try {
|
|
29
29
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
30
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
31
|
switch (op[0]) {
|
|
@@ -63,7 +63,7 @@ var FlinkErrors_1 = require("./FlinkErrors");
|
|
|
63
63
|
var mock_data_generator_1 = __importDefault(require("./mock-data-generator"));
|
|
64
64
|
var utils_1 = require("./utils");
|
|
65
65
|
var ajv = new ajv_1.default();
|
|
66
|
-
|
|
66
|
+
ajv_formats_1.default(ajv);
|
|
67
67
|
var defaultCorsOptions = {
|
|
68
68
|
allowedHeaders: "",
|
|
69
69
|
credentials: true,
|
|
@@ -99,6 +99,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
99
99
|
this.plugins = opts.plugins || [];
|
|
100
100
|
this.corsOpts = __assign(__assign({}, defaultCorsOptions), opts.cors);
|
|
101
101
|
this.auth = opts.auth;
|
|
102
|
+
this.jsonOptions = opts.jsonOptions || { limit: "1mb" };
|
|
102
103
|
}
|
|
103
104
|
Object.defineProperty(FlinkApp.prototype, "ctx", {
|
|
104
105
|
get: function () {
|
|
@@ -125,24 +126,24 @@ var FlinkApp = /** @class */ (function () {
|
|
|
125
126
|
_c.sent();
|
|
126
127
|
if (this.debug) {
|
|
127
128
|
offsetTime = Date.now();
|
|
128
|
-
node_color_log_1.default.bgColorLog("cyan", "Init db took "
|
|
129
|
+
node_color_log_1.default.bgColorLog("cyan", "Init db took " + (offsetTime - startTime) + " ms");
|
|
129
130
|
}
|
|
130
131
|
return [4 /*yield*/, this.buildContext()];
|
|
131
132
|
case 2:
|
|
132
133
|
_c.sent();
|
|
133
134
|
if (this.debug) {
|
|
134
|
-
node_color_log_1.default.bgColorLog("cyan", "Build context took "
|
|
135
|
+
node_color_log_1.default.bgColorLog("cyan", "Build context took " + (Date.now() - offsetTime) + " ms");
|
|
135
136
|
offsetTime = Date.now();
|
|
136
137
|
}
|
|
137
138
|
if (this.debug) {
|
|
138
|
-
node_color_log_1.default.bgColorLog("cyan", "Registered JSON schemas took "
|
|
139
|
+
node_color_log_1.default.bgColorLog("cyan", "Registered JSON schemas took " + (Date.now() - offsetTime) + " ms");
|
|
139
140
|
offsetTime = Date.now();
|
|
140
141
|
}
|
|
141
|
-
this.expressApp =
|
|
142
|
-
this.expressApp.use(
|
|
143
|
-
this.expressApp.use(body_parser_1.default.json());
|
|
142
|
+
this.expressApp = express_1.default();
|
|
143
|
+
this.expressApp.use(cors_1.default(this.corsOpts));
|
|
144
|
+
this.expressApp.use(body_parser_1.default.json(this.jsonOptions));
|
|
144
145
|
this.expressApp.use(function (req, res, next) {
|
|
145
|
-
req.reqId =
|
|
146
|
+
req.reqId = uuid_1.v4();
|
|
146
147
|
next();
|
|
147
148
|
});
|
|
148
149
|
_i = 0, _b = this.plugins;
|
|
@@ -163,7 +164,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
163
164
|
_c.sent();
|
|
164
165
|
_c.label = 7;
|
|
165
166
|
case 7:
|
|
166
|
-
node_color_log_1.default.info("Initialized plugin '"
|
|
167
|
+
node_color_log_1.default.info("Initialized plugin '" + plugin.id + "'");
|
|
167
168
|
_c.label = 8;
|
|
168
169
|
case 8:
|
|
169
170
|
_i++;
|
|
@@ -172,19 +173,19 @@ var FlinkApp = /** @class */ (function () {
|
|
|
172
173
|
case 10:
|
|
173
174
|
_c.sent();
|
|
174
175
|
if (this.debug) {
|
|
175
|
-
node_color_log_1.default.bgColorLog("cyan", "Register handlers took "
|
|
176
|
+
node_color_log_1.default.bgColorLog("cyan", "Register handlers took " + (Date.now() - offsetTime) + " ms");
|
|
176
177
|
offsetTime = Date.now();
|
|
177
178
|
}
|
|
178
179
|
// Register 404 with slight delay to allow all manually added routes to be added
|
|
179
180
|
// TODO: Is there a better solution to force this handler to always run last?
|
|
180
181
|
setTimeout(function () {
|
|
181
182
|
_this.expressApp.use(function (req, res, next) {
|
|
182
|
-
res.status(404).json(
|
|
183
|
+
res.status(404).json(FlinkErrors_1.notFound());
|
|
183
184
|
});
|
|
184
185
|
_this.routingConfigured = true;
|
|
185
186
|
});
|
|
186
187
|
(_a = this.expressApp) === null || _a === void 0 ? void 0 : _a.listen(this.port, function () {
|
|
187
|
-
node_color_log_1.default.fontColorLog("magenta", "\u26A1\uFE0F HTTP server '"
|
|
188
|
+
node_color_log_1.default.fontColorLog("magenta", "\u26A1\uFE0F HTTP server '" + _this.name + "' is running and waiting for connections on " + _this.port);
|
|
188
189
|
_this.started = true;
|
|
189
190
|
});
|
|
190
191
|
return [2 /*return*/, this];
|
|
@@ -205,18 +206,18 @@ var FlinkApp = /** @class */ (function () {
|
|
|
205
206
|
}
|
|
206
207
|
var routeProps = __assign(__assign({}, (handler.Route || {})), routePropsOverride);
|
|
207
208
|
if (!routeProps.method) {
|
|
208
|
-
node_color_log_1.default.error("Failed to register handler '"
|
|
209
|
+
node_color_log_1.default.error("Failed to register handler '" + handler.__file + "': Missing 'method' in route props, either set it or name handler file with HTTP method as prefix");
|
|
209
210
|
return;
|
|
210
211
|
}
|
|
211
212
|
if (!routeProps.path) {
|
|
212
|
-
node_color_log_1.default.error("Failed to register handler '"
|
|
213
|
+
node_color_log_1.default.error("Failed to register handler '" + handler.__file + "': Missing 'path' in route props");
|
|
213
214
|
return;
|
|
214
215
|
}
|
|
215
216
|
var dup = this.handlers.find(function (h) { return h.routeProps.path === routeProps.path && h.routeProps.method === routeProps.method; });
|
|
216
|
-
var methodAndPath =
|
|
217
|
+
var methodAndPath = routeProps.method.toUpperCase() + " " + routeProps.path;
|
|
217
218
|
if (dup) {
|
|
218
219
|
// TODO: Not sure if there is a case where you'd want to overwrite a route?
|
|
219
|
-
node_color_log_1.default.warn(
|
|
220
|
+
node_color_log_1.default.warn(methodAndPath + " overlaps existing route");
|
|
220
221
|
}
|
|
221
222
|
var handlerConfig = {
|
|
222
223
|
routeProps: __assign(__assign({}, routeProps), { method: routeProps.method, path: routeProps.path }),
|
|
@@ -228,10 +229,10 @@ var FlinkApp = /** @class */ (function () {
|
|
|
228
229
|
paramsMetadata: handler.__params || [],
|
|
229
230
|
};
|
|
230
231
|
if (((_c = handler.__schemas) === null || _c === void 0 ? void 0 : _c.reqSchema) && !((_d = handlerConfig.schema) === null || _d === void 0 ? void 0 : _d.reqSchema)) {
|
|
231
|
-
node_color_log_1.default.warn("Expected request schema "
|
|
232
|
+
node_color_log_1.default.warn("Expected request schema " + handler.__schemas.reqSchema + " for handler " + methodAndPath + " but no such schema was found");
|
|
232
233
|
}
|
|
233
234
|
if (((_e = handler.__schemas) === null || _e === void 0 ? void 0 : _e.resSchema) && !((_f = handlerConfig.schema) === null || _f === void 0 ? void 0 : _f.resSchema)) {
|
|
234
|
-
node_color_log_1.default.warn("Expected response schema "
|
|
235
|
+
node_color_log_1.default.warn("Expected response schema " + handler.__schemas.resSchema + " for handler " + methodAndPath + " but no such schema was found");
|
|
235
236
|
}
|
|
236
237
|
this.registerHandler(handlerConfig, handler.default);
|
|
237
238
|
};
|
|
@@ -242,10 +243,10 @@ var FlinkApp = /** @class */ (function () {
|
|
|
242
243
|
var method = routeProps.method;
|
|
243
244
|
var app = this.expressApp;
|
|
244
245
|
if (!method) {
|
|
245
|
-
node_color_log_1.default.error("Route "
|
|
246
|
+
node_color_log_1.default.error("Route " + routeProps.path + " is missing http method");
|
|
246
247
|
}
|
|
247
248
|
if (method) {
|
|
248
|
-
var methodAndRoute_1 =
|
|
249
|
+
var methodAndRoute_1 = method.toUpperCase() + " " + routeProps.path;
|
|
249
250
|
app[method](routeProps.path, function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
250
251
|
var validate, valid, data, handlerRes, err_1, validate, valid;
|
|
251
252
|
return __generator(this, function (_a) {
|
|
@@ -255,7 +256,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
255
256
|
return [4 /*yield*/, this.authenticate(req, routeProps.permissions)];
|
|
256
257
|
case 1:
|
|
257
258
|
if (!(_a.sent())) {
|
|
258
|
-
return [2 /*return*/, res.status(401).json(
|
|
259
|
+
return [2 /*return*/, res.status(401).json(FlinkErrors_1.unauthorized())];
|
|
259
260
|
}
|
|
260
261
|
_a.label = 2;
|
|
261
262
|
case 2:
|
|
@@ -263,21 +264,21 @@ var FlinkApp = /** @class */ (function () {
|
|
|
263
264
|
validate = ajv.compile(schema.reqSchema);
|
|
264
265
|
valid = validate(req.body);
|
|
265
266
|
if (!valid) {
|
|
266
|
-
node_color_log_1.default.warn(
|
|
267
|
-
node_color_log_1.default.debug("Invalid json: "
|
|
267
|
+
node_color_log_1.default.warn(methodAndRoute_1 + ": Bad request " + JSON.stringify(validate.errors, null, 2));
|
|
268
|
+
node_color_log_1.default.debug("Invalid json: " + JSON.stringify(req.body));
|
|
268
269
|
return [2 /*return*/, res.status(400).json({
|
|
269
270
|
status: 400,
|
|
270
271
|
error: {
|
|
271
|
-
id:
|
|
272
|
+
id: uuid_1.v4(),
|
|
272
273
|
title: "Bad request",
|
|
273
|
-
detail: "Schema did not validate "
|
|
274
|
+
detail: "Schema did not validate " + JSON.stringify(validate.errors),
|
|
274
275
|
},
|
|
275
276
|
})];
|
|
276
277
|
}
|
|
277
278
|
}
|
|
278
279
|
if (routeProps.mockApi && schema.resSchema) {
|
|
279
|
-
node_color_log_1.default.warn("Mock response for "
|
|
280
|
-
data =
|
|
280
|
+
node_color_log_1.default.warn("Mock response for " + req.method.toUpperCase() + " " + req.path);
|
|
281
|
+
data = mock_data_generator_1.default(schema.resSchema);
|
|
281
282
|
res.status(200).json({
|
|
282
283
|
status: 200,
|
|
283
284
|
data: data,
|
|
@@ -298,23 +299,23 @@ var FlinkApp = /** @class */ (function () {
|
|
|
298
299
|
return [3 /*break*/, 6];
|
|
299
300
|
case 5:
|
|
300
301
|
err_1 = _a.sent();
|
|
301
|
-
node_color_log_1.default.warn("Handler '"
|
|
302
|
+
node_color_log_1.default.warn("Handler '" + methodAndRoute_1 + "' threw unhandled exception " + err_1);
|
|
302
303
|
console.error(err_1);
|
|
303
|
-
return [2 /*return*/, res.status(500).json(
|
|
304
|
+
return [2 /*return*/, res.status(500).json(FlinkErrors_1.internalServerError(err_1))];
|
|
304
305
|
case 6:
|
|
305
|
-
if (schema.resSchema && !
|
|
306
|
+
if (schema.resSchema && !utils_1.isError(handlerRes)) {
|
|
306
307
|
validate = ajv.compile(schema.resSchema);
|
|
307
308
|
valid = validate(JSON.parse(JSON.stringify(handlerRes.data)));
|
|
308
309
|
if (!valid) {
|
|
309
|
-
node_color_log_1.default.warn("["
|
|
310
|
-
node_color_log_1.default.debug("Invalid json: "
|
|
310
|
+
node_color_log_1.default.warn("[" + req.reqId + "] " + methodAndRoute_1 + ": Bad response " + JSON.stringify(validate.errors, null, 2));
|
|
311
|
+
node_color_log_1.default.debug("Invalid json: " + JSON.stringify(handlerRes.data));
|
|
311
312
|
// log.debug(JSON.stringify(schema, null, 2));
|
|
312
313
|
return [2 /*return*/, res.status(500).json({
|
|
313
314
|
status: 500,
|
|
314
315
|
error: {
|
|
315
|
-
id:
|
|
316
|
+
id: uuid_1.v4(),
|
|
316
317
|
title: "Bad response",
|
|
317
|
-
detail: "Schema did not validate "
|
|
318
|
+
detail: "Schema did not validate " + JSON.stringify(validate.errors),
|
|
318
319
|
},
|
|
319
320
|
})];
|
|
320
321
|
}
|
|
@@ -326,12 +327,12 @@ var FlinkApp = /** @class */ (function () {
|
|
|
326
327
|
});
|
|
327
328
|
}); });
|
|
328
329
|
if (this.handlerRouteCache.has(methodAndRoute_1)) {
|
|
329
|
-
node_color_log_1.default.error("Cannot register handler "
|
|
330
|
+
node_color_log_1.default.error("Cannot register handler " + methodAndRoute_1 + " - route already registered");
|
|
330
331
|
return process.exit(1); // TODO: Do we need to exit?
|
|
331
332
|
}
|
|
332
333
|
else {
|
|
333
334
|
this.handlerRouteCache.set(methodAndRoute_1, JSON.stringify(routeProps));
|
|
334
|
-
node_color_log_1.default.info("Registered route "
|
|
335
|
+
node_color_log_1.default.info("Registered route " + methodAndRoute_1);
|
|
335
336
|
}
|
|
336
337
|
}
|
|
337
338
|
};
|
|
@@ -349,11 +350,11 @@ var FlinkApp = /** @class */ (function () {
|
|
|
349
350
|
for (_i = 0, autoRegisteredHandlers_1 = exports.autoRegisteredHandlers; _i < autoRegisteredHandlers_1.length; _i++) {
|
|
350
351
|
_c = autoRegisteredHandlers_1[_i], handler = _c.handler, assumedHttpMethod = _c.assumedHttpMethod;
|
|
351
352
|
if (!handler.Route) {
|
|
352
|
-
node_color_log_1.default.error("Missing Props in handler "
|
|
353
|
+
node_color_log_1.default.error("Missing Props in handler " + handler.__file);
|
|
353
354
|
continue;
|
|
354
355
|
}
|
|
355
356
|
if (!handler.default) {
|
|
356
|
-
node_color_log_1.default.error("Missing exported handler function in handler "
|
|
357
|
+
node_color_log_1.default.error("Missing exported handler function in handler " + handler.__file);
|
|
357
358
|
continue;
|
|
358
359
|
}
|
|
359
360
|
this.registerHandler({
|
|
@@ -388,7 +389,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
388
389
|
_a = autoRegisteredRepos_1[_i], collectionName = _a.collectionName, repoInstanceName = _a.repoInstanceName, Repo = _a.Repo;
|
|
389
390
|
repoInstance = new Repo(collectionName, this.db);
|
|
390
391
|
this.repos[repoInstanceName] = repoInstance;
|
|
391
|
-
node_color_log_1.default.info("Registered repo "
|
|
392
|
+
node_color_log_1.default.info("Registered repo " + repoInstanceName);
|
|
392
393
|
}
|
|
393
394
|
}
|
|
394
395
|
else if (exports.autoRegisteredRepos.length > 0) {
|
|
@@ -396,7 +397,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
396
397
|
}
|
|
397
398
|
pluginCtx = this.plugins.reduce(function (out, plugin) {
|
|
398
399
|
if (out[plugin.id]) {
|
|
399
|
-
throw new Error("Plugin "
|
|
400
|
+
throw new Error("Plugin " + plugin.id + " is already registered");
|
|
400
401
|
}
|
|
401
402
|
out[plugin.id] = plugin.ctx;
|
|
402
403
|
return out;
|
|
@@ -467,7 +468,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
467
468
|
if (!plugin.db) return [3 /*break*/, 5];
|
|
468
469
|
if (!plugin.db.useHostDb) return [3 /*break*/, 1];
|
|
469
470
|
if (!this.db) {
|
|
470
|
-
node_color_log_1.default.error("Plugin '"
|
|
471
|
+
node_color_log_1.default.error("Plugin '" + this.name + " configured to use host app db, but no db exists in FlinkApp'");
|
|
471
472
|
}
|
|
472
473
|
else {
|
|
473
474
|
return [2 /*return*/, this.db];
|
|
@@ -478,7 +479,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
478
479
|
_a.label = 2;
|
|
479
480
|
case 2:
|
|
480
481
|
_a.trys.push([2, 4, , 5]);
|
|
481
|
-
node_color_log_1.default.debug("Connecting to '"
|
|
482
|
+
node_color_log_1.default.debug("Connecting to '" + plugin.id + "' db");
|
|
482
483
|
return [4 /*yield*/, mongodb_1.default.connect(plugin.db.uri, {
|
|
483
484
|
useUnifiedTopology: true,
|
|
484
485
|
})];
|
|
@@ -487,7 +488,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
487
488
|
return [2 /*return*/, client.db()];
|
|
488
489
|
case 4:
|
|
489
490
|
err_3 = _a.sent();
|
|
490
|
-
node_color_log_1.default.error("Failed to connect to db defined in plugin '"
|
|
491
|
+
node_color_log_1.default.error("Failed to connect to db defined in plugin '" + plugin.id + "': " + err_3);
|
|
491
492
|
return [3 /*break*/, 5];
|
|
492
493
|
case 5: return [2 /*return*/];
|
|
493
494
|
}
|
|
@@ -500,7 +501,7 @@ var FlinkApp = /** @class */ (function () {
|
|
|
500
501
|
switch (_a.label) {
|
|
501
502
|
case 0:
|
|
502
503
|
if (!this.auth) {
|
|
503
|
-
throw new Error("Attempting to authenticate request ("
|
|
504
|
+
throw new Error("Attempting to authenticate request (" + req.method + " " + req.path + ") but no authPlugin is set");
|
|
504
505
|
}
|
|
505
506
|
return [4 /*yield*/, this.auth.authenticateRequest(req, permissions)];
|
|
506
507
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FlinkResponse } from "./FlinkResponse";
|
|
2
|
-
export type FlinkError = undefined;
|
|
2
|
+
export declare type FlinkError = undefined;
|
|
3
3
|
export declare function notFound(detail?: string, code?: string): FlinkResponse<FlinkError>;
|
|
4
4
|
export declare function conflict(detail?: string, code?: string): FlinkResponse<FlinkError>;
|
|
5
5
|
export declare function badRequest(detail?: string, code?: string): FlinkResponse<FlinkError>;
|