Skip to content

Commit

Permalink
Fix parameters to skip response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffSelby committed May 15, 2022
1 parent 64ce414 commit 6333526
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 61 deletions.
81 changes: 51 additions & 30 deletions lib/Forge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
this._baseURL = 'https://forge.laravel.com/api/v1';
}

async _fetchJSON(endpoint, options = {}) {
async _fetchJSON(endpoint, options = {}, parseResponse = true) {
const res = await fetch(this._baseURL + endpoint, {
...options,
headers: {
Expand All @@ -21,43 +21,61 @@

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

if (options.parseResponse !== false && res.status !== 204)
return res.json();
if (parseResponse !== false && res.status !== 204) {
const json = await res.json();
return json;
}

return undefined;
const text = await res.text();
return text;
}

get(endpoint, options = {}) {
return this._fetchJSON(endpoint, {
...options,
body: undefined,
method: 'GET',
});
get(endpoint, options = {}, parseResponse = true) {
return this._fetchJSON(
endpoint,
{
...options,
body: undefined,
method: 'GET',
},
parseResponse,
);
}

post(endpoint, body, options = {}) {
return this._fetchJSON(endpoint, {
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
});
post(endpoint, body, options = {}, parseResponse = true) {
return this._fetchJSON(
endpoint,
{
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
},
parseResponse,
);
}

put(endpoint, body, options = {}) {
return this._fetchJSON(endpoint, {
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
});
put(endpoint, body, options = {}, parseResponse = true) {
return this._fetchJSON(
endpoint,
{
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
},
parseResponse,
);
}

patch(endpoint, operations, options = {}) {
return this._fetchJSON(endpoint, {
parseResponse: false,
return this._fetchJSON(
endpoint,
{
...options,
body: JSON.stringify(operations),
method: 'PATCH',
});
},
false,
);
}

delete(endpoint, options = {}) {
Expand Down Expand Up @@ -273,6 +291,9 @@
activate: (serverId, siteId, certId) =>
this.post(
`/servers/${serverId}/sites/${siteId}/certificates/${certId}/activate`,
undefined,
{},
false,
),
delete: (serverId, siteId, certId) =>
this.delete(
Expand Down Expand Up @@ -356,7 +377,7 @@
disable: (serverId, siteId) =>
this.delete(`/servers/${serverId}/sites/${siteId}/deployment`),
getScript: (serverId, siteId) =>
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`),
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {}, false),
updateScript: (serverId, siteId, payload) =>
this.put(
`/servers/${serverId}/sites/${siteId}/deployment/script`,
Expand Down Expand Up @@ -389,13 +410,13 @@
get config() {
return {
getNginx: (serverId, siteId) =>
this.get(`/servers/${serverId}/sites/${siteId}/nginx`),
this.get(`/servers/${serverId}/sites/${siteId}/nginx`,{}, false),
updateNginx: (serverId, siteId, payload) =>
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload),
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {}, false),
getEnv: (serverId, siteId) =>
this.get(`/servers/${serverId}/sites/${siteId}/env`),
this.get(`/servers/${serverId}/sites/${siteId}/env`, {}, false),
updateEnv: (serverId, siteId, payload) =>
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload),
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {}, false),
};
}

Expand Down
14 changes: 8 additions & 6 deletions src/Forge.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ class Forge extends ForgeRequest {
activate: (serverId, siteId, certId) =>
this.post(
`/servers/${serverId}/sites/${siteId}/certificates/${certId}/activate`,
{parseResponse: false},
undefined,
{},
false,
),
delete: (serverId, siteId, certId) =>
this.delete(
Expand Down Expand Up @@ -288,7 +290,7 @@ class Forge extends ForgeRequest {
disable: (serverId, siteId) =>
this.delete(`/servers/${serverId}/sites/${siteId}/deployment`),
getScript: (serverId, siteId) =>
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {parseResponse: false}),
this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {}, false),
updateScript: (serverId, siteId, payload) =>
this.put(
`/servers/${serverId}/sites/${siteId}/deployment/script`,
Expand Down Expand Up @@ -321,13 +323,13 @@ class Forge extends ForgeRequest {
get config() {
return {
getNginx: (serverId, siteId) =>
this.get(`/servers/${serverId}/sites/${siteId}/nginx`, {parseResponse: false}),
this.get(`/servers/${serverId}/sites/${siteId}/nginx`,{}, false),
updateNginx: (serverId, siteId, payload) =>
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {parseResponse: false}),
this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {}, false),
getEnv: (serverId, siteId) =>
this.get(`/servers/${serverId}/sites/${siteId}/env`, {parseResponse: false}),
this.get(`/servers/${serverId}/sites/${siteId}/env`, {}, false),
updateEnv: (serverId, siteId, payload) =>
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {parseResponse: false}),
this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {}, false),
};
}

Expand Down
68 changes: 43 additions & 25 deletions src/core/ForgeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ForgeRequest {
this._baseURL = 'https://forge.laravel.com/api/v1';
}

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

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

if (options.parseResponse !== false && res.status !== 204)
return res.json();
if (parseResponse !== false && res.status !== 204) {
const json = await res.json();
return json;
}

return res.text();
const text = await res.text();
return text;
}

get(endpoint, options = {}) {
return this._fetchJSON(endpoint, {
...options,
body: undefined,
method: 'GET',
});
get(endpoint, options = {}, parseResponse = true) {
return this._fetchJSON(
endpoint,
{
...options,
body: undefined,
method: 'GET',
},
parseResponse,
);
}

post(endpoint, body, options = {}) {
return this._fetchJSON(endpoint, {
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
});
post(endpoint, body, options = {}, parseResponse = true) {
return this._fetchJSON(
endpoint,
{
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
},
parseResponse,
);
}

put(endpoint, body, options = {}) {
return this._fetchJSON(endpoint, {
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
});
put(endpoint, body, options = {}, parseResponse = true) {
return this._fetchJSON(
endpoint,
{
...options,
body: body ? JSON.stringify(body) : undefined,
method: 'POST',
},
parseResponse,
);
}

patch(endpoint, operations, options = {}) {
return this._fetchJSON(endpoint, {
parseResponse: false,
return this._fetchJSON(
endpoint,
{
...options,
body: JSON.stringify(operations),
method: 'PATCH',
});
},
false,
);
}

delete(endpoint, options = {}) {
Expand Down

0 comments on commit 6333526

Please sign in to comment.