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 +++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 108 deletions(-) (limited to 'src/api/server') 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 */ -- cgit v1.2.3-54-g00ecf