diff --git a/src/api/config.ts b/src/api/config.ts index 200f028fa..ecb78500f 100644 --- a/src/api/config.ts +++ b/src/api/config.ts @@ -2,13 +2,13 @@ import { FrontendConfigState } from '../redux/frontend-config/types' import { BackendConfigState } from '../redux/backend-config/types' import { expectResponseCode, getBackendUrl } from '../utils/apiUtils' -export const getBackendConfig: () => Promise = async () => { +export const getBackendConfig = async (): Promise => { const response = await fetch(getBackendUrl() + '/backend-config.json') expectResponseCode(response) return await response.json() as Promise } -export const getFrontendConfig: (baseUrl: string) => Promise = async (baseUrl) => { +export const getFrontendConfig = async (baseUrl: string): Promise => { const response = await fetch(`${baseUrl}config.json`) expectResponseCode(response) return await response.json() as Promise diff --git a/src/api/default.ts b/src/api/default.ts new file mode 100644 index 000000000..971ca0044 --- /dev/null +++ b/src/api/default.ts @@ -0,0 +1,10 @@ +export const defaultFetchConfig: Partial = { + mode: 'cors', + cache: 'no-cache', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + referrerPolicy: 'no-referrer' +} diff --git a/src/api/history.ts b/src/api/history.ts new file mode 100644 index 000000000..765b020c6 --- /dev/null +++ b/src/api/history.ts @@ -0,0 +1,46 @@ +import { HistoryEntry } from '../components/landing/pages/history/history' +import { expectResponseCode, getBackendUrl } from '../utils/apiUtils' +import { defaultFetchConfig } from './default' + +export const getHistory = async (): Promise => { + const response = await fetch(getBackendUrl() + '/history') + expectResponseCode(response) + return await response.json() as Promise +} + +export const setHistory = async (entries: HistoryEntry[]): Promise => { + const response = await fetch(getBackendUrl() + '/history', { + ...defaultFetchConfig, + method: 'POST', + body: JSON.stringify({ + history: entries + }) + }) + expectResponseCode(response) +} + +export const deleteHistory = async (): Promise => { + const response = await fetch(getBackendUrl() + '/history', { + ...defaultFetchConfig, + method: 'DELETE' + }) + expectResponseCode(response) +} + +export const updateHistoryEntry = async (noteId: string, entry: HistoryEntry): Promise => { + const response = await fetch(getBackendUrl() + '/history/' + noteId, { + ...defaultFetchConfig, + method: 'PUT', + body: JSON.stringify(entry) + }) + expectResponseCode(response) + return await response.json() as Promise +} + +export const deleteHistoryEntry = async (noteId: string): Promise => { + const response = await fetch(getBackendUrl() + '/history/' + noteId, { + ...defaultFetchConfig, + method: 'DELETE' + }) + expectResponseCode(response) +} diff --git a/src/api/user.ts b/src/api/user.ts index 40698b2c0..2a602ea2f 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -1,6 +1,7 @@ import { expectResponseCode, getBackendUrl } from '../utils/apiUtils' +import { defaultFetchConfig } from './default' -export const getMe: (() => Promise) = async () => { +export const getMe = async (): Promise => { const response = await fetch('/me') expectResponseCode(response) return (await response.json()) as meResponse @@ -12,17 +13,10 @@ export interface meResponse { photo: string } -export const postEmailLogin = async (email: string, password: string):Promise => { +export const doEmailLogin = async (email: string, password: string): Promise => { const response = await fetch(getBackendUrl() + '/auth/email', { + ...defaultFetchConfig, method: 'POST', - mode: 'cors', - cache: 'no-cache', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json' - }, - redirect: 'follow', - referrerPolicy: 'no-referrer', body: JSON.stringify({ email: email, password: password @@ -32,17 +26,10 @@ export const postEmailLogin = async (email: string, password: string):Promise Promise) = async (username, password) => { +export const doLdapLogin = async (username: string, password: string): Promise => { const response = await fetch(getBackendUrl() + '/auth/ldap', { + ...defaultFetchConfig, method: 'POST', - mode: 'cors', - cache: 'no-cache', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json' - }, - redirect: 'follow', - referrerPolicy: 'no-referrer', body: JSON.stringify({ username: username, password: password @@ -52,17 +39,10 @@ export const postLdapLogin: ((email: string, password: string) => Promise) expectResponseCode(response) } -export const postOpenIdLogin: ((openid: string) => Promise) = async (openId: string) => { +export const doOpenIdLogin = async (openId: string): Promise => { const response = await fetch(getBackendUrl() + '/auth/openid', { + ...defaultFetchConfig, method: 'POST', - mode: 'cors', - cache: 'no-cache', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json' - }, - redirect: 'follow', - referrerPolicy: 'no-referrer', body: JSON.stringify({ openId: openId }) diff --git a/src/components/landing/pages/login/auth/via-email.tsx b/src/components/landing/pages/login/auth/via-email.tsx index 2ce0a1662..bde12feca 100644 --- a/src/components/landing/pages/login/auth/via-email.tsx +++ b/src/components/landing/pages/login/auth/via-email.tsx @@ -1,7 +1,7 @@ import { Trans, useTranslation } from 'react-i18next' import { Alert, Button, Card, Form } from 'react-bootstrap' import React, { FormEvent, useState } from 'react' -import { postEmailLogin } from '../../../../../api/user' +import { doEmailLogin } from '../../../../../api/user' import { getAndSetUser } from '../../../../../utils/apiUtils' export const ViaEMail: React.FC = () => { @@ -11,7 +11,7 @@ export const ViaEMail: React.FC = () => { const [error, setError] = useState(false) const doAsyncLogin = async () => { - await postEmailLogin(email, password) + await doEmailLogin(email, password) await getAndSetUser() } diff --git a/src/components/landing/pages/login/auth/via-ldap.tsx b/src/components/landing/pages/login/auth/via-ldap.tsx index 1815d68e1..c627b02e0 100644 --- a/src/components/landing/pages/login/auth/via-ldap.tsx +++ b/src/components/landing/pages/login/auth/via-ldap.tsx @@ -2,7 +2,7 @@ import React, { FormEvent, useState } from 'react' import { Trans, useTranslation } from 'react-i18next' import { Alert, Button, Card, Form } from 'react-bootstrap' -import { postLdapLogin } from '../../../../../api/user' +import { doLdapLogin } from '../../../../../api/user' import { getAndSetUser } from '../../../../../utils/apiUtils' import { useSelector } from 'react-redux' import { ApplicationState } from '../../../../../redux' @@ -19,7 +19,7 @@ export const ViaLdap: React.FC = () => { const doAsyncLogin = async () => { try { - await postLdapLogin(username, password) + await doLdapLogin(username, password) await getAndSetUser() } catch { setError(true) diff --git a/src/components/landing/pages/login/auth/via-openid.tsx b/src/components/landing/pages/login/auth/via-openid.tsx index 307d57379..88b442e3e 100644 --- a/src/components/landing/pages/login/auth/via-openid.tsx +++ b/src/components/landing/pages/login/auth/via-openid.tsx @@ -1,7 +1,7 @@ import React, { FormEvent, useState } from 'react' import { Trans, useTranslation } from 'react-i18next' import { Alert, Button, Card, Form } from 'react-bootstrap' -import { postOpenIdLogin } from '../../../../../api/user' +import { doOpenIdLogin } from '../../../../../api/user' import { getAndSetUser } from '../../../../../utils/apiUtils' export const ViaOpenId: React.FC = () => { @@ -9,7 +9,7 @@ export const ViaOpenId: React.FC = () => { const [openId, setOpenId] = useState('') const [error, setError] = useState(false) const doAsyncLogin: (() => Promise) = async () => { - await postOpenIdLogin(openId) + await doOpenIdLogin(openId) await getAndSetUser() } diff --git a/src/redux/frontend-config/methods.ts b/src/redux/frontend-config/methods.ts index 362c7bc1d..c869406d6 100644 --- a/src/redux/frontend-config/methods.ts +++ b/src/redux/frontend-config/methods.ts @@ -5,7 +5,10 @@ export const setFrontendConfig = (state: FrontendConfigState): void => { const action: SetFrontendConfigAction = { type: SET_FRONTEND_CONFIG_ACTION_TYPE, payload: { - state + state: { + ...state, + backendUrl: state.backendUrl + '/api/v2.0/' + } } } store.dispatch(action)