@flink-app/flink 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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/forked-compiler.d.ts +2 -0
- package/dist/cli/forked-compiler.js +33 -0
- package/dist/cli/generate-schemas.js +16 -20
- package/dist/cli/run.js +2 -2
- package/dist/src/FlinkApp.d.ts +1 -1
- package/dist/src/FlinkApp.js +54 -54
- 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/FlinkJob.d.ts +2 -2
- package/dist/src/FlinkRepo.js +1 -1
- package/dist/src/FlinkResponse.d.ts +2 -2
- package/dist/src/FsUtils.js +4 -4
- package/dist/src/TypeScriptCompiler.js +71 -74
- 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.d.ts +0 -10
- package/dist/src/utils.js +7 -69
- package/package.json +2 -2
- package/spec/mock-project/dist/src/handlers/GetCar.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCar2.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema2.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema3.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema2.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile.js +1 -1
- package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile2.js +1 -1
- package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler.js +1 -1
- package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler2.js +1 -1
- package/spec/mock-project/dist/src/handlers/PostCar.js +1 -1
- package/spec/mock-project/dist/src/handlers/PutCar.js +1 -1
- package/spec/utils.spec.ts +187 -225
- package/src/TypeScriptCompiler.ts +9 -9
- package/src/utils.ts +46 -119
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Request } from "express";
|
|
2
|
-
import { JSONSchema7, JSONSchema7Definition } from "json-schema";
|
|
3
2
|
import { join, sep } from "path";
|
|
4
3
|
import tinyGlob from "tiny-glob";
|
|
5
4
|
import { HttpMethod } from "./FlinkHttpHandler";
|
|
@@ -7,60 +6,57 @@ import { log } from "./FlinkLog";
|
|
|
7
6
|
import { FlinkResponse } from "./FlinkResponse";
|
|
8
7
|
|
|
9
8
|
export function handlersPath(appRoot: string) {
|
|
10
|
-
|
|
9
|
+
return join(appRoot, "src", "handlers");
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
export function schemasPath(appRoot: string) {
|
|
14
|
-
|
|
13
|
+
return join(appRoot, "src", "schemas");
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
export function isRouteMatch(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const samePath = req.route.path === path;
|
|
24
|
-
return sameMethod && samePath;
|
|
25
|
-
});
|
|
16
|
+
export function isRouteMatch(req: Request, routes: { method: HttpMethod; path: string }[]) {
|
|
17
|
+
const match = routes.find(({ method, path }) => {
|
|
18
|
+
const sameMethod = req.method.toLowerCase() === method;
|
|
19
|
+
const samePath = req.route.path === path;
|
|
20
|
+
return sameMethod && samePath;
|
|
21
|
+
});
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
return !!match;
|
|
28
24
|
}
|
|
29
25
|
|
|
30
26
|
export function isError(message: FlinkResponse) {
|
|
31
|
-
|
|
27
|
+
return message.status && message.status > 399;
|
|
32
28
|
}
|
|
33
29
|
|
|
34
30
|
export async function getHandlerFiles(appRoot: string) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
try {
|
|
32
|
+
return await tinyGlob(`**/*.ts`, {
|
|
33
|
+
cwd: handlersPath(appRoot),
|
|
34
|
+
absolute: true,
|
|
35
|
+
});
|
|
36
|
+
} catch (err) {
|
|
37
|
+
log.debug(`Failed getting handler files: ${err}`);
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
export async function getSchemaFiles(appRoot: string) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
43
|
+
try {
|
|
44
|
+
return await tinyGlob(`**/*.ts`, {
|
|
45
|
+
cwd: schemasPath(appRoot),
|
|
46
|
+
absolute: true,
|
|
47
|
+
});
|
|
48
|
+
} catch (err) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
55
51
|
}
|
|
56
52
|
|
|
57
53
|
export function getCollectionNameForRepo(repoFilename: string) {
|
|
58
|
-
|
|
54
|
+
return repoFilename.replace("Repo.ts", "").toLowerCase();
|
|
59
55
|
}
|
|
60
56
|
|
|
61
57
|
export function getRepoInstanceName(fn: string) {
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
const [name] = fn.split(".ts");
|
|
59
|
+
return name.charAt(0).toLowerCase() + name.substr(1);
|
|
64
60
|
}
|
|
65
61
|
|
|
66
62
|
/**
|
|
@@ -68,96 +64,27 @@ export function getRepoInstanceName(fn: string) {
|
|
|
68
64
|
* if it starts with i.e "GetFoo"
|
|
69
65
|
*/
|
|
70
66
|
export function getHttpMethodFromHandlerName(handlerFilename: string) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
handlerFilename = handlerFilename.toLocaleLowerCase();
|
|
77
|
-
|
|
78
|
-
if (handlerFilename.startsWith(HttpMethod.get)) return HttpMethod.get;
|
|
79
|
-
if (handlerFilename.startsWith(HttpMethod.post)) return HttpMethod.post;
|
|
80
|
-
if (handlerFilename.startsWith(HttpMethod.put)) return HttpMethod.put;
|
|
81
|
-
if (handlerFilename.startsWith(HttpMethod.delete)) return HttpMethod.delete;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Recursively iterates thru json schema properties and replaces any $ref
|
|
86
|
-
* with the actual definiton if it exists withing provided `jsonSchemas`.
|
|
87
|
-
*
|
|
88
|
-
* @param schemaToDeRef
|
|
89
|
-
* @param jsonSchemas
|
|
90
|
-
* @returns
|
|
91
|
-
*/
|
|
92
|
-
export function deRefSchema(
|
|
93
|
-
schemaToDeRef: JSONSchema7Definition,
|
|
94
|
-
jsonSchemas: JSONSchema7
|
|
95
|
-
) {
|
|
96
|
-
if (typeof schemaToDeRef === "boolean") {
|
|
97
|
-
return schemaToDeRef;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (schemaToDeRef.type === "array") {
|
|
101
|
-
const items = schemaToDeRef.items as JSONSchema7;
|
|
102
|
-
|
|
103
|
-
if (items.$ref) {
|
|
104
|
-
const [_0, _1, defKey] = items.$ref.split("/");
|
|
105
|
-
const refedSchema = (jsonSchemas.definitions || {})[defKey];
|
|
106
|
-
|
|
107
|
-
if (refedSchema) {
|
|
108
|
-
schemaToDeRef.items = deRefSchema(refedSchema, jsonSchemas);
|
|
109
|
-
} else {
|
|
110
|
-
console.warn(`Failed to find deref ${schemaToDeRef.$ref}`);
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
schemaToDeRef.items = deRefSchema(
|
|
114
|
-
schemaToDeRef.items as JSONSchema7,
|
|
115
|
-
jsonSchemas
|
|
116
|
-
);
|
|
67
|
+
if (handlerFilename.includes(sep)) {
|
|
68
|
+
const split = handlerFilename.split(sep);
|
|
69
|
+
handlerFilename = split[split.length - 1];
|
|
117
70
|
}
|
|
118
|
-
} else if (schemaToDeRef.properties) {
|
|
119
|
-
for (const k in schemaToDeRef.properties) {
|
|
120
|
-
let prop = schemaToDeRef.properties[k];
|
|
121
71
|
|
|
122
|
-
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
72
|
+
handlerFilename = handlerFilename.toLocaleLowerCase();
|
|
125
73
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
schemaToDeRef.properties[k] = deRefSchema(refedSchema, jsonSchemas);
|
|
131
|
-
} else {
|
|
132
|
-
console.warn(`Failed to find deref ${prop.$ref}`);
|
|
133
|
-
}
|
|
134
|
-
} else if (prop.type === "array" && (prop.items as JSONSchema7).$ref) {
|
|
135
|
-
const [_0, _1, defKey] = (prop.items as JSONSchema7).$ref!.split("/");
|
|
136
|
-
const refedSchema = (jsonSchemas.definitions || {})[defKey];
|
|
137
|
-
if (refedSchema) {
|
|
138
|
-
(schemaToDeRef.properties[k] as JSONSchema7).items = deRefSchema(
|
|
139
|
-
refedSchema,
|
|
140
|
-
jsonSchemas
|
|
141
|
-
);
|
|
142
|
-
} else {
|
|
143
|
-
console.warn(`Failed to find deref ${prop.$ref}`);
|
|
144
|
-
}
|
|
145
|
-
} else if (prop.type === "object" || prop.type === "array") {
|
|
146
|
-
schemaToDeRef.properties[k] = deRefSchema(prop, jsonSchemas);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return schemaToDeRef;
|
|
74
|
+
if (handlerFilename.startsWith(HttpMethod.get)) return HttpMethod.get;
|
|
75
|
+
if (handlerFilename.startsWith(HttpMethod.post)) return HttpMethod.post;
|
|
76
|
+
if (handlerFilename.startsWith(HttpMethod.put)) return HttpMethod.put;
|
|
77
|
+
if (handlerFilename.startsWith(HttpMethod.delete)) return HttpMethod.delete;
|
|
151
78
|
}
|
|
152
79
|
|
|
153
80
|
export function getJsDocComment(comment: string) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
81
|
+
const rows = comment.split("\n").map((line) => {
|
|
82
|
+
line = line.trim();
|
|
83
|
+
if (line.startsWith("//")) {
|
|
84
|
+
return line.replace("//", line).trim();
|
|
85
|
+
}
|
|
86
|
+
return line.replace(/\/\*\*|\*\/|\*/, "").trim();
|
|
87
|
+
});
|
|
161
88
|
|
|
162
|
-
|
|
89
|
+
return rows.join("\n").trim();
|
|
163
90
|
}
|