diff --git a/src/fetch_request.js b/src/fetch_request.js index 585990a..ab78dd4 100644 --- a/src/fetch_request.js +++ b/src/fetch_request.js @@ -10,12 +10,13 @@ export class FetchRequest { async perform () { const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions)) + if (response.unauthenticated && response.authenticationURL) { return Promise.reject(window.location.href = response.authenticationURL) - } else { - if (response.ok && response.isTurboStream) { response.renderTurboStream() } - return response } + + if (response.ok && response.isTurboStream) { response.renderTurboStream() } + return response } get fetchOptions () { @@ -52,9 +53,9 @@ export class FetchRequest { return undefined } else if (this.body instanceof window.File) { return this.body.type - } else { - return 'application/json' } + + return 'application/json' } get accept () { @@ -89,12 +90,14 @@ export class FetchRequest { function compact (object) { const result = {} + for (const key in object) { const value = object[key] if (value !== undefined) { result[key] = value } } + return result } diff --git a/src/fetch_response.js b/src/fetch_response.js index 5587fad..6ae3b83 100644 --- a/src/fetch_response.js +++ b/src/fetch_response.js @@ -21,6 +21,7 @@ export class FetchResponse { get contentType () { const contentType = this.response.headers.get('Content-Type') || '' + return contentType.replace(/;.*$/, '') } @@ -31,17 +32,17 @@ export class FetchResponse { get html () { if (this.contentType.match(/^(application|text)\/(html|xhtml\+xml)$/)) { return this.response.text() - } else { - return Promise.reject(new Error(`Expected an HTML response but got "${this.contentType}" instead`)) } + + return Promise.reject(new Error(`Expected an HTML response but got "${this.contentType}" instead`)) } get json () { if (this.contentType.match(/^application\/json/)) { return this.response.json() - } else { - return Promise.reject(new Error(`Expected a JSON response but got "${this.contentType}" instead`)) } + + return Promise.reject(new Error(`Expected a JSON response but got "${this.contentType}" instead`)) } get text () { @@ -59,8 +60,8 @@ export class FetchResponse { } else { console.warn('You must set `window.Turbo = Turbo` to automatically process Turbo Stream events with request.js') } - } else { - return Promise.reject(new Error(`Expected a Turbo Stream response but got "${this.contentType}" instead`)) } + + return Promise.reject(new Error(`Expected a Turbo Stream response but got "${this.contentType}" instead`)) } } diff --git a/src/lib/cookie.js b/src/lib/cookie.js index c220a9a..95f2a10 100644 --- a/src/lib/cookie.js +++ b/src/lib/cookie.js @@ -5,6 +5,9 @@ export function getCookie (name) { if (cookie) { const value = cookie.split('=').slice(1).join('=') - return value ? decodeURIComponent(value) : undefined + + if (value) { + return decodeURIComponent(value) + } } }