aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
authorLibravatar Markus Hatvan <markus_hatvan@aon.at>2021-09-14 19:58:52 +0200
committerLibravatar GitHub <noreply@github.com>2021-09-14 19:58:52 +0200
commit95df3522a15631abc51a4295cae0ea401a8d4e1e (patch)
treee5eb0f368c947683f01458e912f21756fb0d99cb /src/api
parentdocs: add sad270 as a contributor for bug, userTesting [skip ci] (#1941) (diff)
downloadferdium-app-95df3522a15631abc51a4295cae0ea401a8d4e1e.tar.gz
ferdium-app-95df3522a15631abc51a4295cae0ea401a8d4e1e.tar.zst
ferdium-app-95df3522a15631abc51a4295cae0ea401a8d4e1e.zip
feat: add eslint-plugin-unicorn (#1936)
Diffstat (limited to 'src/api')
-rw-r--r--src/api/apiBase.ts17
-rw-r--r--src/api/server/ServerApi.js80
-rw-r--r--src/api/utils/auth.js15
3 files changed, 65 insertions, 47 deletions
diff --git a/src/api/apiBase.ts b/src/api/apiBase.ts
index dc10fad91..510ccb619 100644
--- a/src/api/apiBase.ts
+++ b/src/api/apiBase.ts
@@ -12,8 +12,6 @@ import {
12 12
13// Note: This cannot be used from the internal-server since we are not running within the context of a browser window 13// Note: This cannot be used from the internal-server since we are not running within the context of a browser window
14const apiBase = (withVersion = true) => { 14const apiBase = (withVersion = true) => {
15 let url: string;
16
17 if ( 15 if (
18 !(window as any).ferdi || 16 !(window as any).ferdi ||
19 !(window as any).ferdi.stores.settings || 17 !(window as any).ferdi.stores.settings ||
@@ -23,15 +21,12 @@ const apiBase = (withVersion = true) => {
23 // Stores have not yet been loaded - return SERVER_NOT_LOADED to force a retry when stores are loaded 21 // Stores have not yet been loaded - return SERVER_NOT_LOADED to force a retry when stores are loaded
24 return SERVER_NOT_LOADED; 22 return SERVER_NOT_LOADED;
25 } 23 }
26 if ((window as any).ferdi.stores.settings.all.app.server === LOCAL_SERVER) { 24 const url =
27 // Use URL for local server 25 (window as any).ferdi.stores.settings.all.app.server === LOCAL_SERVER
28 url = `http://${LOCAL_HOSTNAME}:${ 26 ? `http://${LOCAL_HOSTNAME}:${
29 (window as any).ferdi.stores.requests.localServerPort 27 (window as any).ferdi.stores.requests.localServerPort
30 }`; 28 }`
31 } else { 29 : (window as any).ferdi.stores.settings.all.app.server;
32 // Load URL from store
33 url = (window as any).ferdi.stores.settings.all.app.server;
34 }
35 30
36 return withVersion ? `${url}/${API_VERSION}` : url; 31 return withVersion ? `${url}/${API_VERSION}` : url;
37}; 32};
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js
index b5042525a..fb0495b19 100644
--- a/src/api/server/ServerApi.js
+++ b/src/api/server/ServerApi.js
@@ -1,6 +1,16 @@
1/* eslint-disable global-require */
1import { join } from 'path'; 2import { join } from 'path';
2import tar from 'tar'; 3import tar from 'tar';
3import { readdirSync, statSync, writeFileSync, copySync, ensureDirSync, pathExistsSync, readJsonSync, removeSync } from 'fs-extra'; 4import {
5 readdirSync,
6 statSync,
7 writeFileSync,
8 copySync,
9 ensureDirSync,
10 pathExistsSync,
11 readJsonSync,
12 removeSync,
13} from 'fs-extra';
4import { require as remoteRequire } from '@electron/remote'; 14import { require as remoteRequire } from '@electron/remote';
5 15
6import ServiceModel from '../../models/Service'; 16import ServiceModel from '../../models/Service';
@@ -12,7 +22,14 @@ import UserModel from '../../models/User';
12import { sleep } from '../../helpers/async-helpers'; 22import { sleep } from '../../helpers/async-helpers';
13 23
14import { SERVER_NOT_LOADED } from '../../config'; 24import { SERVER_NOT_LOADED } from '../../config';
15import { osArch, osPlatform, asarRecipesPath, userDataRecipesPath, userDataPath, ferdiVersion } from '../../environment'; 25import {
26 osArch,
27 osPlatform,
28 asarRecipesPath,
29 userDataRecipesPath,
30 userDataPath,
31 ferdiVersion,
32} from '../../environment';
16import apiBase from '../apiBase'; 33import apiBase from '../apiBase';
17import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; 34import { prepareAuthRequest, sendAuthRequest } from '../utils/auth';
18 35
@@ -310,22 +327,22 @@ export default class ServerApi {
310 // Recipes 327 // Recipes
311 async getInstalledRecipes() { 328 async getInstalledRecipes() {
312 const recipesDirectory = getRecipeDirectory(); 329 const recipesDirectory = getRecipeDirectory();
313 const paths = readdirSync(recipesDirectory) 330 const paths = readdirSync(recipesDirectory).filter(
314 .filter( 331 file =>
315 file => 332 statSync(join(recipesDirectory, file)).isDirectory() &&
316 statSync(join(recipesDirectory, file)).isDirectory() && 333 file !== 'temp' &&
317 file !== 'temp' && 334 file !== 'dev',
318 file !== 'dev', 335 );
319 );
320 336
321 this.recipes = paths 337 this.recipes = paths
322 .map(id => { 338 .map(id => {
323 // eslint-disable-next-line 339 // eslint-disable-next-line import/no-dynamic-require
324 const Recipe = require(id)(RecipeModel); 340 const Recipe = require(id)(RecipeModel);
325 return new Recipe(loadRecipeConfig(id)); 341 return new Recipe(loadRecipeConfig(id));
326 }) 342 })
327 .filter(recipe => recipe.id); 343 .filter(recipe => recipe.id);
328 344
345 // eslint-disable-next-line unicorn/prefer-spread
329 this.recipes = this.recipes.concat(this._getDevRecipes()); 346 this.recipes = this.recipes.concat(this._getDevRecipes());
330 347
331 debug('StubServerApi::getInstalledRecipes resolves', this.recipes); 348 debug('StubServerApi::getInstalledRecipes resolves', this.recipes);
@@ -425,8 +442,8 @@ export default class ServerApi {
425 removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz')); 442 removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz'));
426 443
427 return id; 444 return id;
428 } catch (err) { 445 } catch (error) {
429 console.error(err); 446 console.error(error);
430 447
431 return false; 448 return false;
432 } 449 }
@@ -434,7 +451,9 @@ export default class ServerApi {
434 451
435 // News 452 // News
436 async getLatestNews() { 453 async getLatestNews() {
437 const url = `${apiBase(true)}/news?platform=${osPlatform}&arch=${osArch}&version=${ferdiVersion}`; 454 const url = `${apiBase(
455 true,
456 )}/news?platform=${osPlatform}&arch=${osArch}&version=${ferdiVersion}`;
438 const request = await sendAuthRequest(url); 457 const request = await sendAuthRequest(url);
439 if (!request.ok) throw request; 458 if (!request.ok) throw request;
440 const data = await request.json(); 459 const data = await request.json();
@@ -494,7 +513,7 @@ export default class ServerApi {
494 debug('ServerApi::getLegacyServices resolves', services); 513 debug('ServerApi::getLegacyServices resolves', services);
495 return services; 514 return services;
496 } 515 }
497 } catch (err) { 516 } catch {
498 console.error('ServerApi::getLegacyServices no config found'); 517 console.error('ServerApi::getLegacyServices no config found');
499 } 518 }
500 519
@@ -523,8 +542,8 @@ export default class ServerApi {
523 } 542 }
524 543
525 return new ServiceModel(service, recipe); 544 return new ServiceModel(service, recipe);
526 } catch (e) { 545 } catch (error) {
527 debug(e); 546 debug(error);
528 return null; 547 return null;
529 } 548 }
530 } 549 }
@@ -559,7 +578,7 @@ export default class ServerApi {
559 578
560 return recipe; 579 return recipe;
561 }), 580 }),
562 ).catch(err => console.error("Can't load recipe", err)); 581 ).catch(error => console.error("Can't load recipe", error));
563 } 582 }
564 583
565 _mapRecipePreviewModel(recipes) { 584 _mapRecipePreviewModel(recipes) {
@@ -567,8 +586,8 @@ export default class ServerApi {
567 .map(recipe => { 586 .map(recipe => {
568 try { 587 try {
569 return new RecipePreviewModel(recipe); 588 return new RecipePreviewModel(recipe);
570 } catch (e) { 589 } catch (error) {
571 console.error(e); 590 console.error(error);
572 return null; 591 return null;
573 } 592 }
574 }) 593 })
@@ -580,8 +599,8 @@ export default class ServerApi {
580 .map(newsItem => { 599 .map(newsItem => {
581 try { 600 try {
582 return new NewsModel(newsItem); 601 return new NewsModel(newsItem);
583 } catch (e) { 602 } catch (error) {
584 console.error(e); 603 console.error(error);
585 return null; 604 return null;
586 } 605 }
587 }) 606 })
@@ -591,22 +610,21 @@ export default class ServerApi {
591 _getDevRecipes() { 610 _getDevRecipes() {
592 const recipesDirectory = getDevRecipeDirectory(); 611 const recipesDirectory = getDevRecipeDirectory();
593 try { 612 try {
594 const paths = readdirSync(recipesDirectory) 613 const paths = readdirSync(recipesDirectory).filter(
595 .filter( 614 file =>
596 file => 615 statSync(join(recipesDirectory, file)).isDirectory() &&
597 statSync(join(recipesDirectory, file)).isDirectory() && 616 file !== 'temp',
598 file !== 'temp', 617 );
599 );
600 618
601 const recipes = paths 619 const recipes = paths
602 .map(id => { 620 .map(id => {
603 let Recipe; 621 let Recipe;
604 try { 622 try {
605 // eslint-disable-next-line 623 // eslint-disable-next-line import/no-dynamic-require
606 Recipe = require(id)(RecipeModel); 624 Recipe = require(id)(RecipeModel);
607 return new Recipe(loadRecipeConfig(id)); 625 return new Recipe(loadRecipeConfig(id));
608 } catch (err) { 626 } catch (error) {
609 console.error(err); 627 console.error(error);
610 } 628 }
611 629
612 return false; 630 return false;
@@ -624,7 +642,7 @@ export default class ServerApi {
624 }); 642 });
625 643
626 return recipes; 644 return recipes;
627 } catch (err) { 645 } catch {
628 debug('Could not load dev recipes'); 646 debug('Could not load dev recipes');
629 return false; 647 return false;
630 } 648 }
diff --git a/src/api/utils/auth.js b/src/api/utils/auth.js
index e493b2962..527c68840 100644
--- a/src/api/utils/auth.js
+++ b/src/api/utils/auth.js
@@ -1,7 +1,11 @@
1import localStorage from 'mobx-localstorage'; 1import localStorage from 'mobx-localstorage';
2import { ferdiLocale, ferdiVersion } from '../../environment'; 2import { ferdiLocale, ferdiVersion } from '../../environment';
3 3
4export const prepareAuthRequest = (options = { method: 'GET' }, auth = true) => { 4export const prepareAuthRequest = (
5 // eslint-disable-next-line unicorn/no-object-as-default-parameter
6 options = { method: 'GET' },
7 auth = true,
8) => {
5 const request = Object.assign(options, { 9 const request = Object.assign(options, {
6 mode: 'cors', 10 mode: 'cors',
7 headers: { 11 headers: {
@@ -16,12 +20,13 @@ export const prepareAuthRequest = (options = { method: 'GET' }, auth = true) =>
16 }); 20 });
17 21
18 if (auth) { 22 if (auth) {
19 request.headers.Authorization = `Bearer ${localStorage.getItem('authToken')}`; 23 request.headers.Authorization = `Bearer ${localStorage.getItem(
24 'authToken',
25 )}`;
20 } 26 }
21 27
22 return request; 28 return request;
23}; 29};
24 30
25export const sendAuthRequest = (url, options, auth) => ( 31export const sendAuthRequest = (url, options, auth) =>
26 window.fetch(url, prepareAuthRequest(options, auth)) 32 window.fetch(url, prepareAuthRequest(options, auth));
27);