From 082b64b6c40fcab3ecf303f53ce83e7753894a32 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sun, 15 May 2022 01:44:18 -0500 Subject: Extract utility functions for JSON parsing --- .../app/Controllers/Http/RecipeController.js | 13 ++++---- .../app/Controllers/Http/ServiceController.js | 26 +++++----------- .../app/Controllers/Http/UserController.js | 35 ++++++---------------- .../app/Controllers/Http/WorkspaceController.js | 6 ++-- 4 files changed, 26 insertions(+), 54 deletions(-) (limited to 'src/internal-server/app/Controllers') diff --git a/src/internal-server/app/Controllers/Http/RecipeController.js b/src/internal-server/app/Controllers/Http/RecipeController.js index e88a89a0b..eac34aa5c 100644 --- a/src/internal-server/app/Controllers/Http/RecipeController.js +++ b/src/internal-server/app/Controllers/Http/RecipeController.js @@ -6,6 +6,7 @@ const Env = use('Env'); const fetch = require('node-fetch'); const debug = require('../../../../preload-safe-debug')('Ferdium:internalServer:RecipeController'); const { LIVE_FERDIUM_API } = require('../../../../config'); +const { convertToJSON } = require('../../../../jsUtils'); const { API_VERSION } = require('../../../../environment-remote'); const RECIPES_URL = `${LIVE_FERDIUM_API}/${API_VERSION}/recipes`; @@ -14,13 +15,13 @@ class RecipeController { // List official and custom recipes async list({ response }) { const recipesUrlFetch = await fetch(RECIPES_URL); - const officialRecipes = JSON.parse(await recipesUrlFetch.text()); + const officialRecipes = convertToJSON(await recipesUrlFetch.text()); const allRecipes = await Recipe.all(); const customRecipesArray = allRecipes.rows; const customRecipes = customRecipesArray.map(recipe => ({ id: recipe.recipeId, name: recipe.name, - ...JSON.parse(recipe.data), + ...convertToJSON(recipe.data), })); const recipes = [...officialRecipes, ...customRecipes]; @@ -53,7 +54,7 @@ class RecipeController { results = dbResults.map(recipe => ({ id: recipe.recipeId, name: recipe.name, - ...JSON.parse(recipe.data), + ...convertToJSON(recipe.data), })); } else { let remoteResults = []; @@ -62,7 +63,7 @@ class RecipeController { const recipesUrlFetch = await fetch( `${RECIPES_URL}/search?needle=${encodeURIComponent(needle)}`, ); - remoteResults = JSON.parse(await recipesUrlFetch.text()); + remoteResults = convertToJSON(await recipesUrlFetch.text()); } debug('remoteResults:', remoteResults); @@ -74,7 +75,7 @@ class RecipeController { const localResults = localResultsArray.map(recipe => ({ id: recipe.recipeId, name: recipe.name, - ...JSON.parse(recipe.data), + ...convertToJSON(recipe.data), })); debug('localResults:', localResults); @@ -96,7 +97,7 @@ class RecipeController { response, }) { const recipesUrlFetch = await fetch(`${RECIPES_URL}/popular`); - const featuredRecipes = JSON.parse(await recipesUrlFetch.text()); + const featuredRecipes = convertToJSON(await recipesUrlFetch.text()); return response.send(featuredRecipes); } diff --git a/src/internal-server/app/Controllers/Http/ServiceController.js b/src/internal-server/app/Controllers/Http/ServiceController.js index f2f6e7028..81c03e5ff 100644 --- a/src/internal-server/app/Controllers/Http/ServiceController.js +++ b/src/internal-server/app/Controllers/Http/ServiceController.js @@ -4,6 +4,7 @@ const Env = use('Env'); const { v4: uuid } = require('uuid'); const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); +const { convertToJSON } = require('../../../../jsUtils'); const { API_VERSION } = require('../../../../environment-remote'); const moveIcon = require('../../ImageHelper'); @@ -72,10 +73,7 @@ class ServiceController { const services = allServices.rows; // Convert to array with all data Franz wants const servicesArray = services.map(service => { - const settings = - typeof service.settings === 'string' - ? JSON.parse(service.settings) - : service.settings; + const settings = convertToJSON(service.settings); return { customRecipe: false, @@ -88,7 +86,7 @@ class ServiceController { order: DEFAULT_SERVICE_ORDER, spellcheckerLanguage: '', workspaces: [], - ...JSON.parse(service.settings), + ...settings, iconUrl: settings.iconId ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` : null, @@ -108,10 +106,7 @@ class ServiceController { const { id } = params; const serviceQuery = await Service.query().where('serviceId', id).fetch(); const service = serviceQuery.rows[0]; - const settings = - typeof service.settings === 'string' - ? JSON.parse(service.settings) - : service.settings; + const settings = convertToJSON(service.settings); const icon = request.file('icon', { types: ['image'], @@ -160,9 +155,7 @@ class ServiceController { const serviceData = serviceQuery.rows[0]; const settings = { - ...(typeof serviceData.settings === 'string' - ? JSON.parse(serviceData.settings) - : serviceData.settings), + ...convertToJSON(serviceData.settings), ...data, }; @@ -205,7 +198,7 @@ class ServiceController { const serviceData = serviceQuery.rows[0]; const settings = { - ...JSON.parse(serviceData.settings), + ...convertToJSON(serviceData.settings), order: data[service], }; @@ -222,10 +215,7 @@ class ServiceController { const services = allServices.rows; // Convert to array with all data Franz wants const servicesArray = services.map(service => { - const settings = - typeof service.settings === 'string' - ? JSON.parse(service.settings) - : service.settings; + const settings = convertToJSON(service.settings); return { customRecipe: false, @@ -238,7 +228,7 @@ class ServiceController { order: DEFAULT_SERVICE_ORDER, spellcheckerLanguage: '', workspaces: [], - ...JSON.parse(service.settings), + ...settings, iconUrl: settings.iconId ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` : null, diff --git a/src/internal-server/app/Controllers/Http/UserController.js b/src/internal-server/app/Controllers/Http/UserController.js index 6bd4f85a7..fb1a19db6 100644 --- a/src/internal-server/app/Controllers/Http/UserController.js +++ b/src/internal-server/app/Controllers/Http/UserController.js @@ -9,6 +9,7 @@ const fetch = require('node-fetch'); const { v4: uuid } = require('uuid'); const crypto = require('crypto'); const { DEFAULT_APP_SETTINGS } = require('../../../../config'); +const { convertToJSON } = require('../../../../jsUtils'); const { API_VERSION } = require('../../../../environment-remote'); const { default: userAgent } = require('../../../../helpers/userAgent-helpers'); @@ -87,10 +88,7 @@ class UserController { async me({ response }) { const user = await User.find(1); - const settings = - typeof user.settings === 'string' - ? JSON.parse(user.settings) - : user.settings; + const settings = convertToJSON(user.settings); return response.send({ ...DEFAULT_USER_DATA, @@ -101,10 +99,7 @@ class UserController { async updateMe({ request, response }) { const user = await User.find(1); - let settings = user.settings || {}; - if (typeof settings === 'string') { - settings = JSON.parse(settings); - } + const settings = convertToJSON(user.settings || {}); const newSettings = { ...settings, @@ -304,12 +299,8 @@ class UserController { .rows.length > 0 ); - if ( - workspace.services && - typeof workspace.services === 'string' && - workspace.services.length > 0 - ) { - workspace.services = JSON.parse(workspace.services); + if (workspace.services) { + workspace.services = convertToJSON(workspace.services); } const services = workspace.services && typeof workspace.services === 'object' @@ -317,12 +308,8 @@ class UserController { oldServiceId => serviceIdTranslation[oldServiceId], ) : []; - if ( - workspace.data && - typeof workspace.data === 'string' && - workspace.data.length > 0 - ) { - workspace.data = JSON.parse(workspace.data); + if (workspace.data) { + workspace.data = convertToJSON(workspace.data); } await Workspace.create({ @@ -347,12 +334,8 @@ class UserController { // store the old serviceId as the key for future lookup serviceIdTranslation[service.serviceId] = newServiceId; - if ( - service.settings && - typeof service.settings === 'string' && - service.settings.length > 0 - ) { - service.settings = JSON.parse(service.settings); + if (service.settings) { + service.settings = convertToJSON(service.settings); } await Service.create({ diff --git a/src/internal-server/app/Controllers/Http/WorkspaceController.js b/src/internal-server/app/Controllers/Http/WorkspaceController.js index ee5731caf..352589567 100644 --- a/src/internal-server/app/Controllers/Http/WorkspaceController.js +++ b/src/internal-server/app/Controllers/Http/WorkspaceController.js @@ -2,6 +2,7 @@ const Workspace = use('App/Models/Workspace'); const { validateAll } = use('Validator'); const { v4: uuid } = require('uuid'); +const { convertToJSON } = require('../../../../jsUtils'); class WorkspaceController { // Create a new workspace for user @@ -130,10 +131,7 @@ class WorkspaceController { id: workspace.workspaceId, name: workspace.name, order: workspace.order, - services: - typeof workspace.services === 'string' - ? JSON.parse(workspace.services) - : workspace.services, + services: convertToJSON(workspace.services), userId: 1, })); } -- cgit v1.2.3-54-g00ecf