Skip to content

Commit 6333526

Browse files
committed
Fix parameters to skip response parsing
1 parent 64ce414 commit 6333526

File tree

3 files changed

+102
-61
lines changed

3 files changed

+102
-61
lines changed

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

src/Forge.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ class Forge extends ForgeRequest {
204204
activate: (serverId, siteId, certId) =>
205205
this.post(
206206
`/servers/${serverId}/sites/${siteId}/certificates/${certId}/activate`,
207-
{parseResponse: false},
207+
undefined,
208+
{},
209+
false,
208210
),
209211
delete: (serverId, siteId, certId) =>
210212
this.delete(
@@ -288,7 +290,7 @@ class Forge extends ForgeRequest {
288290
disable: (serverId, siteId) =>
289291
this.delete(`/servers/${serverId}/sites/${siteId}/deployment`),
290292
getScript: (serverId, siteId) =>
291-
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {parseResponse: false}),
293+
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {}, false),
292294
updateScript: (serverId, siteId, payload) =>
293295
this.put(
294296
`/servers/${serverId}/sites/${siteId}/deployment/script`,
@@ -321,13 +323,13 @@ class Forge extends ForgeRequest {
321323
get config() {
322324
return {
323325
getNginx: (serverId, siteId) =>
324-
this.get(`/servers/${serverId}/sites/${siteId}/nginx`, {parseResponse: false}),
326+
this.get(`/servers/${serverId}/sites/${siteId}/nginx`,{}, false),
325327
updateNginx: (serverId, siteId, payload) =>
326-
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {parseResponse: false}),
328+
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {}, false),
327329
getEnv: (serverId, siteId) =>
328-
this.get(`/servers/${serverId}/sites/${siteId}/env`, {parseResponse: false}),
330+
this.get(`/servers/${serverId}/sites/${siteId}/env`, {}, false),
329331
updateEnv: (serverId, siteId, payload) =>
330-
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {parseResponse: false}),
332+
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {}, false),
331333
};
332334
}
333335

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 res.text();
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 = {}) {

0 commit comments

Comments
 (0)