2018-11-15 19:48:37 +01:00
|
|
|
import axios, { AxiosInstance, AxiosPromise, AxiosResponse, AxiosInterceptorManager } from 'axios'
|
|
|
|
|
|
2019-02-03 00:51:58 +01:00
|
|
|
async function sendRequest(url: string, lang: string, mytok: string, method: string, mydata: any) {
|
2018-11-15 19:48:37 +01:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
console.log('sendRequest', method, url, '[', lang, ']')
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
const authHeader = new Headers()
|
|
|
|
|
authHeader.append('content-Type', 'application/json')
|
|
|
|
|
authHeader.append('Accept', 'application/json')
|
2019-02-12 12:06:01 +01:00
|
|
|
if (url !== process.env.MONGODB_HOST + '/users/login') {
|
|
|
|
|
authHeader.append('x-auth', mytok)
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log('TOK PASSATO ALLA FETCH:', mytok)
|
2019-02-12 12:06:01 +01:00
|
|
|
}
|
2019-02-02 20:13:06 +01:00
|
|
|
// authHeader.append('accept-language', lang)
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
let configInit: RequestInit
|
|
|
|
|
|
|
|
|
|
if (method === 'GET') {
|
|
|
|
|
configInit = {
|
|
|
|
|
method: method,
|
|
|
|
|
cache: 'no-cache',
|
2019-02-09 18:04:49 +01:00
|
|
|
mode: 'cors',
|
2019-02-01 04:10:31 +01:00
|
|
|
headers: authHeader
|
|
|
|
|
}
|
2019-02-03 00:51:58 +01:00
|
|
|
} else if (method === 'DELETE') {
|
|
|
|
|
configInit = {
|
|
|
|
|
method: method,
|
|
|
|
|
cache: 'no-cache',
|
2019-02-09 18:04:49 +01:00
|
|
|
mode: 'cors',
|
2019-02-03 00:51:58 +01:00
|
|
|
headers: authHeader
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
} else {
|
|
|
|
|
configInit = {
|
|
|
|
|
method: method,
|
|
|
|
|
cache: 'no-cache',
|
2019-02-09 18:04:49 +01:00
|
|
|
mode: 'cors',
|
2019-02-01 04:10:31 +01:00
|
|
|
headers: authHeader
|
|
|
|
|
}
|
2019-02-11 02:58:53 +01:00
|
|
|
|
|
|
|
|
if (mydata !== null)
|
|
|
|
|
configInit.body = JSON.stringify(mydata)
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const request: Promise<Response> = fetch(url, configInit)
|
|
|
|
|
return request
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default sendRequest
|