Syncrhonize apiClient with chrome storage
This commit is contained in:
@@ -1,65 +1,62 @@
|
||||
import axios from 'axios'
|
||||
import axios from "axios"
|
||||
|
||||
class Client {
|
||||
constructor() {
|
||||
// this.warningPresent = false
|
||||
this.client = axios.create({responseType: 'json', headers: {'x-client-version': this.clientVersion()}})
|
||||
// this.client.interceptors.response.use(this.forceUpdateInterceptor)
|
||||
this.client = axios.create({
|
||||
responseType: "json"
|
||||
})
|
||||
}
|
||||
|
||||
// csrfToken() {
|
||||
// return document.getElementsByName('csrf-token')[0].getAttribute('content')
|
||||
// }
|
||||
registerStorage(storage) {
|
||||
storage.sync.get(["subdomain", "apiKey"], store => {
|
||||
this.setSubdomain(store.subdomain)
|
||||
this.setCredentials(store.apiKey)
|
||||
})
|
||||
|
||||
clientVersion() {
|
||||
return "1.0.0"
|
||||
// FIXME: on server sync with real version numbers return chrome.runtime.getManifest().version
|
||||
storage.onChanged.addListener(({ subdomain, apiKey }) => {
|
||||
subdomain && this.setSubdomain(subdomain.newValue)
|
||||
apiKey && this.setCredentials(apiKey.newValue)
|
||||
})
|
||||
}
|
||||
|
||||
// clientVersionMajor() {
|
||||
// const [major] = this.clientVersion().split('.')
|
||||
// return parseInt(major)
|
||||
// }
|
||||
setSubdomain(subdomain) {
|
||||
this.client.defaults.baseURL = `https://${encodeURIComponent(
|
||||
subdomain
|
||||
)}.mocoapp.com/api/v1`
|
||||
}
|
||||
|
||||
// forceUpdateInterceptor = (response) => {
|
||||
// const [serverMajor] = response.headers['x-moco-version'].split('.')
|
||||
setCredentials(apiKey) {
|
||||
this.client.defaults.headers.common[
|
||||
"Authorization"
|
||||
] = `Token token=${encodeURIComponent(apiKey)}`
|
||||
}
|
||||
|
||||
// // if (parseInt(serverMajor) > this.clientVersionMajor() && !this.warningPresent) {
|
||||
// // this.warningPresent = true
|
||||
// // }
|
||||
setClientVersion(version) {
|
||||
this.client.defaults.headers.common["x-client-version"] = version
|
||||
}
|
||||
|
||||
// if (response.headers['x-moco-version'] != this.clientVersion())
|
||||
// window.updateRequired = true
|
||||
|
||||
// return response
|
||||
// }
|
||||
get defaults() {
|
||||
return this.client.defaults
|
||||
}
|
||||
|
||||
get(url, config = {}) {
|
||||
return this.client.get(url, config)
|
||||
}
|
||||
|
||||
post(url, data) {
|
||||
return this.client.post(url, data, {
|
||||
// headers: {'x-csrf-token': this.csrfToken()}
|
||||
})
|
||||
return this.client.post(url, data)
|
||||
}
|
||||
|
||||
put(url, data) {
|
||||
return this.client.put(url, data, {
|
||||
// headers: { 'x-csrf-token': this.csrfToken() }
|
||||
})
|
||||
return this.client.put(url, data)
|
||||
}
|
||||
|
||||
patch(url, data) {
|
||||
return this.client.patch(url, data, {
|
||||
// headers: { 'x-csrf-token': this.csrfToken() }
|
||||
})
|
||||
return this.client.patch(url, data)
|
||||
}
|
||||
|
||||
delete(url) {
|
||||
return this.client.delete(url, {
|
||||
// headers: { 'x-csrf-token': this.csrfToken() }
|
||||
})
|
||||
return this.client.delete(url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import DomainCheck from "./services/DomainCheck"
|
||||
import apiClient from "api/client"
|
||||
import config from "./config"
|
||||
|
||||
apiClient.registerStorage(chrome.storage)
|
||||
apiClient.setClientVersion(chrome.runtime.getManifest().version)
|
||||
|
||||
const domainCheck = new DomainCheck(config)
|
||||
|
||||
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||||
// inject files only after the page is fully loaded
|
||||
// run only after the page is fully loaded
|
||||
if (changeInfo.status != "complete") {
|
||||
return
|
||||
}
|
||||
|
||||
// inject files only for supported services
|
||||
const service = domainCheck.match(tab.url)
|
||||
|
||||
if (service) {
|
||||
|
||||
@@ -9,26 +9,28 @@ class Setup extends Component {
|
||||
@observable apiKey = ""
|
||||
|
||||
componentDidMount() {
|
||||
chrome.storage.sync.get(null, store => {
|
||||
chrome.storage.sync.get(["subdomain", "apiKey"], store => {
|
||||
this.loading = false
|
||||
this.subdomain = store.subdomain || ""
|
||||
this.apiKey = store.api_key || ""
|
||||
this.apiKey = store.apiKey || ""
|
||||
})
|
||||
}
|
||||
|
||||
// EVENTS
|
||||
|
||||
onChange = event => {
|
||||
this[event.target.name] = event.target.value
|
||||
this[event.target.name] = event.target.value.trim()
|
||||
}
|
||||
|
||||
onSubmit = _event => {
|
||||
chrome.storage.sync.set(
|
||||
{
|
||||
subdomain: this.subdomain.trim(),
|
||||
api_key: this.apiKey.trim()
|
||||
subdomain: this.subdomain,
|
||||
apiKey: this.apiKey
|
||||
},
|
||||
() => window.close()
|
||||
() => {
|
||||
window.close()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user