From 8440d51cef61297eb34a193d6a3ad420a0947340 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Thu, 22 Aug 2019 11:04:50 +0200 Subject: Add support to use custom servers --- src/api/server/ServerApi.js | 72 +++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 25 deletions(-) (limited to 'src/api') diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index a9ce202ff..069994028 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js @@ -46,9 +46,31 @@ export default class ServerApi { recipes = []; + stores = {}; + + setStores(stores) { + this.stores = stores; + } + + apiUrl() { + let url; + if (!this.stores.settings) { + // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded + url = 'https://localhost:9999' + } else if (this.stores.settings.all.app.server) { + // Load URL from store + url = this.stores.settings.all.app.server; + } else { + // Use default server url + url = SERVER_URL; + } + + return `${url}/${API_VERSION}`; + } + // User async login(email, passwordHash) { - const request = await sendAuthRequest(`${API_URL}/auth/login`, { + const request = await sendAuthRequest(`${this.apiUrl()}/auth/login`, { method: 'POST', headers: { Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, @@ -64,7 +86,7 @@ export default class ServerApi { } async signup(data) { - const request = await sendAuthRequest(`${API_URL}/auth/signup`, { + const request = await sendAuthRequest(`${this.apiUrl()}/auth/signup`, { method: 'POST', body: JSON.stringify(data), }, false); @@ -78,7 +100,7 @@ export default class ServerApi { } async inviteUser(data) { - const request = await sendAuthRequest(`${API_URL}/invite`, { + const request = await sendAuthRequest(`${this.apiUrl()}/invite`, { method: 'POST', body: JSON.stringify(data), }); @@ -91,7 +113,7 @@ export default class ServerApi { } async retrievePassword(email) { - const request = await sendAuthRequest(`${API_URL}/auth/password`, { + const request = await sendAuthRequest(`${this.apiUrl()}/auth/password`, { method: 'POST', body: JSON.stringify({ email, @@ -107,7 +129,7 @@ export default class ServerApi { } async userInfo() { - const request = await sendAuthRequest(`${API_URL}/me`); + const request = await sendAuthRequest(`${this.apiUrl()}/me`); if (!request.ok) { throw request; } @@ -120,7 +142,7 @@ export default class ServerApi { } async updateUserInfo(data) { - const request = await sendAuthRequest(`${API_URL}/me`, { + const request = await sendAuthRequest(`${this.apiUrl()}/me`, { method: 'PUT', body: JSON.stringify(data), }); @@ -135,7 +157,7 @@ export default class ServerApi { } async deleteAccount() { - const request = await sendAuthRequest(`${API_URL}/me`, { + const request = await sendAuthRequest(`${this.apiUrl()}/me`, { method: 'DELETE', }); if (!request.ok) { @@ -149,7 +171,7 @@ export default class ServerApi { // Services async getServices() { - const request = await sendAuthRequest(`${API_URL}/me/services`); + const request = await sendAuthRequest(`${this.apiUrl()}/me/services`); if (!request.ok) { throw request; } @@ -162,7 +184,7 @@ export default class ServerApi { } async createService(recipeId, data) { - const request = await sendAuthRequest(`${API_URL}/service`, { + const request = await sendAuthRequest(`${this.apiUrl()}/service`, { method: 'POST', body: JSON.stringify(Object.assign({ recipeId, @@ -192,7 +214,7 @@ export default class ServerApi { await this.uploadServiceIcon(serviceId, data.iconFile); } - const request = await sendAuthRequest(`${API_URL}/service/${serviceId}`, { + const request = await sendAuthRequest(`${this.apiUrl()}/service/${serviceId}`, { method: 'PUT', body: JSON.stringify(data), }); @@ -220,7 +242,7 @@ export default class ServerApi { delete requestData.headers['Content-Type']; - const request = await window.fetch(`${API_URL}/service/${serviceId}`, requestData); + const request = await window.fetch(`${this.apiUrl()}/service/${serviceId}`, requestData); if (!request.ok) { throw request; @@ -232,7 +254,7 @@ export default class ServerApi { } async reorderService(data) { - const request = await sendAuthRequest(`${API_URL}/service/reorder`, { + const request = await sendAuthRequest(`${this.apiUrl()}/service/reorder`, { method: 'PUT', body: JSON.stringify(data), }); @@ -245,7 +267,7 @@ export default class ServerApi { } async deleteService(id) { - const request = await sendAuthRequest(`${API_URL}/service/${id}`, { + const request = await sendAuthRequest(`${this.apiUrl()}/service/${id}`, { method: 'DELETE', }); if (!request.ok) { @@ -261,7 +283,7 @@ export default class ServerApi { // Features async getDefaultFeatures() { - const request = await sendAuthRequest(`${API_URL}/features/default`); + const request = await sendAuthRequest(`${this.apiUrl()}/features/default`); if (!request.ok) { throw request; } @@ -273,7 +295,7 @@ export default class ServerApi { } async getFeatures() { - const request = await sendAuthRequest(`${API_URL}/features`); + const request = await sendAuthRequest(`${this.apiUrl()}/features`); if (!request.ok) { throw request; } @@ -307,7 +329,7 @@ export default class ServerApi { } async getRecipeUpdates(recipeVersions) { - const request = await sendAuthRequest(`${API_URL}/recipes/update`, { + const request = await sendAuthRequest(`${this.apiUrl()}/recipes/update`, { method: 'POST', body: JSON.stringify(recipeVersions), }); @@ -321,7 +343,7 @@ export default class ServerApi { // Recipes Previews async getRecipePreviews() { - const request = await sendAuthRequest(`${API_URL}/recipes`); + const request = await sendAuthRequest(`${this.apiUrl()}/recipes`); if (!request.ok) throw request; const data = await request.json(); const recipePreviews = this._mapRecipePreviewModel(data); @@ -330,7 +352,7 @@ export default class ServerApi { } async getFeaturedRecipePreviews() { - const request = await sendAuthRequest(`${API_URL}/recipes/popular`); + const request = await sendAuthRequest(`${this.apiUrl()}/recipes/popular`); if (!request.ok) throw request; const data = await request.json(); @@ -342,7 +364,7 @@ export default class ServerApi { } async searchRecipePreviews(needle) { - const url = `${API_URL}/recipes/search?needle=${needle}`; + const url = `${this.apiUrl()}/recipes/search?needle=${needle}`; const request = await sendAuthRequest(url); if (!request.ok) throw request; @@ -357,7 +379,7 @@ export default class ServerApi { 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 = `${API_URL}/recipes/download/${recipeId}`; + const packageUrl = `${this.apiUrl()}/recipes/download/${recipeId}`; fs.ensureDirSync(recipeTempDirectory); const res = await fetch(packageUrl); @@ -394,7 +416,7 @@ export default class ServerApi { // Payment async getPlans() { - const request = await sendAuthRequest(`${API_URL}/payment/plans`); + const request = await sendAuthRequest(`${this.apiUrl()}/payment/plans`); if (!request.ok) throw request; const data = await request.json(); const plan = new PlanModel(data); @@ -403,7 +425,7 @@ export default class ServerApi { } async getHostedPage(planId) { - const request = await sendAuthRequest(`${API_URL}/payment/init`, { + const request = await sendAuthRequest(`${this.apiUrl()}/payment/init`, { method: 'POST', body: JSON.stringify({ planId, @@ -420,7 +442,7 @@ export default class ServerApi { // News async getLatestNews() { - const url = `${API_URL}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; + const url = `${this.apiUrl()}/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(); @@ -430,7 +452,7 @@ export default class ServerApi { } async hideNews(id) { - const request = await sendAuthRequest(`${API_URL}/news/${id}/read`); + const request = await sendAuthRequest(`${this.apiUrl()}/news/${id}/read`); if (!request.ok) throw request; debug('ServerApi::hideNews resolves', id); } @@ -455,7 +477,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 sendAuthRequest(`${API_URL}/recipes/${s.service}`); + const request = await sendAuthRequest(`${this.apiUrl()}/recipes/${s.service}`); if (request.status === 200) { const data = await request.json(); -- cgit v1.2.3-54-g00ecf