@flink-app/flink 0.12.1-alpha.7 → 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 (50) 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 +8 -2
  7. package/dist/src/FlinkApp.js +29 -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 +13 -5
  13. package/dist/src/FlinkRepo.js +11 -3
  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 +26 -5
  42. package/src/FlinkErrors.ts +86 -6
  43. package/src/FlinkHttpHandler.ts +95 -96
  44. package/src/FlinkRepo.ts +10 -3
  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/spec/mock-project/dist/src/handlers/GetCarWithTypeSchema.js +0 -60
  50. package/spec/mock-project/package-lock.json +0 -108
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @flink-app/flink
2
+
3
+ ## 0.13.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Migrate to pnpm and streamlines build process.
package/dist/cli/build.js CHANGED
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  });
11
11
  };
12
12
  var __generator = (this && this.__generator) || function (thisArg, body) {
13
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ 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);
14
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
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.");
package/dist/cli/clean.js CHANGED
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  });
11
11
  };
12
12
  var __generator = (this && this.__generator) || function (thisArg, body) {
13
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ 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);
14
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
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.");
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getOption = void 0;
3
+ exports.getOption = getOption;
4
4
  function getOption(args, name, defaultValue, opts) {
5
5
  if (opts === void 0) { opts = {}; }
6
6
  var option = "--".concat(name);
@@ -17,4 +17,3 @@ function getOption(args, name, defaultValue, opts) {
17
17
  }
18
18
  return defaultValue;
19
19
  }
20
- exports.getOption = getOption;
package/dist/cli/run.js CHANGED
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  });
11
11
  };
12
12
  var __generator = (this && this.__generator) || function (thisArg, body) {
13
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ 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);
14
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
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.");
@@ -1,7 +1,7 @@
1
1
  import { OptionsJson } from "body-parser";
2
- import { Express } from "express";
2
+ import express, { Express } from "express";
3
3
  import { JSONSchema7 } from "json-schema";
4
- import { Db } from "mongodb";
4
+ import { Db, MongoClient } from "mongodb";
5
5
  import { ToadScheduler } from "toad-scheduler";
6
6
  import { FlinkAuthPlugin } from "./auth/FlinkAuthPlugin";
7
7
  import { FlinkContext } from "./FlinkContext";
@@ -10,6 +10,11 @@ import { FlinkJobFile } from "./FlinkJob";
10
10
  import { FlinkPlugin } from "./FlinkPlugin";
11
11
  import { FlinkRepo } from "./FlinkRepo";
12
12
  export type JSONSchema = JSONSchema7;
13
+ /**
14
+ * Re-export express factory function so sub packages
15
+ * do not need its own express
16
+ */
17
+ export declare const expressFn: typeof express;
13
18
  /**
14
19
  * This will be populated at compile time when the apps handlers
15
20
  * are picked up by TypeScript compiler
@@ -156,6 +161,7 @@ export declare class FlinkApp<C extends FlinkContext> {
156
161
  name: string;
157
162
  expressApp?: Express;
158
163
  db?: Db;
164
+ dbClient?: MongoClient;
159
165
  handlers: HandlerConfig[];
160
166
  port?: number;
161
167
  started: boolean;
@@ -20,8 +20,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
20
20
  });
21
21
  };
22
22
  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;
23
+ 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);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
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.");
@@ -50,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.FlinkApp = exports.autoRegisteredJobs = exports.autoRegisteredRepos = exports.autoRegisteredHandlers = void 0;
53
+ exports.FlinkApp = exports.autoRegisteredJobs = exports.autoRegisteredRepos = exports.autoRegisteredHandlers = exports.expressFn = void 0;
54
54
  var ajv_1 = __importDefault(require("ajv"));
55
55
  var ajv_formats_1 = __importDefault(require("ajv-formats"));
56
56
  var body_parser_1 = __importDefault(require("body-parser"));
@@ -72,6 +72,11 @@ var defaultCorsOptions = {
72
72
  credentials: true,
73
73
  origin: [/.*/],
74
74
  };
75
+ /**
76
+ * Re-export express factory function so sub packages
77
+ * do not need its own express
78
+ */
79
+ exports.expressFn = express_1.default;
75
80
  /**
76
81
  * This will be populated at compile time when the apps handlers
77
82
  * are picked up by TypeScript compiler
@@ -442,11 +447,11 @@ var FlinkApp = /** @class */ (function () {
442
447
  */
443
448
  FlinkApp.prototype.registerAutoRegisterableHandlers = function () {
444
449
  return __awaiter(this, void 0, void 0, function () {
445
- var _i, autoRegisteredHandlers_1, _a, handler, assumedHttpMethod, pathParams, _b, _c, param;
446
- var _d, _e, _f;
447
- return __generator(this, function (_g) {
448
- for (_i = 0, autoRegisteredHandlers_1 = exports.autoRegisteredHandlers; _i < autoRegisteredHandlers_1.length; _i++) {
449
- _a = autoRegisteredHandlers_1[_i], handler = _a.handler, assumedHttpMethod = _a.assumedHttpMethod;
450
+ var _i, _a, _b, handler, assumedHttpMethod, pathParams, _c, _d, param;
451
+ var _e, _f, _g;
452
+ return __generator(this, function (_h) {
453
+ for (_i = 0, _a = exports.autoRegisteredHandlers.sort(function (a, b) { var _a, _b; return (((_a = a.handler.Route) === null || _a === void 0 ? void 0 : _a.order) || 0) - (((_b = b.handler.Route) === null || _b === void 0 ? void 0 : _b.order) || 0); }); _i < _a.length; _i++) {
454
+ _b = _a[_i], handler = _b.handler, assumedHttpMethod = _b.assumedHttpMethod;
450
455
  if (!handler.Route) {
451
456
  FlinkLog_1.log.error("Missing Props in handler ".concat(handler.__file));
452
457
  continue;
@@ -455,10 +460,10 @@ var FlinkApp = /** @class */ (function () {
455
460
  FlinkLog_1.log.error("Missing exported handler function in handler ".concat(handler.__file));
456
461
  continue;
457
462
  }
458
- if (!!((_d = handler.__params) === null || _d === void 0 ? void 0 : _d.length)) {
463
+ if (!!((_e = handler.__params) === null || _e === void 0 ? void 0 : _e.length)) {
459
464
  pathParams = (0, utils_1.getPathParams)(handler.Route.path);
460
- for (_b = 0, _c = handler.__params; _b < _c.length; _b++) {
461
- param = _c[_b];
465
+ for (_c = 0, _d = handler.__params; _c < _d.length; _c++) {
466
+ param = _d[_c];
462
467
  if (!pathParams.includes(param.name)) {
463
468
  FlinkLog_1.log.error("Handler ".concat(handler.__file, " has param ").concat(param.name, " but it is not present in the path '").concat(handler.Route.path, "'"));
464
469
  throw new Error("Invalid/missing handler path param");
@@ -471,8 +476,8 @@ var FlinkApp = /** @class */ (function () {
471
476
  this.registerHandler({
472
477
  routeProps: __assign(__assign({}, handler.Route), { method: handler.Route.method || assumedHttpMethod, origin: this.name }),
473
478
  schema: {
474
- reqSchema: (_e = handler.__schemas) === null || _e === void 0 ? void 0 : _e.reqSchema,
475
- resSchema: (_f = handler.__schemas) === null || _f === void 0 ? void 0 : _f.resSchema,
479
+ reqSchema: (_f = handler.__schemas) === null || _f === void 0 ? void 0 : _f.reqSchema,
480
+ resSchema: (_g = handler.__schemas) === null || _g === void 0 ? void 0 : _g.resSchema,
476
481
  },
477
482
  queryMetadata: handler.__query || [],
478
483
  paramsMetadata: handler.__params || [],
@@ -583,7 +588,7 @@ var FlinkApp = /** @class */ (function () {
583
588
  if (this.dbOpts) {
584
589
  for (_i = 0, autoRegisteredRepos_1 = exports.autoRegisteredRepos; _i < autoRegisteredRepos_1.length; _i++) {
585
590
  _a = autoRegisteredRepos_1[_i], collectionName = _a.collectionName, repoInstanceName = _a.repoInstanceName, Repo = _a.Repo;
586
- repoInstance = new Repo(collectionName, this.db);
591
+ repoInstance = new Repo(collectionName, this.db, this.dbClient);
587
592
  this.repos[repoInstanceName] = repoInstance;
588
593
  FlinkLog_1.log.info("Registered repo ".concat(repoInstanceName));
589
594
  }
@@ -629,6 +634,7 @@ var FlinkApp = /** @class */ (function () {
629
634
  case 2:
630
635
  client = _a.sent();
631
636
  this.db = client.db();
637
+ this.dbClient = client;
632
638
  return [3 /*break*/, 4];
633
639
  case 3:
634
640
  err_2 = _a.sent();
@@ -715,6 +721,15 @@ var FlinkApp = /** @class */ (function () {
715
721
  if (!this.dbOpts) {
716
722
  throw new Error("No db configured");
717
723
  }
724
+ var driverVersion = require("mongodb/package.json").version;
725
+ if (driverVersion.startsWith("3")) {
726
+ FlinkLog_1.log.debug("Using legacy mongodb connection options as mongo client is version ".concat(driverVersion));
727
+ return {
728
+ useNewUrlParser: true,
729
+ useUnifiedTopology: true,
730
+ };
731
+ }
732
+ FlinkLog_1.log.debug("Using modern MongoDB client options (driver version ".concat(driverVersion, ")"));
718
733
  return {
719
734
  serverApi: {
720
735
  version: mongodb_1.ServerApiVersion.v1,
@@ -1,7 +1,76 @@
1
1
  import { FlinkResponse } from "./FlinkResponse";
2
2
  export type FlinkError = undefined;
3
+ /**
4
+ * Returns a 404 Not Found error response.
5
+ * Use when a requested resource doesn't exist (e.g., invalid ID, missing entity).
6
+ *
7
+ * @param detail - Optional custom error message
8
+ * @param code - Optional custom error code
9
+ * @example
10
+ * ```ts
11
+ * if (!user) return notFound("User not found");
12
+ * ```
13
+ */
3
14
  export declare function notFound(detail?: string, code?: string): FlinkResponse<FlinkError>;
15
+ /**
16
+ * Returns a 409 Conflict error response.
17
+ * Use when a request conflicts with existing data (e.g., duplicate username/email).
18
+ *
19
+ * @param detail - Optional custom error message
20
+ * @param code - Optional custom error code
21
+ * @example
22
+ * ```ts
23
+ * if (existingUser) return conflict("Email already registered");
24
+ * ```
25
+ */
4
26
  export declare function conflict(detail?: string, code?: string): FlinkResponse<FlinkError>;
27
+ /**
28
+ * Returns a 400 Bad Request error response.
29
+ * Use when the request is malformed or contains invalid data (e.g., validation errors).
30
+ *
31
+ * @param detail - Optional custom error message
32
+ * @param code - Optional custom error code
33
+ * @example
34
+ * ```ts
35
+ * if (!email || !password) return badRequest("Email and password are required");
36
+ * ```
37
+ */
5
38
  export declare function badRequest(detail?: string, code?: string): FlinkResponse<FlinkError>;
39
+ /**
40
+ * Returns a 401 Unauthorized error response.
41
+ * Use when authentication is required but missing or invalid (e.g., no token, expired token).
42
+ * This means "who are you?" - the user needs to identify themselves first.
43
+ *
44
+ * @param detail - Optional custom error message
45
+ * @param code - Optional custom error code
46
+ * @example
47
+ * ```ts
48
+ * if (!ctx.auth?.user) return unauthorized("Authentication required");
49
+ * ```
50
+ */
6
51
  export declare function unauthorized(detail?: string, code?: string): FlinkResponse<FlinkError>;
52
+ /**
53
+ * Returns a 403 Forbidden error response.
54
+ * Use when the user is authenticated but lacks permission to access the resource.
55
+ * This means "I know who you are, but you're not allowed to do this."
56
+ *
57
+ * @param detail - Optional custom error message
58
+ * @param code - Optional custom error code
59
+ * @example
60
+ * ```ts
61
+ * if (ctx.auth?.user?.role !== "admin") return forbidden("Admin access required");
62
+ * ```
63
+ */
64
+ export declare function forbidden(detail?: string, code?: string): FlinkResponse<FlinkError>;
65
+ /**
66
+ * Returns a 500 Internal Server Error response.
67
+ * Use when an unexpected error occurs on the server side.
68
+ *
69
+ * @param detail - Optional custom error message
70
+ * @param code - Optional custom error code
71
+ * @example
72
+ * ```ts
73
+ * try { ... } catch (error) { return internalServerError("Failed to process request"); }
74
+ * ```
75
+ */
7
76
  export declare function internalServerError(detail?: string, code?: string): FlinkResponse<FlinkError>;
@@ -1,7 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.internalServerError = exports.unauthorized = exports.badRequest = exports.conflict = exports.notFound = void 0;
3
+ exports.notFound = notFound;
4
+ exports.conflict = conflict;
5
+ exports.badRequest = badRequest;
6
+ exports.unauthorized = unauthorized;
7
+ exports.forbidden = forbidden;
8
+ exports.internalServerError = internalServerError;
4
9
  var uuid_1 = require("uuid");
10
+ /**
11
+ * Returns a 404 Not Found error response.
12
+ * Use when a requested resource doesn't exist (e.g., invalid ID, missing entity).
13
+ *
14
+ * @param detail - Optional custom error message
15
+ * @param code - Optional custom error code
16
+ * @example
17
+ * ```ts
18
+ * if (!user) return notFound("User not found");
19
+ * ```
20
+ */
5
21
  function notFound(detail, code) {
6
22
  return {
7
23
  status: 404,
@@ -13,7 +29,17 @@ function notFound(detail, code) {
13
29
  },
14
30
  };
15
31
  }
16
- exports.notFound = notFound;
32
+ /**
33
+ * Returns a 409 Conflict error response.
34
+ * Use when a request conflicts with existing data (e.g., duplicate username/email).
35
+ *
36
+ * @param detail - Optional custom error message
37
+ * @param code - Optional custom error code
38
+ * @example
39
+ * ```ts
40
+ * if (existingUser) return conflict("Email already registered");
41
+ * ```
42
+ */
17
43
  function conflict(detail, code) {
18
44
  return {
19
45
  status: 409,
@@ -25,7 +51,17 @@ function conflict(detail, code) {
25
51
  },
26
52
  };
27
53
  }
28
- exports.conflict = conflict;
54
+ /**
55
+ * Returns a 400 Bad Request error response.
56
+ * Use when the request is malformed or contains invalid data (e.g., validation errors).
57
+ *
58
+ * @param detail - Optional custom error message
59
+ * @param code - Optional custom error code
60
+ * @example
61
+ * ```ts
62
+ * if (!email || !password) return badRequest("Email and password are required");
63
+ * ```
64
+ */
29
65
  function badRequest(detail, code) {
30
66
  return {
31
67
  status: 400,
@@ -37,19 +73,63 @@ function badRequest(detail, code) {
37
73
  },
38
74
  };
39
75
  }
40
- exports.badRequest = badRequest;
76
+ /**
77
+ * Returns a 401 Unauthorized error response.
78
+ * Use when authentication is required but missing or invalid (e.g., no token, expired token).
79
+ * This means "who are you?" - the user needs to identify themselves first.
80
+ *
81
+ * @param detail - Optional custom error message
82
+ * @param code - Optional custom error code
83
+ * @example
84
+ * ```ts
85
+ * if (!ctx.auth?.user) return unauthorized("Authentication required");
86
+ * ```
87
+ */
41
88
  function unauthorized(detail, code) {
42
89
  return {
43
90
  status: 401,
44
91
  error: {
45
92
  id: (0, uuid_1.v4)(),
46
93
  title: "Unauthorized",
47
- detail: detail || "User not logged in or or not allowed to access resource",
48
- code: code || "badRequest"
94
+ detail: detail || "Authentication required",
95
+ code: code || "unauthorized"
49
96
  },
50
97
  };
51
98
  }
52
- exports.unauthorized = unauthorized;
99
+ /**
100
+ * Returns a 403 Forbidden error response.
101
+ * Use when the user is authenticated but lacks permission to access the resource.
102
+ * This means "I know who you are, but you're not allowed to do this."
103
+ *
104
+ * @param detail - Optional custom error message
105
+ * @param code - Optional custom error code
106
+ * @example
107
+ * ```ts
108
+ * if (ctx.auth?.user?.role !== "admin") return forbidden("Admin access required");
109
+ * ```
110
+ */
111
+ function forbidden(detail, code) {
112
+ return {
113
+ status: 403,
114
+ error: {
115
+ id: (0, uuid_1.v4)(),
116
+ title: "Forbidden",
117
+ detail: detail || "You do not have permission to access this resource",
118
+ code: code || "forbidden"
119
+ },
120
+ };
121
+ }
122
+ /**
123
+ * Returns a 500 Internal Server Error response.
124
+ * Use when an unexpected error occurs on the server side.
125
+ *
126
+ * @param detail - Optional custom error message
127
+ * @param code - Optional custom error code
128
+ * @example
129
+ * ```ts
130
+ * try { ... } catch (error) { return internalServerError("Failed to process request"); }
131
+ * ```
132
+ */
53
133
  function internalServerError(detail, code) {
54
134
  return {
55
135
  status: 500,
@@ -61,4 +141,3 @@ function internalServerError(detail, code) {
61
141
  },
62
142
  };
63
143
  }
64
- exports.internalServerError = internalServerError;
@@ -10,7 +10,12 @@ export declare enum HttpMethod {
10
10
  delete = "delete"
11
11
  }
12
12
  type Params = Request["params"];
13
- type Query = Request["query"];
13
+ /**
14
+ * Query type for request query parameters.
15
+ * Does currently not allow nested objects, although
16
+ * underlying express Request does allow it.
17
+ */
18
+ type Query = Record<string, string | string[] | undefined>;
14
19
  /**
15
20
  * Flink request extends express Request but adds reqId and user object.
16
21
  */
@@ -63,6 +68,14 @@ export interface RouteProps {
63
68
  * I.e. filename or plugin name that describes where handler origins from
64
69
  */
65
70
  origin?: string;
71
+ /**
72
+ * Order handler should be registered in.
73
+ *
74
+ * By default all handlers has order 0 and in most cases this is fine,
75
+ * but if for example you want to register a handler before all others
76
+ * to avoid conflicts you can set a negative order.
77
+ */
78
+ order?: number;
66
79
  }
67
80
  /**
68
81
  * Http handler function that handlers implements in order to
@@ -11,7 +11,7 @@ export declare const log: {
11
11
  dim?: boolean | undefined;
12
12
  underscore?: boolean | undefined;
13
13
  strikethrough?: boolean | undefined;
14
- } | undefined) => void;
14
+ }) => void;
15
15
  fontColorLog: (ticket: "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white", text: string, setting?: {
16
16
  reverse?: boolean | undefined;
17
17
  bold?: boolean | undefined;
@@ -19,6 +19,6 @@ export declare const log: {
19
19
  dim?: boolean | undefined;
20
20
  underscore?: boolean | undefined;
21
21
  strikethrough?: boolean | undefined;
22
- } | undefined) => void;
22
+ }) => void;
23
23
  setLevel: (level: "debug" | "info" | "warn" | "error") => void;
24
24
  };
@@ -1,4 +1,4 @@
1
- import { Collection, Db, Document, ObjectId } from "mongodb";
1
+ import { Collection, Db, Document, MongoClient, ObjectId } from "mongodb";
2
2
  import { FlinkContext } from "./FlinkContext";
3
3
  /**
4
4
  * Partial model to have intellisense for partial updates but
@@ -8,13 +8,14 @@ type PartialModel<Model> = Partial<Model> & {
8
8
  [x: string]: any;
9
9
  };
10
10
  export declare abstract class FlinkRepo<C extends FlinkContext, Model extends Document> {
11
- private collectionName;
12
- private db;
11
+ collectionName: string;
12
+ db: Db;
13
+ client?: MongoClient | undefined;
13
14
  collection: Collection;
14
15
  private _ctx?;
15
16
  set ctx(ctx: FlinkContext);
16
17
  get ctx(): FlinkContext;
17
- constructor(collectionName: string, db: Db);
18
+ constructor(collectionName: string, db: Db, client?: MongoClient | undefined);
18
19
  findAll(query?: {}): Promise<Model[]>;
19
20
  getById(id: string | ObjectId): Promise<Model | null>;
20
21
  getOne(query?: {}): Promise<Model | null>;
@@ -24,7 +25,14 @@ export declare abstract class FlinkRepo<C extends FlinkContext, Model extends Do
24
25
  updateOne(id: string | ObjectId, model: PartialModel<Model>): Promise<Model | null>;
25
26
  updateMany<U = PartialModel<Model>>(query: any, model: U): Promise<number>;
26
27
  deleteById(id: string | ObjectId): Promise<number>;
27
- private buildId;
28
+ /**
29
+ * Helper to ensure the id is always an ObjectId.
30
+ * If a string is passed, it will be converted to an ObjectId.
31
+ * If an ObjectId is passed, it will be returned as is.
32
+ * @param id
33
+ * @returns
34
+ */
35
+ buildId(id: string | ObjectId): ObjectId;
28
36
  private objectIdToString;
29
37
  }
30
38
  export {};
@@ -20,8 +20,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
20
20
  });
21
21
  };
22
22
  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;
23
+ 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);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
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.");
@@ -61,9 +61,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
61
61
  exports.FlinkRepo = void 0;
62
62
  var mongodb_1 = require("mongodb");
63
63
  var FlinkRepo = /** @class */ (function () {
64
- function FlinkRepo(collectionName, db) {
64
+ function FlinkRepo(collectionName, db, client) {
65
65
  this.collectionName = collectionName;
66
66
  this.db = db;
67
+ this.client = client;
67
68
  this.collection = db.collection(this.collectionName);
68
69
  }
69
70
  Object.defineProperty(FlinkRepo.prototype, "ctx", {
@@ -192,6 +193,13 @@ var FlinkRepo = /** @class */ (function () {
192
193
  });
193
194
  });
194
195
  };
196
+ /**
197
+ * Helper to ensure the id is always an ObjectId.
198
+ * If a string is passed, it will be converted to an ObjectId.
199
+ * If an ObjectId is passed, it will be returned as is.
200
+ * @param id
201
+ * @returns
202
+ */
195
203
  FlinkRepo.prototype.buildId = function (id) {
196
204
  var oid;
197
205
  if (typeof id === "string") {
@@ -1,4 +1,3 @@
1
- import { Response, Request } from "express";
2
1
  export interface FlinkResponse<T = any> {
3
2
  /**
4
3
  * Unique id of request.
@@ -37,5 +36,3 @@ export interface FlinkResponse<T = any> {
37
36
  [x: string]: string;
38
37
  };
39
38
  }
40
- export type ExpressResponse = Response;
41
- export type ExpressRequest = Request;
@@ -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,9 @@ 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.writeJsonFile = exports.readJsonFiles = exports.readJsonFile = void 0;
42
+ exports.readJsonFile = readJsonFile;
43
+ exports.readJsonFiles = readJsonFiles;
44
+ exports.writeJsonFile = writeJsonFile;
43
45
  var fs_1 = require("fs");
44
46
  var path_1 = require("path");
45
47
  var tiny_glob_1 = __importDefault(require("tiny-glob"));
@@ -64,7 +66,6 @@ function readJsonFile(path) {
64
66
  });
65
67
  });
66
68
  }
67
- exports.readJsonFile = readJsonFile;
68
69
  function readJsonFiles(globPattern) {
69
70
  return __awaiter(this, void 0, void 0, function () {
70
71
  var files, readPromises;
@@ -88,7 +89,6 @@ function readJsonFiles(globPattern) {
88
89
  });
89
90
  });
90
91
  }
91
- exports.readJsonFiles = readJsonFiles;
92
92
  function writeJsonFile(path_2, content_1) {
93
93
  return __awaiter(this, arguments, void 0, function (path, content, opts) {
94
94
  var i, jsonStr;
@@ -117,4 +117,3 @@ function writeJsonFile(path_2, content_1) {
117
117
  });
118
118
  });
119
119
  }
120
- exports.writeJsonFile = writeJsonFile;
@@ -3,6 +3,7 @@ declare class TypeScriptCompiler {
3
3
  private cwd;
4
4
  private project;
5
5
  private schemaGenerator?;
6
+ private isEsm;
6
7
  /**
7
8
  * Parsed typescript schemas that will be added to intermediate
8
9
  * schemas.ts file.
@@ -17,6 +18,15 @@ declare class TypeScriptCompiler {
17
18
  */
18
19
  private tsSchemasSymbolsToImports;
19
20
  constructor(cwd: string);
21
+ /**
22
+ * Detects if the project is using ESM (ECMAScript Modules)
23
+ * by checking type in package.json.
24
+ */
25
+ private isEsmProject;
26
+ /**
27
+ * Gets the module specifier for imports, adding .js extension for ESM
28
+ */
29
+ private getModuleSpecifier;
20
30
  /**
21
31
  * Deletes all generated files.
22
32
  * @param cwd