Disable CapacitorHttp and add plugin to replace axios calls #781

This commit is contained in:
advplyr 2023-09-15 17:35:44 -05:00
parent 27bbe40874
commit 57d1fbfa83
21 changed files with 142 additions and 62 deletions

40
plugins/nativeHttp.js Normal file
View file

@ -0,0 +1,40 @@
import { CapacitorHttp } from '@capacitor/core'
export default function ({ store }, inject) {
const nativeHttp = {
request(method, _url, data, options = {}) {
let url = _url
const headers = {}
if (!url.startsWith('http') && !url.startsWith('capacitor')) {
const bearerToken = store.getters['user/getToken']
if (bearerToken) {
headers['Authorization'] = `Bearer ${bearerToken}`
} else {
console.warn('[nativeHttp] No Bearer Token for request')
}
const serverUrl = store.getters['user/getServerAddress']
if (serverUrl) {
url = `${serverUrl}${url}`
}
}
console.log(`[nativeHttp] Making ${method} request to ${url}`)
return CapacitorHttp.request({
method,
url,
data,
headers,
...options
}).then(res => res.data)
},
get(url, options = {}) {
return this.request('GET', url, undefined, options)
},
post(url, data, options = {}) {
return this.request('POST', url, data, options)
},
patch(url, data, options = {}) {
return this.request('PATCH', url, data, options)
}
}
inject('nativeHttp', nativeHttp)
}