From f4f7983315c52f01d55af2ced7c70237ae2a0af1 Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Tue, 12 Mar 2019 13:33:37 +0100 Subject: add useful helpers for building standalone app features --- src/actions/lib/actions.js | 29 +++++++++++++++++------------ src/api/utils/auth.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/api/utils/auth.js (limited to 'src') diff --git a/src/actions/lib/actions.js b/src/actions/lib/actions.js index 499018d70..6571e9441 100644 --- a/src/actions/lib/actions.js +++ b/src/actions/lib/actions.js @@ -1,18 +1,23 @@ +export const createActionsFromDefinitions = (actionDefinitions, validate) => { + const actions = {}; + Object.keys(actionDefinitions).forEach((actionName) => { + const action = (params) => { + const schema = actionDefinitions[actionName]; + validate(schema, params, actionName); + action.notify(params); + }; + actions[actionName] = action; + action.listeners = []; + action.listen = listener => action.listeners.push(listener); + action.notify = params => action.listeners.forEach(listener => listener(params)); + }); + return actions; +}; + export default (definitions, validate) => { const newActions = {}; Object.keys(definitions).forEach((scopeName) => { - newActions[scopeName] = {}; - Object.keys(definitions[scopeName]).forEach((actionName) => { - const action = (params) => { - const schema = definitions[scopeName][actionName]; - validate(schema, params, actionName); - action.notify(params); - }; - newActions[scopeName][actionName] = action; - action.listeners = []; - action.listen = listener => action.listeners.push(listener); - action.notify = params => action.listeners.forEach(listener => listener(params)); - }); + newActions[scopeName] = createActionsFromDefinitions(definitions[scopeName], validate); }); return newActions; }; diff --git a/src/api/utils/auth.js b/src/api/utils/auth.js new file mode 100644 index 000000000..d469853a5 --- /dev/null +++ b/src/api/utils/auth.js @@ -0,0 +1,28 @@ +import { remote } from 'electron'; +import localStorage from 'mobx-localstorage'; + +const { app } = remote; + +export const prepareAuthRequest = (options, auth = true) => { + const request = Object.assign(options, { + mode: 'cors', + headers: Object.assign({ + 'Content-Type': 'application/json', + 'X-Franz-Source': 'desktop', + 'X-Franz-Version': app.getVersion(), + 'X-Franz-platform': process.platform, + 'X-Franz-Timezone-Offset': new Date().getTimezoneOffset(), + 'X-Franz-System-Locale': app.getLocale(), + }, options.headers), + }); + + if (auth) { + request.headers.Authorization = `Bearer ${localStorage.getItem('authToken')}`; + } + + return request; +}; + +export const sendAuthRequest = (url, options) => ( + window.fetch(url, prepareAuthRequest(options)) +); -- cgit v1.2.3-70-g09d2 From 09ffc03997113beec7efa00d9699736d342afda5 Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Tue, 12 Mar 2019 18:24:19 +0100 Subject: refactor server api to use prepare auth request util --- src/api/server/ServerApi.js | 76 +++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 48 deletions(-) (limited to 'src') 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'; import tar from 'tar'; import fs from 'fs-extra'; import { remote } from 'electron'; -import localStorage from 'mobx-localstorage'; import ServiceModel from '../../models/Service'; import RecipePreviewModel from '../../models/RecipePreview'; @@ -16,6 +15,7 @@ import OrderModel from '../../models/Order'; import { sleep } from '../../helpers/async-helpers'; import { API } from '../../environment'; +import { prepareAuthRequest } from '../utils/auth'; import { getRecipeDirectory, @@ -47,7 +47,7 @@ export default class ServerApi { // User async login(email, passwordHash) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/login`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/login`, prepareAuthRequest({ method: 'POST', headers: { Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, @@ -63,7 +63,7 @@ export default class ServerApi { } async signup(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/signup`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/signup`, prepareAuthRequest({ method: 'POST', body: JSON.stringify(data), }, false)); @@ -77,7 +77,7 @@ export default class ServerApi { } async inviteUser(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/invite`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/invite`, prepareAuthRequest({ method: 'POST', body: JSON.stringify(data), })); @@ -90,7 +90,7 @@ export default class ServerApi { } async retrievePassword(email) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/password`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/password`, prepareAuthRequest({ method: 'POST', body: JSON.stringify({ email, @@ -106,7 +106,7 @@ export default class ServerApi { } async userInfo() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -121,7 +121,7 @@ export default class ServerApi { } async updateUserInfo(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ method: 'PUT', body: JSON.stringify(data), })); @@ -136,7 +136,7 @@ export default class ServerApi { } async deleteAccount() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ method: 'DELETE', })); if (!request.ok) { @@ -150,7 +150,7 @@ export default class ServerApi { // Services async getServices() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -165,7 +165,7 @@ export default class ServerApi { } async createService(recipeId, data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service`, prepareAuthRequest({ method: 'POST', body: JSON.stringify(Object.assign({ recipeId, @@ -195,7 +195,7 @@ export default class ServerApi { await this.uploadServiceIcon(serviceId, data.iconFile); } - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, prepareAuthRequest({ method: 'PUT', body: JSON.stringify(data), })); @@ -216,7 +216,7 @@ export default class ServerApi { const formData = new FormData(); formData.append('icon', icon); - const requestData = this._prepareAuthRequest({ + const requestData = prepareAuthRequest({ method: 'PUT', body: formData, }); @@ -235,7 +235,7 @@ export default class ServerApi { } async reorderService(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, prepareAuthRequest({ method: 'PUT', body: JSON.stringify(data), })); @@ -248,7 +248,7 @@ export default class ServerApi { } async deleteService(id) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${id}`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${id}`, prepareAuthRequest({ method: 'DELETE', })); if (!request.ok) { @@ -264,7 +264,7 @@ export default class ServerApi { // Features async getDefaultFeatures() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features/default`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features/default`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -278,7 +278,7 @@ export default class ServerApi { } async getFeatures() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -314,7 +314,7 @@ export default class ServerApi { } async getRecipeUpdates(recipeVersions) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/update`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/update`, prepareAuthRequest({ method: 'POST', body: JSON.stringify(recipeVersions), })); @@ -328,7 +328,7 @@ export default class ServerApi { // Recipes Previews async getRecipePreviews() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -343,7 +343,7 @@ export default class ServerApi { } async getFeaturedRecipePreviews() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/popular`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/popular`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -359,7 +359,7 @@ export default class ServerApi { } async searchRecipePreviews(needle) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/search?needle=${needle}`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/search?needle=${needle}`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -415,7 +415,7 @@ export default class ServerApi { // Payment async getPlans() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/plans`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/plans`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -429,7 +429,7 @@ export default class ServerApi { } async getHostedPage(planId) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/init`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/init`, prepareAuthRequest({ method: 'POST', body: JSON.stringify({ planId, @@ -445,7 +445,7 @@ export default class ServerApi { } async getPaymentDashboardUrl() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/billing`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/billing`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -458,7 +458,7 @@ export default class ServerApi { } async getSubscriptionOrders() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/subscription`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/subscription`, prepareAuthRequest({ method: 'GET', })); if (!request.ok) { @@ -474,7 +474,7 @@ export default class ServerApi { async getLatestNews() { // eslint-disable-next-line const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`, - this._prepareAuthRequest({ + prepareAuthRequest({ method: 'GET', })); @@ -489,7 +489,7 @@ export default class ServerApi { async hideNews(id) { const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news/${id}/read`, - this._prepareAuthRequest({ + prepareAuthRequest({ method: 'GET', })); @@ -502,7 +502,7 @@ export default class ServerApi { // Health Check async healthCheck() { - const request = await window.fetch(`${SERVER_URL}/health`, this._prepareAuthRequest({ + const request = await window.fetch(`${SERVER_URL}/health`, prepareAuthRequest({ method: 'GET', }, false)); if (!request.ok) { @@ -521,7 +521,7 @@ export default class ServerApi { const services = await Promise.all(config.services.map(async (s) => { const service = s; const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/${s.service}`, - this._prepareAuthRequest({ + prepareAuthRequest({ method: 'GET', })); @@ -632,26 +632,6 @@ export default class ServerApi { }).filter(orderItem => orderItem !== null); } - _prepareAuthRequest(options, auth = true) { - const request = Object.assign(options, { - mode: 'cors', - headers: Object.assign({ - 'Content-Type': 'application/json', - 'X-Franz-Source': 'desktop', - 'X-Franz-Version': app.getVersion(), - 'X-Franz-platform': process.platform, - 'X-Franz-Timezone-Offset': new Date().getTimezoneOffset(), - 'X-Franz-System-Locale': app.getLocale(), - }, options.headers), - }); - - if (auth) { - request.headers.Authorization = `Bearer ${localStorage.getItem('authToken')}`; - } - - return request; - } - _getDevRecipes() { const recipesDirectory = getDevRecipeDirectory(); try { -- cgit v1.2.3-70-g09d2 From dc6d91e179cea94e8653cf7b568a143ce4ac3887 Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Tue, 12 Mar 2019 18:43:43 +0100 Subject: refactor server api even more --- src/api/server/ServerApi.js | 163 +++++++++++++++----------------------------- src/api/utils/auth.js | 6 +- 2 files changed, 58 insertions(+), 111 deletions(-) (limited to 'src') 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'; import { sleep } from '../../helpers/async-helpers'; import { API } from '../../environment'; -import { prepareAuthRequest } from '../utils/auth'; +import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; import { getRecipeDirectory, @@ -39,6 +39,7 @@ const { default: fetch } = remote.require('electron-fetch'); const SERVER_URL = API; const API_VERSION = 'v1'; +const API_URL = `${SERVER_URL}/${API_VERSION}`; export default class ServerApi { recipePreviews = []; @@ -47,12 +48,12 @@ export default class ServerApi { // User async login(email, passwordHash) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/login`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/auth/login`, { method: 'POST', headers: { Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, }, - }, false)); + }, false); if (!request.ok) { throw request; } @@ -63,10 +64,10 @@ export default class ServerApi { } async signup(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/signup`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/auth/signup`, { method: 'POST', body: JSON.stringify(data), - }, false)); + }, false); if (!request.ok) { throw request; } @@ -77,10 +78,10 @@ export default class ServerApi { } async inviteUser(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/invite`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/invite`, { method: 'POST', body: JSON.stringify(data), - })); + }); if (!request.ok) { throw request; } @@ -90,12 +91,12 @@ export default class ServerApi { } async retrievePassword(email) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/auth/password`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/auth/password`, { method: 'POST', body: JSON.stringify({ email, }), - }, false)); + }, false); if (!request.ok) { throw request; } @@ -106,9 +107,7 @@ export default class ServerApi { } async userInfo() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ - method: 'GET', - })); + const request = await sendAuthRequest(`${API_URL}/me`); if (!request.ok) { throw request; } @@ -121,10 +120,10 @@ export default class ServerApi { } async updateUserInfo(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/me`, { method: 'PUT', body: JSON.stringify(data), - })); + }); if (!request.ok) { throw request; } @@ -136,9 +135,9 @@ export default class ServerApi { } async deleteAccount() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/me`, { method: 'DELETE', - })); + }); if (!request.ok) { throw request; } @@ -150,9 +149,7 @@ export default class ServerApi { // Services async getServices() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/services`, prepareAuthRequest({ - method: 'GET', - })); + const request = await sendAuthRequest(`${API_URL}/me/services`); if (!request.ok) { throw request; } @@ -165,12 +162,12 @@ export default class ServerApi { } async createService(recipeId, data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/service`, { method: 'POST', body: JSON.stringify(Object.assign({ recipeId, }, data)), - })); + }); if (!request.ok) { throw request; } @@ -195,10 +192,10 @@ export default class ServerApi { await this.uploadServiceIcon(serviceId, data.iconFile); } - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/service/${serviceId}`, { method: 'PUT', body: JSON.stringify(data), - })); + }); if (!request.ok) { throw request; @@ -223,7 +220,7 @@ export default class ServerApi { delete requestData.headers['Content-Type']; - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${serviceId}`, requestData); + const request = await window.fetch(`${API_URL}/service/${serviceId}`, requestData); if (!request.ok) { throw request; @@ -235,10 +232,10 @@ export default class ServerApi { } async reorderService(data) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/reorder`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/service/reorder`, { method: 'PUT', body: JSON.stringify(data), - })); + }); if (!request.ok) { throw request; } @@ -248,9 +245,9 @@ export default class ServerApi { } async deleteService(id) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/service/${id}`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/service/${id}`, { method: 'DELETE', - })); + }); if (!request.ok) { throw request; } @@ -264,9 +261,7 @@ export default class ServerApi { // Features async getDefaultFeatures() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features/default`, prepareAuthRequest({ - method: 'GET', - })); + const request = await sendAuthRequest(`${API_URL}/features/default`); if (!request.ok) { throw request; } @@ -278,9 +273,7 @@ export default class ServerApi { } async getFeatures() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/features`, prepareAuthRequest({ - method: 'GET', - })); + const request = await sendAuthRequest(`${API_URL}/features`); if (!request.ok) { throw request; } @@ -314,10 +307,10 @@ export default class ServerApi { } async getRecipeUpdates(recipeVersions) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/update`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/recipes/update`, { method: 'POST', body: JSON.stringify(recipeVersions), - })); + }); if (!request.ok) { throw request; } @@ -328,29 +321,19 @@ export default class ServerApi { // Recipes Previews async getRecipePreviews() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes`, prepareAuthRequest({ - method: 'GET', - })); - if (!request.ok) { - throw request; - } + const request = await sendAuthRequest(`${API_URL}/recipes`); + if (!request.ok) throw request; const data = await request.json(); - const recipePreviews = this._mapRecipePreviewModel(data); debug('ServerApi::getRecipes resolves', recipePreviews); - return recipePreviews; } async getFeaturedRecipePreviews() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/popular`, prepareAuthRequest({ - method: 'GET', - })); - if (!request.ok) { - throw request; - } - const data = await request.json(); + const request = await sendAuthRequest(`${API_URL}/recipes/popular`); + if (!request.ok) throw request; + const data = await request.json(); // data = this._addLocalRecipesToPreviews(data); const recipePreviews = this._mapRecipePreviewModel(data); @@ -359,14 +342,11 @@ export default class ServerApi { } async searchRecipePreviews(needle) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/search?needle=${needle}`, prepareAuthRequest({ - method: 'GET', - })); - if (!request.ok) { - throw request; - } - const data = await request.json(); + const url = `${API_URL}/recipes/search?needle=${needle}`; + const request = await sendAuthRequest(url); + if (!request.ok) throw request; + const data = await request.json(); const recipePreviews = this._mapRecipePreviewModel(data); debug('ServerApi::searchRecipePreviews resolves', recipePreviews); return recipePreviews; @@ -375,10 +355,9 @@ export default class ServerApi { async getRecipePackage(recipeId) { try { const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); - const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); - const packageUrl = `${SERVER_URL}/${API_VERSION}/recipes/download/${recipeId}`; + const packageUrl = `${API_URL}/recipes/download/${recipeId}`; fs.ensureDirSync(recipeTempDirectory); const res = await fetch(packageUrl); @@ -415,26 +394,21 @@ export default class ServerApi { // Payment async getPlans() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/plans`, prepareAuthRequest({ - method: 'GET', - })); - if (!request.ok) { - throw request; - } + const request = await sendAuthRequest(`${API_URL}/payment/plans`); + if (!request.ok) throw request; const data = await request.json(); - const plan = new PlanModel(data); debug('ServerApi::getPlans resolves', plan); return plan; } async getHostedPage(planId) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/payment/init`, prepareAuthRequest({ + const request = await sendAuthRequest(`${API_URL}/payment/init`, { method: 'POST', body: JSON.stringify({ planId, }), - })); + }); if (!request.ok) { throw request; } @@ -445,25 +419,16 @@ export default class ServerApi { } async getPaymentDashboardUrl() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/billing`, prepareAuthRequest({ - method: 'GET', - })); - if (!request.ok) { - throw request; - } + const request = await sendAuthRequest(`${API_URL}/me/billing`); + if (!request.ok) throw request; const data = await request.json(); - debug('ServerApi::getPaymentDashboardUrl resolves', data); return data; } async getSubscriptionOrders() { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/me/subscription`, prepareAuthRequest({ - method: 'GET', - })); - if (!request.ok) { - throw request; - } + const request = await sendAuthRequest(`${API_URL}/me/subscription`); + if (!request.ok) throw request; const data = await request.json(); const orders = this._mapOrderModels(data); debug('ServerApi::getSubscriptionOrders resolves', orders); @@ -472,15 +437,9 @@ export default class ServerApi { // News async getLatestNews() { - // eslint-disable-next-line - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`, - prepareAuthRequest({ - method: 'GET', - })); - - if (!request.ok) { - throw request; - } + const url = `${API_URL}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; + const request = await sendAuthRequest(url); + if (!request.ok) throw request; const data = await request.json(); const news = this._mapNewsModels(data); debug('ServerApi::getLatestNews resolves', news); @@ -488,23 +447,16 @@ export default class ServerApi { } async hideNews(id) { - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/news/${id}/read`, - prepareAuthRequest({ - method: 'GET', - })); - - if (!request.ok) { - throw request; - } - + const request = await sendAuthRequest(`${API_URL}/news/${id}/read`); + if (!request.ok) throw request; debug('ServerApi::hideNews resolves', id); } // Health Check async healthCheck() { - const request = await window.fetch(`${SERVER_URL}/health`, prepareAuthRequest({ + const request = await sendAuthRequest(`${SERVER_URL}/health`, { method: 'GET', - }, false)); + }, false); if (!request.ok) { throw request; } @@ -520,10 +472,7 @@ export default class ServerApi { if (Object.prototype.hasOwnProperty.call(config, 'services')) { const services = await Promise.all(config.services.map(async (s) => { const service = s; - const request = await window.fetch(`${SERVER_URL}/${API_VERSION}/recipes/${s.service}`, - prepareAuthRequest({ - method: 'GET', - })); + const request = await sendAuthRequest(`${API_URL}/recipes/${s.service}`); if (request.status === 200) { const data = await request.json(); @@ -546,9 +495,7 @@ export default class ServerApi { // Helper async _mapServiceModels(services) { const recipes = services.map(s => s.recipeId); - await this._bulkRecipeCheck(recipes); - /* eslint-disable no-return-await */ return Promise.all(services.map(async service => await this._prepareServiceModel(service))); /* 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'; const { app } = remote; -export const prepareAuthRequest = (options, auth = true) => { +export const prepareAuthRequest = (options = { method: 'GET' }, auth = true) => { const request = Object.assign(options, { mode: 'cors', headers: Object.assign({ @@ -23,6 +23,6 @@ export const prepareAuthRequest = (options, auth = true) => { return request; }; -export const sendAuthRequest = (url, options) => ( - window.fetch(url, prepareAuthRequest(options)) +export const sendAuthRequest = (url, options, auth) => ( + window.fetch(url, prepareAuthRequest(options, auth)) ); -- cgit v1.2.3-70-g09d2