mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 22:54:42 -04:00
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:
parent
e2155e735d
commit
02f1b2abcc
8 changed files with 76 additions and 37 deletions
|
@ -2,13 +2,13 @@ import { FrontendConfigState } from '../redux/frontend-config/types'
|
||||||
import { BackendConfigState } from '../redux/backend-config/types'
|
import { BackendConfigState } from '../redux/backend-config/types'
|
||||||
import { expectResponseCode, getBackendUrl } from '../utils/apiUtils'
|
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')
|
const response = await fetch(getBackendUrl() + '/backend-config.json')
|
||||||
expectResponseCode(response)
|
expectResponseCode(response)
|
||||||
return await response.json() as Promise<BackendConfigState>
|
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`)
|
const response = await fetch(`${baseUrl}config.json`)
|
||||||
expectResponseCode(response)
|
expectResponseCode(response)
|
||||||
return await response.json() as Promise<FrontendConfigState>
|
return await response.json() as Promise<FrontendConfigState>
|
||||||
|
|
10
src/api/default.ts
Normal file
10
src/api/default.ts
Normal 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
46
src/api/history.ts
Normal 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)
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import { expectResponseCode, getBackendUrl } from '../utils/apiUtils'
|
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')
|
const response = await fetch('/me')
|
||||||
expectResponseCode(response)
|
expectResponseCode(response)
|
||||||
return (await response.json()) as meResponse
|
return (await response.json()) as meResponse
|
||||||
|
@ -12,17 +13,10 @@ export interface meResponse {
|
||||||
photo: string
|
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', {
|
const response = await fetch(getBackendUrl() + '/auth/email', {
|
||||||
|
...defaultFetchConfig,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
redirect: 'follow',
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
email: email,
|
email: email,
|
||||||
password: password
|
password: password
|
||||||
|
@ -32,17 +26,10 @@ export const postEmailLogin = async (email: string, password: string):Promise<vo
|
||||||
expectResponseCode(response)
|
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', {
|
const response = await fetch(getBackendUrl() + '/auth/ldap', {
|
||||||
|
...defaultFetchConfig,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
redirect: 'follow',
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
username: username,
|
username: username,
|
||||||
password: password
|
password: password
|
||||||
|
@ -52,17 +39,10 @@ export const postLdapLogin: ((email: string, password: string) => Promise<void>)
|
||||||
expectResponseCode(response)
|
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', {
|
const response = await fetch(getBackendUrl() + '/auth/openid', {
|
||||||
|
...defaultFetchConfig,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
redirect: 'follow',
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
openId: openId
|
openId: openId
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Trans, useTranslation } from 'react-i18next'
|
import { Trans, useTranslation } from 'react-i18next'
|
||||||
import { Alert, Button, Card, Form } from 'react-bootstrap'
|
import { Alert, Button, Card, Form } from 'react-bootstrap'
|
||||||
import React, { FormEvent, useState } from 'react'
|
import React, { FormEvent, useState } from 'react'
|
||||||
import { postEmailLogin } from '../../../../../api/user'
|
import { doEmailLogin } from '../../../../../api/user'
|
||||||
import { getAndSetUser } from '../../../../../utils/apiUtils'
|
import { getAndSetUser } from '../../../../../utils/apiUtils'
|
||||||
|
|
||||||
export const ViaEMail: React.FC = () => {
|
export const ViaEMail: React.FC = () => {
|
||||||
|
@ -11,7 +11,7 @@ export const ViaEMail: React.FC = () => {
|
||||||
const [error, setError] = useState(false)
|
const [error, setError] = useState(false)
|
||||||
|
|
||||||
const doAsyncLogin = async () => {
|
const doAsyncLogin = async () => {
|
||||||
await postEmailLogin(email, password)
|
await doEmailLogin(email, password)
|
||||||
await getAndSetUser()
|
await getAndSetUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import React, { FormEvent, useState } from 'react'
|
||||||
|
|
||||||
import { Trans, useTranslation } from 'react-i18next'
|
import { Trans, useTranslation } from 'react-i18next'
|
||||||
import { Alert, Button, Card, Form } from 'react-bootstrap'
|
import { Alert, Button, Card, Form } from 'react-bootstrap'
|
||||||
import { postLdapLogin } from '../../../../../api/user'
|
import { doLdapLogin } from '../../../../../api/user'
|
||||||
import { getAndSetUser } from '../../../../../utils/apiUtils'
|
import { getAndSetUser } from '../../../../../utils/apiUtils'
|
||||||
import { useSelector } from 'react-redux'
|
import { useSelector } from 'react-redux'
|
||||||
import { ApplicationState } from '../../../../../redux'
|
import { ApplicationState } from '../../../../../redux'
|
||||||
|
@ -19,7 +19,7 @@ export const ViaLdap: React.FC = () => {
|
||||||
|
|
||||||
const doAsyncLogin = async () => {
|
const doAsyncLogin = async () => {
|
||||||
try {
|
try {
|
||||||
await postLdapLogin(username, password)
|
await doLdapLogin(username, password)
|
||||||
await getAndSetUser()
|
await getAndSetUser()
|
||||||
} catch {
|
} catch {
|
||||||
setError(true)
|
setError(true)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import React, { FormEvent, useState } from 'react'
|
import React, { FormEvent, useState } from 'react'
|
||||||
import { Trans, useTranslation } from 'react-i18next'
|
import { Trans, useTranslation } from 'react-i18next'
|
||||||
import { Alert, Button, Card, Form } from 'react-bootstrap'
|
import { Alert, Button, Card, Form } from 'react-bootstrap'
|
||||||
import { postOpenIdLogin } from '../../../../../api/user'
|
import { doOpenIdLogin } from '../../../../../api/user'
|
||||||
import { getAndSetUser } from '../../../../../utils/apiUtils'
|
import { getAndSetUser } from '../../../../../utils/apiUtils'
|
||||||
|
|
||||||
export const ViaOpenId: React.FC = () => {
|
export const ViaOpenId: React.FC = () => {
|
||||||
|
@ -9,7 +9,7 @@ export const ViaOpenId: React.FC = () => {
|
||||||
const [openId, setOpenId] = useState('')
|
const [openId, setOpenId] = useState('')
|
||||||
const [error, setError] = useState(false)
|
const [error, setError] = useState(false)
|
||||||
const doAsyncLogin: (() => Promise<void>) = async () => {
|
const doAsyncLogin: (() => Promise<void>) = async () => {
|
||||||
await postOpenIdLogin(openId)
|
await doOpenIdLogin(openId)
|
||||||
await getAndSetUser()
|
await getAndSetUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,10 @@ export const setFrontendConfig = (state: FrontendConfigState): void => {
|
||||||
const action: SetFrontendConfigAction = {
|
const action: SetFrontendConfigAction = {
|
||||||
type: SET_FRONTEND_CONFIG_ACTION_TYPE,
|
type: SET_FRONTEND_CONFIG_ACTION_TYPE,
|
||||||
payload: {
|
payload: {
|
||||||
state
|
state: {
|
||||||
|
...state,
|
||||||
|
backendUrl: state.backendUrl + '/api/v2.0/'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
store.dispatch(action)
|
store.dispatch(action)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue