mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import axios from 'axios'
|
|
import log4js from 'log4js'
|
|
import CONFIG from '@/config'
|
|
|
|
log4js.configure(CONFIG.LOG4JS_CONFIG)
|
|
const logger = log4js.getLogger('http')
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export const apiPost = async (url: string, payload: unknown): Promise<any> => {
|
|
logger.trace('POST: url=' + url + ' payload=' + payload)
|
|
return axios
|
|
.post(url, payload)
|
|
.then((result) => {
|
|
logger.trace('POST-Response: result=' + result)
|
|
if (result.status !== 200) {
|
|
throw new Error('HTTP Status Error ' + result.status)
|
|
}
|
|
if (result.data.state !== 'success') {
|
|
throw new Error(result.data.msg)
|
|
}
|
|
return { success: true, data: result.data }
|
|
})
|
|
.catch((error) => {
|
|
return { success: false, data: error.message }
|
|
})
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export const apiGet = async (url: string): Promise<any> => {
|
|
logger.trace('GET: url=' + url)
|
|
return axios
|
|
.get(url)
|
|
.then((result) => {
|
|
logger.trace('GET-Response: result=' + result)
|
|
if (result.status !== 200) {
|
|
throw new Error('HTTP Status Error ' + result.status)
|
|
}
|
|
if (!['success', 'warning'].includes(result.data.state)) {
|
|
throw new Error(result.data.msg)
|
|
}
|
|
return { success: true, data: result.data }
|
|
})
|
|
.catch((error) => {
|
|
return { success: false, data: error.message }
|
|
})
|
|
}
|