Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroborges committed Jul 12, 2019
1 parent a27f3ff commit 76adc2c
Show file tree
Hide file tree
Showing 3 changed files with 804 additions and 24 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"license": "MIT",
"typings": "index.d.ts",
"dependencies": {
"axios": "^0.18.0",
"nanobar": "^0.4.2"
},
"devDependencies": {
Expand Down
53 changes: 30 additions & 23 deletions src/inertia.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Axios from 'axios'
import Modal from './modal'
import Progress from './progress'

Expand All @@ -7,7 +6,7 @@ export default {
updatePage: null,
version: null,
visitId: null,
cancelToken: null,
abortController: null,

init({ initialPage, resolveComponent, updatePage }) {
this.resolveComponent = resolveComponent
Expand All @@ -30,15 +29,24 @@ export default {
},

isInertiaResponse(response) {
return response && response.headers['x-inertia']
return response && response.headers.has('x-inertia')
},

hasBody(method) {
return ['GET', 'HEAD'].indexOf(method.toUpperCase()) === -1
},

getCookieValue(name) {
let match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'))
return match ? decodeURIComponent(match[3]) : null
},

cancelActiveVisits() {
if (this.cancelToken) {
this.cancelToken.cancel(this.cancelToken)
if (this.abortController) {
this.abortController.abort()
}

this.cancelToken = Axios.CancelToken.source()
this.abortController = new AbortController()
},

createVisitId() {
Expand All @@ -51,35 +59,34 @@ export default {
this.cancelActiveVisits()
let visitId = this.createVisitId()

return Axios({
return window.fetch(url, {
method,
url,
data: method.toLowerCase() === 'get' ? {} : data,
params: method.toLowerCase() === 'get' ? data : {},
cancelToken: this.cancelToken.token,
...this.hasBody(method) ? { body: JSON.stringify(data) } : {},
signal: this.abortController.signal,
credentials: 'include',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
Accept: 'text/html, application/xhtml+xml',
'X-Requested-With': 'XMLHttpRequest',
'X-Inertia': true,
'X-Xsrf-Token': this.getCookieValue('XSRF-TOKEN'),
...(this.version ? { 'X-Inertia-Version': this.version } : {}),
},
}).then(response => {
if (this.isInertiaResponse(response)) {
return response.data
if (response.status === 409 && response.headers.has('x-inertia-location')) {
Progress.stop()
return this.hardVisit(true, response.headers.get('x-inertia-location'))
} else if (this.isInertiaResponse(response)) {
return response.json()
} else {
Modal.show(response.data)
response.text().then(data => {
Progress.stop()
Modal.show(data)
})
}
}).catch(error => {
if (Axios.isCancel(error)) {
if (error.name === 'AbortError') {
return
} else if (error.response.status === 409 && error.response.headers['x-inertia-location']) {
Progress.stop()
return this.hardVisit(true, error.response.headers['x-inertia-location'])
} else if (this.isInertiaResponse(error.response)) {
return error.response.data
} else if (error.response) {
Progress.stop()
Modal.show(error.response.data)
} else {
return Promise.reject(error)
}
Expand Down
Loading

0 comments on commit 76adc2c

Please sign in to comment.