|
9 | 9 | this._baseURL = 'https://forge.laravel.com/api/v1';
|
10 | 10 | }
|
11 | 11 |
|
12 |
| - async _fetchJSON(endpoint, options = {}) { |
| 12 | + async _fetchJSON(endpoint, options = {}, parseResponse = true) { |
13 | 13 | const res = await fetch(this._baseURL + endpoint, {
|
14 | 14 | ...options,
|
15 | 15 | headers: {
|
|
21 | 21 |
|
22 | 22 | if (!res.ok) throw new Error(res.statusText);
|
23 | 23 |
|
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 | + } |
26 | 28 |
|
27 |
| - return undefined; |
| 29 | + const text = await res.text(); |
| 30 | + return text; |
28 | 31 | }
|
29 | 32 |
|
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 | + ); |
36 | 43 | }
|
37 | 44 |
|
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 | + ); |
44 | 55 | }
|
45 | 56 |
|
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 | + ); |
52 | 67 | }
|
53 | 68 |
|
54 | 69 | patch(endpoint, operations, options = {}) {
|
55 |
| - return this._fetchJSON(endpoint, { |
56 |
| - parseResponse: false, |
| 70 | + return this._fetchJSON( |
| 71 | + endpoint, |
| 72 | + { |
57 | 73 | ...options,
|
58 | 74 | body: JSON.stringify(operations),
|
59 | 75 | method: 'PATCH',
|
60 |
| - }); |
| 76 | + }, |
| 77 | + false, |
| 78 | + ); |
61 | 79 | }
|
62 | 80 |
|
63 | 81 | delete(endpoint, options = {}) {
|
|
273 | 291 | activate: (serverId, siteId, certId) =>
|
274 | 292 | this.post(
|
275 | 293 | `/servers/${serverId}/sites/${siteId}/certificates/${certId}/activate`,
|
| 294 | + undefined, |
| 295 | + {}, |
| 296 | + false, |
276 | 297 | ),
|
277 | 298 | delete: (serverId, siteId, certId) =>
|
278 | 299 | this.delete(
|
|
356 | 377 | disable: (serverId, siteId) =>
|
357 | 378 | this.delete(`/servers/${serverId}/sites/${siteId}/deployment`),
|
358 | 379 | getScript: (serverId, siteId) =>
|
359 |
| - this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`), |
| 380 | + this.get(`/servers/${serverId}/sites/${siteId}/deployment/script`, {}, false), |
360 | 381 | updateScript: (serverId, siteId, payload) =>
|
361 | 382 | this.put(
|
362 | 383 | `/servers/${serverId}/sites/${siteId}/deployment/script`,
|
|
389 | 410 | get config() {
|
390 | 411 | return {
|
391 | 412 | getNginx: (serverId, siteId) =>
|
392 |
| - this.get(`/servers/${serverId}/sites/${siteId}/nginx`), |
| 413 | + this.get(`/servers/${serverId}/sites/${siteId}/nginx`,{}, false), |
393 | 414 | updateNginx: (serverId, siteId, payload) =>
|
394 |
| - this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload), |
| 415 | + this.put(`/servers/${serverId}/sites/${siteId}/nginx`, payload, {}, false), |
395 | 416 | getEnv: (serverId, siteId) =>
|
396 |
| - this.get(`/servers/${serverId}/sites/${siteId}/env`), |
| 417 | + this.get(`/servers/${serverId}/sites/${siteId}/env`, {}, false), |
397 | 418 | updateEnv: (serverId, siteId, payload) =>
|
398 |
| - this.put(`/servers/${serverId}/sites/${siteId}/env`, payload), |
| 419 | + this.put(`/servers/${serverId}/sites/${siteId}/env`, payload, {}, false), |
399 | 420 | };
|
400 | 421 | }
|
401 | 422 |
|
|
0 commit comments