Skip to content

Removing useless else statements #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 7 additions & 6 deletions src/fetch_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class FetchResponse {

get contentType () {
const contentType = this.response.headers.get('Content-Type') || ''

return contentType.replace(/;.*$/, '')
}

Expand All @@ -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 () {
Expand All @@ -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`))
}
}
5 changes: 4 additions & 1 deletion src/lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}