diff options
-rw-r--r-- | src/api/server/ServerApi.js | 76 |
1 files changed, 28 insertions, 48 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index 2871769a9..cba08f43f 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -3,7 +3,6 @@ import path from 'path'; | |||
3 | import tar from 'tar'; | 3 | import tar from 'tar'; |
4 | import fs from 'fs-extra'; | 4 | import fs from 'fs-extra'; |
5 | import { remote } from 'electron'; | 5 | import { remote } from 'electron'; |
6 | import localStorage from 'mobx-localstorage'; | ||
7 | 6 | ||
8 | import ServiceModel from '../../models/Service'; | 7 | import ServiceModel from '../../models/Service'; |
9 | import RecipePreviewModel from '../../models/RecipePreview'; | 8 | import RecipePreviewModel from '../../models/RecipePreview'; |
@@ -16,6 +15,7 @@ import OrderModel from '../../models/Order'; | |||
16 | import { sleep } from '../../helpers/async-helpers'; | 15 | import { sleep } from '../../helpers/async-helpers'; |
17 | 16 | ||
18 | import { API } from '../../environment'; | 17 | import { API } from '../../environment'; |
18 | import { prepareAuthRequest } from '../utils/auth'; | ||
19 | 19 | ||
20 | import { | 20 | import { |
21 | getRecipeDirectory, | 21 | getRecipeDirectory, |
@@ -47,7 +47,7 @@ export default class ServerApi { | |||
47 | 47 | ||
48 | // User | 48 | // User |
49 | async login(email, passwordHash) { | 49 | async login(email, passwordHash) { |
50 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/login`, this._prepareAuthRequest({ | 50 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/login`, prepareAuthRequest({ |
51 | method: 'POST', | 51 | method: 'POST', |
52 | headers: { | 52 | headers: { |
53 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, | 53 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, |
@@ -63,7 +63,7 @@ export default class ServerApi { | |||
63 | } | 63 | } |
64 | 64 | ||
65 | async signup(data) { | 65 | async signup(data) { |
66 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/signup`, this._prepareAuthRequest({ | 66 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/signup`, prepareAuthRequest({ |
67 | method: 'POST', | 67 | method: 'POST', |
68 | body: JSON.stringify(data), | 68 | body: JSON.stringify(data), |
69 | }, false)); | 69 | }, false)); |
@@ -77,7 +77,7 @@ export default class ServerApi { | |||
77 | } | 77 | } |
78 | 78 | ||
79 | async inviteUser(data) { | 79 | async inviteUser(data) { |
80 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/invite`, this._prepareAuthRequest({ | 80 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/invite`, prepareAuthRequest({ |
81 | method: 'POST', | 81 | method: 'POST', |
82 | body: JSON.stringify(data), | 82 | body: JSON.stringify(data), |
83 | })); | 83 | })); |
@@ -90,7 +90,7 @@ export default class ServerApi { | |||
90 | } | 90 | } |
91 | 91 | ||
92 | async retrievePassword(email) { | 92 | async retrievePassword(email) { |
93 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/password`, this._prepareAuthRequest({ | 93 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/password`, prepareAuthRequest({ |
94 | method: 'POST', | 94 | method: 'POST', |
95 | body: JSON.stringify({ | 95 | body: JSON.stringify({ |
96 | email, | 96 | email, |
@@ -106,7 +106,7 @@ export default class ServerApi { | |||
106 | } | 106 | } |
107 | 107 | ||
108 | async userInfo() { | 108 | async userInfo() { |
109 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({ | 109 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ |
110 | method: 'GET', | 110 | method: 'GET', |
111 | })); | 111 | })); |
112 | if (!request.ok) { | 112 | if (!request.ok) { |
@@ -121,7 +121,7 @@ export default class ServerApi { | |||
121 | } | 121 | } |
122 | 122 | ||
123 | async updateUserInfo(data) { | 123 | async updateUserInfo(data) { |
124 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({ | 124 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ |
125 | method: 'PUT', | 125 | method: 'PUT', |
126 | body: JSON.stringify(data), | 126 | body: JSON.stringify(data), |
127 | })); | 127 | })); |
@@ -136,7 +136,7 @@ export default class ServerApi { | |||
136 | } | 136 | } |
137 | 137 | ||
138 | async deleteAccount() { | 138 | async deleteAccount() { |
139 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({ | 139 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ |
140 | method: 'DELETE', | 140 | method: 'DELETE', |
141 | })); | 141 | })); |
142 | if (!request.ok) { | 142 | if (!request.ok) { |
@@ -150,7 +150,7 @@ export default class ServerApi { | |||
150 | 150 | ||
151 | // Services | 151 | // Services |
152 | async getServices() { | 152 | async getServices() { |
153 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, this._prepareAuthRequest({ | 153 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, prepareAuthRequest({ |
154 | method: 'GET', | 154 | method: 'GET', |
155 | })); | 155 | })); |
156 | if (!request.ok) { | 156 | if (!request.ok) { |
@@ -165,7 +165,7 @@ export default class ServerApi { | |||
165 | } | 165 | } |
166 | 166 | ||
167 | async createService(recipeId, data) { | 167 | async createService(recipeId, data) { |
168 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service`, this._prepareAuthRequest({ | 168 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service`, prepareAuthRequest({ |
169 | method: 'POST', | 169 | method: 'POST', |
170 | body: JSON.stringify(Object.assign({ | 170 | body: JSON.stringify(Object.assign({ |
171 | recipeId, | 171 | recipeId, |
@@ -195,7 +195,7 @@ export default class ServerApi { | |||
195 | await this.uploadServiceIcon(serviceId, data.iconFile); | 195 | await this.uploadServiceIcon(serviceId, data.iconFile); |
196 | } | 196 | } |
197 | 197 | ||
198 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, this._prepareAuthRequest({ | 198 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, prepareAuthRequest({ |
199 | method: 'PUT', | 199 | method: 'PUT', |
200 | body: JSON.stringify(data), | 200 | body: JSON.stringify(data), |
201 | })); | 201 | })); |
@@ -216,7 +216,7 @@ export default class ServerApi { | |||
216 | const formData = new FormData(); | 216 | const formData = new FormData(); |
217 | formData.append('icon', icon); | 217 | formData.append('icon', icon); |
218 | 218 | ||
219 | const requestData = this._prepareAuthRequest({ | 219 | const requestData = prepareAuthRequest({ |
220 | method: 'PUT', | 220 | method: 'PUT', |
221 | body: formData, | 221 | body: formData, |
222 | }); | 222 | }); |
@@ -235,7 +235,7 @@ export default class ServerApi { | |||
235 | } | 235 | } |
236 | 236 | ||
237 | async reorderService(data) { | 237 | async reorderService(data) { |
238 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, this._prepareAuthRequest({ | 238 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, prepareAuthRequest({ |
239 | method: 'PUT', | 239 | method: 'PUT', |
240 | body: JSON.stringify(data), | 240 | body: JSON.stringify(data), |
241 | })); | 241 | })); |
@@ -248,7 +248,7 @@ export default class ServerApi { | |||
248 | } | 248 | } |
249 | 249 | ||
250 | async deleteService(id) { | 250 | async deleteService(id) { |
251 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${id}`, this._prepareAuthRequest({ | 251 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${id}`, prepareAuthRequest({ |
252 | method: 'DELETE', | 252 | method: 'DELETE', |
253 | })); | 253 | })); |
254 | if (!request.ok) { | 254 | if (!request.ok) { |
@@ -264,7 +264,7 @@ export default class ServerApi { | |||
264 | 264 | ||
265 | // Features | 265 | // Features |
266 | async getDefaultFeatures() { | 266 | async getDefaultFeatures() { |
267 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features/default`, this._prepareAuthRequest({ | 267 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features/default`, prepareAuthRequest({ |
268 | method: 'GET', | 268 | method: 'GET', |
269 | })); | 269 | })); |
270 | if (!request.ok) { | 270 | if (!request.ok) { |
@@ -278,7 +278,7 @@ export default class ServerApi { | |||
278 | } | 278 | } |
279 | 279 | ||
280 | async getFeatures() { | 280 | async getFeatures() { |
281 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features`, this._prepareAuthRequest({ | 281 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features`, prepareAuthRequest({ |
282 | method: 'GET', | 282 | method: 'GET', |
283 | })); | 283 | })); |
284 | if (!request.ok) { | 284 | if (!request.ok) { |
@@ -314,7 +314,7 @@ export default class ServerApi { | |||
314 | } | 314 | } |
315 | 315 | ||
316 | async getRecipeUpdates(recipeVersions) { | 316 | async getRecipeUpdates(recipeVersions) { |
317 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/update`, this._prepareAuthRequest({ | 317 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/update`, prepareAuthRequest({ |
318 | method: 'POST', | 318 | method: 'POST', |
319 | body: JSON.stringify(recipeVersions), | 319 | body: JSON.stringify(recipeVersions), |
320 | })); | 320 | })); |
@@ -328,7 +328,7 @@ export default class ServerApi { | |||
328 | 328 | ||
329 | // Recipes Previews | 329 | // Recipes Previews |
330 | async getRecipePreviews() { | 330 | async getRecipePreviews() { |
331 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes`, this._prepareAuthRequest({ | 331 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes`, prepareAuthRequest({ |
332 | method: 'GET', | 332 | method: 'GET', |
333 | })); | 333 | })); |
334 | if (!request.ok) { | 334 | if (!request.ok) { |
@@ -343,7 +343,7 @@ export default class ServerApi { | |||
343 | } | 343 | } |
344 | 344 | ||
345 | async getFeaturedRecipePreviews() { | 345 | async getFeaturedRecipePreviews() { |
346 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/popular`, this._prepareAuthRequest({ | 346 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/popular`, prepareAuthRequest({ |
347 | method: 'GET', | 347 | method: 'GET', |
348 | })); | 348 | })); |
349 | if (!request.ok) { | 349 | if (!request.ok) { |
@@ -359,7 +359,7 @@ export default class ServerApi { | |||
359 | } | 359 | } |
360 | 360 | ||
361 | async searchRecipePreviews(needle) { | 361 | async searchRecipePreviews(needle) { |
362 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/search?needle=${needle}`, this._prepareAuthRequest({ | 362 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/search?needle=${needle}`, prepareAuthRequest({ |
363 | method: 'GET', | 363 | method: 'GET', |
364 | })); | 364 | })); |
365 | if (!request.ok) { | 365 | if (!request.ok) { |
@@ -415,7 +415,7 @@ export default class ServerApi { | |||
415 | 415 | ||
416 | // Payment | 416 | // Payment |
417 | async getPlans() { | 417 | async getPlans() { |
418 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/plans`, this._prepareAuthRequest({ | 418 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/plans`, prepareAuthRequest({ |
419 | method: 'GET', | 419 | method: 'GET', |
420 | })); | 420 | })); |
421 | if (!request.ok) { | 421 | if (!request.ok) { |
@@ -429,7 +429,7 @@ export default class ServerApi { | |||
429 | } | 429 | } |
430 | 430 | ||
431 | async getHostedPage(planId) { | 431 | async getHostedPage(planId) { |
432 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/init`, this._prepareAuthRequest({ | 432 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/init`, prepareAuthRequest({ |
433 | method: 'POST', | 433 | method: 'POST', |
434 | body: JSON.stringify({ | 434 | body: JSON.stringify({ |
435 | planId, | 435 | planId, |
@@ -445,7 +445,7 @@ export default class ServerApi { | |||
445 | } | 445 | } |
446 | 446 | ||
447 | async getPaymentDashboardUrl() { | 447 | async getPaymentDashboardUrl() { |
448 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/billing`, this._prepareAuthRequest({ | 448 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/billing`, prepareAuthRequest({ |
449 | method: 'GET', | 449 | method: 'GET', |
450 | })); | 450 | })); |
451 | if (!request.ok) { | 451 | if (!request.ok) { |
@@ -458,7 +458,7 @@ export default class ServerApi { | |||
458 | } | 458 | } |
459 | 459 | ||
460 | async getSubscriptionOrders() { | 460 | async getSubscriptionOrders() { |
461 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/subscription`, this._prepareAuthRequest({ | 461 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/subscription`, prepareAuthRequest({ |
462 | method: 'GET', | 462 | method: 'GET', |
463 | })); | 463 | })); |
464 | if (!request.ok) { | 464 | if (!request.ok) { |
@@ -474,7 +474,7 @@ export default class ServerApi { | |||
474 | async getLatestNews() { | 474 | async getLatestNews() { |
475 | // eslint-disable-next-line | 475 | // eslint-disable-next-line |
476 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`, | 476 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`, |
477 | this._prepareAuthRequest({ | 477 | prepareAuthRequest({ |
478 | method: 'GET', | 478 | method: 'GET', |
479 | })); | 479 | })); |
480 | 480 | ||
@@ -489,7 +489,7 @@ export default class ServerApi { | |||
489 | 489 | ||
490 | async hideNews(id) { | 490 | async hideNews(id) { |
491 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news/${id}/read`, | 491 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news/${id}/read`, |
492 | this._prepareAuthRequest({ | 492 | prepareAuthRequest({ |
493 | method: 'GET', | 493 | method: 'GET', |
494 | })); | 494 | })); |
495 | 495 | ||
@@ -502,7 +502,7 @@ export default class ServerApi { | |||
502 | 502 | ||
503 | // Health Check | 503 | // Health Check |
504 | async healthCheck() { | 504 | async healthCheck() { |
505 | const request = await window.fetch(`${SERVER_URL}/health`, this._prepareAuthRequest({ | 505 | const request = await window.fetch(`${SERVER_URL}/health`, prepareAuthRequest({ |
506 | method: 'GET', | 506 | method: 'GET', |
507 | }, false)); | 507 | }, false)); |
508 | if (!request.ok) { | 508 | if (!request.ok) { |
@@ -521,7 +521,7 @@ export default class ServerApi { | |||
521 | const services = await Promise.all(config.services.map(async (s) => { | 521 | const services = await Promise.all(config.services.map(async (s) => { |
522 | const service = s; | 522 | const service = s; |
523 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/${s.service}`, | 523 | const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/${s.service}`, |
524 | this._prepareAuthRequest({ | 524 | prepareAuthRequest({ |
525 | method: 'GET', | 525 | method: 'GET', |
526 | })); | 526 | })); |
527 | 527 | ||
@@ -632,26 +632,6 @@ export default class ServerApi { | |||
632 | }).filter(orderItem => orderItem !== null); | 632 | }).filter(orderItem => orderItem !== null); |
633 | } | 633 | } |
634 | 634 | ||
635 | _prepareAuthRequest(options, auth = true) { | ||
636 | const request = Object.assign(options, { | ||
637 | mode: 'cors', | ||
638 | headers: Object.assign({ | ||
639 | 'Content-Type': 'application/json', | ||
640 | 'X-Franz-Source': 'desktop', | ||
641 | 'X-Franz-Version': app.getVersion(), | ||
642 | 'X-Franz-platform': process.platform, | ||
643 | 'X-Franz-Timezone-Offset': new Date().getTimezoneOffset(), | ||
644 | 'X-Franz-System-Locale': app.getLocale(), | ||
645 | }, options.headers), | ||
646 | }); | ||
647 | |||
648 | if (auth) { | ||
649 | request.headers.Authorization = `Bearer ${localStorage.getItem('authToken')}`; | ||
650 | } | ||
651 | |||
652 | return request; | ||
653 | } | ||
654 | |||
655 | _getDevRecipes() { | 635 | _getDevRecipes() { |
656 | const recipesDirectory = getDevRecipeDirectory(); | 636 | const recipesDirectory = getDevRecipeDirectory(); |
657 | try { | 637 | try { |