diff options
author | vantezzen <properly@protonmail.com> | 2019-08-22 11:04:50 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-08-22 11:04:50 +0200 |
commit | 8440d51cef61297eb34a193d6a3ad420a0947340 (patch) | |
tree | c92b9f96dbcc570ddcfbd595c9dca97421b74ea9 /src/api/server/ServerApi.js | |
parent | Add development info to README (diff) | |
download | ferdium-app-8440d51cef61297eb34a193d6a3ad420a0947340.tar.gz ferdium-app-8440d51cef61297eb34a193d6a3ad420a0947340.tar.zst ferdium-app-8440d51cef61297eb34a193d6a3ad420a0947340.zip |
Add support to use custom servers
Diffstat (limited to 'src/api/server/ServerApi.js')
-rw-r--r-- | src/api/server/ServerApi.js | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index a9ce202ff..069994028 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -46,9 +46,31 @@ export default class ServerApi { | |||
46 | 46 | ||
47 | recipes = []; | 47 | recipes = []; |
48 | 48 | ||
49 | stores = {}; | ||
50 | |||
51 | setStores(stores) { | ||
52 | this.stores = stores; | ||
53 | } | ||
54 | |||
55 | apiUrl() { | ||
56 | let url; | ||
57 | if (!this.stores.settings) { | ||
58 | // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded | ||
59 | url = 'https://localhost:9999' | ||
60 | } else if (this.stores.settings.all.app.server) { | ||
61 | // Load URL from store | ||
62 | url = this.stores.settings.all.app.server; | ||
63 | } else { | ||
64 | // Use default server url | ||
65 | url = SERVER_URL; | ||
66 | } | ||
67 | |||
68 | return `${url}/${API_VERSION}`; | ||
69 | } | ||
70 | |||
49 | // User | 71 | // User |
50 | async login(email, passwordHash) { | 72 | async login(email, passwordHash) { |
51 | const request = await sendAuthRequest(`${API_URL}/auth/login`, { | 73 | const request = await sendAuthRequest(`${this.apiUrl()}/auth/login`, { |
52 | method: 'POST', | 74 | method: 'POST', |
53 | headers: { | 75 | headers: { |
54 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, | 76 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, |
@@ -64,7 +86,7 @@ export default class ServerApi { | |||
64 | } | 86 | } |
65 | 87 | ||
66 | async signup(data) { | 88 | async signup(data) { |
67 | const request = await sendAuthRequest(`${API_URL}/auth/signup`, { | 89 | const request = await sendAuthRequest(`${this.apiUrl()}/auth/signup`, { |
68 | method: 'POST', | 90 | method: 'POST', |
69 | body: JSON.stringify(data), | 91 | body: JSON.stringify(data), |
70 | }, false); | 92 | }, false); |
@@ -78,7 +100,7 @@ export default class ServerApi { | |||
78 | } | 100 | } |
79 | 101 | ||
80 | async inviteUser(data) { | 102 | async inviteUser(data) { |
81 | const request = await sendAuthRequest(`${API_URL}/invite`, { | 103 | const request = await sendAuthRequest(`${this.apiUrl()}/invite`, { |
82 | method: 'POST', | 104 | method: 'POST', |
83 | body: JSON.stringify(data), | 105 | body: JSON.stringify(data), |
84 | }); | 106 | }); |
@@ -91,7 +113,7 @@ export default class ServerApi { | |||
91 | } | 113 | } |
92 | 114 | ||
93 | async retrievePassword(email) { | 115 | async retrievePassword(email) { |
94 | const request = await sendAuthRequest(`${API_URL}/auth/password`, { | 116 | const request = await sendAuthRequest(`${this.apiUrl()}/auth/password`, { |
95 | method: 'POST', | 117 | method: 'POST', |
96 | body: JSON.stringify({ | 118 | body: JSON.stringify({ |
97 | email, | 119 | email, |
@@ -107,7 +129,7 @@ export default class ServerApi { | |||
107 | } | 129 | } |
108 | 130 | ||
109 | async userInfo() { | 131 | async userInfo() { |
110 | const request = await sendAuthRequest(`${API_URL}/me`); | 132 | const request = await sendAuthRequest(`${this.apiUrl()}/me`); |
111 | if (!request.ok) { | 133 | if (!request.ok) { |
112 | throw request; | 134 | throw request; |
113 | } | 135 | } |
@@ -120,7 +142,7 @@ export default class ServerApi { | |||
120 | } | 142 | } |
121 | 143 | ||
122 | async updateUserInfo(data) { | 144 | async updateUserInfo(data) { |
123 | const request = await sendAuthRequest(`${API_URL}/me`, { | 145 | const request = await sendAuthRequest(`${this.apiUrl()}/me`, { |
124 | method: 'PUT', | 146 | method: 'PUT', |
125 | body: JSON.stringify(data), | 147 | body: JSON.stringify(data), |
126 | }); | 148 | }); |
@@ -135,7 +157,7 @@ export default class ServerApi { | |||
135 | } | 157 | } |
136 | 158 | ||
137 | async deleteAccount() { | 159 | async deleteAccount() { |
138 | const request = await sendAuthRequest(`${API_URL}/me`, { | 160 | const request = await sendAuthRequest(`${this.apiUrl()}/me`, { |
139 | method: 'DELETE', | 161 | method: 'DELETE', |
140 | }); | 162 | }); |
141 | if (!request.ok) { | 163 | if (!request.ok) { |
@@ -149,7 +171,7 @@ export default class ServerApi { | |||
149 | 171 | ||
150 | // Services | 172 | // Services |
151 | async getServices() { | 173 | async getServices() { |
152 | const request = await sendAuthRequest(`${API_URL}/me/services`); | 174 | const request = await sendAuthRequest(`${this.apiUrl()}/me/services`); |
153 | if (!request.ok) { | 175 | if (!request.ok) { |
154 | throw request; | 176 | throw request; |
155 | } | 177 | } |
@@ -162,7 +184,7 @@ export default class ServerApi { | |||
162 | } | 184 | } |
163 | 185 | ||
164 | async createService(recipeId, data) { | 186 | async createService(recipeId, data) { |
165 | const request = await sendAuthRequest(`${API_URL}/service`, { | 187 | const request = await sendAuthRequest(`${this.apiUrl()}/service`, { |
166 | method: 'POST', | 188 | method: 'POST', |
167 | body: JSON.stringify(Object.assign({ | 189 | body: JSON.stringify(Object.assign({ |
168 | recipeId, | 190 | recipeId, |
@@ -192,7 +214,7 @@ export default class ServerApi { | |||
192 | await this.uploadServiceIcon(serviceId, data.iconFile); | 214 | await this.uploadServiceIcon(serviceId, data.iconFile); |
193 | } | 215 | } |
194 | 216 | ||
195 | const request = await sendAuthRequest(`${API_URL}/service/${serviceId}`, { | 217 | const request = await sendAuthRequest(`${this.apiUrl()}/service/${serviceId}`, { |
196 | method: 'PUT', | 218 | method: 'PUT', |
197 | body: JSON.stringify(data), | 219 | body: JSON.stringify(data), |
198 | }); | 220 | }); |
@@ -220,7 +242,7 @@ export default class ServerApi { | |||
220 | 242 | ||
221 | delete requestData.headers['Content-Type']; | 243 | delete requestData.headers['Content-Type']; |
222 | 244 | ||
223 | const request = await window.fetch(`${API_URL}/service/${serviceId}`, requestData); | 245 | const request = await window.fetch(`${this.apiUrl()}/service/${serviceId}`, requestData); |
224 | 246 | ||
225 | if (!request.ok) { | 247 | if (!request.ok) { |
226 | throw request; | 248 | throw request; |
@@ -232,7 +254,7 @@ export default class ServerApi { | |||
232 | } | 254 | } |
233 | 255 | ||
234 | async reorderService(data) { | 256 | async reorderService(data) { |
235 | const request = await sendAuthRequest(`${API_URL}/service/reorder`, { | 257 | const request = await sendAuthRequest(`${this.apiUrl()}/service/reorder`, { |
236 | method: 'PUT', | 258 | method: 'PUT', |
237 | body: JSON.stringify(data), | 259 | body: JSON.stringify(data), |
238 | }); | 260 | }); |
@@ -245,7 +267,7 @@ export default class ServerApi { | |||
245 | } | 267 | } |
246 | 268 | ||
247 | async deleteService(id) { | 269 | async deleteService(id) { |
248 | const request = await sendAuthRequest(`${API_URL}/service/${id}`, { | 270 | const request = await sendAuthRequest(`${this.apiUrl()}/service/${id}`, { |
249 | method: 'DELETE', | 271 | method: 'DELETE', |
250 | }); | 272 | }); |
251 | if (!request.ok) { | 273 | if (!request.ok) { |
@@ -261,7 +283,7 @@ export default class ServerApi { | |||
261 | 283 | ||
262 | // Features | 284 | // Features |
263 | async getDefaultFeatures() { | 285 | async getDefaultFeatures() { |
264 | const request = await sendAuthRequest(`${API_URL}/features/default`); | 286 | const request = await sendAuthRequest(`${this.apiUrl()}/features/default`); |
265 | if (!request.ok) { | 287 | if (!request.ok) { |
266 | throw request; | 288 | throw request; |
267 | } | 289 | } |
@@ -273,7 +295,7 @@ export default class ServerApi { | |||
273 | } | 295 | } |
274 | 296 | ||
275 | async getFeatures() { | 297 | async getFeatures() { |
276 | const request = await sendAuthRequest(`${API_URL}/features`); | 298 | const request = await sendAuthRequest(`${this.apiUrl()}/features`); |
277 | if (!request.ok) { | 299 | if (!request.ok) { |
278 | throw request; | 300 | throw request; |
279 | } | 301 | } |
@@ -307,7 +329,7 @@ export default class ServerApi { | |||
307 | } | 329 | } |
308 | 330 | ||
309 | async getRecipeUpdates(recipeVersions) { | 331 | async getRecipeUpdates(recipeVersions) { |
310 | const request = await sendAuthRequest(`${API_URL}/recipes/update`, { | 332 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes/update`, { |
311 | method: 'POST', | 333 | method: 'POST', |
312 | body: JSON.stringify(recipeVersions), | 334 | body: JSON.stringify(recipeVersions), |
313 | }); | 335 | }); |
@@ -321,7 +343,7 @@ export default class ServerApi { | |||
321 | 343 | ||
322 | // Recipes Previews | 344 | // Recipes Previews |
323 | async getRecipePreviews() { | 345 | async getRecipePreviews() { |
324 | const request = await sendAuthRequest(`${API_URL}/recipes`); | 346 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes`); |
325 | if (!request.ok) throw request; | 347 | if (!request.ok) throw request; |
326 | const data = await request.json(); | 348 | const data = await request.json(); |
327 | const recipePreviews = this._mapRecipePreviewModel(data); | 349 | const recipePreviews = this._mapRecipePreviewModel(data); |
@@ -330,7 +352,7 @@ export default class ServerApi { | |||
330 | } | 352 | } |
331 | 353 | ||
332 | async getFeaturedRecipePreviews() { | 354 | async getFeaturedRecipePreviews() { |
333 | const request = await sendAuthRequest(`${API_URL}/recipes/popular`); | 355 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes/popular`); |
334 | if (!request.ok) throw request; | 356 | if (!request.ok) throw request; |
335 | 357 | ||
336 | const data = await request.json(); | 358 | const data = await request.json(); |
@@ -342,7 +364,7 @@ export default class ServerApi { | |||
342 | } | 364 | } |
343 | 365 | ||
344 | async searchRecipePreviews(needle) { | 366 | async searchRecipePreviews(needle) { |
345 | const url = `${API_URL}/recipes/search?needle=${needle}`; | 367 | const url = `${this.apiUrl()}/recipes/search?needle=${needle}`; |
346 | const request = await sendAuthRequest(url); | 368 | const request = await sendAuthRequest(url); |
347 | if (!request.ok) throw request; | 369 | if (!request.ok) throw request; |
348 | 370 | ||
@@ -357,7 +379,7 @@ export default class ServerApi { | |||
357 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); | 379 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); |
358 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); | 380 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); |
359 | const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); | 381 | const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); |
360 | const packageUrl = `${API_URL}/recipes/download/${recipeId}`; | 382 | const packageUrl = `${this.apiUrl()}/recipes/download/${recipeId}`; |
361 | 383 | ||
362 | fs.ensureDirSync(recipeTempDirectory); | 384 | fs.ensureDirSync(recipeTempDirectory); |
363 | const res = await fetch(packageUrl); | 385 | const res = await fetch(packageUrl); |
@@ -394,7 +416,7 @@ export default class ServerApi { | |||
394 | 416 | ||
395 | // Payment | 417 | // Payment |
396 | async getPlans() { | 418 | async getPlans() { |
397 | const request = await sendAuthRequest(`${API_URL}/payment/plans`); | 419 | const request = await sendAuthRequest(`${this.apiUrl()}/payment/plans`); |
398 | if (!request.ok) throw request; | 420 | if (!request.ok) throw request; |
399 | const data = await request.json(); | 421 | const data = await request.json(); |
400 | const plan = new PlanModel(data); | 422 | const plan = new PlanModel(data); |
@@ -403,7 +425,7 @@ export default class ServerApi { | |||
403 | } | 425 | } |
404 | 426 | ||
405 | async getHostedPage(planId) { | 427 | async getHostedPage(planId) { |
406 | const request = await sendAuthRequest(`${API_URL}/payment/init`, { | 428 | const request = await sendAuthRequest(`${this.apiUrl()}/payment/init`, { |
407 | method: 'POST', | 429 | method: 'POST', |
408 | body: JSON.stringify({ | 430 | body: JSON.stringify({ |
409 | planId, | 431 | planId, |
@@ -420,7 +442,7 @@ export default class ServerApi { | |||
420 | 442 | ||
421 | // News | 443 | // News |
422 | async getLatestNews() { | 444 | async getLatestNews() { |
423 | const url = `${API_URL}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; | 445 | const url = `${this.apiUrl()}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; |
424 | const request = await sendAuthRequest(url); | 446 | const request = await sendAuthRequest(url); |
425 | if (!request.ok) throw request; | 447 | if (!request.ok) throw request; |
426 | const data = await request.json(); | 448 | const data = await request.json(); |
@@ -430,7 +452,7 @@ export default class ServerApi { | |||
430 | } | 452 | } |
431 | 453 | ||
432 | async hideNews(id) { | 454 | async hideNews(id) { |
433 | const request = await sendAuthRequest(`${API_URL}/news/${id}/read`); | 455 | const request = await sendAuthRequest(`${this.apiUrl()}/news/${id}/read`); |
434 | if (!request.ok) throw request; | 456 | if (!request.ok) throw request; |
435 | debug('ServerApi::hideNews resolves', id); | 457 | debug('ServerApi::hideNews resolves', id); |
436 | } | 458 | } |
@@ -455,7 +477,7 @@ export default class ServerApi { | |||
455 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { | 477 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { |
456 | const services = await Promise.all(config.services.map(async (s) => { | 478 | const services = await Promise.all(config.services.map(async (s) => { |
457 | const service = s; | 479 | const service = s; |
458 | const request = await sendAuthRequest(`${API_URL}/recipes/${s.service}`); | 480 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes/${s.service}`); |
459 | 481 | ||
460 | if (request.status === 200) { | 482 | if (request.status === 200) { |
461 | const data = await request.json(); | 483 | const data = await request.json(); |