axe-api 0.31.0 → 0.31.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Release Notes
2
2
 
3
+ ## [0.31.2 (2023-07-04)](https://github.com/axe-api/axe-api/compare/0.31.2...0.31.1)
4
+
5
+ ### Fixed
6
+
7
+ - Fixed many internal issues. [#219](https://github.com/axe-api/axe-api/issues/219), [#174](https://github.com/axe-api/axe-api/issues/174), [#183](https://github.com/axe-api/axe-api/issues/183), [#149](https://github.com/axe-api/axe-api/issues/149), [#186](https://github.com/axe-api/axe-api/issues/186)
8
+
9
+ ## [0.31.1 (2023-05-08)](https://github.com/axe-api/axe-api/compare/0.31.1...0.31.0)
10
+
11
+ ### Fixed
12
+
13
+ - The app crashes on database errors [#155](https://github.com/axe-api/axe-api/issues/155)
14
+ - We should return 404 if the record is not found [#169](https://github.com/axe-api/axe-api/issues/169)
15
+ - An error should be thrown if the per_page value is not acceptable [#172](https://github.com/axe-api/axe-api/issues/172)
16
+
3
17
  ## [0.31.0 (2023-05-05)](https://github.com/axe-api/axe-api/compare/0.31.0...0.30.3)
4
18
 
5
19
  ### Features
@@ -125,11 +125,15 @@ class RouterBuilder {
125
125
  return __awaiter(this, void 0, void 0, function* () {
126
126
  const docs = Services_1.DocumentationService.getInstance();
127
127
  const app = yield Services_1.IoCService.useByType("App");
128
- const handler = (req, res) => {
129
- this.requestHandler(handlerType, req, res, model, parentModel, relation).catch((error) => {
130
- this.sendErrorAsResponse(res, error);
131
- });
132
- };
128
+ const handler = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
129
+ try {
130
+ yield this.requestHandler(handlerType, req, res, model, parentModel, relation);
131
+ }
132
+ catch (error) {
133
+ // Catch error then pass it to the express error handler
134
+ next(error);
135
+ }
136
+ });
133
137
  switch (handlerType) {
134
138
  case Enums_1.HandlerTypes.ALL:
135
139
  app.get(url, middlewares, handler);
@@ -215,12 +219,8 @@ class RouterBuilder {
215
219
  });
216
220
  break;
217
221
  default:
218
- // We should not show the real errors on production
219
- if (process.env.NODE_ENV === "production") {
220
- res.status(Enums_1.StatusCodes.INTERNAL_SERVER_ERROR).json({
221
- error: "An error occurredxx.",
222
- });
223
- }
222
+ // We should log error and send general error response
223
+ Services_1.LogService.getInstance().error(`SERVER ERROR: ${JSON.stringify(Object.assign(Object.assign({}, error), { message: error.message }), null, " ")}`);
224
224
  throw error;
225
225
  }
226
226
  }
@@ -3,6 +3,6 @@ declare class ApiError extends Error {
3
3
  type: string;
4
4
  status: StatusCodes;
5
5
  message: string;
6
- constructor(message: string);
6
+ constructor(message: string, status?: StatusCodes);
7
7
  }
8
8
  export default ApiError;
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const Enums_1 = require("../Enums");
4
4
  class ApiError extends Error {
5
- constructor(message) {
5
+ constructor(message, status = Enums_1.StatusCodes.BAD_REQUEST) {
6
6
  super(message);
7
7
  this.type = "ApiError";
8
- this.status = Enums_1.StatusCodes.BAD_REQUEST;
8
+ this.status = status;
9
9
  this.message = message;
10
10
  }
11
11
  }
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const Helpers_1 = require("./Helpers");
16
16
  const Enums_1 = require("../Enums");
17
17
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
18
+ const Enums_2 = require("../Enums");
18
19
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
19
20
  const { model, req, res, database, relation, parentModel } = pack;
20
21
  // We should check the parameter type
@@ -31,7 +32,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
31
32
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onBeforeDeleteQuery, Object.assign(Object.assign({}, pack), { query }));
32
33
  const item = yield query.first();
33
34
  if (!item) {
34
- throw new ApiError_1.default(`The item is not found on ${model.name}.`);
35
+ throw new ApiError_1.default(`The item is not found on ${model.name}.`, Enums_2.StatusCodes.NOT_FOUND);
35
36
  }
36
37
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterDeleteQuery, Object.assign(Object.assign({}, pack), { query,
37
38
  item }));
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const Helpers_1 = require("./Helpers");
16
16
  const Enums_1 = require("../Enums");
17
17
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
18
+ const Enums_2 = require("../Enums");
18
19
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
19
20
  const { model, req, res, database, relation, parentModel } = pack;
20
21
  // We should check the parameter type
@@ -26,7 +27,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
26
27
  .where(model.instance.primaryKey, value);
27
28
  // If there is a deletedAtColumn, it means that this table support soft-delete
28
29
  if (model.instance.deletedAtColumn === null) {
29
- throw new ApiError_1.default("You can use force delete only soft-delete supported models.");
30
+ throw new ApiError_1.default("You can use force delete only soft-delete supported models.", Enums_2.StatusCodes.NOT_FOUND);
30
31
  }
31
32
  // If there is a relation, we should bind it
32
33
  (0, Helpers_1.addForeignKeyQuery)(req, query, relation, parentModel);
@@ -16,6 +16,7 @@ const validatorjs_1 = __importDefault(require("validatorjs"));
16
16
  const Helpers_1 = require("./Helpers");
17
17
  const Enums_1 = require("../Enums");
18
18
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
19
+ const Enums_2 = require("../Enums");
19
20
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
20
21
  const { version, model, req, res, database, relation, parentModel } = pack;
21
22
  const query = database.from(model.instance.table);
@@ -30,7 +31,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
30
31
  // Adding the main query
31
32
  let item = yield query.where(model.instance.primaryKey, value).first();
32
33
  if (!item) {
33
- throw new ApiError_1.default(`The item is not found on ${model.name}.`);
34
+ throw new ApiError_1.default(`The item is not found on ${model.name}.`, Enums_2.StatusCodes.NOT_FOUND);
34
35
  }
35
36
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterUpdateQuery, Object.assign(Object.assign({}, pack), { item,
36
37
  query }));
@@ -16,6 +16,7 @@ const Helpers_1 = require("./Helpers");
16
16
  const Enums_1 = require("../Enums");
17
17
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
18
18
  const Services_1 = require("../Services");
19
+ const Enums_2 = require("../Enums");
19
20
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
20
21
  const { version, model, req, res, database, relation, parentModel } = pack;
21
22
  const queryParser = new Services_1.QueryService(model, version.modelList.get(), version.config);
@@ -40,7 +41,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
40
41
  queryParser.applyWheres(query, conditions.q);
41
42
  let item = yield query.first();
42
43
  if (!item) {
43
- throw new ApiError_1.default(`The item is not found on ${model.name}.`);
44
+ throw new ApiError_1.default(`The item is not found on ${model.name}.`, Enums_2.StatusCodes.NOT_FOUND);
44
45
  }
45
46
  // We should try to get related data if there is any
46
47
  yield (0, Helpers_1.getRelatedData)(version, [item], conditions.with, model, version.modelList, database, Enums_1.HandlerTypes.ALL, req);
@@ -16,6 +16,7 @@ const validatorjs_1 = __importDefault(require("validatorjs"));
16
16
  const Helpers_1 = require("./Helpers");
17
17
  const Enums_1 = require("../Enums");
18
18
  const ApiError_1 = __importDefault(require("../Exceptions/ApiError"));
19
+ const Enums_2 = require("../Enums");
19
20
  exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
20
21
  const { version, model, req, res, database, relation, parentModel } = pack;
21
22
  const query = database.from(model.instance.table);
@@ -30,7 +31,7 @@ exports.default = (pack) => __awaiter(void 0, void 0, void 0, function* () {
30
31
  // Adding the main query
31
32
  let item = yield query.where(model.instance.primaryKey, value).first();
32
33
  if (!item) {
33
- throw new ApiError_1.default(`The item is not found on ${model.name}.`);
34
+ throw new ApiError_1.default(`The item is not found on ${model.name}.`, Enums_2.StatusCodes.NOT_FOUND);
34
35
  }
35
36
  yield (0, Helpers_1.callHooks)(model, Enums_1.HookFunctionTypes.onAfterUpdateQuery, Object.assign(Object.assign({}, pack), { item,
36
37
  query }));
@@ -205,7 +205,7 @@ class QueryService {
205
205
  return value;
206
206
  }
207
207
  parsePerPage(content) {
208
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
208
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
209
209
  const value = parseInt(content ||
210
210
  ((_b = (_a = this.config.query) === null || _a === void 0 ? void 0 : _a.defaults) === null || _b === void 0 ? void 0 : _b.perPage) ||
211
211
  ((_c = constants_1.DEFAULT_VERSION_CONFIG.query.defaults) === null || _c === void 0 ? void 0 : _c.perPage) ||
@@ -216,10 +216,8 @@ class QueryService {
216
216
  const maxPerPage = ((_h = (_g = this.config.query) === null || _g === void 0 ? void 0 : _g.defaults) === null || _h === void 0 ? void 0 : _h.maxPerPage) ||
217
217
  ((_j = constants_1.DEFAULT_VERSION_CONFIG.query.defaults) === null || _j === void 0 ? void 0 : _j.maxPerPage) ||
218
218
  100;
219
- if (isNaN(value) || value <= minPerPage || value > maxPerPage) {
220
- return (((_l = (_k = this.config.query) === null || _k === void 0 ? void 0 : _k.defaults) === null || _l === void 0 ? void 0 : _l.perPage) ||
221
- ((_m = constants_1.DEFAULT_VERSION_CONFIG.query.defaults) === null || _m === void 0 ? void 0 : _m.perPage) ||
222
- 10);
219
+ if (isNaN(value) || value < minPerPage || value > maxPerPage) {
220
+ throw new ApiError_1.default(`Unacceptable 'per_page' value! Current value is '${value}'. It should be between ${minPerPage}-${maxPerPage}`);
223
221
  }
224
222
  return value;
225
223
  }
@@ -24,6 +24,7 @@ exports.RESERVED_KEYWORDS = [
24
24
  "hooks",
25
25
  "event",
26
26
  "events",
27
+ "all",
27
28
  ];
28
29
  exports.DEFAULT_HANDLERS = [
29
30
  Enums_1.HandlerTypes.INSERT,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axe-api",
3
- "version": "0.31.0",
3
+ "version": "0.31.2",
4
4
  "description": "AXE API is a simple tool which has been created based on Express and Knex.js to create Rest APIs quickly.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -35,19 +35,21 @@
35
35
  "dev-kit:remove": "node ./scripts/dev-kit-remove.js",
36
36
  "test": "jest --runInBand",
37
37
  "test:dev": "jest --watch",
38
- "lint": "eslint src/**",
38
+ "lint": "eslint --max-warnings=0 .",
39
39
  "lint:watch": "esw --watch --color",
40
40
  "prepare:integration": "nodemon --ignore \"./tests/**\" ./scripts/run-integration-test.js",
41
41
  "test:postgres": "sh ./scripts/test-postgres.sh",
42
42
  "test:mysql57": "sh ./scripts/test-mysql57.sh",
43
- "test:mysql8": "sh ./scripts/test-mysql8.sh"
43
+ "test:mysql8": "sh ./scripts/test-mysql8.sh",
44
+ "prettier:check": "prettier --check .",
45
+ "prepare": "husky install"
44
46
  },
45
47
  "dependencies": {
46
48
  "chalk": "^4.1.2",
47
49
  "change-case": "^4.1.2",
48
- "dotenv": "^16.0.3",
50
+ "dotenv": "^16.3.1",
49
51
  "knex": "^2.4.2",
50
- "knex-paginate": "^3.1.0",
52
+ "knex-paginate": "^3.1.1",
51
53
  "knex-schema-inspector": "^3.0.1",
52
54
  "pluralize": "^8.0.0",
53
55
  "validatorjs": "^3.22.1"
@@ -61,8 +63,8 @@
61
63
  "@types/multer": "^1.4.7",
62
64
  "@types/pluralize": "^0.0.29",
63
65
  "@types/validatorjs": "^3.15.0",
64
- "@typescript-eslint/eslint-plugin": "^5.56.0",
65
- "@typescript-eslint/parser": "^5.56.0",
66
+ "@typescript-eslint/eslint-plugin": "^5.61.0",
67
+ "@typescript-eslint/parser": "^5.61.0",
66
68
  "babel-jest": "^29.5.0",
67
69
  "eslint": "^7.32.0",
68
70
  "eslint-config-standard": "^16.0.3",
@@ -73,7 +75,9 @@
73
75
  "eslint-watch": "^7.0.0",
74
76
  "express": "^4.18.2",
75
77
  "glob": "^9.3.2",
78
+ "husky": "^8.0.3",
76
79
  "jest": "^29.5.0",
80
+ "lint-staged": "^13.2.3",
77
81
  "multer": "^1.4.5-lts.1",
78
82
  "mysql": "^2.18.1",
79
83
  "node-color-log": "^10.0.2",
@@ -84,5 +88,8 @@
84
88
  "sqlite3": "^5.1.6",
85
89
  "ts-node": "^10.9.1",
86
90
  "typescript": "^5.0.2"
91
+ },
92
+ "lint-staged": {
93
+ "**/*": "prettier --write --ignore-unknown"
87
94
  }
88
95
  }
package/readme.md CHANGED
@@ -134,8 +134,7 @@ You will see the following API response if you visit [localhost:3000](http://loc
134
134
  ```json
135
135
  {
136
136
  "name": "AXE API",
137
- "description": "The best API creation tool in the world.",
138
- "aim": "To kill them all!"
137
+ "description": "The best API creation tool in the world."
139
138
  }
140
139
  ```
141
140
 
@@ -145,6 +144,17 @@ If you can see that response, it means that your project is running properly.
145
144
 
146
145
  Axe API has great documentation. Please [check it out in here](https://axe-api.com/).
147
146
 
147
+ ## Contributors
148
+
149
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
150
+ <!-- prettier-ignore-start -->
151
+ <!-- markdownlint-disable -->
152
+
153
+ <!-- markdownlint-restore -->
154
+ <!-- prettier-ignore-end -->
155
+
156
+ <!-- ALL-CONTRIBUTORS-LIST:END -->
157
+
148
158
  ## License
149
159
 
150
160
  [MIT License](LICENSE)