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.
- package/classes/Pact.js +15 -9
- 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
|
-
|
|
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
|
|
86
|
-
|
|
87
|
-
|
|
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 (
|
|
90
|
-
throw new PactError(`${get(
|
|
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
|
|
99
|
+
return resBody;
|
|
94
100
|
}
|
|
95
101
|
|
|
96
102
|
/**
|