From 32f76b74a69ad4d60a014bf075c39517888436bc Mon Sep 17 00:00:00 2001 From: MCMXC <16797721+mcmxcdev@users.noreply.github.com> Date: Sat, 29 Jul 2023 21:12:16 -0600 Subject: refactor: various improvements (#1296) * refactor: various improvements - enable no-use-before-define eslint rule - shuffle code to conform to no-use-before-define eslint rule - remove btoa dependency which is deprecated and replace with Buffer.from(string).toString('base64') - convert some any types into useful ones - add type annotations where possible - remove unused @types/expect.js - install @types/semver and ts-node which were missing - repair and rewrite add-crowdin-contributors script - remove export keyword from variables which are never consumed in another file - remove unity indicator hack where linked issue was closed - remove module declaration for kebab-case which is unused - add missing state interface for certain components - remove default exports for files which already have a named export - export IRecipePreview so it can be used throughout codebase - remove unused removeCacheForCallWith method from CachedRequest.ts - cleanup unused colors and styles inside legacy theme * - improve ColorPickerInput - fix invalid DOM nesting with div inside p in EditSettingsForm - fix progressbarAccentColor color picker not updating input when using slider - install missing @types/react-color dependency --- src/api/server/ServerApi.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src/api/server/ServerApi.ts') diff --git a/src/api/server/ServerApi.ts b/src/api/server/ServerApi.ts index 860b7b76e..de8e0a85c 100644 --- a/src/api/server/ServerApi.ts +++ b/src/api/server/ServerApi.ts @@ -15,8 +15,8 @@ import { } from 'fs-extra'; import ServiceModel from '../../models/Service'; -import RecipePreviewModel from '../../models/RecipePreview'; -import RecipeModel from '../../models/Recipe'; +import RecipePreviewModel, { IRecipePreview } from '../../models/RecipePreview'; +import RecipeModel, { IRecipe } from '../../models/Recipe'; import UserModel from '../../models/User'; import sleep from '../../helpers/async-helpers'; @@ -44,9 +44,9 @@ const debug = require('../../preload-safe-debug')('Ferdium:ServerApi'); module.paths.unshift(getDevRecipeDirectory(), getRecipeDirectory()); export default class ServerApi { - recipePreviews: any[] = []; + recipePreviews: IRecipePreview[] = []; - recipes: any[] = []; + recipes: IRecipe[] = []; // User async login(email: string, passwordHash: string) { @@ -55,7 +55,9 @@ export default class ServerApi { { method: 'POST', headers: { - Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, + Authorization: `Basic ${Buffer.from( + `${email}:${passwordHash}`, + ).toString('base64')}`, }, }, false, @@ -346,6 +348,7 @@ export default class ServerApi { }) .filter(recipe => recipe.id); + // @ts-expect-error Type 'boolean' is not assignable to type 'ConcatArray'. // eslint-disable-next-line unicorn/prefer-spread this.recipes = this.recipes.concat(this._getDevRecipes()); @@ -515,9 +518,8 @@ export default class ServerApi { } async _prepareServiceModel(service: { recipeId: string }) { - let recipe: undefined; try { - recipe = this.recipes.find(r => r.id === service.recipeId); + const recipe = this.recipes.find(r => r.id === service.recipeId); if (!recipe) { console.warn(`Recipe ${service.recipeId} not loaded`); @@ -531,11 +533,10 @@ export default class ServerApi { } } - async _bulkRecipeCheck(unfilteredRecipes: any[]) { + async _bulkRecipeCheck(unfilteredRecipes: string[]) { // Filter recipe duplicates as we don't need to download 3 Slack recipes const recipes = unfilteredRecipes.filter( - (elem: any, pos: number, arr: string | any[]) => - arr.indexOf(elem) === pos, + (elem: string, pos: number, arr: string[]) => arr.indexOf(elem) === pos, ); return Promise.all( @@ -565,7 +566,7 @@ export default class ServerApi { ).catch(error => console.error("Can't load recipe", error)); } - _mapRecipePreviewModel(recipes: any[]) { + _mapRecipePreviewModel(recipes: IRecipePreview[]) { return recipes .map(recipe => { try { @@ -575,7 +576,7 @@ export default class ServerApi { return null; } }) - .filter(recipe => recipe !== null); + .filter(Boolean); } _getDevRecipes() { @@ -589,15 +590,14 @@ export default class ServerApi { const recipes = paths .map(id => { - let Recipe; try { // eslint-disable-next-line import/no-dynamic-require - Recipe = require(id)(RecipeModel); + const Recipe = require(id)(RecipeModel); + return new Recipe(loadRecipeConfig(id)); } catch (error) { console.error(error); } - return false; }) .filter(recipe => recipe.id) -- cgit v1.2.3-54-g00ecf