Feature/history api (#84)

added /api/v2.0/ to the backend url

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2020-05-30 10:12:01 +02:00 committed by GitHub
parent e2155e735d
commit 02f1b2abcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 37 deletions

View file

@ -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<BackendConfigState> = async () => {
export const getBackendConfig = async (): Promise<BackendConfigState> => {
const response = await fetch(getBackendUrl() + '/backend-config.json')
expectResponseCode(response)
return await response.json() as Promise<BackendConfigState>
}
export const getFrontendConfig: (baseUrl: string) => Promise<FrontendConfigState> = async (baseUrl) => {
export const getFrontendConfig = async (baseUrl: string): Promise<FrontendConfigState> => {
const response = await fetch(`${baseUrl}config.json`)
expectResponseCode(response)
return await response.json() as Promise<FrontendConfigState>

10
src/api/default.ts Normal file
View file

@ -0,0 +1,10 @@
export const defaultFetchConfig: Partial<RequestInit> = {
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer'
}

46
src/api/history.ts Normal file
View file

@ -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<HistoryEntry[]> => {
const response = await fetch(getBackendUrl() + '/history')
expectResponseCode(response)
return await response.json() as Promise<HistoryEntry[]>
}
export const setHistory = async (entries: HistoryEntry[]): Promise<void> => {
const response = await fetch(getBackendUrl() + '/history', {
...defaultFetchConfig,
method: 'POST',
body: JSON.stringify({
history: entries
})
})
expectResponseCode(response)
}
export const deleteHistory = async (): Promise<void> => {
const response = await fetch(getBackendUrl() + '/history', {
...defaultFetchConfig,
method: 'DELETE'
})
expectResponseCode(response)
}
export const updateHistoryEntry = async (noteId: string, entry: HistoryEntry): Promise<HistoryEntry> => {
const response = await fetch(getBackendUrl() + '/history/' + noteId, {
...defaultFetchConfig,
method: 'PUT',
body: JSON.stringify(entry)
})
expectResponseCode(response)
return await response.json() as Promise<HistoryEntry>
}
export const deleteHistoryEntry = async (noteId: string): Promise<void> => {
const response = await fetch(getBackendUrl() + '/history/' + noteId, {
...defaultFetchConfig,
method: 'DELETE'
})
expectResponseCode(response)
}

View file

@ -1,6 +1,7 @@
import { expectResponseCode, getBackendUrl } from '../utils/apiUtils'
import { defaultFetchConfig } from './default'
export const getMe: (() => Promise<meResponse>) = async () => {
export const getMe = async (): Promise<meResponse> => {
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<void> => {
export const doEmailLogin = async (email: string, password: string): Promise<void> => {
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<vo
expectResponseCode(response)
}
export const postLdapLogin: ((email: string, password: string) => Promise<void>) = async (username, password) => {
export const doLdapLogin = async (username: string, password: string): Promise<void> => {
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<void>)
expectResponseCode(response)
}
export const postOpenIdLogin: ((openid: string) => Promise<void>) = async (openId: string) => {
export const doOpenIdLogin = async (openId: string): Promise<void> => {
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
})

View file

@ -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()
}

View file

@ -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)

View file

@ -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<void>) = async () => {
await postOpenIdLogin(openId)
await doOpenIdLogin(openId)
await getAndSetUser()
}

View file

@ -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)