@autofleet/network 1.0.0 → 1.1.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.
package/index.js CHANGED
@@ -3,6 +3,7 @@ const axiosRetry = require('axios-retry');
3
3
  const { Logger } = require('@autofleet/node-common');
4
4
  const qs = require('qs');
5
5
  const httpAdapter = require('axios/lib/adapters/http');
6
+ const merge = require('deepmerge');
6
7
  require('dotenv').config();
7
8
  /**
8
9
  * Add support for nock testing
@@ -23,14 +24,17 @@ const HTTPMethods = [
23
24
  ];
24
25
 
25
26
  const defaultSettings = {
26
- timeout: 2500,
27
+ timeout: 10000,
27
28
  headers: { 'X-AF-AUTH': 'ANYONE' },
28
29
  paramsSerializer: params => qs.stringify(params, { arrayFormat: 'brackets' }),
30
+ 'axios-retry': {
31
+ shouldResetTimeout: true,
32
+ },
29
33
  };
30
34
 
31
35
  module.exports = class Network {
32
36
  constructor(settings = {}) {
33
- this.settings = Object.assign({}, defaultSettings, settings);
37
+ this.settings = merge(defaultSettings, settings);
34
38
  this.createBaseUrl();
35
39
 
36
40
  this.axios = axios.create(this.settings);
@@ -92,4 +96,25 @@ module.exports = class Network {
92
96
  this[method] = (...args) => this.axios[method](...args);
93
97
  });
94
98
  }
99
+
100
+ async getAllPages(url, options = {}) {
101
+ let currentResult = [];
102
+ let resultsInFirstPage = null;
103
+ let lastResultsSize = 0;
104
+
105
+ const localOptions = { params: {}, ...options };
106
+ localOptions.params.page = 1;
107
+ while(localOptions.params.page === 1 || lastResultsSize < resultsInFirstPage) {
108
+ const { data } = await this.get(url, localOptions);
109
+ lastResultsSize = data.length;
110
+ if(localOptions.params.page === 1) {
111
+ resultsInFirstPage = data.length;
112
+ }
113
+
114
+ localOptions.params.page += 1;
115
+ currentResult = currentResult.concat(data);
116
+ }
117
+
118
+ return currentResult;
119
+ }
95
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/network",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,7 @@
14
14
  "@autofleet/node-common": "^1.1.50",
15
15
  "axios": "^0.18.0",
16
16
  "axios-retry": "^3.1.1",
17
+ "deepmerge": "^3.0.0",
17
18
  "dotenv": "^6.0.0",
18
19
  "qs": "^6.5.2"
19
20
  },
@@ -1,26 +0,0 @@
1
- version: 2
2
- jobs:
3
- test:
4
- docker:
5
- - image: circleci/node:8.9.4
6
- steps:
7
- - checkout
8
- - restore_cache:
9
- keys:
10
- - v1-dependencies-{{ checksum "package.json" }}
11
- # fallback to using the latest cache if no exact match is found
12
- - v1-dependencies-
13
- - run: npm i
14
- - save_cache:
15
- paths:
16
- - node_modules
17
- key: v1-dependencies-{{ checksum "package.json" }}
18
- - run: npm run coverage
19
- - run: rm -rf ./coverage
20
- - run: npm run linter
21
-
22
- workflows:
23
- version: 2
24
- build_and_test:
25
- jobs:
26
- - test