rubik-pact 1.0.7 → 1.0.9

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 (2) hide show
  1. package/classes/Pact.js +15 -9
  2. package/package.json +1 -1
package/classes/Pact.js CHANGED
@@ -12,6 +12,7 @@ const PactError = require('../errors/PactError');
12
12
 
13
13
  const DEFAULT_HOST = 'https://api.pact.im';
14
14
  const DEFAULT_VERSION = 'v8.0';
15
+ const SUCCESS_STATUS_REG = /2\d\d/;
15
16
 
16
17
  /**
17
18
  * Кубик для запросов к API Pact
@@ -72,25 +73,30 @@ class Pact extends Kubik {
72
73
  if (!token) token = this.token;
73
74
  const headers = { 'X-Private-Api-Token': token };
74
75
 
75
- method = method || 'POST';
76
+ if (!method) {
77
+ if (body) method = 'POST';
78
+ else method = 'GET';
79
+ }
80
+
76
81
  if (body instanceof FormData) {
77
82
  Object.assign(headers, body.getHeaders());
78
83
  } else if (isObject(body)) {
79
84
  body = JSON.stringify(body);
80
85
  headers['Content-Type'] = 'application/json';
81
- } else {
82
- method = 'GET';
83
86
  }
84
87
 
85
- const request = await fetch(url, { method, body, headers });
86
- // console.log(await request.text());
87
- const result = await request.json();
88
+ const res = await fetch(url, { method, body, headers });
89
+ if (!SUCCESS_STATUS_REG.test(`${res.status}`)) {
90
+ throw new Error(`Invalid status ${res.status}`);
91
+ }
92
+
93
+ const resBody = await res.json();
88
94
 
89
- if (result.status === 'errored') {
90
- throw new PactError(`${get(result, 'errors.0.code')} ${get(result, 'errors.0.description')}`);
95
+ if (resBody.status === 'errored') {
96
+ throw new PactError(`${get(resBody, 'errors.0.code')} ${get(resBody, 'errors.0.description')}`);
91
97
  }
92
98
 
93
- return result;
99
+ return resBody;
94
100
  }
95
101
 
96
102
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rubik-pact",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Pact Bot Api kubik for the Rubik application system",
5
5
  "main": "classes/Pact.js",
6
6
  "author": "ns@indotech.ru",