@apolitical/server 1.5.0 → 1.5.2-beta.7

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/.gitlab-ci.yml CHANGED
@@ -13,7 +13,7 @@ publish-module:
13
13
  stage: publish
14
14
  image: node:12.20.1-alpine3.11
15
15
  extends:
16
- - .use-cache
16
+ - .yarn-install
17
17
  only:
18
18
  - tags
19
19
  - /^v\d+\.\d+\.\d+$/
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.5.1] - 2021-09-15
9
+ ### Added
10
+ - Extend error class to handle too many request error
11
+
8
12
  ## [1.5.0] - 2021-09-06
9
13
  ### Added
10
14
  - Documentation
package/README.md CHANGED
@@ -120,7 +120,7 @@ function appLoader(app) {
120
120
  start({ appLoader, port: 3000, handleErrors: true });
121
121
  ```
122
122
 
123
- The `errors` object includes a predefined set of errors: `BadRequest`, `Forbidden`, `NotFound`, and `InternalError`. Each error is an extension of the generic JavaScript `Error` object. These errors are designed to store the response status code, error message, and error key words (an array of strings).
123
+ The `errors` object includes a predefined set of errors: `BadRequest`, `Forbidden`, `NotFound`, `TooManyRequests` and `InternalError`. Each error is an extension of the generic JavaScript `Error` object. These errors are designed to store the response status code, error message, and error key words (an array of strings).
124
124
 
125
125
  The `start` server function can take the `handleErrors` parameter. When enabling the `handleErrors` parameter, you can forward an error with the use of the `next` function on your controller and the server will take care of producing the error response. In the example above, all of the incoming requests to the `/error` endpoint are going to produce a 400 (bad request) error with the following JSON object on the body:
126
126
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "1.5.0",
3
+ "version": "1.5.2-beta.7",
4
4
  "description": "Node.js module to encapsulate Apolitical's express server setup",
5
5
  "author": "Apolitical Group Limited <engineering@apolitical.co>",
6
6
  "contributors": [
package/src/config.js CHANGED
@@ -22,8 +22,13 @@ module.exports = {
22
22
  },
23
23
  SERVER: {
24
24
  CORS_OPTIONS: {
25
- origin: true,
26
- credentials: true,
25
+ ORIGIN_TRUE: {
26
+ origin: true,
27
+ credentials: true,
28
+ },
29
+ ORIGIN_FALSE: {
30
+ origin: false,
31
+ },
27
32
  },
28
33
  BODY_PARSER_OPTIONS: {
29
34
  extended: false,
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = ({ httpStatusCodes }) => {
4
- const { BAD_REQUEST, FORBIDDEN, INTERNAL_SERVER_ERROR, NOT_FOUND } = httpStatusCodes;
4
+ const { BAD_REQUEST, FORBIDDEN, INTERNAL_SERVER_ERROR, NOT_FOUND, TOO_MANY_REQUESTS } = httpStatusCodes;
5
5
 
6
6
  class GeneralError extends Error {
7
7
  constructor(message, code, errors = []) {
@@ -35,11 +35,18 @@ module.exports = ({ httpStatusCodes }) => {
35
35
  }
36
36
  }
37
37
 
38
+ class TooManyRequests extends GeneralError {
39
+ constructor(message, errors) {
40
+ super(message, TOO_MANY_REQUESTS, errors);
41
+ }
42
+ }
43
+
38
44
  return {
39
45
  BadRequest,
40
46
  GeneralError,
41
47
  InternalError,
42
48
  Forbidden,
43
49
  NotFound,
50
+ TooManyRequests,
44
51
  };
45
52
  };
@@ -1,16 +1,21 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = ({ bodyParser: { json, urlencoded }, compression, cors, cookieParser, config, logger }) => {
4
- const { BODY_PARSER_OPTIONS, CORS_OPTIONS } = config.SERVER;
4
+ const {
5
+ BODY_PARSER_OPTIONS,
6
+ CORS_OPTIONS: { ORIGIN_TRUE, ORIGIN_FALSE },
7
+ } = config.SERVER;
5
8
 
6
- return function load(app) {
9
+ return function load(app, { disableCors }) {
7
10
  const childLogger = logger.where(__filename, 'load');
8
11
  childLogger.debug('Started');
12
+ // Compute cors config
13
+ const corsOptions = disableCors ? ORIGIN_FALSE : ORIGIN_TRUE;
9
14
  // Load useful middlewares
10
15
  app.use(cookieParser());
11
16
  app.use(json());
12
17
  app.use(urlencoded(BODY_PARSER_OPTIONS));
13
- app.use(cors(CORS_OPTIONS));
18
+ app.use(cors(corsOptions));
14
19
  app.use(compression());
15
20
  childLogger.debug('Finished');
16
21
  };
@@ -42,7 +42,7 @@ module.exports = ({
42
42
  // Load probes
43
43
  probesLoader(app);
44
44
  // Load useful middlewares
45
- middlewaresLoader(app);
45
+ middlewaresLoader(app, opts);
46
46
  // Load logger
47
47
  loggerLoader(app, opts);
48
48
  // Load documentation
@@ -52,6 +52,12 @@ describe('Error Handling', () => {
52
52
  expect(res.body).toEqual(metadata);
53
53
  });
54
54
 
55
+ test('should return 429', async () => {
56
+ const res = await agents.loggedOut.get(url.replace(':code', 429));
57
+ expect(res.statusCode).toEqual(429);
58
+ expect(res.body).toEqual(metadata);
59
+ });
60
+
55
61
  test('should return 500', async () => {
56
62
  const res = await agents.loggedOut.get(url.replace(':code', 500));
57
63
  expect(res.statusCode).toEqual(500);
@@ -23,6 +23,10 @@ module.exports = (errors) => ({
23
23
  next(new errors.NotFound(errorMessage, errorArray));
24
24
  break;
25
25
  }
26
+ case 429: {
27
+ next(new errors.TooManyRequests(errorMessage, errorArray));
28
+ break;
29
+ }
26
30
  case 500: {
27
31
  next(new errors.InternalError(errorMessage));
28
32
  break;