Skip to content

Commit 6a83348

Browse files
authored
Merge pull request #28 from GeoffSelby/BUGFIX-response-exceptions
Fix response type issues
2 parents 391609e + 6333526 commit 6a83348

File tree

6 files changed

+105
-62
lines changed

6 files changed

+105
-62
lines changed

__tests__/stub/fetchStub.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exports.setupFetchStub = () => {
66
Promise.resolve({
77
status: 200,
88
}),
9+
text: () => Promise.resolve('Sample response text'),
910
};
1011
return Promise.resolve(fetchResponse);
1112
});

lib/Forge.js

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
this._baseURL = 'https://forge.laravel.com/api/v1';
1010
}
1111

12-
async _fetchJSON(endpoint, options = {}) {
12+
async _fetchJSON(endpoint, options = {}, parseResponse = true) {
1313
const res = await fetch(this._baseURL + endpoint, {
1414
...options,
1515
headers: {
@@ -21,43 +21,61 @@
2121

2222
if (!res.ok) throw new Error(res.statusText);
2323

24-
if (options.parseResponse !== false && res.status !== 204)
25-
return res.json();
24+
if (parseResponse !== false && res.status !== 204) {
25+
const json = await res.json();
26+
return json;
27+
}
2628

27-
return undefined;
29+
const text = await res.text();
30+
return text;
2831
}
2932

30-
get(endpoint, options = {}) {
31-
return this._fetchJSON(endpoint, {
32-
...options,
33-
body: undefined,
34-
method: 'GET',
35-
});
33+
get(endpoint, options = {}, parseResponse = true) {
34+
return this._fetchJSON(
35+
endpoint,
36+
{
37+
...options,
38+
body: undefined,
39+
method: 'GET',
40+
},
41+
parseResponse,
42+
);
3643
}
3744

38-
post(endpoint, body, options = {}) {
39-
return this._fetchJSON(endpoint, {
40-
...options,
41-
body: body ? JSON.stringify(body) : undefined,
42-
method: 'POST',
43-
});
45+
post(endpoint, body, options = {}, parseResponse = true) {
46+
return this._fetchJSON(
47+
endpoint,
48+
{
49+
...options,
50+
body: body ? JSON.stringify(body) : undefined,
51+
method: 'POST',
52+
},
53+
parseResponse,
54+
);
4455
}
4556

46-
put(endpoint, body, options = {}) {
47-
return this._fetchJSON(endpoint, {
48-
...options,
49-
body: body ? JSON.stringify(body) : undefined,
50-
method: 'POST',
51-
});
57+
put(endpoint, body, options = {}, parseResponse = true) {
58+
return this._fetchJSON(
59+
endpoint,
60+
{
61+
...options,
62+
body: body ? JSON.stringify(body) : undefined,
63+
method: 'POST',
64+
},
65+
parseResponse,
66+
);
5267
}
5368

5469
patch(endpoint, operations, options = {}) {
55-
return this._fetchJSON(endpoint, {
56-
parseResponse: false,
70+
return this._fetchJSON(
71+
endpoint,
72+
{
5773
...options,
5874
body: JSON.stringify(operations),
5975
method: 'PATCH',
60-
});
76+
},
77+
false,
78+
);
6179
}
6280

6381
delete(endpoint, options = {}) {
@@ -273,6 +291,9 @@
273291
activate: (serverId, siteId, certId) =>
274292
this.post(
275293
`/servers/${serverId}/sites/${siteId}/certificates/${certId}/activate`,
294+
undefined,
295+
{},
296+
false,
276297
),
277298
delete: (serverId, siteId, certId) =>
278299
this.delete(
@@ -356,7 +377,7 @@
356377
disable: (serverId, siteId) =>
357378
this.delete(`/servers/${serverId}/sites/${siteId}/deployment`),
358379
getScript: (serverId, siteId) =>
359-
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`),
380+
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {}, false),
360381
updateScript: (serverId, siteId, payload) =>
361382
this.put(
362383
`/servers/${serverId}/sites/${siteId}/deployment/script`,
@@ -389,13 +410,13 @@
389410
get config() {
390411
return {
391412
getNginx: (serverId, siteId) =>
392-
this.get(`/servers/${serverId}/sites/${siteId}/nginx`),
413+
this.get(`/servers/${serverId}/sites/${siteId}/nginx`,{}, false),
393414
updateNginx: (serverId, siteId, payload) =>
394-
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload),
415+
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {}, false),
395416
getEnv: (serverId, siteId) =>
396-
this.get(`/servers/${serverId}/sites/${siteId}/env`),
417+
this.get(`/servers/${serverId}/sites/${siteId}/env`, {}, false),
397418
updateEnv: (serverId, siteId, payload) =>
398-
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload),
419+
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {}, false),
399420
};
400421
}
401422

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"rollup": "^2.30.0"
3939
},
4040
"dependencies": {
41-
"cross-fetch": "^3.0.6"
41+
"cross-fetch": "^3.1.5"
4242
},
4343
"jest": {
4444
"testPathIgnorePatterns": [

src/Forge.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ class Forge extends ForgeRequest {
204204
activate: (serverId, siteId, certId) =>
205205
this.post(
206206
`/servers/${serverId}/sites/${siteId}/certificates/${certId}/activate`,
207+
undefined,
208+
{},
209+
false,
207210
),
208211
delete: (serverId, siteId, certId) =>
209212
this.delete(
@@ -287,7 +290,7 @@ class Forge extends ForgeRequest {
287290
disable: (serverId, siteId) =>
288291
this.delete(`/servers/${serverId}/sites/${siteId}/deployment`),
289292
getScript: (serverId, siteId) =>
290-
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`),
293+
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {}, false),
291294
updateScript: (serverId, siteId, payload) =>
292295
this.put(
293296
`/servers/${serverId}/sites/${siteId}/deployment/script`,
@@ -320,13 +323,13 @@ class Forge extends ForgeRequest {
320323
get config() {
321324
return {
322325
getNginx: (serverId, siteId) =>
323-
this.get(`/servers/${serverId}/sites/${siteId}/nginx`),
326+
this.get(`/servers/${serverId}/sites/${siteId}/nginx`,{}, false),
324327
updateNginx: (serverId, siteId, payload) =>
325-
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload),
328+
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {}, false),
326329
getEnv: (serverId, siteId) =>
327-
this.get(`/servers/${serverId}/sites/${siteId}/env`),
330+
this.get(`/servers/${serverId}/sites/${siteId}/env`, {}, false),
328331
updateEnv: (serverId, siteId, payload) =>
329-
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload),
332+
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {}, false),
330333
};
331334
}
332335

src/core/ForgeRequest.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ForgeRequest {
66
this._baseURL = 'https://forge.laravel.com/api/v1';
77
}
88

9-
async _fetchJSON(endpoint, options = {}) {
9+
async _fetchJSON(endpoint, options = {}, parseResponse = true) {
1010
const res = await fetch(this._baseURL + endpoint, {
1111
...options,
1212
headers: {
@@ -18,43 +18,61 @@ class ForgeRequest {
1818

1919
if (!res.ok) throw new Error(res.statusText);
2020

21-
if (options.parseResponse !== false && res.status !== 204)
22-
return res.json();
21+
if (parseResponse !== false && res.status !== 204) {
22+
const json = await res.json();
23+
return json;
24+
}
2325

24-
return undefined;
26+
const text = await res.text();
27+
return text;
2528
}
2629

27-
get(endpoint, options = {}) {
28-
return this._fetchJSON(endpoint, {
29-
...options,
30-
body: undefined,
31-
method: 'GET',
32-
});
30+
get(endpoint, options = {}, parseResponse = true) {
31+
return this._fetchJSON(
32+
endpoint,
33+
{
34+
...options,
35+
body: undefined,
36+
method: 'GET',
37+
},
38+
parseResponse,
39+
);
3340
}
3441

35-
post(endpoint, body, options = {}) {
36-
return this._fetchJSON(endpoint, {
37-
...options,
38-
body: body ? JSON.stringify(body) : undefined,
39-
method: 'POST',
40-
});
42+
post(endpoint, body, options = {}, parseResponse = true) {
43+
return this._fetchJSON(
44+
endpoint,
45+
{
46+
...options,
47+
body: body ? JSON.stringify(body) : undefined,
48+
method: 'POST',
49+
},
50+
parseResponse,
51+
);
4152
}
4253

43-
put(endpoint, body, options = {}) {
44-
return this._fetchJSON(endpoint, {
45-
...options,
46-
body: body ? JSON.stringify(body) : undefined,
47-
method: 'POST',
48-
});
54+
put(endpoint, body, options = {}, parseResponse = true) {
55+
return this._fetchJSON(
56+
endpoint,
57+
{
58+
...options,
59+
body: body ? JSON.stringify(body) : undefined,
60+
method: 'POST',
61+
},
62+
parseResponse,
63+
);
4964
}
5065

5166
patch(endpoint, operations, options = {}) {
52-
return this._fetchJSON(endpoint, {
53-
parseResponse: false,
67+
return this._fetchJSON(
68+
endpoint,
69+
{
5470
...options,
5571
body: JSON.stringify(operations),
5672
method: 'PATCH',
57-
});
73+
},
74+
false,
75+
);
5876
}
5977

6078
delete(endpoint, options = {}) {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,7 @@ [email protected], core-util-is@~1.0.0:
18241824
version "1.0.2"
18251825
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
18261826

1827-
cross-fetch@^3.0.6:
1827+
cross-fetch@^3.1.5:
18281828
version "3.1.5"
18291829
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f"
18301830
integrity sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==

0 commit comments

Comments
 (0)