axe-api 0.22.0 → 0.30.0-rc1

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 (145) hide show
  1. package/build/dev-kit/app/Config/Application.d.ts +3 -0
  2. package/build/dev-kit/app/Config/Application.js +14 -0
  3. package/build/dev-kit/app/Config/Database.d.ts +3 -0
  4. package/build/dev-kit/app/Config/Database.js +24 -0
  5. package/build/dev-kit/app/Events/UserEvent.d.ts +3 -0
  6. package/build/dev-kit/app/Events/UserEvent.js +16 -0
  7. package/build/dev-kit/app/Hooks/CustomerHook.d.ts +3 -0
  8. package/build/dev-kit/app/Hooks/CustomerHook.js +16 -0
  9. package/build/dev-kit/app/Hooks/EmployeeHook.d.ts +2 -0
  10. package/build/dev-kit/app/Hooks/EmployeeHook.js +16 -0
  11. package/build/dev-kit/app/Hooks/UserHook.d.ts +3 -0
  12. package/build/dev-kit/app/Hooks/UserHook.js +17 -0
  13. package/build/dev-kit/app/Models/Customer.d.ts +7 -0
  14. package/build/dev-kit/app/Models/Customer.js +15 -0
  15. package/build/dev-kit/app/Models/Employee.d.ts +9 -0
  16. package/build/dev-kit/app/Models/Employee.js +21 -0
  17. package/build/dev-kit/app/Models/EmployeeAddress.d.ts +8 -0
  18. package/build/dev-kit/app/Models/EmployeeAddress.js +18 -0
  19. package/build/dev-kit/app/Models/Post.d.ts +6 -0
  20. package/build/dev-kit/app/Models/Post.js +14 -0
  21. package/build/dev-kit/app/Models/User.d.ts +7 -0
  22. package/build/dev-kit/app/Models/User.js +23 -0
  23. package/build/dev-kit/app/Serialization/UserSerialization.d.ts +3 -0
  24. package/build/dev-kit/app/Serialization/UserSerialization.js +6 -0
  25. package/build/dev-kit/app/config.d.ts +3 -0
  26. package/build/dev-kit/app/config.js +31 -0
  27. package/build/dev-kit/app/init.d.ts +4 -0
  28. package/build/dev-kit/app/init.js +16 -0
  29. package/build/dev-kit/app/v1/Events/UserEvent.d.ts +3 -0
  30. package/build/dev-kit/app/v1/Events/UserEvent.js +16 -0
  31. package/build/dev-kit/app/v1/Hooks/CustomerHook.d.ts +3 -0
  32. package/build/dev-kit/app/v1/Hooks/CustomerHook.js +16 -0
  33. package/build/dev-kit/app/v1/Hooks/EmployeeHook.d.ts +2 -0
  34. package/build/dev-kit/app/v1/Hooks/EmployeeHook.js +16 -0
  35. package/build/dev-kit/app/v1/Hooks/UserHook.d.ts +3 -0
  36. package/build/dev-kit/app/v1/Hooks/UserHook.js +17 -0
  37. package/build/dev-kit/app/v1/Models/Customer.d.ts +7 -0
  38. package/build/dev-kit/app/v1/Models/Customer.js +15 -0
  39. package/build/dev-kit/app/v1/Models/Employee.d.ts +9 -0
  40. package/build/dev-kit/app/v1/Models/Employee.js +21 -0
  41. package/build/dev-kit/app/v1/Models/EmployeeAddress.d.ts +8 -0
  42. package/build/dev-kit/app/v1/Models/EmployeeAddress.js +18 -0
  43. package/build/dev-kit/app/v1/Models/Post.d.ts +6 -0
  44. package/build/dev-kit/app/v1/Models/Post.js +14 -0
  45. package/build/dev-kit/app/v1/Models/User.d.ts +7 -0
  46. package/build/dev-kit/app/v1/Models/User.js +23 -0
  47. package/build/dev-kit/app/v1/Serialization/PostSerialization.d.ts +3 -0
  48. package/build/dev-kit/app/v1/Serialization/PostSerialization.js +6 -0
  49. package/build/dev-kit/app/v1/Serialization/UserSerialization.d.ts +3 -0
  50. package/build/dev-kit/app/v1/Serialization/UserSerialization.js +5 -0
  51. package/build/dev-kit/app/v1/config.d.ts +3 -0
  52. package/build/dev-kit/app/v1/config.js +19 -0
  53. package/build/dev-kit/app/v1/init.d.ts +4 -0
  54. package/build/dev-kit/app/v1/init.js +16 -0
  55. package/build/dev-kit/app/v2/Events/UserEvent.d.ts +3 -0
  56. package/build/dev-kit/app/v2/Events/UserEvent.js +16 -0
  57. package/build/dev-kit/app/v2/Hooks/CustomerHook.d.ts +3 -0
  58. package/build/dev-kit/app/v2/Hooks/CustomerHook.js +16 -0
  59. package/build/dev-kit/app/v2/Hooks/EmployeeHook.d.ts +2 -0
  60. package/build/dev-kit/app/v2/Hooks/EmployeeHook.js +16 -0
  61. package/build/dev-kit/app/v2/Hooks/UserHook.d.ts +3 -0
  62. package/build/dev-kit/app/v2/Hooks/UserHook.js +17 -0
  63. package/build/dev-kit/app/v2/Models/Customer.d.ts +10 -0
  64. package/build/dev-kit/app/v2/Models/Customer.js +20 -0
  65. package/build/dev-kit/app/v2/Models/Employee.d.ts +9 -0
  66. package/build/dev-kit/app/v2/Models/Employee.js +21 -0
  67. package/build/dev-kit/app/v2/Models/EmployeeAddress.d.ts +8 -0
  68. package/build/dev-kit/app/v2/Models/EmployeeAddress.js +18 -0
  69. package/build/dev-kit/app/v2/Models/Post.d.ts +6 -0
  70. package/build/dev-kit/app/v2/Models/Post.js +14 -0
  71. package/build/dev-kit/app/v2/Models/User.d.ts +7 -0
  72. package/build/dev-kit/app/v2/Models/User.js +23 -0
  73. package/build/dev-kit/app/v2/Serialization/UserSerialization.d.ts +3 -0
  74. package/build/dev-kit/app/v2/Serialization/UserSerialization.js +5 -0
  75. package/build/dev-kit/app/v2/config.d.ts +3 -0
  76. package/build/dev-kit/app/v2/config.js +9 -0
  77. package/build/dev-kit/app/v2/init.d.ts +4 -0
  78. package/build/dev-kit/app/v2/init.js +16 -0
  79. package/build/dev-kit/app/v3/config.d.ts +3 -0
  80. package/build/dev-kit/app/v3/config.js +9 -0
  81. package/build/dev-kit/config.d.ts +3 -0
  82. package/build/dev-kit/config.js +31 -0
  83. package/build/dev-kit.d.ts +1 -0
  84. package/build/dev-kit.js +16 -0
  85. package/build/package.json +67 -0
  86. package/build/src/Builders/ModelTreeBuilder.d.ts +3 -0
  87. package/build/src/Builders/ModelTreeBuilder.js +21 -17
  88. package/build/src/Builders/RouterBuilder.d.ts +3 -0
  89. package/build/src/Builders/RouterBuilder.js +27 -25
  90. package/build/src/Enums.d.ts +8 -0
  91. package/build/src/Enums.js +10 -1
  92. package/build/src/Exceptions/AxeError.d.ts +8 -0
  93. package/build/src/Exceptions/AxeError.js +11 -0
  94. package/build/src/Handlers/AllHandler.js +4 -5
  95. package/build/src/Handlers/DocsHandler.d.ts +3 -0
  96. package/build/src/Handlers/DocsHandler.js +22 -0
  97. package/build/src/Handlers/HandlerFactory.d.ts +1 -1
  98. package/build/src/Handlers/HandlerFactory.js +1 -1
  99. package/build/src/Handlers/Helpers.d.ts +4 -3
  100. package/build/src/Handlers/Helpers.js +9 -10
  101. package/build/src/Handlers/PaginateHandler.js +4 -5
  102. package/build/src/Handlers/PatchHandler.js +2 -2
  103. package/build/src/Handlers/RoutesHandler.d.ts +3 -0
  104. package/build/src/Handlers/RoutesHandler.js +16 -0
  105. package/build/src/Handlers/ShowHandler.js +4 -5
  106. package/build/src/Handlers/StoreHandler.js +2 -2
  107. package/build/src/Handlers/UpdateHandler.js +2 -2
  108. package/build/src/Helpers.d.ts +4 -0
  109. package/build/src/Helpers.js +78 -0
  110. package/build/src/Interfaces.d.ts +34 -11
  111. package/build/src/Middlewares/acceptLanguageMiddleware.js +3 -5
  112. package/build/src/Model.d.ts +0 -1
  113. package/build/src/Model.js +0 -4
  114. package/build/src/Resolvers/FolderResolver.js +1 -0
  115. package/build/src/Resolvers/GeneralHookResolver.d.ts +4 -2
  116. package/build/src/Resolvers/GeneralHookResolver.js +5 -8
  117. package/build/src/Resolvers/ModelResolver.d.ts +4 -0
  118. package/build/src/Resolvers/ModelResolver.js +26 -8
  119. package/build/src/Resolvers/TransactionResolver.d.ts +4 -2
  120. package/build/src/Resolvers/TransactionResolver.js +5 -4
  121. package/build/src/Resolvers/VersionConfigResolver.d.ts +7 -0
  122. package/build/src/Resolvers/VersionConfigResolver.js +58 -0
  123. package/build/src/Resolvers/VersionResolver.d.ts +7 -0
  124. package/build/src/Resolvers/VersionResolver.js +65 -0
  125. package/build/src/Resolvers/index.d.ts +3 -2
  126. package/build/src/Resolvers/index.js +5 -3
  127. package/build/src/Server.d.ts +3 -2
  128. package/build/src/Server.js +73 -46
  129. package/build/src/Services/APIService.d.ts +16 -0
  130. package/build/src/Services/APIService.js +79 -0
  131. package/build/src/Services/DocumentationService.d.ts +3 -1
  132. package/build/src/Services/DocumentationService.js +6 -0
  133. package/build/src/Services/LogService.d.ts +4 -1
  134. package/build/src/Services/LogService.js +6 -0
  135. package/build/src/Services/ModelService.d.ts +3 -0
  136. package/build/src/Services/ModelService.js +4 -0
  137. package/build/src/Services/SchemaValidatorService.d.ts +3 -0
  138. package/build/src/Services/SchemaValidatorService.js +12 -8
  139. package/build/src/Services/index.d.ts +2 -1
  140. package/build/src/Services/index.js +3 -1
  141. package/build/src/Types.d.ts +2 -0
  142. package/build/src/Types.js +2 -0
  143. package/build/src/constants.js +0 -1
  144. package/package.json +2 -1
  145. package/CHANGELOG.md +0 -212
@@ -13,15 +13,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const pluralize_1 = __importDefault(require("pluralize"));
16
+ const path_1 = __importDefault(require("path"));
16
17
  const http_status_codes_1 = require("http-status-codes");
17
18
  const change_case_1 = require("change-case");
18
19
  const Resolvers_1 = require("../Resolvers");
19
20
  const constants_1 = require("../constants");
20
21
  const Enums_1 = require("../Enums");
22
+ const HandlerFactory_1 = __importDefault(require("../Handlers/HandlerFactory"));
21
23
  const Services_1 = require("../Services");
22
24
  const Middlewares_1 = require("../Middlewares");
23
25
  class RouterBuilder {
24
- constructor() {
26
+ constructor(version) {
25
27
  this.getPrimaryKeyName = (model) => {
26
28
  return (pluralize_1.default.singular(model.name).toLowerCase() +
27
29
  this.ucFirst(model.instance.primaryKey));
@@ -30,9 +32,8 @@ class RouterBuilder {
30
32
  return value.charAt(0).toUpperCase() + value.slice(1);
31
33
  };
32
34
  this.getRootPrefix = () => __awaiter(this, void 0, void 0, function* () {
33
- var _a;
34
- const config = yield Services_1.IoCService.use("Config");
35
- let prefix = ((_a = config === null || config === void 0 ? void 0 : config.Application) === null || _a === void 0 ? void 0 : _a.prefix) || "api";
35
+ const api = Services_1.APIService.getInstance();
36
+ let prefix = api.config.prefix || "api";
36
37
  if (prefix.substr(0, 1) === "/") {
37
38
  prefix = prefix.substr(1);
38
39
  }
@@ -41,32 +42,31 @@ class RouterBuilder {
41
42
  }
42
43
  return prefix;
43
44
  });
45
+ this.version = version;
44
46
  }
45
47
  build() {
46
48
  return __awaiter(this, void 0, void 0, function* () {
47
49
  const app = yield Services_1.IoCService.useByType("App");
48
- const logger = yield Services_1.IoCService.useByType("LogService");
49
- const modelTree = yield Services_1.IoCService.useByType("ModelTree");
50
- const modelList = yield Services_1.IoCService.useByType("ModelListService");
51
- const generalHooks = yield Resolvers_1.GeneralHookResolver.resolve();
50
+ const logger = Services_1.LogService.getInstance();
51
+ const generalHooks = yield new Resolvers_1.GeneralHookResolver(this.version).resolve();
52
52
  if (generalHooks.onBeforeInit) {
53
53
  generalHooks.onBeforeInit(app);
54
54
  }
55
- yield this.createRoutesByModelTree(modelTree, modelList);
56
- logger.info("Express routes have been created.");
55
+ yield this.createRoutesByModelTree();
56
+ logger.info(`[${this.version.name}] Express routes have been created.`);
57
57
  if (generalHooks.onAfterInit) {
58
58
  generalHooks.onAfterInit(app);
59
59
  }
60
60
  });
61
61
  }
62
- createRoutesByModelTree(modelTree, modelList) {
62
+ createRoutesByModelTree() {
63
63
  return __awaiter(this, void 0, void 0, function* () {
64
- for (const model of modelTree) {
65
- yield this.createRouteByModel(model, modelList);
64
+ for (const model of this.version.modelTree) {
65
+ yield this.createRouteByModel(model);
66
66
  }
67
67
  });
68
68
  }
69
- createRouteByModel(model, modelList, urlPrefix = "", parentModel = null, relation = null, allowRecursive = true) {
69
+ createRouteByModel(model, urlPrefix = "", parentModel = null, relation = null, allowRecursive = true) {
70
70
  return __awaiter(this, void 0, void 0, function* () {
71
71
  if (model.instance.ignore) {
72
72
  return;
@@ -79,7 +79,7 @@ class RouterBuilder {
79
79
  continue;
80
80
  }
81
81
  const urlCreator = constants_1.API_ROUTE_TEMPLATES[handlerType];
82
- const url = urlCreator(yield this.getRootPrefix(), urlPrefix, resource, model.instance.primaryKey);
82
+ const url = urlCreator(path_1.default.join(yield this.getRootPrefix(), this.version.name), urlPrefix, resource, model.instance.primaryKey);
83
83
  // Creating the middleware list for the route. As default, we support some
84
84
  // internal middlewares such as `Accept Language Middleware` which parse
85
85
  // the "accept-language" header to use in the application general.
@@ -90,11 +90,11 @@ class RouterBuilder {
90
90
  // Adding the route to the express
91
91
  yield this.addExpressRoute(handlerType, url, middlewares, model, parentModel, relation);
92
92
  }
93
- yield this.createChildRoutes(model, modelList, resource, urlPrefix);
94
- yield this.createNestedRoutes(model, modelList, allowRecursive, urlPrefix, resource);
93
+ yield this.createChildRoutes(model, resource, urlPrefix);
94
+ yield this.createNestedRoutes(model, allowRecursive, urlPrefix, resource);
95
95
  });
96
96
  }
97
- createNestedRoutes(model, modelList, allowRecursive, urlPrefix, resource) {
97
+ createNestedRoutes(model, allowRecursive, urlPrefix, resource) {
98
98
  return __awaiter(this, void 0, void 0, function* () {
99
99
  if (!model.isRecursive || !allowRecursive) {
100
100
  return;
@@ -103,11 +103,11 @@ class RouterBuilder {
103
103
  const relation = model.relations.find((relation) => relation.model === model.name &&
104
104
  relation.type === Enums_1.Relationships.HAS_MANY);
105
105
  if (relation) {
106
- yield this.createRouteByModel(model, modelList, `${urlPrefix}${resource}/:${(0, change_case_1.camelCase)(relation.foreignKey)}/`, model, relation, false);
106
+ yield this.createRouteByModel(model, `${urlPrefix}${resource}/:${(0, change_case_1.camelCase)(relation.foreignKey)}/`, model, relation, false);
107
107
  }
108
108
  });
109
109
  }
110
- createChildRoutes(model, modelList, resource, urlPrefix) {
110
+ createChildRoutes(model, resource, urlPrefix) {
111
111
  return __awaiter(this, void 0, void 0, function* () {
112
112
  if (model.children.length === 0) {
113
113
  return;
@@ -118,14 +118,14 @@ class RouterBuilder {
118
118
  const child = model.children.find((item) => item.name === relation.model);
119
119
  // It should be recursive
120
120
  if (child) {
121
- yield this.createRouteByModel(child, modelList, `${urlPrefix}${resource}/:${(0, change_case_1.camelCase)(relation.foreignKey)}/`, model, relation);
121
+ yield this.createRouteByModel(child, `${urlPrefix}${resource}/:${(0, change_case_1.camelCase)(relation.foreignKey)}/`, model, relation);
122
122
  }
123
123
  }
124
124
  });
125
125
  }
126
126
  addExpressRoute(handlerType, url, middlewares, model, parentModel, relation) {
127
127
  return __awaiter(this, void 0, void 0, function* () {
128
- const docs = yield Services_1.IoCService.useByType("DocumentationService");
128
+ const docs = Services_1.DocumentationService.getInstance();
129
129
  const app = yield Services_1.IoCService.useByType("App");
130
130
  const handler = (req, res) => {
131
131
  this.requestHandler(handlerType, req, res, model, parentModel, relation);
@@ -173,14 +173,16 @@ class RouterBuilder {
173
173
  let trx = null;
174
174
  let hasTransaction = false;
175
175
  try {
176
- const factory = yield Services_1.IoCService.useByType("HandlerFactory");
177
176
  const database = (yield Services_1.IoCService.use("Database"));
178
- hasTransaction = yield Resolvers_1.TransactionResolver.resolve(model, handlerType);
177
+ const api = Services_1.APIService.getInstance();
178
+ hasTransaction = yield new Resolvers_1.TransactionResolver(this.version).resolve(model, handlerType);
179
179
  if (hasTransaction) {
180
180
  trx = yield database.transaction();
181
181
  }
182
- const handler = factory.get(handlerType);
182
+ const handler = HandlerFactory_1.default.get(handlerType);
183
183
  const pack = {
184
+ api,
185
+ version: this.version,
184
186
  req,
185
187
  res,
186
188
  handlerType,
@@ -80,3 +80,11 @@ export declare enum TimestampColumns {
80
80
  CREATED_AT = "createdAtColumn",
81
81
  UPDATED_AT = "updatedAtColumn"
82
82
  }
83
+ export declare enum AxeErrorCode {
84
+ UNACCEPTABLE_VERSION_NAME = "UNACCEPTABLE_VERSION_NAME",
85
+ VERSION_CONFIG_NOT_FOUND = "VERSION_CONFIG_NOT_FOUND",
86
+ TABLE_DOESNT_HAVE_ANY_COLUMN = "TABLE_DOESNT_HAVE_ANY_COLUMN",
87
+ RESERVED_VERSION_NAME = "RESERVED_VERSION_NAME",
88
+ UNDEFINED_COLUMN = "UNDEFINED_COLUMN",
89
+ UNDEFINED_RELATION_MODEL = "UNDEFINED_RELATION_MODEL"
90
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TimestampColumns = exports.SortTypes = exports.Relationships = exports.LogLevels = exports.HttpMethods = exports.Extensions = exports.HookFunctionTypes = exports.HandlerTypes = exports.DependencyTypes = exports.ConditionTypes = void 0;
3
+ exports.AxeErrorCode = exports.TimestampColumns = exports.SortTypes = exports.Relationships = exports.LogLevels = exports.HttpMethods = exports.Extensions = exports.HookFunctionTypes = exports.HandlerTypes = exports.DependencyTypes = exports.ConditionTypes = void 0;
4
4
  var ConditionTypes;
5
5
  (function (ConditionTypes) {
6
6
  ConditionTypes["NotNull"] = "NotNull";
@@ -93,3 +93,12 @@ var TimestampColumns;
93
93
  TimestampColumns["CREATED_AT"] = "createdAtColumn";
94
94
  TimestampColumns["UPDATED_AT"] = "updatedAtColumn";
95
95
  })(TimestampColumns = exports.TimestampColumns || (exports.TimestampColumns = {}));
96
+ var AxeErrorCode;
97
+ (function (AxeErrorCode) {
98
+ AxeErrorCode["UNACCEPTABLE_VERSION_NAME"] = "UNACCEPTABLE_VERSION_NAME";
99
+ AxeErrorCode["VERSION_CONFIG_NOT_FOUND"] = "VERSION_CONFIG_NOT_FOUND";
100
+ AxeErrorCode["TABLE_DOESNT_HAVE_ANY_COLUMN"] = "TABLE_DOESNT_HAVE_ANY_COLUMN";
101
+ AxeErrorCode["RESERVED_VERSION_NAME"] = "RESERVED_VERSION_NAME";
102
+ AxeErrorCode["UNDEFINED_COLUMN"] = "UNDEFINED_COLUMN";
103
+ AxeErrorCode["UNDEFINED_RELATION_MODEL"] = "UNDEFINED_RELATION_MODEL";
104
+ })(AxeErrorCode = exports.AxeErrorCode || (exports.AxeErrorCode = {}));
@@ -0,0 +1,8 @@
1
+ import { AxeErrorCode } from "src/Enums";
2
+ declare class AxeError extends Error {
3
+ type: string;
4
+ code: AxeErrorCode;
5
+ message: string;
6
+ constructor(code: AxeErrorCode, message: string);
7
+ }
8
+ export default AxeError;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class AxeError extends Error {
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.type = "AxeError";
7
+ this.code = code;
8
+ this.message = message;
9
+ }
10
+ }
11
+ exports.default = AxeError;
@@ -13,9 +13,8 @@ const Helpers_1 = require("./Helpers");
13
13
  const Services_1 = require("../Services");
14
14
  const Enums_1 = require("../Enums");
15
15
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
16
- const modelList = yield Services_1.IoCService.useByType("ModelListService");
17
- const { model, req, res, database, relation, parentModel } = pack;
18
- const queryParser = new Services_1.QueryService(model, modelList.get());
16
+ const { version, model, req, res, database, relation, parentModel } = pack;
17
+ const queryParser = new Services_1.QueryService(model, version.modelList.get());
19
18
  // We should parse URL query string to use as condition in Lucid query
20
19
  const conditions = queryParser.get(req.query);
21
20
  // Creating a new database query
@@ -34,12 +33,12 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
34
33
  queryParser.applySorting(query, conditions.sort);
35
34
  let result = yield query;
36
35
  // We should try to get related data if there is any
37
- yield (0, Helpers_1.getRelatedData)(result, conditions.with, model, modelList, database, Enums_1.HandlerTypes.ALL, req);
36
+ yield (0, Helpers_1.getRelatedData)(version, result, conditions.with, model, version.modelList, database, Enums_1.HandlerTypes.ALL, req);
38
37
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterAll, Object.assign(Object.assign({}, pack), { result,
39
38
  conditions,
40
39
  query }));
41
40
  // Serializing the data by the model's serialize method
42
- result = yield (0, Helpers_1.serializeData)(result, model.instance.serialize, Enums_1.HandlerTypes.ALL, req);
41
+ result = yield (0, Helpers_1.serializeData)(version, result, model.serialize, Enums_1.HandlerTypes.ALL, req);
43
42
  // Filtering hidden fields from the response data.
44
43
  (0, Helpers_1.filterHiddenFields)(result, model.instance.hiddens);
45
44
  return res.json(result);
@@ -0,0 +1,3 @@
1
+ import { Request, Response } from "express";
2
+ declare const _default: (req: Request, res: Response) => Promise<void>;
3
+ export default _default;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const Services_1 = require("../Services");
13
+ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
14
+ const docs = Services_1.DocumentationService.getInstance();
15
+ const api = Services_1.APIService.getInstance();
16
+ res.json({
17
+ routes: docs.get(),
18
+ versions: api.versions.map((version) => {
19
+ return Object.assign(Object.assign({}, version), { config: Object.assign(Object.assign({}, version.config), { transaction: undefined }), folders: undefined, modelList: undefined, models: version.modelList.get() });
20
+ }),
21
+ });
22
+ });
@@ -1,6 +1,6 @@
1
1
  import { IRequestPack } from "../Interfaces";
2
2
  import { HandlerTypes } from "../Enums";
3
3
  declare class HandlerFactory {
4
- get(handleType: HandlerTypes): (pack: IRequestPack) => void;
4
+ static get(handleType: HandlerTypes): (pack: IRequestPack) => void;
5
5
  }
6
6
  export default HandlerFactory;
@@ -13,7 +13,7 @@ const ShowHandler_1 = __importDefault(require("./ShowHandler"));
13
13
  const StoreHandler_1 = __importDefault(require("./StoreHandler"));
14
14
  const Enums_1 = require("../Enums");
15
15
  class HandlerFactory {
16
- get(handleType) {
16
+ static get(handleType) {
17
17
  switch (handleType) {
18
18
  case Enums_1.HandlerTypes.ALL:
19
19
  return AllHandler_1.default;
@@ -1,15 +1,16 @@
1
1
  import { Request } from "express";
2
- import { IModelService, IRelation, IHookParameter, IQuery } from "../Interfaces";
2
+ import { IModelService, IRelation, IHookParameter, IQuery, IVersion } from "../Interfaces";
3
3
  import { Knex } from "knex";
4
4
  import { IWith } from "../Interfaces";
5
5
  import { HandlerTypes, HookFunctionTypes, TimestampColumns } from "../Enums";
6
6
  import { ModelListService } from "../Services";
7
+ import { SerializationFunction } from "../Types";
7
8
  export declare const bindTimestampValues: (formData: Record<string, any>, columnTypes: TimestampColumns[] | undefined, model: IModelService) => void;
8
9
  export declare const getMergedFormData: (req: Request, fillables: string[]) => Record<string, any>;
9
10
  export declare const callHooks: (model: IModelService, type: HookFunctionTypes, params: IHookParameter) => Promise<void>;
10
11
  export declare const getParentColumn: (relation: IRelation | null) => string | null;
11
12
  export declare const addForeignKeyQuery: (request: Request, query: Knex.QueryBuilder, relation: IRelation | null, parentModel: IModelService | null) => void;
12
- export declare const serializeData: (itemArray: any[] | any, modelSerializer: (data: any, request: Request) => void, handler: HandlerTypes, request: Request) => Promise<any[]>;
13
+ export declare const serializeData: (version: IVersion, itemArray: any[] | any, modelSerializer: SerializationFunction | null, handler: HandlerTypes, request: Request) => Promise<any[]>;
13
14
  export declare const filterHiddenFields: (itemArray: any[], hiddens: string[] | null) => void;
14
15
  export declare const addSoftDeleteQuery: (model: IModelService, conditions: IQuery | null, query: Knex.QueryBuilder) => void;
15
- export declare const getRelatedData: (data: any[], withArray: IWith[], model: IModelService, modelList: ModelListService, database: Knex | Knex.Transaction, handler: HandlerTypes, request: Request) => Promise<void>;
16
+ export declare const getRelatedData: (version: IVersion, data: any[], withArray: IWith[], model: IModelService, modelList: ModelListService, database: Knex | Knex.Transaction, handler: HandlerTypes, request: Request) => Promise<void>;
@@ -30,7 +30,7 @@ const bindTimestampValues = (formData, columnTypes = [], model) => {
30
30
  exports.bindTimestampValues = bindTimestampValues;
31
31
  const getMergedFormData = (req, fillables) => {
32
32
  const formData = {};
33
- Object.keys(req.body).forEach((key) => {
33
+ Object.keys((req === null || req === void 0 ? void 0 : req.body) || {}).forEach((key) => {
34
34
  if (fillables.includes(key)) {
35
35
  formData[key] = req.body[key];
36
36
  }
@@ -92,14 +92,13 @@ const serialize = (data, callback, request) => {
92
92
  }
93
93
  return callback(data, request);
94
94
  };
95
- const globalSerializer = (itemArray, handler, request) => __awaiter(void 0, void 0, void 0, function* () {
96
- const Application = yield Services_1.IoCService.useByType("Config");
97
- if (!Application.serializers) {
95
+ const globalSerializer = (version, itemArray, handler, request) => __awaiter(void 0, void 0, void 0, function* () {
96
+ if (!version.config.serializers) {
98
97
  return itemArray;
99
98
  }
100
99
  const callbacks = [];
101
100
  // Push all runable serializer into callbacks.
102
- Application.serializers.map((configSerializer) => {
101
+ version.config.serializers.map((configSerializer) => {
103
102
  // Serialize data for all requests types.
104
103
  if (typeof configSerializer === "function") {
105
104
  callbacks.push(configSerializer);
@@ -116,9 +115,9 @@ const globalSerializer = (itemArray, handler, request) => __awaiter(void 0, void
116
115
  });
117
116
  return itemArray;
118
117
  });
119
- const serializeData = (itemArray, modelSerializer, handler, request) => __awaiter(void 0, void 0, void 0, function* () {
118
+ const serializeData = (version, itemArray, modelSerializer, handler, request) => __awaiter(void 0, void 0, void 0, function* () {
120
119
  itemArray = serialize(itemArray, modelSerializer, request);
121
- itemArray = yield globalSerializer(itemArray, handler, request);
120
+ itemArray = yield globalSerializer(version, itemArray, handler, request);
122
121
  return itemArray;
123
122
  });
124
123
  exports.serializeData = serializeData;
@@ -146,7 +145,7 @@ const addSoftDeleteQuery = (model, conditions, query) => {
146
145
  }
147
146
  };
148
147
  exports.addSoftDeleteQuery = addSoftDeleteQuery;
149
- const getRelatedData = (data, withArray, model, modelList, database, handler, request) => __awaiter(void 0, void 0, void 0, function* () {
148
+ const getRelatedData = (version, data, withArray, model, modelList, database, handler, request) => __awaiter(void 0, void 0, void 0, function* () {
150
149
  if (withArray.length === 0) {
151
150
  return;
152
151
  }
@@ -208,12 +207,12 @@ const getRelatedData = (data, withArray, model, modelList, database, handler, re
208
207
  // Fetching related records by foreignKey and primary key values.
209
208
  let relatedRecords = yield foreignModelQuery.whereIn(searchFieldKey, parentPrimaryKeyValues);
210
209
  // We should serialize related data if there is any serialization function
211
- relatedRecords = yield (0, exports.serializeData)(relatedRecords, foreignModel.instance.serialize, handler, request);
210
+ relatedRecords = yield (0, exports.serializeData)(version, relatedRecords, foreignModel.serialize, handler, request);
212
211
  // We should hide hidden fields if there is any
213
212
  (0, exports.filterHiddenFields)(relatedRecords, foreignModel.instance.hiddens);
214
213
  // We should try to get child data if there is any on the query
215
214
  if (clientQuery.children.length > 0) {
216
- yield (0, exports.getRelatedData)(relatedRecords, clientQuery.children, foreignModel, modelList, database, handler, request);
215
+ yield (0, exports.getRelatedData)(version, relatedRecords, clientQuery.children, foreignModel, modelList, database, handler, request);
217
216
  }
218
217
  // Binding relation data to the parent rows.
219
218
  data.forEach((row) => {
@@ -13,9 +13,8 @@ const Helpers_1 = require("./Helpers");
13
13
  const Enums_1 = require("../Enums");
14
14
  const Services_1 = require("../Services");
15
15
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
16
- const modelList = yield Services_1.IoCService.useByType("ModelListService");
17
- const { model, req, database, relation, parentModel } = pack;
18
- const queryParser = new Services_1.QueryService(model, modelList.get());
16
+ const { version, model, req, database, relation, parentModel } = pack;
17
+ const queryParser = new Services_1.QueryService(model, version.modelList.get());
19
18
  // We should parse URL query string to use as condition in Lucid query
20
19
  const conditions = queryParser.get(req.query);
21
20
  // Creating a new database query
@@ -38,12 +37,12 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
38
37
  isLengthAware: true,
39
38
  });
40
39
  // We should try to get related data if there is any
41
- yield (0, Helpers_1.getRelatedData)(result.data, conditions.with, model, modelList, database, Enums_1.HandlerTypes.PAGINATE, req);
40
+ yield (0, Helpers_1.getRelatedData)(version, result.data, conditions.with, model, version.modelList, database, Enums_1.HandlerTypes.PAGINATE, req);
42
41
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterPaginate, Object.assign(Object.assign({}, pack), { conditions,
43
42
  result,
44
43
  query }));
45
44
  // Serializing the data by the model's serialize method
46
- result.data = yield (0, Helpers_1.serializeData)(result.data, model.instance.serialize, Enums_1.HandlerTypes.PAGINATE, req);
45
+ result.data = yield (0, Helpers_1.serializeData)(version, result.data, model.serialize, Enums_1.HandlerTypes.PAGINATE, req);
47
46
  // Filtering hidden fields from the response data.
48
47
  (0, Helpers_1.filterHiddenFields)(result.data, model.instance.hiddens);
49
48
  return pack.res.json(result);
@@ -17,7 +17,7 @@ const Helpers_1 = require("./Helpers");
17
17
  const Enums_1 = require("../Enums");
18
18
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
19
19
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
20
- const { model, req, res, database, relation, parentModel } = pack;
20
+ const { version, model, req, res, database, relation, parentModel } = pack;
21
21
  const query = database.from(model.instance.table);
22
22
  // If there is a relation, we should bind it
23
23
  (0, Helpers_1.addForeignKeyQuery)(req, query, relation, parentModel);
@@ -60,7 +60,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
60
60
  formData,
61
61
  query }));
62
62
  // Serializing the data by the model's serialize method
63
- item = yield (0, Helpers_1.serializeData)(item, model.instance.serialize, Enums_1.HandlerTypes.PATCH, req);
63
+ item = yield (0, Helpers_1.serializeData)(version, item, model.serialize, Enums_1.HandlerTypes.PATCH, req);
64
64
  // Filtering hidden fields from the response data.
65
65
  (0, Helpers_1.filterHiddenFields)([item], model.instance.hiddens);
66
66
  return res.json(item);
@@ -0,0 +1,3 @@
1
+ import { Request, Response } from "express";
2
+ declare const _default: (req: Request, res: Response) => Promise<void>;
3
+ export default _default;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const Services_1 = require("../Services");
13
+ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
14
+ const docs = Services_1.DocumentationService.getInstance();
15
+ res.json(docs.get().map((route) => `${route.method} ${route.url}`));
16
+ });
@@ -17,9 +17,8 @@ const Enums_1 = require("../Enums");
17
17
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
18
18
  const Services_1 = require("../Services");
19
19
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
20
- const modelList = yield Services_1.IoCService.useByType("ModelListService");
21
- const { model, req, res, database, relation, parentModel } = pack;
22
- const queryParser = new Services_1.QueryService(model, modelList.get());
20
+ const { version, model, req, res, database, relation, parentModel } = pack;
21
+ const queryParser = new Services_1.QueryService(model, version.modelList.get());
23
22
  // We should parse URL query string to use as condition in Lucid query
24
23
  const conditions = queryParser.get(req.query);
25
24
  // Fetching item
@@ -41,12 +40,12 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
41
40
  throw new ApiError_1.default(`The item is not found on ${model.name}.`);
42
41
  }
43
42
  // We should try to get related data if there is any
44
- yield (0, Helpers_1.getRelatedData)([item], conditions.with, model, modelList, database, Enums_1.HandlerTypes.ALL, req);
43
+ yield (0, Helpers_1.getRelatedData)(version, [item], conditions.with, model, version.modelList, database, Enums_1.HandlerTypes.ALL, req);
45
44
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterShow, Object.assign(Object.assign({}, pack), { query,
46
45
  conditions,
47
46
  item }));
48
47
  // Serializing the data by the model's serialize method
49
- item = yield (0, Helpers_1.serializeData)(item, model.instance.serialize, Enums_1.HandlerTypes.SHOW, req);
48
+ item = yield (0, Helpers_1.serializeData)(version, item, model.serialize, Enums_1.HandlerTypes.SHOW, req);
50
49
  // Filtering hidden fields from the response data.
51
50
  (0, Helpers_1.filterHiddenFields)([item], model.instance.hiddens);
52
51
  return res.json(item);
@@ -16,7 +16,7 @@ const validatorjs_1 = __importDefault(require("validatorjs"));
16
16
  const Helpers_1 = require("./Helpers");
17
17
  const Enums_1 = require("../Enums");
18
18
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
19
- const { model, req, res, database, relation, parentModel } = pack;
19
+ const { version, model, req, res, database, relation, parentModel } = pack;
20
20
  const requestMethod = req.method;
21
21
  const fillables = model.instance.getFillableFields(requestMethod);
22
22
  const formData = (0, Helpers_1.getMergedFormData)(req, fillables);
@@ -55,7 +55,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
55
55
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterInsert, Object.assign(Object.assign({}, pack), { formData,
56
56
  item }));
57
57
  // Serializing the data by the model's serialize method
58
- item = yield (0, Helpers_1.serializeData)(item, model.instance.serialize, Enums_1.HandlerTypes.INSERT, req);
58
+ item = yield (0, Helpers_1.serializeData)(version, item, model.serialize, Enums_1.HandlerTypes.INSERT, req);
59
59
  // Filtering hidden fields from the response data.
60
60
  (0, Helpers_1.filterHiddenFields)([item], model.instance.hiddens);
61
61
  return res.json(item);
@@ -17,7 +17,7 @@ const Helpers_1 = require("./Helpers");
17
17
  const Enums_1 = require("../Enums");
18
18
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
19
19
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
20
- const { model, req, res, database, relation, parentModel } = pack;
20
+ const { version, model, req, res, database, relation, parentModel } = pack;
21
21
  const query = database.from(model.instance.table);
22
22
  // If there is a relation, we should bind it
23
23
  (0, Helpers_1.addForeignKeyQuery)(req, query, relation, parentModel);
@@ -60,7 +60,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
60
60
  formData,
61
61
  query }));
62
62
  // Serializing the data by the model's serialize method
63
- item = yield (0, Helpers_1.serializeData)(item, model.instance.serialize, Enums_1.HandlerTypes.UPDATE, req);
63
+ item = yield (0, Helpers_1.serializeData)(version, item, model.serialize, Enums_1.HandlerTypes.UPDATE, req);
64
64
  // Filtering hidden fields from the response data.
65
65
  (0, Helpers_1.filterHiddenFields)([item], model.instance.hiddens);
66
66
  return res.json(item);
@@ -0,0 +1,4 @@
1
+ import { Request } from "express";
2
+ import { IVersion } from "./Interfaces";
3
+ export declare const getVersionByRequest: (req: Request) => Promise<IVersion>;
4
+ export declare const consoleAxeError: (error: any) => void;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.consoleAxeError = exports.getVersionByRequest = void 0;
16
+ const chalk_1 = __importDefault(require("chalk"));
17
+ const change_case_1 = require("change-case");
18
+ const Services_1 = require("./Services");
19
+ const getVersionByRequest = (req) => __awaiter(void 0, void 0, void 0, function* () {
20
+ // Application configuration is need for the default setting.
21
+ const api = Services_1.APIService.getInstance();
22
+ const matchedVersion = api.versions.find((version) => {
23
+ const path = `/${api.config.prefix}/${version.name}`;
24
+ return req.path.indexOf(path) === 0;
25
+ });
26
+ if (!matchedVersion) {
27
+ throw new Error(`Version is not matched: ${req.path}`);
28
+ }
29
+ return matchedVersion;
30
+ });
31
+ exports.getVersionByRequest = getVersionByRequest;
32
+ const consoleAxeError = (error) => {
33
+ const putWithSpace = (text, max) => {
34
+ const diff = max - text.length + 3;
35
+ for (let index = 0; index <= diff; index++) {
36
+ text = `${text} `;
37
+ }
38
+ text = ` ${text}`;
39
+ if (text.length > 77) {
40
+ let lines = [];
41
+ text = text.trim();
42
+ for (let step = 0; step < text.length / 70; step++) {
43
+ lines.push(text.substring(0, 70));
44
+ text = text.substring(70);
45
+ }
46
+ lines.push(text);
47
+ lines = lines.map((line) => putWithSpace(line, max));
48
+ return lines.flat();
49
+ }
50
+ return [text];
51
+ };
52
+ const getMaxLength = (code, message) => {
53
+ let maxLength = message.length;
54
+ if (code.length > message.length) {
55
+ maxLength = code.length;
56
+ }
57
+ if (maxLength > 70) {
58
+ maxLength = 70;
59
+ }
60
+ return maxLength;
61
+ };
62
+ const maxLength = getMaxLength(error.code, error.message);
63
+ const messages = [
64
+ "\n",
65
+ ...putWithSpace(" ", maxLength),
66
+ ...putWithSpace(`[${error.code}]`, maxLength),
67
+ ...putWithSpace(" ", maxLength),
68
+ ...putWithSpace(error.message, maxLength),
69
+ ...putWithSpace(" ", maxLength),
70
+ ];
71
+ console.log(chalk_1.default.bgRed.white(messages.join("\n")));
72
+ console.log(chalk_1.default.cyan([
73
+ "\n",
74
+ "You can find more in the documentation;",
75
+ `https://axe-api.com/errors.html#${(0, change_case_1.paramCase)(error.code)}`,
76
+ ].join("\n")));
77
+ };
78
+ exports.consoleAxeError = consoleAxeError;