diff options
author | vantezzen <properly@protonmail.com> | 2019-08-25 13:56:14 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-08-25 13:56:14 +0200 |
commit | 051314eac6f062f18ea52a2fda2f1ecd1164b64b (patch) | |
tree | 067f432695184ae78ed2474723e9b0ec70d42507 | |
parent | Add custom tray icons (diff) | |
download | ferdium-app-051314eac6f062f18ea52a2fda2f1ecd1164b64b.tar.gz ferdium-app-051314eac6f062f18ea52a2fda2f1ecd1164b64b.tar.zst ferdium-app-051314eac6f062f18ea52a2fda2f1ecd1164b64b.zip |
Unifying apiBase function
-rw-r--r-- | src/api/apiBase.js (renamed from src/features/apiBase.js) | 2 | ||||
-rw-r--r-- | src/api/server/ServerApi.js | 76 | ||||
-rw-r--r-- | src/app.js | 2 | ||||
-rw-r--r-- | src/features/announcements/api.js | 2 | ||||
-rw-r--r-- | src/features/workspaces/api.js | 2 |
5 files changed, 30 insertions, 54 deletions
diff --git a/src/features/apiBase.js b/src/api/apiBase.js index d4b092995..42f09eaa8 100644 --- a/src/features/apiBase.js +++ b/src/api/apiBase.js | |||
@@ -8,7 +8,7 @@ import { | |||
8 | 8 | ||
9 | const apiBase = () => { | 9 | const apiBase = () => { |
10 | let url; | 10 | let url; |
11 | if (!window.ferdi.stores.settings) { | 11 | if (!window.ferdi.stores.settings || !window.ferdi.stores.settings.all) { |
12 | // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded | 12 | // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded |
13 | url = 'https://localhost:9999'; | 13 | url = 'https://localhost:9999'; |
14 | } else if (window.ferdi.stores.settings.all.app.server) { | 14 | } else if (window.ferdi.stores.settings.all.app.server) { |
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index 46a5b928d..2111af9fe 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -15,6 +15,7 @@ import OrderModel from '../../models/Order'; | |||
15 | import { sleep } from '../../helpers/async-helpers'; | 15 | import { sleep } from '../../helpers/async-helpers'; |
16 | 16 | ||
17 | import { API } from '../../environment'; | 17 | import { API } from '../../environment'; |
18 | import apiBase from '../apiBase'; | ||
18 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; | 19 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; |
19 | 20 | ||
20 | import { | 21 | import { |
@@ -38,38 +39,15 @@ const { app } = remote; | |||
38 | const { default: fetch } = remote.require('electron-fetch'); | 39 | const { default: fetch } = remote.require('electron-fetch'); |
39 | 40 | ||
40 | const SERVER_URL = API; | 41 | const SERVER_URL = API; |
41 | const API_VERSION = 'v1'; | ||
42 | 42 | ||
43 | export default class ServerApi { | 43 | export default class ServerApi { |
44 | recipePreviews = []; | 44 | recipePreviews = []; |
45 | 45 | ||
46 | recipes = []; | 46 | recipes = []; |
47 | 47 | ||
48 | stores = {}; | ||
49 | |||
50 | setStores(stores) { | ||
51 | this.stores = stores; | ||
52 | } | ||
53 | |||
54 | apiUrl() { | ||
55 | let url; | ||
56 | if (!this.stores.settings) { | ||
57 | // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded | ||
58 | url = 'https://localhost:9999'; | ||
59 | } else if (this.stores.settings.all.app.server) { | ||
60 | // Load URL from store | ||
61 | url = this.stores.settings.all.app.server; | ||
62 | } else { | ||
63 | // Use default server url | ||
64 | url = SERVER_URL; | ||
65 | } | ||
66 | |||
67 | return `${url}/${API_VERSION}`; | ||
68 | } | ||
69 | |||
70 | // User | 48 | // User |
71 | async login(email, passwordHash) { | 49 | async login(email, passwordHash) { |
72 | const request = await sendAuthRequest(`${this.apiUrl()}/auth/login`, { | 50 | const request = await sendAuthRequest(`${apiBase()}/auth/login`, { |
73 | method: 'POST', | 51 | method: 'POST', |
74 | headers: { | 52 | headers: { |
75 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, | 53 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, |
@@ -85,7 +63,7 @@ export default class ServerApi { | |||
85 | } | 63 | } |
86 | 64 | ||
87 | async signup(data) { | 65 | async signup(data) { |
88 | const request = await sendAuthRequest(`${this.apiUrl()}/auth/signup`, { | 66 | const request = await sendAuthRequest(`${apiBase()}/auth/signup`, { |
89 | method: 'POST', | 67 | method: 'POST', |
90 | body: JSON.stringify(data), | 68 | body: JSON.stringify(data), |
91 | }, false); | 69 | }, false); |
@@ -99,7 +77,7 @@ export default class ServerApi { | |||
99 | } | 77 | } |
100 | 78 | ||
101 | async inviteUser(data) { | 79 | async inviteUser(data) { |
102 | const request = await sendAuthRequest(`${this.apiUrl()}/invite`, { | 80 | const request = await sendAuthRequest(`${apiBase()}/invite`, { |
103 | method: 'POST', | 81 | method: 'POST', |
104 | body: JSON.stringify(data), | 82 | body: JSON.stringify(data), |
105 | }); | 83 | }); |
@@ -112,7 +90,7 @@ export default class ServerApi { | |||
112 | } | 90 | } |
113 | 91 | ||
114 | async retrievePassword(email) { | 92 | async retrievePassword(email) { |
115 | const request = await sendAuthRequest(`${this.apiUrl()}/auth/password`, { | 93 | const request = await sendAuthRequest(`${apiBase()}/auth/password`, { |
116 | method: 'POST', | 94 | method: 'POST', |
117 | body: JSON.stringify({ | 95 | body: JSON.stringify({ |
118 | email, | 96 | email, |
@@ -128,7 +106,7 @@ export default class ServerApi { | |||
128 | } | 106 | } |
129 | 107 | ||
130 | async userInfo() { | 108 | async userInfo() { |
131 | const request = await sendAuthRequest(`${this.apiUrl()}/me`); | 109 | const request = await sendAuthRequest(`${apiBase()}/me`); |
132 | if (!request.ok) { | 110 | if (!request.ok) { |
133 | throw request; | 111 | throw request; |
134 | } | 112 | } |
@@ -141,7 +119,7 @@ export default class ServerApi { | |||
141 | } | 119 | } |
142 | 120 | ||
143 | async updateUserInfo(data) { | 121 | async updateUserInfo(data) { |
144 | const request = await sendAuthRequest(`${this.apiUrl()}/me`, { | 122 | const request = await sendAuthRequest(`${apiBase()}/me`, { |
145 | method: 'PUT', | 123 | method: 'PUT', |
146 | body: JSON.stringify(data), | 124 | body: JSON.stringify(data), |
147 | }); | 125 | }); |
@@ -156,7 +134,7 @@ export default class ServerApi { | |||
156 | } | 134 | } |
157 | 135 | ||
158 | async deleteAccount() { | 136 | async deleteAccount() { |
159 | const request = await sendAuthRequest(`${this.apiUrl()}/me`, { | 137 | const request = await sendAuthRequest(`${apiBase()}/me`, { |
160 | method: 'DELETE', | 138 | method: 'DELETE', |
161 | }); | 139 | }); |
162 | if (!request.ok) { | 140 | if (!request.ok) { |
@@ -170,7 +148,7 @@ export default class ServerApi { | |||
170 | 148 | ||
171 | // Services | 149 | // Services |
172 | async getServices() { | 150 | async getServices() { |
173 | const request = await sendAuthRequest(`${this.apiUrl()}/me/services`); | 151 | const request = await sendAuthRequest(`${apiBase()}/me/services`); |
174 | if (!request.ok) { | 152 | if (!request.ok) { |
175 | throw request; | 153 | throw request; |
176 | } | 154 | } |
@@ -183,7 +161,7 @@ export default class ServerApi { | |||
183 | } | 161 | } |
184 | 162 | ||
185 | async createService(recipeId, data) { | 163 | async createService(recipeId, data) { |
186 | const request = await sendAuthRequest(`${this.apiUrl()}/service`, { | 164 | const request = await sendAuthRequest(`${apiBase()}/service`, { |
187 | method: 'POST', | 165 | method: 'POST', |
188 | body: JSON.stringify(Object.assign({ | 166 | body: JSON.stringify(Object.assign({ |
189 | recipeId, | 167 | recipeId, |
@@ -213,7 +191,7 @@ export default class ServerApi { | |||
213 | await this.uploadServiceIcon(serviceId, data.iconFile); | 191 | await this.uploadServiceIcon(serviceId, data.iconFile); |
214 | } | 192 | } |
215 | 193 | ||
216 | const request = await sendAuthRequest(`${this.apiUrl()}/service/${serviceId}`, { | 194 | const request = await sendAuthRequest(`${apiBase()}/service/${serviceId}`, { |
217 | method: 'PUT', | 195 | method: 'PUT', |
218 | body: JSON.stringify(data), | 196 | body: JSON.stringify(data), |
219 | }); | 197 | }); |
@@ -241,7 +219,7 @@ export default class ServerApi { | |||
241 | 219 | ||
242 | delete requestData.headers['Content-Type']; | 220 | delete requestData.headers['Content-Type']; |
243 | 221 | ||
244 | const request = await window.fetch(`${this.apiUrl()}/service/${serviceId}`, requestData); | 222 | const request = await window.fetch(`${apiBase()}/service/${serviceId}`, requestData); |
245 | 223 | ||
246 | if (!request.ok) { | 224 | if (!request.ok) { |
247 | throw request; | 225 | throw request; |
@@ -253,7 +231,7 @@ export default class ServerApi { | |||
253 | } | 231 | } |
254 | 232 | ||
255 | async reorderService(data) { | 233 | async reorderService(data) { |
256 | const request = await sendAuthRequest(`${this.apiUrl()}/service/reorder`, { | 234 | const request = await sendAuthRequest(`${apiBase()}/service/reorder`, { |
257 | method: 'PUT', | 235 | method: 'PUT', |
258 | body: JSON.stringify(data), | 236 | body: JSON.stringify(data), |
259 | }); | 237 | }); |
@@ -266,7 +244,7 @@ export default class ServerApi { | |||
266 | } | 244 | } |
267 | 245 | ||
268 | async deleteService(id) { | 246 | async deleteService(id) { |
269 | const request = await sendAuthRequest(`${this.apiUrl()}/service/${id}`, { | 247 | const request = await sendAuthRequest(`${apiBase()}/service/${id}`, { |
270 | method: 'DELETE', | 248 | method: 'DELETE', |
271 | }); | 249 | }); |
272 | if (!request.ok) { | 250 | if (!request.ok) { |
@@ -282,7 +260,7 @@ export default class ServerApi { | |||
282 | 260 | ||
283 | // Features | 261 | // Features |
284 | async getDefaultFeatures() { | 262 | async getDefaultFeatures() { |
285 | const request = await sendAuthRequest(`${this.apiUrl()}/features/default`); | 263 | const request = await sendAuthRequest(`${apiBase()}/features/default`); |
286 | if (!request.ok) { | 264 | if (!request.ok) { |
287 | throw request; | 265 | throw request; |
288 | } | 266 | } |
@@ -294,7 +272,7 @@ export default class ServerApi { | |||
294 | } | 272 | } |
295 | 273 | ||
296 | async getFeatures() { | 274 | async getFeatures() { |
297 | const request = await sendAuthRequest(`${this.apiUrl()}/features`); | 275 | const request = await sendAuthRequest(`${apiBase()}/features`); |
298 | if (!request.ok) { | 276 | if (!request.ok) { |
299 | throw request; | 277 | throw request; |
300 | } | 278 | } |
@@ -328,7 +306,7 @@ export default class ServerApi { | |||
328 | } | 306 | } |
329 | 307 | ||
330 | async getRecipeUpdates(recipeVersions) { | 308 | async getRecipeUpdates(recipeVersions) { |
331 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes/update`, { | 309 | const request = await sendAuthRequest(`${apiBase()}/recipes/update`, { |
332 | method: 'POST', | 310 | method: 'POST', |
333 | body: JSON.stringify(recipeVersions), | 311 | body: JSON.stringify(recipeVersions), |
334 | }); | 312 | }); |
@@ -342,7 +320,7 @@ export default class ServerApi { | |||
342 | 320 | ||
343 | // Recipes Previews | 321 | // Recipes Previews |
344 | async getRecipePreviews() { | 322 | async getRecipePreviews() { |
345 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes`); | 323 | const request = await sendAuthRequest(`${apiBase()}/recipes`); |
346 | if (!request.ok) throw request; | 324 | if (!request.ok) throw request; |
347 | const data = await request.json(); | 325 | const data = await request.json(); |
348 | const recipePreviews = this._mapRecipePreviewModel(data); | 326 | const recipePreviews = this._mapRecipePreviewModel(data); |
@@ -351,7 +329,7 @@ export default class ServerApi { | |||
351 | } | 329 | } |
352 | 330 | ||
353 | async getFeaturedRecipePreviews() { | 331 | async getFeaturedRecipePreviews() { |
354 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes/popular`); | 332 | const request = await sendAuthRequest(`${apiBase()}/recipes/popular`); |
355 | if (!request.ok) throw request; | 333 | if (!request.ok) throw request; |
356 | 334 | ||
357 | const data = await request.json(); | 335 | const data = await request.json(); |
@@ -363,7 +341,7 @@ export default class ServerApi { | |||
363 | } | 341 | } |
364 | 342 | ||
365 | async searchRecipePreviews(needle) { | 343 | async searchRecipePreviews(needle) { |
366 | const url = `${this.apiUrl()}/recipes/search?needle=${needle}`; | 344 | const url = `${apiBase()}/recipes/search?needle=${needle}`; |
367 | const request = await sendAuthRequest(url); | 345 | const request = await sendAuthRequest(url); |
368 | if (!request.ok) throw request; | 346 | if (!request.ok) throw request; |
369 | 347 | ||
@@ -378,7 +356,7 @@ export default class ServerApi { | |||
378 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); | 356 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); |
379 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); | 357 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); |
380 | const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); | 358 | const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); |
381 | const packageUrl = `${this.apiUrl()}/recipes/download/${recipeId}`; | 359 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; |
382 | 360 | ||
383 | fs.ensureDirSync(recipeTempDirectory); | 361 | fs.ensureDirSync(recipeTempDirectory); |
384 | const res = await fetch(packageUrl); | 362 | const res = await fetch(packageUrl); |
@@ -415,7 +393,7 @@ export default class ServerApi { | |||
415 | 393 | ||
416 | // Payment | 394 | // Payment |
417 | async getPlans() { | 395 | async getPlans() { |
418 | const request = await sendAuthRequest(`${this.apiUrl()}/payment/plans`); | 396 | const request = await sendAuthRequest(`${apiBase()}/payment/plans`); |
419 | if (!request.ok) throw request; | 397 | if (!request.ok) throw request; |
420 | const data = await request.json(); | 398 | const data = await request.json(); |
421 | const plan = new PlanModel(data); | 399 | const plan = new PlanModel(data); |
@@ -424,7 +402,7 @@ export default class ServerApi { | |||
424 | } | 402 | } |
425 | 403 | ||
426 | async getHostedPage(planId) { | 404 | async getHostedPage(planId) { |
427 | const request = await sendAuthRequest(`${this.apiUrl()}/payment/init`, { | 405 | const request = await sendAuthRequest(`${apiBase()}/payment/init`, { |
428 | method: 'POST', | 406 | method: 'POST', |
429 | body: JSON.stringify({ | 407 | body: JSON.stringify({ |
430 | planId, | 408 | planId, |
@@ -441,7 +419,7 @@ export default class ServerApi { | |||
441 | 419 | ||
442 | // News | 420 | // News |
443 | async getLatestNews() { | 421 | async getLatestNews() { |
444 | const url = `${this.apiUrl()}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; | 422 | const url = `${apiBase()}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; |
445 | const request = await sendAuthRequest(url); | 423 | const request = await sendAuthRequest(url); |
446 | if (!request.ok) throw request; | 424 | if (!request.ok) throw request; |
447 | const data = await request.json(); | 425 | const data = await request.json(); |
@@ -451,7 +429,7 @@ export default class ServerApi { | |||
451 | } | 429 | } |
452 | 430 | ||
453 | async hideNews(id) { | 431 | async hideNews(id) { |
454 | const request = await sendAuthRequest(`${this.apiUrl()}/news/${id}/read`); | 432 | const request = await sendAuthRequest(`${apiBase()}/news/${id}/read`); |
455 | if (!request.ok) throw request; | 433 | if (!request.ok) throw request; |
456 | debug('ServerApi::hideNews resolves', id); | 434 | debug('ServerApi::hideNews resolves', id); |
457 | } | 435 | } |
@@ -476,7 +454,7 @@ export default class ServerApi { | |||
476 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { | 454 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { |
477 | const services = await Promise.all(config.services.map(async (s) => { | 455 | const services = await Promise.all(config.services.map(async (s) => { |
478 | const service = s; | 456 | const service = s; |
479 | const request = await sendAuthRequest(`${this.apiUrl()}/recipes/${s.service}`); | 457 | const request = await sendAuthRequest(`${apiBase()}/recipes/${s.service}`); |
480 | 458 | ||
481 | if (request.status === 200) { | 459 | if (request.status === 200) { |
482 | const data = await request.json(); | 460 | const data = await request.json(); |
diff --git a/src/app.js b/src/app.js index ccf13b555..797c178a3 100644 --- a/src/app.js +++ b/src/app.js | |||
@@ -57,12 +57,10 @@ window.addEventListener('load', () => { | |||
57 | const api = apiFactory(serverApi, new LocalApi()); | 57 | const api = apiFactory(serverApi, new LocalApi()); |
58 | const router = new RouterStore(); | 58 | const router = new RouterStore(); |
59 | const stores = storeFactory(api, actions, router); | 59 | const stores = storeFactory(api, actions, router); |
60 | serverApi.setStores(stores); | ||
61 | const history = syncHistoryWithStore(hashHistory, router); | 60 | const history = syncHistoryWithStore(hashHistory, router); |
62 | const menu = new MenuFactory(stores, actions); | 61 | const menu = new MenuFactory(stores, actions); |
63 | const touchBar = new TouchBarFactory(stores, actions); | 62 | const touchBar = new TouchBarFactory(stores, actions); |
64 | 63 | ||
65 | |||
66 | window.ferdi = { | 64 | window.ferdi = { |
67 | stores, | 65 | stores, |
68 | actions, | 66 | actions, |
diff --git a/src/features/announcements/api.js b/src/features/announcements/api.js index 3cf90fa4a..eadb7dfce 100644 --- a/src/features/announcements/api.js +++ b/src/features/announcements/api.js | |||
@@ -1,6 +1,6 @@ | |||
1 | import { remote } from 'electron'; | 1 | import { remote } from 'electron'; |
2 | import Request from '../../stores/lib/Request'; | 2 | import Request from '../../stores/lib/Request'; |
3 | import apiBase from '../apiBase'; | 3 | import apiBase from '../../api/apiBase'; |
4 | 4 | ||
5 | const debug = require('debug')('Franz:feature:announcements:api'); | 5 | const debug = require('debug')('Franz:feature:announcements:api'); |
6 | 6 | ||
diff --git a/src/features/workspaces/api.js b/src/features/workspaces/api.js index 81fd6b65d..b8893363f 100644 --- a/src/features/workspaces/api.js +++ b/src/features/workspaces/api.js | |||
@@ -2,7 +2,7 @@ import { pick } from 'lodash'; | |||
2 | import { sendAuthRequest } from '../../api/utils/auth'; | 2 | import { sendAuthRequest } from '../../api/utils/auth'; |
3 | import Request from '../../stores/lib/Request'; | 3 | import Request from '../../stores/lib/Request'; |
4 | import Workspace from './models/Workspace'; | 4 | import Workspace from './models/Workspace'; |
5 | import apiBase from '../apiBase'; | 5 | import apiBase from '../../api/apiBase'; |
6 | 6 | ||
7 | const debug = require('debug')('Franz:feature:workspaces:api'); | 7 | const debug = require('debug')('Franz:feature:workspaces:api'); |
8 | 8 | ||