diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-07-10 16:07:45 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-07-11 17:47:53 +0200 |
commit | fa1a7037b47f2e0114d8abc5a99d29239bd3637b (patch) | |
tree | 83404acf711aa8976dce47950edcca64836e0cd8 /src/api | |
parent | 6.0.0-nightly.96 [skip ci] (diff) | |
download | ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.gz ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.zst ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.zip |
refactor: local server import/export
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/apiBase.ts | 17 | ||||
-rw-r--r-- | src/api/server/ServerApi.ts | 4 | ||||
-rw-r--r-- | src/api/utils/auth.ts | 21 |
3 files changed, 38 insertions, 4 deletions
diff --git a/src/api/apiBase.ts b/src/api/apiBase.ts index 701919785..974d513a1 100644 --- a/src/api/apiBase.ts +++ b/src/api/apiBase.ts | |||
@@ -34,6 +34,23 @@ export default function apiBase(withVersion = true) { | |||
34 | return fixUrl(withVersion ? `${url}/${API_VERSION}` : url); | 34 | return fixUrl(withVersion ? `${url}/${API_VERSION}` : url); |
35 | }; | 35 | }; |
36 | 36 | ||
37 | export function needsToken(): boolean { | ||
38 | return (window as any).ferdium.stores.settings.all.app.server === LOCAL_SERVER; | ||
39 | } | ||
40 | |||
41 | export function localServerToken(): string | undefined { | ||
42 | return needsToken() | ||
43 | ? (window as any).ferdium.stores.requests.localServerToken | ||
44 | : undefined; | ||
45 | } | ||
46 | |||
47 | export function importExportURL() { | ||
48 | const base = apiBase(false); | ||
49 | return needsToken() | ||
50 | ? `${base}/token/${localServerToken()}` | ||
51 | : base; | ||
52 | } | ||
53 | |||
37 | export function serverBase() { | 54 | export function serverBase() { |
38 | const serverType = (window as any).ferdium.stores.settings.all.app.server; | 55 | const serverType = (window as any).ferdium.stores.settings.all.app.server; |
39 | const noServer = 'You are using Ferdium without a server'; | 56 | const noServer = 'You are using Ferdium without a server'; |
diff --git a/src/api/server/ServerApi.ts b/src/api/server/ServerApi.ts index 77a759b3e..8b551ade2 100644 --- a/src/api/server/ServerApi.ts +++ b/src/api/server/ServerApi.ts | |||
@@ -25,7 +25,7 @@ import { SERVER_NOT_LOADED } from '../../config'; | |||
25 | import { userDataRecipesPath, userDataPath } from '../../environment-remote'; | 25 | import { userDataRecipesPath, userDataPath } from '../../environment-remote'; |
26 | import { asarRecipesPath } from '../../helpers/asar-helpers'; | 26 | import { asarRecipesPath } from '../../helpers/asar-helpers'; |
27 | import apiBase from '../apiBase'; | 27 | import apiBase from '../apiBase'; |
28 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; | 28 | import { prepareAuthRequest, prepareLocalToken, sendAuthRequest } from '../utils/auth'; |
29 | 29 | ||
30 | import { | 30 | import { |
31 | getRecipeDirectory, | 31 | getRecipeDirectory, |
@@ -246,6 +246,8 @@ export default class ServerApi { | |||
246 | 246 | ||
247 | delete requestData.headers['Content-Type']; | 247 | delete requestData.headers['Content-Type']; |
248 | 248 | ||
249 | await prepareLocalToken(requestData); | ||
250 | |||
249 | const request = await window.fetch( | 251 | const request = await window.fetch( |
250 | `${apiBase()}/service/${serviceId}`, | 252 | `${apiBase()}/service/${serviceId}`, |
251 | // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'. | 253 | // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'. |
diff --git a/src/api/utils/auth.ts b/src/api/utils/auth.ts index a7a73309d..282d00459 100644 --- a/src/api/utils/auth.ts +++ b/src/api/utils/auth.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | import localStorage from 'mobx-localstorage'; | 1 | import localStorage from 'mobx-localstorage'; |
2 | import { when } from 'mobx'; | ||
3 | import { localServerToken, needsToken } from '../apiBase'; | ||
2 | import { ferdiumLocale, ferdiumVersion } from '../../environment-remote'; | 4 | import { ferdiumLocale, ferdiumVersion } from '../../environment-remote'; |
3 | 5 | ||
4 | export const prepareAuthRequest = ( | 6 | export const prepareAuthRequest = ( |
@@ -29,10 +31,23 @@ export const prepareAuthRequest = ( | |||
29 | return request; | 31 | return request; |
30 | }; | 32 | }; |
31 | 33 | ||
32 | export const sendAuthRequest = ( | 34 | export const prepareLocalToken = async ( |
35 | requestData: { method: string; headers?: any; body?: any }, | ||
36 | ) => { | ||
37 | await when(() => !needsToken() || !!localServerToken(), { timeout: 2000 }); | ||
38 | const token = localServerToken(); | ||
39 | if (token) { | ||
40 | requestData.headers['X-Ferdium-Local-Token'] = token; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | export const sendAuthRequest = async ( | ||
33 | url: RequestInfo, | 45 | url: RequestInfo, |
34 | options?: { method: string; headers?: any; body?: any }, | 46 | options?: { method: string; headers?: any; body?: any }, |
35 | auth?: boolean, | 47 | auth?: boolean, |
36 | ) => | 48 | ) => { |
49 | const request = prepareAuthRequest(options, auth); | ||
50 | await prepareLocalToken(request); | ||
37 | // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'. | 51 | // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'. |
38 | window.fetch(url, prepareAuthRequest(options, auth)); | 52 | return window.fetch(url, request); |
53 | }; | ||