@flink-app/flink 0.4.2 → 0.4.5

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/src/utils.ts CHANGED
@@ -7,60 +7,57 @@ import { log } from "./FlinkLog";
7
7
  import { FlinkResponse } from "./FlinkResponse";
8
8
 
9
9
  export function handlersPath(appRoot: string) {
10
- return join(appRoot, "src", "handlers");
10
+ return join(appRoot, "src", "handlers");
11
11
  }
12
12
 
13
13
  export function schemasPath(appRoot: string) {
14
- return join(appRoot, "src", "schemas");
14
+ return join(appRoot, "src", "schemas");
15
15
  }
16
16
 
17
- export function isRouteMatch(
18
- req: Request,
19
- routes: { method: HttpMethod; path: string }[]
20
- ) {
21
- const match = routes.find(({ method, path }) => {
22
- const sameMethod = req.method.toLowerCase() === method;
23
- const samePath = req.route.path === path;
24
- return sameMethod && samePath;
25
- });
26
-
27
- return !!match;
17
+ export function isRouteMatch(req: Request, routes: { method: HttpMethod; path: string }[]) {
18
+ const match = routes.find(({ method, path }) => {
19
+ const sameMethod = req.method.toLowerCase() === method;
20
+ const samePath = req.route.path === path;
21
+ return sameMethod && samePath;
22
+ });
23
+
24
+ return !!match;
28
25
  }
29
26
 
30
27
  export function isError(message: FlinkResponse) {
31
- return message.status && message.status > 399;
28
+ return message.status && message.status > 399;
32
29
  }
33
30
 
34
31
  export async function getHandlerFiles(appRoot: string) {
35
- try {
36
- return await tinyGlob(`**/*.ts`, {
37
- cwd: handlersPath(appRoot),
38
- absolute: true,
39
- });
40
- } catch (err) {
41
- log.debug(`Failed getting handler files: ${err}`);
42
- return [];
43
- }
32
+ try {
33
+ return await tinyGlob(`**/*.ts`, {
34
+ cwd: handlersPath(appRoot),
35
+ absolute: true,
36
+ });
37
+ } catch (err) {
38
+ log.debug(`Failed getting handler files: ${err}`);
39
+ return [];
40
+ }
44
41
  }
45
42
 
46
43
  export async function getSchemaFiles(appRoot: string) {
47
- try {
48
- return await tinyGlob(`**/*.ts`, {
49
- cwd: schemasPath(appRoot),
50
- absolute: true,
51
- });
52
- } catch (err) {
53
- return [];
54
- }
44
+ try {
45
+ return await tinyGlob(`**/*.ts`, {
46
+ cwd: schemasPath(appRoot),
47
+ absolute: true,
48
+ });
49
+ } catch (err) {
50
+ return [];
51
+ }
55
52
  }
56
53
 
57
54
  export function getCollectionNameForRepo(repoFilename: string) {
58
- return repoFilename.replace("Repo.ts", "").toLowerCase();
55
+ return repoFilename.replace("Repo.ts", "").toLowerCase();
59
56
  }
60
57
 
61
58
  export function getRepoInstanceName(fn: string) {
62
- const [name] = fn.split(".ts");
63
- return name.charAt(0).toLowerCase() + name.substr(1);
59
+ const [name] = fn.split(".ts");
60
+ return name.charAt(0).toLowerCase() + name.substr(1);
64
61
  }
65
62
 
66
63
  /**
@@ -68,17 +65,17 @@ export function getRepoInstanceName(fn: string) {
68
65
  * if it starts with i.e "GetFoo"
69
66
  */
70
67
  export function getHttpMethodFromHandlerName(handlerFilename: string) {
71
- if (handlerFilename.includes(sep)) {
72
- const split = handlerFilename.split(sep);
73
- handlerFilename = split[split.length - 1];
74
- }
68
+ if (handlerFilename.includes(sep)) {
69
+ const split = handlerFilename.split(sep);
70
+ handlerFilename = split[split.length - 1];
71
+ }
75
72
 
76
- handlerFilename = handlerFilename.toLocaleLowerCase();
73
+ handlerFilename = handlerFilename.toLocaleLowerCase();
77
74
 
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;
75
+ if (handlerFilename.startsWith(HttpMethod.get)) return HttpMethod.get;
76
+ if (handlerFilename.startsWith(HttpMethod.post)) return HttpMethod.post;
77
+ if (handlerFilename.startsWith(HttpMethod.put)) return HttpMethod.put;
78
+ if (handlerFilename.startsWith(HttpMethod.delete)) return HttpMethod.delete;
82
79
  }
83
80
 
84
81
  /**
@@ -89,75 +86,84 @@ export function getHttpMethodFromHandlerName(handlerFilename: string) {
89
86
  * @param jsonSchemas
90
87
  * @returns
91
88
  */
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
- );
89
+ export function deRefSchema(schemaToDeRef: JSONSchema7Definition, jsonSchemas: JSONSchema7) {
90
+ if (typeof schemaToDeRef === "boolean") {
91
+ return schemaToDeRef;
117
92
  }
118
- } else if (schemaToDeRef.properties) {
119
- for (const k in schemaToDeRef.properties) {
120
- let prop = schemaToDeRef.properties[k];
121
-
122
- if (typeof prop === "boolean") {
123
- continue;
124
- }
125
-
126
- if (prop.$ref) {
127
- const [_0, _1, defKey] = prop.$ref.split("/");
128
- const refedSchema = (jsonSchemas.definitions || {})[defKey];
129
- if (refedSchema) {
130
- schemaToDeRef.properties[k] = deRefSchema(refedSchema, jsonSchemas);
93
+
94
+ if (schemaToDeRef.type === "array") {
95
+ const items = schemaToDeRef.items as JSONSchema7;
96
+
97
+ if (items.$ref) {
98
+ const [_0, _1, defKey] = items.$ref.split("/");
99
+ const refedSchema = (jsonSchemas.definitions || {})[defKey];
100
+
101
+ if (refedSchema) {
102
+ schemaToDeRef.items = deRefSchema(refedSchema, jsonSchemas);
103
+ } else {
104
+ console.warn(`Failed to find deref ${schemaToDeRef.$ref}`);
105
+ }
131
106
  } else {
132
- console.warn(`Failed to find deref ${prop.$ref}`);
107
+ schemaToDeRef.items = deRefSchema(schemaToDeRef.items as JSONSchema7, jsonSchemas);
133
108
  }
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}`);
109
+ } else if (schemaToDeRef.properties) {
110
+ for (const k in schemaToDeRef.properties) {
111
+ let prop = schemaToDeRef.properties[k];
112
+
113
+ if (typeof prop === "boolean") {
114
+ continue;
115
+ }
116
+
117
+ if (prop.$ref) {
118
+ const [_0, _1, defKey] = prop.$ref.split("/");
119
+ const refedSchema = (jsonSchemas.definitions || {})[defKey];
120
+ if (refedSchema) {
121
+ schemaToDeRef.properties[k] = deRefSchema(refedSchema, jsonSchemas);
122
+ } else {
123
+ console.warn(`Failed to find deref ${prop.$ref}`);
124
+ }
125
+ } else if (prop.type === "array" && (prop.items as JSONSchema7).$ref) {
126
+ const [_0, _1, defKey] = (prop.items as JSONSchema7).$ref!.split("/");
127
+ const refedSchema = (jsonSchemas.definitions || {})[defKey];
128
+ if (refedSchema) {
129
+ (schemaToDeRef.properties[k] as JSONSchema7).items = deRefSchema(refedSchema, jsonSchemas);
130
+ } else {
131
+ console.warn(`Failed to find deref ${prop.$ref}`);
132
+ }
133
+ } else if (prop.type === "object" || prop.type === "array") {
134
+ schemaToDeRef.properties[k] = deRefSchema(prop, jsonSchemas);
135
+ }
136
+ }
137
+ } else if (schemaToDeRef.anyOf) {
138
+ let i = 0;
139
+ for (const anyOf of schemaToDeRef.anyOf) {
140
+ const anyOfSchema = anyOf as JSONSchema7;
141
+
142
+ if (anyOfSchema.$ref) {
143
+ const [_0, _1, defKey] = anyOfSchema.$ref.split("/");
144
+ const refedSchema = (jsonSchemas.definitions || {})[defKey];
145
+ if (refedSchema) {
146
+ schemaToDeRef.anyOf[i] = deRefSchema(refedSchema, jsonSchemas);
147
+ } else {
148
+ console.warn(`Failed to find deref ${anyOfSchema.$ref}`);
149
+ }
150
+ }
151
+
152
+ i++;
144
153
  }
145
- } else if (prop.type === "object" || prop.type === "array") {
146
- schemaToDeRef.properties[k] = deRefSchema(prop, jsonSchemas);
147
- }
148
154
  }
149
- }
150
- return schemaToDeRef;
155
+
156
+ return schemaToDeRef;
151
157
  }
152
158
 
153
159
  export function getJsDocComment(comment: string) {
154
- const rows = comment.split("\n").map((line) => {
155
- line = line.trim();
156
- if (line.startsWith("//")) {
157
- return line.replace("//", line).trim();
158
- }
159
- return line.replace(/\/\*\*|\*\/|\*/, "").trim();
160
- });
160
+ const rows = comment.split("\n").map((line) => {
161
+ line = line.trim();
162
+ if (line.startsWith("//")) {
163
+ return line.replace("//", line).trim();
164
+ }
165
+ return line.replace(/\/\*\*|\*\/|\*/, "").trim();
166
+ });
161
167
 
162
- return rows.join("\n").trim();
168
+ return rows.join("\n").trim();
163
169
  }