aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Dominik Guzei <dominik.guzei@gmail.com>2019-03-12 18:43:43 +0100
committerLibravatar Dominik Guzei <dominik.guzei@gmail.com>2019-03-12 18:43:43 +0100
commitdc6d91e179cea94e8653cf7b568a143ce4ac3887 (patch)
tree30171a818f8dd643ea2ff3629f636a9db663e7b5 /src
parentrefactor server api to use prepare auth request util (diff)
downloadferdium-app-dc6d91e179cea94e8653cf7b568a143ce4ac3887.tar.gz
ferdium-app-dc6d91e179cea94e8653cf7b568a143ce4ac3887.tar.zst
ferdium-app-dc6d91e179cea94e8653cf7b568a143ce4ac3887.zip
refactor server api even more
Diffstat (limited to 'src')
-rw-r--r--src/api/server/ServerApi.js163
-rw-r--r--src/api/utils/auth.js6
2 files changed, 58 insertions, 111 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js
index cba08f43f..bafeef005 100644
--- a/src/api/server/ServerApi.js
+++ b/src/api/server/ServerApi.js
@@ -15,7 +15,7 @@ import OrderModel from '../../models/Order';
15import { sleep } from '../../helpers/async-helpers'; 15import { sleep } from '../../helpers/async-helpers';
16 16
17import { API } from '../../environment'; 17import { API } from '../../environment';
18import { prepareAuthRequest } from '../utils/auth'; 18import { prepareAuthRequest, sendAuthRequest } from '../utils/auth';
19 19
20import { 20import {
21 getRecipeDirectory, 21 getRecipeDirectory,
@@ -39,6 +39,7 @@ const { default: fetch } = remote.require('electron-fetch');
39 39
40const SERVER_URL = API; 40const SERVER_URL = API;
41const API_VERSION = 'v1'; 41const API_VERSION = 'v1';
42const API_URL = `${SERVER_URL}/${API_VERSION}`;
42 43
43export default class ServerApi { 44export default class ServerApi {
44 recipePreviews = []; 45 recipePreviews = [];
@@ -47,12 +48,12 @@ export default class ServerApi {
47 48
48 // User 49 // User
49 async login(email, passwordHash) { 50 async login(email, passwordHash) {
50 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/login`, prepareAuthRequest({ 51 const request = await sendAuthRequest(`${API_URL}/auth/login`, {
51 method: 'POST', 52 method: 'POST',
52 headers: { 53 headers: {
53 Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, 54 Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`,
54 }, 55 },
55 }, false)); 56 }, false);
56 if (!request.ok) { 57 if (!request.ok) {
57 throw request; 58 throw request;
58 } 59 }
@@ -63,10 +64,10 @@ export default class ServerApi {
63 } 64 }
64 65
65 async signup(data) { 66 async signup(data) {
66 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/signup`, prepareAuthRequest({ 67 const request = await sendAuthRequest(`${API_URL}/auth/signup`, {
67 method: 'POST', 68 method: 'POST',
68 body: JSON.stringify(data), 69 body: JSON.stringify(data),
69 }, false)); 70 }, false);
70 if (!request.ok) { 71 if (!request.ok) {
71 throw request; 72 throw request;
72 } 73 }
@@ -77,10 +78,10 @@ export default class ServerApi {
77 } 78 }
78 79
79 async inviteUser(data) { 80 async inviteUser(data) {
80 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/invite`, prepareAuthRequest({ 81 const request = await sendAuthRequest(`${API_URL}/invite`, {
81 method: 'POST', 82 method: 'POST',
82 body: JSON.stringify(data), 83 body: JSON.stringify(data),
83 })); 84 });
84 if (!request.ok) { 85 if (!request.ok) {
85 throw request; 86 throw request;
86 } 87 }
@@ -90,12 +91,12 @@ export default class ServerApi {
90 } 91 }
91 92
92 async retrievePassword(email) { 93 async retrievePassword(email) {
93 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/password`, prepareAuthRequest({ 94 const request = await sendAuthRequest(`${API_URL}/auth/password`, {
94 method: 'POST', 95 method: 'POST',
95 body: JSON.stringify({ 96 body: JSON.stringify({
96 email, 97 email,
97 }), 98 }),
98 }, false)); 99 }, false);
99 if (!request.ok) { 100 if (!request.ok) {
100 throw request; 101 throw request;
101 } 102 }
@@ -106,9 +107,7 @@ export default class ServerApi {
106 } 107 }
107 108
108 async userInfo() { 109 async userInfo() {
109 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ 110 const request = await sendAuthRequest(`${API_URL}/me`);
110 method: 'GET',
111 }));
112 if (!request.ok) { 111 if (!request.ok) {
113 throw request; 112 throw request;
114 } 113 }
@@ -121,10 +120,10 @@ export default class ServerApi {
121 } 120 }
122 121
123 async updateUserInfo(data) { 122 async updateUserInfo(data) {
124 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ 123 const request = await sendAuthRequest(`${API_URL}/me`, {
125 method: 'PUT', 124 method: 'PUT',
126 body: JSON.stringify(data), 125 body: JSON.stringify(data),
127 })); 126 });
128 if (!request.ok) { 127 if (!request.ok) {
129 throw request; 128 throw request;
130 } 129 }
@@ -136,9 +135,9 @@ export default class ServerApi {
136 } 135 }
137 136
138 async deleteAccount() { 137 async deleteAccount() {
139 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ 138 const request = await sendAuthRequest(`${API_URL}/me`, {
140 method: 'DELETE', 139 method: 'DELETE',
141 })); 140 });
142 if (!request.ok) { 141 if (!request.ok) {
143 throw request; 142 throw request;
144 } 143 }
@@ -150,9 +149,7 @@ export default class ServerApi {
150 149
151 // Services 150 // Services
152 async getServices() { 151 async getServices() {
153 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, prepareAuthRequest({ 152 const request = await sendAuthRequest(`${API_URL}/me/services`);
154 method: 'GET',
155 }));
156 if (!request.ok) { 153 if (!request.ok) {
157 throw request; 154 throw request;
158 } 155 }
@@ -165,12 +162,12 @@ export default class ServerApi {
165 } 162 }
166 163
167 async createService(recipeId, data) { 164 async createService(recipeId, data) {
168 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service`, prepareAuthRequest({ 165 const request = await sendAuthRequest(`${API_URL}/service`, {
169 method: 'POST', 166 method: 'POST',
170 body: JSON.stringify(Object.assign({ 167 body: JSON.stringify(Object.assign({
171 recipeId, 168 recipeId,
172 }, data)), 169 }, data)),
173 })); 170 });
174 if (!request.ok) { 171 if (!request.ok) {
175 throw request; 172 throw request;
176 } 173 }
@@ -195,10 +192,10 @@ export default class ServerApi {
195 await this.uploadServiceIcon(serviceId, data.iconFile); 192 await this.uploadServiceIcon(serviceId, data.iconFile);
196 } 193 }
197 194
198 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, prepareAuthRequest({ 195 const request = await sendAuthRequest(`${API_URL}/service/${serviceId}`, {
199 method: 'PUT', 196 method: 'PUT',
200 body: JSON.stringify(data), 197 body: JSON.stringify(data),
201 })); 198 });
202 199
203 if (!request.ok) { 200 if (!request.ok) {
204 throw request; 201 throw request;
@@ -223,7 +220,7 @@ export default class ServerApi {
223 220
224 delete requestData.headers['Content-Type']; 221 delete requestData.headers['Content-Type'];
225 222
226 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, requestData); 223 const request = await window.fetch(`${API_URL}/service/${serviceId}`, requestData);
227 224
228 if (!request.ok) { 225 if (!request.ok) {
229 throw request; 226 throw request;
@@ -235,10 +232,10 @@ export default class ServerApi {
235 } 232 }
236 233
237 async reorderService(data) { 234 async reorderService(data) {
238 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, prepareAuthRequest({ 235 const request = await sendAuthRequest(`${API_URL}/service/reorder`, {
239 method: 'PUT', 236 method: 'PUT',
240 body: JSON.stringify(data), 237 body: JSON.stringify(data),
241 })); 238 });
242 if (!request.ok) { 239 if (!request.ok) {
243 throw request; 240 throw request;
244 } 241 }
@@ -248,9 +245,9 @@ export default class ServerApi {
248 } 245 }
249 246
250 async deleteService(id) { 247 async deleteService(id) {
251 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${id}`, prepareAuthRequest({ 248 const request = await sendAuthRequest(`${API_URL}/service/${id}`, {
252 method: 'DELETE', 249 method: 'DELETE',
253 })); 250 });
254 if (!request.ok) { 251 if (!request.ok) {
255 throw request; 252 throw request;
256 } 253 }
@@ -264,9 +261,7 @@ export default class ServerApi {
264 261
265 // Features 262 // Features
266 async getDefaultFeatures() { 263 async getDefaultFeatures() {
267 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features/default`, prepareAuthRequest({ 264 const request = await sendAuthRequest(`${API_URL}/features/default`);
268 method: 'GET',
269 }));
270 if (!request.ok) { 265 if (!request.ok) {
271 throw request; 266 throw request;
272 } 267 }
@@ -278,9 +273,7 @@ export default class ServerApi {
278 } 273 }
279 274
280 async getFeatures() { 275 async getFeatures() {
281 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features`, prepareAuthRequest({ 276 const request = await sendAuthRequest(`${API_URL}/features`);
282 method: 'GET',
283 }));
284 if (!request.ok) { 277 if (!request.ok) {
285 throw request; 278 throw request;
286 } 279 }
@@ -314,10 +307,10 @@ export default class ServerApi {
314 } 307 }
315 308
316 async getRecipeUpdates(recipeVersions) { 309 async getRecipeUpdates(recipeVersions) {
317 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/update`, prepareAuthRequest({ 310 const request = await sendAuthRequest(`${API_URL}/recipes/update`, {
318 method: 'POST', 311 method: 'POST',
319 body: JSON.stringify(recipeVersions), 312 body: JSON.stringify(recipeVersions),
320 })); 313 });
321 if (!request.ok) { 314 if (!request.ok) {
322 throw request; 315 throw request;
323 } 316 }
@@ -328,29 +321,19 @@ export default class ServerApi {
328 321
329 // Recipes Previews 322 // Recipes Previews
330 async getRecipePreviews() { 323 async getRecipePreviews() {
331 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes`, prepareAuthRequest({ 324 const request = await sendAuthRequest(`${API_URL}/recipes`);
332 method: 'GET', 325 if (!request.ok) throw request;
333 }));
334 if (!request.ok) {
335 throw request;
336 }
337 const data = await request.json(); 326 const data = await request.json();
338
339 const recipePreviews = this._mapRecipePreviewModel(data); 327 const recipePreviews = this._mapRecipePreviewModel(data);
340 debug('ServerApi::getRecipes resolves', recipePreviews); 328 debug('ServerApi::getRecipes resolves', recipePreviews);
341
342 return recipePreviews; 329 return recipePreviews;
343 } 330 }
344 331
345 async getFeaturedRecipePreviews() { 332 async getFeaturedRecipePreviews() {
346 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/popular`, prepareAuthRequest({ 333 const request = await sendAuthRequest(`${API_URL}/recipes/popular`);
347 method: 'GET', 334 if (!request.ok) throw request;
348 }));
349 if (!request.ok) {
350 throw request;
351 }
352 const data = await request.json();
353 335
336 const data = await request.json();
354 // data = this._addLocalRecipesToPreviews(data); 337 // data = this._addLocalRecipesToPreviews(data);
355 338
356 const recipePreviews = this._mapRecipePreviewModel(data); 339 const recipePreviews = this._mapRecipePreviewModel(data);
@@ -359,14 +342,11 @@ export default class ServerApi {
359 } 342 }
360 343
361 async searchRecipePreviews(needle) { 344 async searchRecipePreviews(needle) {
362 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/search?needle=${needle}`, prepareAuthRequest({ 345 const url = `${API_URL}/recipes/search?needle=${needle}`;
363 method: 'GET', 346 const request = await sendAuthRequest(url);
364 })); 347 if (!request.ok) throw request;
365 if (!request.ok) {
366 throw request;
367 }
368 const data = await request.json();
369 348
349 const data = await request.json();
370 const recipePreviews = this._mapRecipePreviewModel(data); 350 const recipePreviews = this._mapRecipePreviewModel(data);
371 debug('ServerApi::searchRecipePreviews resolves', recipePreviews); 351 debug('ServerApi::searchRecipePreviews resolves', recipePreviews);
372 return recipePreviews; 352 return recipePreviews;
@@ -375,10 +355,9 @@ export default class ServerApi {
375 async getRecipePackage(recipeId) { 355 async getRecipePackage(recipeId) {
376 try { 356 try {
377 const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); 357 const recipesDirectory = path.join(app.getPath('userData'), 'recipes');
378
379 const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); 358 const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId);
380 const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); 359 const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz');
381 const packageUrl = `${SERVER_URL}/${API_VERSION}/recipes/download/${recipeId}`; 360 const packageUrl = `${API_URL}/recipes/download/${recipeId}`;
382 361
383 fs.ensureDirSync(recipeTempDirectory); 362 fs.ensureDirSync(recipeTempDirectory);
384 const res = await fetch(packageUrl); 363 const res = await fetch(packageUrl);
@@ -415,26 +394,21 @@ export default class ServerApi {
415 394
416 // Payment 395 // Payment
417 async getPlans() { 396 async getPlans() {
418 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/plans`, prepareAuthRequest({ 397 const request = await sendAuthRequest(`${API_URL}/payment/plans`);
419 method: 'GET', 398 if (!request.ok) throw request;
420 }));
421 if (!request.ok) {
422 throw request;
423 }
424 const data = await request.json(); 399 const data = await request.json();
425
426 const plan = new PlanModel(data); 400 const plan = new PlanModel(data);
427 debug('ServerApi::getPlans resolves', plan); 401 debug('ServerApi::getPlans resolves', plan);
428 return plan; 402 return plan;
429 } 403 }
430 404
431 async getHostedPage(planId) { 405 async getHostedPage(planId) {
432 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/init`, prepareAuthRequest({ 406 const request = await sendAuthRequest(`${API_URL}/payment/init`, {
433 method: 'POST', 407 method: 'POST',
434 body: JSON.stringify({ 408 body: JSON.stringify({
435 planId, 409 planId,
436 }), 410 }),
437 })); 411 });
438 if (!request.ok) { 412 if (!request.ok) {
439 throw request; 413 throw request;
440 } 414 }
@@ -445,25 +419,16 @@ export default class ServerApi {
445 } 419 }
446 420
447 async getPaymentDashboardUrl() { 421 async getPaymentDashboardUrl() {
448 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/billing`, prepareAuthRequest({ 422 const request = await sendAuthRequest(`${API_URL}/me/billing`);
449 method: 'GET', 423 if (!request.ok) throw request;
450 }));
451 if (!request.ok) {
452 throw request;
453 }
454 const data = await request.json(); 424 const data = await request.json();
455
456 debug('ServerApi::getPaymentDashboardUrl resolves', data); 425 debug('ServerApi::getPaymentDashboardUrl resolves', data);
457 return data; 426 return data;
458 } 427 }
459 428
460 async getSubscriptionOrders() { 429 async getSubscriptionOrders() {
461 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/subscription`, prepareAuthRequest({ 430 const request = await sendAuthRequest(`${API_URL}/me/subscription`);
462 method: 'GET', 431 if (!request.ok) throw request;
463 }));
464 if (!request.ok) {
465 throw request;
466 }
467 const data = await request.json(); 432 const data = await request.json();
468 const orders = this._mapOrderModels(data); 433 const orders = this._mapOrderModels(data);
469 debug('ServerApi::getSubscriptionOrders resolves', orders); 434 debug('ServerApi::getSubscriptionOrders resolves', orders);
@@ -472,15 +437,9 @@ export default class ServerApi {
472 437
473 // News 438 // News
474 async getLatestNews() { 439 async getLatestNews() {
475 // eslint-disable-next-line 440 const url = `${API_URL}/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()}`, 441 const request = await sendAuthRequest(url);
477 prepareAuthRequest({ 442 if (!request.ok) throw request;
478 method: 'GET',
479 }));
480
481 if (!request.ok) {
482 throw request;
483 }
484 const data = await request.json(); 443 const data = await request.json();
485 const news = this._mapNewsModels(data); 444 const news = this._mapNewsModels(data);
486 debug('ServerApi::getLatestNews resolves', news); 445 debug('ServerApi::getLatestNews resolves', news);
@@ -488,23 +447,16 @@ export default class ServerApi {
488 } 447 }
489 448
490 async hideNews(id) { 449 async hideNews(id) {
491 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news/${id}/read`, 450 const request = await sendAuthRequest(`${API_URL}/news/${id}/read`);
492 prepareAuthRequest({ 451 if (!request.ok) throw request;
493 method: 'GET',
494 }));
495
496 if (!request.ok) {
497 throw request;
498 }
499
500 debug('ServerApi::hideNews resolves', id); 452 debug('ServerApi::hideNews resolves', id);
501 } 453 }
502 454
503 // Health Check 455 // Health Check
504 async healthCheck() { 456 async healthCheck() {
505 const request = await window.fetch(`${SERVER_URL}/health`, prepareAuthRequest({ 457 const request = await sendAuthRequest(`${SERVER_URL}/health`, {
506 method: 'GET', 458 method: 'GET',
507 }, false)); 459 }, false);
508 if (!request.ok) { 460 if (!request.ok) {
509 throw request; 461 throw request;
510 } 462 }
@@ -520,10 +472,7 @@ export default class ServerApi {
520 if (Object.prototype.hasOwnProperty.call(config, 'services')) { 472 if (Object.prototype.hasOwnProperty.call(config, 'services')) {
521 const services = await Promise.all(config.services.map(async (s) => { 473 const services = await Promise.all(config.services.map(async (s) => {
522 const service = s; 474 const service = s;
523 const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/${s.service}`, 475 const request = await sendAuthRequest(`${API_URL}/recipes/${s.service}`);
524 prepareAuthRequest({
525 method: 'GET',
526 }));
527 476
528 if (request.status === 200) { 477 if (request.status === 200) {
529 const data = await request.json(); 478 const data = await request.json();
@@ -546,9 +495,7 @@ export default class ServerApi {
546 // Helper 495 // Helper
547 async _mapServiceModels(services) { 496 async _mapServiceModels(services) {
548 const recipes = services.map(s => s.recipeId); 497 const recipes = services.map(s => s.recipeId);
549
550 await this._bulkRecipeCheck(recipes); 498 await this._bulkRecipeCheck(recipes);
551
552 /* eslint-disable no-return-await */ 499 /* eslint-disable no-return-await */
553 return Promise.all(services.map(async service => await this._prepareServiceModel(service))); 500 return Promise.all(services.map(async service => await this._prepareServiceModel(service)));
554 /* eslint-enable no-return-await */ 501 /* eslint-enable no-return-await */
diff --git a/src/api/utils/auth.js b/src/api/utils/auth.js
index d469853a5..6dbdeaa7f 100644
--- a/src/api/utils/auth.js
+++ b/src/api/utils/auth.js
@@ -3,7 +3,7 @@ import localStorage from 'mobx-localstorage';
3 3
4const { app } = remote; 4const { app } = remote;
5 5
6export const prepareAuthRequest = (options, auth = true) => { 6export const prepareAuthRequest = (options = { method: 'GET' }, auth = true) => {
7 const request = Object.assign(options, { 7 const request = Object.assign(options, {
8 mode: 'cors', 8 mode: 'cors',
9 headers: Object.assign({ 9 headers: Object.assign({
@@ -23,6 +23,6 @@ export const prepareAuthRequest = (options, auth = true) => {
23 return request; 23 return request;
24}; 24};
25 25
26export const sendAuthRequest = (url, options) => ( 26export const sendAuthRequest = (url, options, auth) => (
27 window.fetch(url, prepareAuthRequest(options)) 27 window.fetch(url, prepareAuthRequest(options, auth))
28); 28);