195 lines
5.7 KiB
JavaScript
195 lines
5.7 KiB
JavaScript
import superagent from "superagent";
|
|
import superagentIntercept from 'superagent-intercept';
|
|
import {appConfig} from "../config/app";
|
|
import {TokenUtil} from "./token";
|
|
import {attachSuperagentLogger} from "./http_logger";
|
|
|
|
let authIntercept = superagentIntercept((err, res) => {
|
|
if (res && res.status === 401) {
|
|
TokenUtil.clearAccessToken();
|
|
TokenUtil.persistToken();
|
|
window.location.href = "/login";
|
|
}
|
|
});
|
|
|
|
export const http = {
|
|
get: (url, opts = {}) => {
|
|
let req = superagent.get(appConfig.apiUrl + url)
|
|
.use(authIntercept)
|
|
.use(attachSuperagentLogger);
|
|
if (TokenUtil.accessToken) {
|
|
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
}
|
|
return req;
|
|
},
|
|
post: (url, opts) => {
|
|
let req = superagent.post(appConfig.apiUrl + url)
|
|
.use(authIntercept)
|
|
.use(attachSuperagentLogger);
|
|
if (TokenUtil.accessToken) {
|
|
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
}
|
|
return req;
|
|
},
|
|
put: (url, opts) => {
|
|
let req = superagent.put(appConfig.apiUrl + url)
|
|
.use(authIntercept)
|
|
.use(attachSuperagentLogger);
|
|
if (TokenUtil.accessToken) {
|
|
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
}
|
|
return req;
|
|
},
|
|
del: (url, opts) => {
|
|
let req = superagent.del(appConfig.apiUrl + url)
|
|
.use(authIntercept)
|
|
.use(attachSuperagentLogger);
|
|
if (TokenUtil.accessToken) {
|
|
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
}
|
|
return req;
|
|
},
|
|
upload: (file) => {
|
|
let req = superagent
|
|
.post(appConfig.apiUrl + '/config/upload-files')
|
|
.attach('file', file)
|
|
.use(authIntercept)
|
|
.use(attachSuperagentLogger);
|
|
if (TokenUtil.accessToken) {
|
|
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
}
|
|
|
|
return req;
|
|
},
|
|
uploadAntd: (args) => {
|
|
const file = args.file;
|
|
const request = http.upload(file)
|
|
.use(authIntercept)
|
|
.use(attachSuperagentLogger);
|
|
request
|
|
.on('progress', event => {
|
|
args.onProgress(event);
|
|
})
|
|
.then(it => {
|
|
args.onSuccess(it);
|
|
}).catch(err => {
|
|
args.onError(err);
|
|
});
|
|
|
|
return request;
|
|
}
|
|
};
|
|
|
|
// import superagent from "superagent";
|
|
// import appConfig from "../Config/StaticVar";
|
|
// import { TokenUtil } from './token'
|
|
// import { navigationService } from './NavigationService'
|
|
// import { Alert } from 'react-native'
|
|
// import StaticVar from '../Config/StaticVar'
|
|
// import AsyncStorage from '@react-native-async-storage/async-storage'
|
|
// import FirebaseLib from '../Lib/FirebaseLib'
|
|
|
|
// let AuthIntercept = require('superagent-intercept')((err, res) => {
|
|
// if (res?.status === 403) {
|
|
// console.log("masuk sini ya")
|
|
// Alert.alert("Sesi Anda Telah Habis", "Sesi Anda Telah Habis, Harap Login Kembali.", [
|
|
// { text: "Oke", onPress: async () => {
|
|
// navigationService.reset('LoginScreen');
|
|
// await AsyncStorage.removeItem(StaticVar.DB_PROFILE)
|
|
// await FirebaseLib.signOut()
|
|
// }}
|
|
// ]);
|
|
// }
|
|
// });
|
|
|
|
// export const http = {
|
|
// get: (url, opts = {}) => {
|
|
// try{
|
|
// let req = superagent.get(appConfig.API_URL + url)
|
|
// .use(AuthIntercept)
|
|
// if (TokenUtil.accessToken) {
|
|
// req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
// }
|
|
// return req;
|
|
// }catch (e) {
|
|
// console.log(e, "error get superagent")
|
|
// throw e
|
|
// }
|
|
|
|
// },
|
|
// post: (url, opts) => {
|
|
// try{
|
|
// let req = superagent.post(appConfig.API_URL + url)
|
|
// .use(AuthIntercept)
|
|
// if (TokenUtil.accessToken) {
|
|
// req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
// }
|
|
// return req;
|
|
// }catch (e) {
|
|
// console.log(e, "error post superagent")
|
|
// throw e
|
|
// }
|
|
// },
|
|
// put: (url, opts) => {
|
|
// try{
|
|
// let req = superagent.put(appConfig.API_URL + url)
|
|
// .use(AuthIntercept)
|
|
// if (TokenUtil.accessToken) {
|
|
// req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
// }
|
|
// return req;
|
|
// }catch (e) {
|
|
// console.log(e, "error put superagent")
|
|
// throw e
|
|
// }
|
|
// },
|
|
// del: (url, opts) => {
|
|
// try{
|
|
// let req = superagent.del(appConfig.API_URL + url)
|
|
// .use(AuthIntercept)
|
|
// if (TokenUtil.accessToken) {
|
|
// req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
|
// }
|
|
// return req;
|
|
// }catch (e) {
|
|
// console.log(e, "error del superagent")
|
|
// throw e
|
|
// }
|
|
// },
|
|
// upload: (file) => {
|
|
// try{
|
|
// const request = superagent
|
|
// .post(appConfig.API_URL + '/v1/files')
|
|
// .use(AuthIntercept)
|
|
// .attach('file', file);
|
|
|
|
// return request;
|
|
// }catch (e) {
|
|
// console.log(e, "error upload superagent")
|
|
// throw e
|
|
// }
|
|
// },
|
|
// uploadAntd: (args) => {
|
|
// try{
|
|
// const file = args.file;
|
|
// const request = http.upload(file)
|
|
// .use(AuthIntercept)
|
|
|
|
// request
|
|
// .on('progress', event => {
|
|
// args.onProgress(event);
|
|
// })
|
|
// .then(it => {
|
|
// args.onSuccess(it);
|
|
// }).catch(err => {
|
|
// args.onError(err);
|
|
// });
|
|
// return request;
|
|
// }catch (e) {
|
|
// console.log(e, "error uploadAntd superagent")
|
|
// throw e
|
|
// }
|
|
// }
|
|
|
|
// };
|