@flink-app/flink 0.12.1-alpha.9 → 0.13.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cli/build.js +2 -2
  3. package/dist/cli/clean.js +2 -2
  4. package/dist/cli/cli-utils.js +1 -2
  5. package/dist/cli/run.js +2 -2
  6. package/dist/src/FlinkApp.d.ts +6 -1
  7. package/dist/src/FlinkApp.js +28 -14
  8. package/dist/src/FlinkErrors.d.ts +69 -0
  9. package/dist/src/FlinkErrors.js +87 -8
  10. package/dist/src/FlinkHttpHandler.d.ts +14 -1
  11. package/dist/src/FlinkLog.d.ts +2 -2
  12. package/dist/src/FlinkRepo.d.ts +8 -1
  13. package/dist/src/FlinkRepo.js +9 -2
  14. package/dist/src/FlinkResponse.d.ts +0 -3
  15. package/dist/src/FsUtils.js +5 -6
  16. package/dist/src/TypeScriptCompiler.d.ts +10 -0
  17. package/dist/src/TypeScriptCompiler.js +169 -49
  18. package/dist/src/TypeScriptUtils.js +7 -8
  19. package/dist/src/index.d.ts +1 -0
  20. package/dist/src/utils.js +13 -14
  21. package/package.json +67 -69
  22. package/spec/TypeScriptCompiler.spec.ts +11 -1
  23. package/spec/mock-project/dist/src/handlers/GetCar.js +2 -2
  24. package/spec/mock-project/dist/src/handlers/GetCar2.js +2 -2
  25. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema.js +2 -2
  26. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema2.js +2 -2
  27. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema3.js +2 -2
  28. package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema.js +2 -2
  29. package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema2.js +2 -2
  30. package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile.js +2 -2
  31. package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile2.js +2 -2
  32. package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler.js +3 -4
  33. package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler2.js +3 -4
  34. package/spec/mock-project/dist/src/handlers/PostCar.js +2 -2
  35. package/spec/mock-project/dist/src/handlers/PostLogin.js +2 -2
  36. package/spec/mock-project/dist/src/handlers/{GetCarWithOmitSchema.js → PostLogout.js} +16 -20
  37. package/spec/mock-project/dist/src/handlers/PutCar.js +2 -2
  38. package/spec/mock-project/dist/src/index.js +2 -2
  39. package/spec/mock-project/src/handlers/PostLogout.ts +19 -0
  40. package/spec/mock-project/tsconfig.json +1 -1
  41. package/src/FlinkApp.ts +24 -5
  42. package/src/FlinkErrors.ts +86 -6
  43. package/src/FlinkHttpHandler.ts +95 -96
  44. package/src/FlinkRepo.ts +8 -1
  45. package/src/FlinkResponse.ts +36 -39
  46. package/src/TypeScriptCompiler.ts +145 -39
  47. package/src/index.ts +12 -0
  48. package/tsconfig.json +1 -1
  49. package/dist/cli/generate-schemas.d.ts +0 -2
  50. package/dist/cli/generate-schemas.js +0 -140
  51. package/spec/mock-project/dist/src/handlers/GetCarWithTypeSchema.js +0 -60
  52. package/spec/mock-project/package-lock.json +0 -108
@@ -10,6 +10,29 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
13
36
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
37
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
38
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -20,8 +43,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
20
43
  });
21
44
  };
22
45
  var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
46
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
47
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
48
  function verb(n) { return function (v) { return step([n, v]); }; }
26
49
  function step(op) {
27
50
  if (f) throw new TypeError("Generator is already executing.");
@@ -59,7 +82,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
59
82
  return (mod && mod.__esModule) ? mod : { "default": mod };
60
83
  };
61
84
  Object.defineProperty(exports, "__esModule", { value: true });
62
- var fs_1 = require("fs");
85
+ var fs_1 = __importStar(require("fs"));
63
86
  var path_1 = require("path");
64
87
  var tiny_glob_1 = __importDefault(require("tiny-glob"));
65
88
  var ts_json_schema_generator_1 = require("ts-json-schema-generator");
@@ -83,16 +106,65 @@ var TypeScriptCompiler = /** @class */ (function () {
83
106
  * This will be added to file in a batch for performance reasons.
84
107
  */
85
108
  this.tsSchemasSymbolsToImports = [];
109
+ // Detect if project is using ESM based solely on package.json "type": "module"
110
+ this.isEsm = this.isEsmProject(cwd);
111
+ var compilerOptions = {
112
+ noEmit: false, // We need to emit files
113
+ outDir: (0, path_1.join)(cwd, "dist"),
114
+ };
115
+ // Set appropriate module settings based on detected module system
116
+ if (this.isEsm) {
117
+ // For ESM projects, use ESNext module with Node resolution
118
+ compilerOptions.module = ts_morph_1.ts.ModuleKind.ESNext;
119
+ compilerOptions.moduleResolution = ts_morph_1.ts.ModuleResolutionKind.NodeJs;
120
+ compilerOptions.esModuleInterop = true;
121
+ }
122
+ else {
123
+ // For CommonJS projects, use CommonJS module with Node resolution
124
+ compilerOptions.module = ts_morph_1.ts.ModuleKind.CommonJS;
125
+ compilerOptions.moduleResolution = ts_morph_1.ts.ModuleResolutionKind.NodeJs;
126
+ }
127
+ var tsConfigPath = (0, path_1.join)(cwd, "tsconfig.json");
128
+ console.log("TypeScript config path:", require("path").resolve(tsConfigPath));
129
+ console.log("TypeScript version:", ts_morph_1.ts.version);
86
130
  this.project = new ts_morph_1.Project({
87
- tsConfigFilePath: (0, path_1.join)(cwd, "tsconfig.json"),
88
- compilerOptions: {
89
- noEmit: false,
90
- outDir: (0, path_1.join)(cwd, "dist"),
91
- // incremental: true,
92
- },
131
+ tsConfigFilePath: tsConfigPath,
132
+ compilerOptions: compilerOptions,
93
133
  });
94
134
  console.log("Loaded", this.project.getSourceFiles().length, "source file(s) from", cwd);
135
+ console.log("Module system:", this.isEsm ? "ESM" : "CommonJS");
136
+ console.log("Using module:", compilerOptions.module === ts_morph_1.ts.ModuleKind.ESNext ? "ESNext" : "CommonJS");
95
137
  }
138
+ /**
139
+ * Detects if the project is using ESM (ECMAScript Modules)
140
+ * by checking type in package.json.
141
+ */
142
+ TypeScriptCompiler.prototype.isEsmProject = function (cwd) {
143
+ try {
144
+ // Check package.json for "type": "module"
145
+ var packageJsonPath = (0, path_1.join)(cwd, "package.json");
146
+ if (fs_1.default.existsSync(packageJsonPath)) {
147
+ var packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf8"));
148
+ return packageJson.type === "module";
149
+ }
150
+ }
151
+ catch (error) {
152
+ // If we can't determine, default to CommonJS
153
+ console.warn("Error detecting module system, defaulting to CommonJS:", error);
154
+ }
155
+ return false;
156
+ };
157
+ /**
158
+ * Gets the module specifier for imports, adding .js extension for ESM
159
+ */
160
+ TypeScriptCompiler.prototype.getModuleSpecifier = function (fromFile, toFile) {
161
+ var moduleSpecifier = fromFile.getRelativePathAsModuleSpecifierTo(toFile);
162
+ // Add .js extension for ESM imports (only for relative paths)
163
+ if (this.isEsm && !moduleSpecifier.startsWith("@") && !moduleSpecifier.endsWith(".js")) {
164
+ moduleSpecifier += ".js";
165
+ }
166
+ return moduleSpecifier;
167
+ };
96
168
  /**
97
169
  * Deletes all generated files.
98
170
  * @param cwd
@@ -182,7 +254,7 @@ var TypeScriptCompiler = /** @class */ (function () {
182
254
  */
183
255
  TypeScriptCompiler.prototype.parseHandlers = function () {
184
256
  return __awaiter(this, arguments, void 0, function (excludeDirs) {
185
- var generatedFile, handlersArr, handlers, jsonSchemas;
257
+ var generatedFile, handlersArr, handlers, schemaFilePath, jsonSchemas;
186
258
  if (excludeDirs === void 0) { excludeDirs = []; }
187
259
  return __generator(this, function (_a) {
188
260
  switch (_a.label) {
@@ -198,8 +270,8 @@ var TypeScriptCompiler = /** @class */ (function () {
198
270
  _a.sent();
199
271
  return [4 /*yield*/, this.createIntermediateSchemaFile()];
200
272
  case 3:
201
- _a.sent();
202
- return [4 /*yield*/, this.generateAndSaveJsonSchemas(handlers.schemasToGenerate)];
273
+ schemaFilePath = _a.sent();
274
+ return [4 /*yield*/, this.generateAndSaveJsonSchemas(handlers.schemasToGenerate, schemaFilePath)];
203
275
  case 4:
204
276
  jsonSchemas = _a.sent();
205
277
  this.appendSchemasToHandlerSourceFiles(handlers.schemasToGenerate, jsonSchemas);
@@ -213,32 +285,44 @@ var TypeScriptCompiler = /** @class */ (function () {
213
285
  */
214
286
  TypeScriptCompiler.prototype.parseHandlerDir = function (generatedFile, handlersArr) {
215
287
  return __awaiter(this, void 0, void 0, function () {
216
- var imports, i, schemasToGenerate, _i, _a, sf, isAutoRegister, namespaceImport, assumedHttpMethod, schemaTypes;
217
- return __generator(this, function (_b) {
218
- switch (_b.label) {
288
+ var imports, i, schemasToGenerate, _i, _a, sf, isAutoRegister, namespaceImport, assumedHttpMethod, schemaTypes, _b, existingVars;
289
+ return __generator(this, function (_c) {
290
+ switch (_c.label) {
219
291
  case 0:
220
292
  imports = [];
221
293
  i = 0;
222
294
  schemasToGenerate = [];
223
295
  _i = 0, _a = this.project.getSourceFiles();
224
- _b.label = 1;
296
+ _c.label = 1;
225
297
  case 1:
226
- if (!(_i < _a.length)) return [3 /*break*/, 4];
298
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
227
299
  sf = _a[_i];
228
300
  if (!sf.getFilePath().includes("src/handlers/")) {
229
- return [3 /*break*/, 3];
301
+ return [3 /*break*/, 5];
230
302
  }
231
303
  isAutoRegister = this.isAutoRegisterableHandler(sf);
232
304
  console.log("Detected handler ".concat(sf.getBaseName(), " ").concat(!isAutoRegister ? "(requires manual registration)" : ""));
233
305
  namespaceImport = sf.getBaseNameWithoutExtension().replace(/\./g, "_") + "_" + i;
234
306
  imports.push({
235
307
  defaultImport: "* as " + namespaceImport,
236
- moduleSpecifier: generatedFile.getRelativePathAsModuleSpecifierTo(sf),
308
+ moduleSpecifier: this.getModuleSpecifier(generatedFile, sf),
237
309
  });
238
310
  assumedHttpMethod = (0, utils_1.getHttpMethodFromHandlerName)(sf.getBaseName());
311
+ if (!isAutoRegister) return [3 /*break*/, 3];
239
312
  return [4 /*yield*/, this.extractSchemasFromHandlerSourceFile(sf)];
240
313
  case 2:
241
- schemaTypes = _b.sent();
314
+ _b = _c.sent();
315
+ return [3 /*break*/, 4];
316
+ case 3:
317
+ _b = undefined;
318
+ _c.label = 4;
319
+ case 4:
320
+ schemaTypes = _b;
321
+ existingVars = sf.getVariableStatements().filter(function (vs) {
322
+ var varNames = vs.getDeclarations().map(function (d) { return d.getName(); });
323
+ return varNames.some(function (name) { return ["__assumedHttpMethod", "__file", "__query", "__params"].includes(name); });
324
+ });
325
+ existingVars.forEach(function (v) { return v.remove(); });
242
326
  // Append schemas and metadata to source file that will be part of emitted dist bundle (javascript)
243
327
  sf.addVariableStatement({
244
328
  declarationKind: ts_morph_1.VariableDeclarationKind.Const,
@@ -275,15 +359,16 @@ var TypeScriptCompiler = /** @class */ (function () {
275
359
  if (isAutoRegister) {
276
360
  handlersArr.insertElement(i, "{handler: ".concat(namespaceImport, ", assumedHttpMethod: ").concat(assumedHttpMethod ? "HttpMethod." + assumedHttpMethod : undefined, "}"));
277
361
  i++;
362
+ // Add schemas to generate list
363
+ if (schemaTypes) {
364
+ schemasToGenerate.push(__assign(__assign({}, schemaTypes), { sourceFile: sf }));
365
+ }
278
366
  }
279
- if (schemaTypes) {
280
- schemasToGenerate.push(__assign(__assign({}, schemaTypes), { sourceFile: sf }));
281
- }
282
- _b.label = 3;
283
- case 3:
367
+ _c.label = 5;
368
+ case 5:
284
369
  _i++;
285
370
  return [3 /*break*/, 1];
286
- case 4: return [2 /*return*/, {
371
+ case 6: return [2 /*return*/, {
287
372
  imports: imports,
288
373
  schemasToGenerate: schemasToGenerate,
289
374
  }];
@@ -309,7 +394,7 @@ var TypeScriptCompiler = /** @class */ (function () {
309
394
  console.log("Detected repo ".concat(sf.getBaseName()));
310
395
  imports.push({
311
396
  defaultImport: sf.getBaseNameWithoutExtension(),
312
- moduleSpecifier: generatedFile.getRelativePathAsModuleSpecifierTo(sf),
397
+ moduleSpecifier: this.getModuleSpecifier(generatedFile, sf),
313
398
  });
314
399
  reposArr.insertElement(i, "{collectionName: \"".concat((0, utils_1.getCollectionNameForRepo)(sf.getBaseName()), "\", repoInstanceName: \"").concat((0, utils_1.getRepoInstanceName)(sf.getBaseName()), "\", Repo: ").concat(sf.getBaseNameWithoutExtension(), "}"));
315
400
  i++;
@@ -341,7 +426,7 @@ var TypeScriptCompiler = /** @class */ (function () {
341
426
  console.error("Cannot find entry script '".concat(appEntryScript, "'"));
342
427
  return [2 /*return*/, process.exit(1)];
343
428
  }
344
- sf = this.createSourceFile(["start.ts"], "// Generated ".concat(new Date(), "\nimport \"./generatedHandlers\";\nimport \"./generatedRepos\";\nimport \"./generatedJobs\";\nimport \"..").concat(appEntryScript.replace(/\.ts/g, ""), "\";\n"));
429
+ sf = this.createSourceFile(["start.ts"], "// Generated ".concat(new Date(), "\nimport \"./generatedHandlers").concat(this.isEsm ? ".js" : "", "\";\nimport \"./generatedRepos").concat(this.isEsm ? ".js" : "", "\";\nimport \"./generatedJobs").concat(this.isEsm ? ".js" : "", "\";\nimport \"..").concat(appEntryScript.replace(/\.ts/g, "")).concat(this.isEsm ? ".js" : "", "\";\nexport default {}; // Export an empty object to make it a module\n"));
345
430
  return [4 /*yield*/, sf.save()];
346
431
  case 1:
347
432
  _a.sent();
@@ -397,7 +482,7 @@ var TypeScriptCompiler = /** @class */ (function () {
397
482
  };
398
483
  TypeScriptCompiler.prototype.saveIntermediateTsSchema = function (schema, handlerFile, suffix) {
399
484
  return __awaiter(this, void 0, void 0, function () {
400
- var handlerFileName, generatedSchemaInterfaceStr, schemaInterfaceName, schemaSymbol, interfaceName, declaration, _i, _a, typeToImport, arrayTypeArg, schemaSymbol, interfaceName, declaration, props, _b, _c, typeToImport, declaration, typeRefIdentifiers;
485
+ var handlerFileName, generatedSchemaInterfaceStr, schemaInterfaceName, schemaSymbol, interfaceName, declaration, _i, _a, typeToImport, arrayTypeArg, schemaSymbol, interfaceName, declaration, props, _b, _c, typeToImport, declarations, declaration, typeRefIdentifiers;
401
486
  var _this = this;
402
487
  return __generator(this, function (_d) {
403
488
  if (schema.isAny()) {
@@ -454,13 +539,17 @@ var TypeScriptCompiler = /** @class */ (function () {
454
539
  }
455
540
  }
456
541
  else if (schema.isObject()) {
457
- declaration = schema.getSymbolOrThrow().getDeclarations()[0];
458
- typeRefIdentifiers = declaration
459
- .getDescendantsOfKind(ts_morph_1.SyntaxKind.TypeReference)
460
- .map(function (typeRef) { return typeRef.getFirstChildByKindOrThrow(ts_morph_1.SyntaxKind.Identifier); });
461
- typeRefIdentifiers.forEach(function (tr) {
462
- _this.tsSchemasSymbolsToImports.push(tr.getSymbolOrThrow().getDeclaredType().getSymbolOrThrow());
463
- });
542
+ declarations = schema.getSymbolOrThrow().getDeclarations();
543
+ declaration = declarations[0];
544
+ // Only extract type references if declaration exists (won't exist for empty object literals like {})
545
+ if (declaration) {
546
+ typeRefIdentifiers = declaration
547
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.TypeReference)
548
+ .map(function (typeRef) { return typeRef.getFirstChildByKindOrThrow(ts_morph_1.SyntaxKind.Identifier); });
549
+ typeRefIdentifiers.forEach(function (tr) {
550
+ _this.tsSchemasSymbolsToImports.push(tr.getSymbolOrThrow().getDeclaredType().getSymbolOrThrow());
551
+ });
552
+ }
464
553
  generatedSchemaInterfaceStr = "export interface ".concat(schemaInterfaceName, " { ").concat(schema
465
554
  .getProperties()
466
555
  .map(function (p) { return p.getValueDeclarationOrThrow().getText(); })
@@ -477,8 +566,10 @@ var TypeScriptCompiler = /** @class */ (function () {
477
566
  });
478
567
  });
479
568
  };
480
- TypeScriptCompiler.prototype.initJsonSchemaGenerator = function () {
569
+ TypeScriptCompiler.prototype.initJsonSchemaGenerator = function (schemaFilePath) {
570
+ var tsconfigPath = (0, path_1.join)(this.cwd, "tsconfig.json");
481
571
  var conf = {
572
+ path: schemaFilePath, // Point to the intermediate schema file
482
573
  expose: "none", // Do not create shared $ref definitions.
483
574
  topRef: false, // Removes the wrapper object around the schema.
484
575
  additionalProperties: false,
@@ -492,27 +583,38 @@ var TypeScriptCompiler = /** @class */ (function () {
492
583
  extraTags: [],
493
584
  functions: "fail",
494
585
  discriminatorType: "json-schema",
586
+ // tsconfig: tsconfigPath,
495
587
  };
588
+ console.log("Creating TypeScript program for schema generation:");
589
+ console.log(" Schema file:", schemaFilePath);
590
+ console.log(" tsconfig:", tsconfigPath);
591
+ // Create a fresh TypeScript Program that includes the schema file
592
+ // This ensures ts-json-schema-generator can find the types we just generated
593
+ var program = (0, ts_json_schema_generator_1.createProgram)(conf);
594
+ console.log(" TypeScript version:", ts_morph_1.ts.version);
595
+ console.log(" Program root files:", program.getRootFileNames().length);
496
596
  var formatter = (0, ts_json_schema_generator_1.createFormatter)(conf);
497
- var parser = (0, ts_json_schema_generator_1.createParser)(this.project.getProgram().compilerObject, conf);
498
- var generator = new ts_json_schema_generator_1.SchemaGenerator(this.project.getProgram().compilerObject, parser, formatter, conf);
597
+ var parser = (0, ts_json_schema_generator_1.createParser)(program, conf);
598
+ var generator = new ts_json_schema_generator_1.SchemaGenerator(program, parser, formatter, conf);
499
599
  return generator;
500
600
  };
501
- TypeScriptCompiler.prototype.generateAndSaveJsonSchemas = function (schemas) {
601
+ TypeScriptCompiler.prototype.generateAndSaveJsonSchemas = function (schemas, schemaFilePath) {
502
602
  return __awaiter(this, void 0, void 0, function () {
503
603
  var jsonSchemas, _i, schemas_1, _a, reqSchemaType, resSchemaType, mergedSchemas, filePath;
504
604
  var _b, _c;
505
605
  return __generator(this, function (_d) {
506
606
  switch (_d.label) {
507
607
  case 0:
608
+ // Reset schema generator to use the newly created intermediate schema file
609
+ this.schemaGenerator = undefined;
508
610
  jsonSchemas = [];
509
611
  for (_i = 0, schemas_1 = schemas; _i < schemas_1.length; _i++) {
510
612
  _a = schemas_1[_i], reqSchemaType = _a.reqSchemaType, resSchemaType = _a.resSchemaType;
511
613
  if (reqSchemaType) {
512
- jsonSchemas.push({ definitions: (_b = {}, _b[reqSchemaType] = this.generateJsonSchema(reqSchemaType), _b) });
614
+ jsonSchemas.push({ definitions: (_b = {}, _b[reqSchemaType] = this.generateJsonSchema(reqSchemaType, schemaFilePath), _b) });
513
615
  }
514
616
  if (resSchemaType) {
515
- jsonSchemas.push({ definitions: (_c = {}, _c[resSchemaType] = this.generateJsonSchema(resSchemaType), _c) });
617
+ jsonSchemas.push({ definitions: (_c = {}, _c[resSchemaType] = this.generateJsonSchema(resSchemaType, schemaFilePath), _c) });
516
618
  }
517
619
  }
518
620
  mergedSchemas = jsonSchemas.reduce(function (out, schema) {
@@ -535,9 +637,9 @@ var TypeScriptCompiler = /** @class */ (function () {
535
637
  });
536
638
  });
537
639
  };
538
- TypeScriptCompiler.prototype.generateJsonSchema = function (typeName) {
640
+ TypeScriptCompiler.prototype.generateJsonSchema = function (typeName, schemaFilePath) {
539
641
  if (!this.schemaGenerator) {
540
- this.schemaGenerator = this.initJsonSchemaGenerator();
642
+ this.schemaGenerator = this.initJsonSchemaGenerator(schemaFilePath);
541
643
  }
542
644
  return this.schemaGenerator.createSchema(typeName);
543
645
  };
@@ -595,9 +697,16 @@ var TypeScriptCompiler = /** @class */ (function () {
595
697
  return __awaiter(this, void 0, void 0, function () {
596
698
  var schemaSourceFile;
597
699
  return __generator(this, function (_a) {
598
- schemaSourceFile = this.createSourceFile(["schemas", "schemas.ts"], "// Generated ".concat(new Date(), "\n").concat(this.parsedTsSchemas.join("\n\n")));
599
- (0, TypeScriptUtils_1.addImports)(schemaSourceFile, this.tsSchemasSymbolsToImports);
600
- return [2 /*return*/, schemaSourceFile.save()];
700
+ switch (_a.label) {
701
+ case 0:
702
+ schemaSourceFile = this.createSourceFile(["schemas", "schemas.ts"], "// Generated ".concat(new Date(), "\n").concat(this.parsedTsSchemas.join("\n\n")));
703
+ (0, TypeScriptUtils_1.addImports)(schemaSourceFile, this.tsSchemasSymbolsToImports);
704
+ return [4 /*yield*/, schemaSourceFile.save()];
705
+ case 1:
706
+ _a.sent();
707
+ // Return the file path so it can be used by ts-json-schema-generator
708
+ return [2 /*return*/, schemaSourceFile.getFilePath()];
709
+ }
601
710
  });
602
711
  });
603
712
  };
@@ -637,6 +746,12 @@ var TypeScriptCompiler = /** @class */ (function () {
637
746
  }
638
747
  var reqJsonSchema = JSON.stringify(reqSchemaType ? jsonSchemaDefs[reqSchemaType] : undefined);
639
748
  var resJsonSchema = JSON.stringify(resSchemaType ? jsonSchemaDefs[resSchemaType] : undefined);
749
+ // Remove existing __schemas variable if it exists (to avoid redeclaration errors)
750
+ var existingSchemas = sourceFile.getVariableStatements().filter(function (vs) {
751
+ var varNames = vs.getDeclarations().map(function (d) { return d.getName(); });
752
+ return varNames.includes("__schemas");
753
+ });
754
+ existingSchemas.forEach(function (v) { return v.remove(); });
640
755
  sourceFile.addVariableStatement({
641
756
  declarationKind: ts_morph_1.VariableDeclarationKind.Const,
642
757
  isExported: true,
@@ -655,7 +770,7 @@ var TypeScriptCompiler = /** @class */ (function () {
655
770
  */
656
771
  TypeScriptCompiler.prototype.parseJobs = function () {
657
772
  return __awaiter(this, void 0, void 0, function () {
658
- var generatedFile, jobsArr, imports, i, _i, _a, sf, namespaceImport;
773
+ var generatedFile, jobsArr, imports, i, _i, _a, sf, namespaceImport, existingFile;
659
774
  return __generator(this, function (_b) {
660
775
  switch (_b.label) {
661
776
  case 0:
@@ -672,8 +787,13 @@ var TypeScriptCompiler = /** @class */ (function () {
672
787
  namespaceImport = sf.getBaseNameWithoutExtension().replace(/\./g, "_") + "_" + i;
673
788
  imports.push({
674
789
  defaultImport: "* as " + namespaceImport,
675
- moduleSpecifier: generatedFile.getRelativePathAsModuleSpecifierTo(sf),
790
+ moduleSpecifier: this.getModuleSpecifier(generatedFile, sf),
791
+ });
792
+ existingFile = sf.getVariableStatements().filter(function (vs) {
793
+ var varNames = vs.getDeclarations().map(function (d) { return d.getName(); });
794
+ return varNames.includes("__file");
676
795
  });
796
+ existingFile.forEach(function (v) { return v.remove(); });
677
797
  // Append metadata to source file that will be part of emitted dist bundle (javascript)
678
798
  sf.addVariableStatement({
679
799
  declarationKind: ts_morph_1.VariableDeclarationKind.Const,
@@ -1,6 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTypeMetadata = exports.getSymbolOrAlias = exports.getInterfaceName = exports.getDefaultExport = exports.addImports = exports.addImport = exports.getTypesToImport = void 0;
3
+ exports.getTypesToImport = getTypesToImport;
4
+ exports.addImport = addImport;
5
+ exports.addImports = addImports;
6
+ exports.getDefaultExport = getDefaultExport;
7
+ exports.getInterfaceName = getInterfaceName;
8
+ exports.getSymbolOrAlias = getSymbolOrAlias;
9
+ exports.getTypeMetadata = getTypeMetadata;
4
10
  var ts_morph_1 = require("ts-morph");
5
11
  var utils_1 = require("./utils");
6
12
  /**
@@ -34,7 +40,6 @@ function getTypesToImport(node) {
34
40
  }
35
41
  return typesToImport;
36
42
  }
37
- exports.getTypesToImport = getTypesToImport;
38
43
  function addImport(toSourceFile, symbol) {
39
44
  var symbolDeclaration = symbol.getDeclarations()[0];
40
45
  if (!symbolDeclaration) {
@@ -66,7 +71,6 @@ function addImport(toSourceFile, symbol) {
66
71
  });
67
72
  }
68
73
  }
69
- exports.addImport = addImport;
70
74
  /**
71
75
  * Adds imports to modules where provided symbols resides.
72
76
  * Imports are added to provided source file.
@@ -110,7 +114,6 @@ function addImports(toSourceFile, symbols) {
110
114
  });
111
115
  }));
112
116
  }
113
- exports.addImports = addImports;
114
117
  /**
115
118
  * Helper to get the default export, if any, from a source file.
116
119
  * @param sf
@@ -128,7 +131,6 @@ function getDefaultExport(sf) {
128
131
  }
129
132
  }
130
133
  }
131
- exports.getDefaultExport = getDefaultExport;
132
134
  /**
133
135
  * Returns name of interface from interface symbol.
134
136
  *
@@ -142,11 +144,9 @@ function getInterfaceName(symbol) {
142
144
  }
143
145
  return declaration.getFirstChildByKindOrThrow(ts_morph_1.SyntaxKind.Identifier).getText();
144
146
  }
145
- exports.getInterfaceName = getInterfaceName;
146
147
  function getSymbolOrAlias(type) {
147
148
  return type.getAliasSymbol() || type.getSymbol();
148
149
  }
149
- exports.getSymbolOrAlias = getSymbolOrAlias;
150
150
  function getTypeMetadata(type) {
151
151
  if (!type || ["void", "any"].includes(type.getText())) {
152
152
  return [];
@@ -168,4 +168,3 @@ function getTypeMetadata(type) {
168
168
  };
169
169
  });
170
170
  }
171
- exports.getTypeMetadata = getTypeMetadata;
@@ -9,3 +9,4 @@ export * from "./FlinkPlugin";
9
9
  export * from "./FlinkJob";
10
10
  export * from "./auth/FlinkAuthUser";
11
11
  export * from "./auth/FlinkAuthPlugin";
12
+ export type { Request as ExpressRequest, Response as ExpressResponse, NextFunction as ExpressNextFunction, RequestHandler as ExpressRequestHandler, ErrorRequestHandler as ExpressErrorRequestHandler, Express, static as expressStatic, } from "express";
package/dist/src/utils.js CHANGED
@@ -9,8 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
@@ -39,7 +39,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.getPathParams = exports.getJsDocComment = exports.getHttpMethodFromHandlerName = exports.getRepoInstanceName = exports.getCollectionNameForRepo = exports.getSchemaFiles = exports.getHandlerFiles = exports.isError = exports.isRouteMatch = exports.schemasPath = exports.handlersPath = void 0;
42
+ exports.handlersPath = handlersPath;
43
+ exports.schemasPath = schemasPath;
44
+ exports.isRouteMatch = isRouteMatch;
45
+ exports.isError = isError;
46
+ exports.getHandlerFiles = getHandlerFiles;
47
+ exports.getSchemaFiles = getSchemaFiles;
48
+ exports.getCollectionNameForRepo = getCollectionNameForRepo;
49
+ exports.getRepoInstanceName = getRepoInstanceName;
50
+ exports.getHttpMethodFromHandlerName = getHttpMethodFromHandlerName;
51
+ exports.getJsDocComment = getJsDocComment;
52
+ exports.getPathParams = getPathParams;
43
53
  var path_1 = require("path");
44
54
  var tiny_glob_1 = __importDefault(require("tiny-glob"));
45
55
  var FlinkHttpHandler_1 = require("./FlinkHttpHandler");
@@ -47,11 +57,9 @@ var FlinkLog_1 = require("./FlinkLog");
47
57
  function handlersPath(appRoot) {
48
58
  return (0, path_1.join)(appRoot, "src", "handlers");
49
59
  }
50
- exports.handlersPath = handlersPath;
51
60
  function schemasPath(appRoot) {
52
61
  return (0, path_1.join)(appRoot, "src", "schemas");
53
62
  }
54
- exports.schemasPath = schemasPath;
55
63
  function isRouteMatch(req, routes) {
56
64
  var match = routes.find(function (_a) {
57
65
  var method = _a.method, path = _a.path;
@@ -61,11 +69,9 @@ function isRouteMatch(req, routes) {
61
69
  });
62
70
  return !!match;
63
71
  }
64
- exports.isRouteMatch = isRouteMatch;
65
72
  function isError(message) {
66
73
  return message.status && message.status > 399;
67
74
  }
68
- exports.isError = isError;
69
75
  function getHandlerFiles(appRoot) {
70
76
  return __awaiter(this, void 0, void 0, function () {
71
77
  var err_1;
@@ -87,7 +93,6 @@ function getHandlerFiles(appRoot) {
87
93
  });
88
94
  });
89
95
  }
90
- exports.getHandlerFiles = getHandlerFiles;
91
96
  function getSchemaFiles(appRoot) {
92
97
  return __awaiter(this, void 0, void 0, function () {
93
98
  var err_2;
@@ -108,16 +113,13 @@ function getSchemaFiles(appRoot) {
108
113
  });
109
114
  });
110
115
  }
111
- exports.getSchemaFiles = getSchemaFiles;
112
116
  function getCollectionNameForRepo(repoFilename) {
113
117
  return repoFilename.replace("Repo.ts", "").toLowerCase();
114
118
  }
115
- exports.getCollectionNameForRepo = getCollectionNameForRepo;
116
119
  function getRepoInstanceName(fn) {
117
120
  var name = fn.split(".ts")[0];
118
121
  return name.charAt(0).toLowerCase() + name.substr(1);
119
122
  }
120
- exports.getRepoInstanceName = getRepoInstanceName;
121
123
  /**
122
124
  * Get http method from props or convention based on file name
123
125
  * if it starts with i.e "GetFoo"
@@ -137,7 +139,6 @@ function getHttpMethodFromHandlerName(handlerFilename) {
137
139
  if (handlerFilename.startsWith(FlinkHttpHandler_1.HttpMethod.delete))
138
140
  return FlinkHttpHandler_1.HttpMethod.delete;
139
141
  }
140
- exports.getHttpMethodFromHandlerName = getHttpMethodFromHandlerName;
141
142
  function getJsDocComment(comment) {
142
143
  var rows = comment.split("\n").map(function (line) {
143
144
  line = line.trim();
@@ -148,7 +149,6 @@ function getJsDocComment(comment) {
148
149
  });
149
150
  return rows.join("\n").trim();
150
151
  }
151
- exports.getJsDocComment = getJsDocComment;
152
152
  var pathParamsRegex = /:([a-zA-Z0-9]+)/g;
153
153
  /**
154
154
  * Returns array of path params from path string.
@@ -160,4 +160,3 @@ function getPathParams(path) {
160
160
  var _a;
161
161
  return ((_a = path.match(pathParamsRegex)) === null || _a === void 0 ? void 0 : _a.map(function (match) { return match.slice(1); })) || [];
162
162
  }
163
- exports.getPathParams = getPathParams;