Skip to content

Add support to stringify automatically the body of the request if the type of it isn't a string #24

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 18, 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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ async myMethod () {
}
```

#### Request body

Instead of stringify the body of a request which the content type is `application/json`, you can just pass the javascript object as the body of the request and Request.JS will stringify it for you.

```js
import { post } from '@rails/request.js'

...

async myMethod () {
const response = await post('localhost:3000/my_endpoint', { body: { name: 'Request.JS' } })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need responseKind: 'json' to trigger the stringify?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is required is the Content-Type to be application/json and this is the default content type in this scenario https://github.com/rails/request.js/blob/main/src/fetch_request.js#L73

Copy link
Contributor

@excid3 excid3 Jun 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I didn't catch json as the default content type. 👍

Haven't had any caffeine yet this morning. 😅

if (response.ok) {
...
}
}
```


#### Turbo Streams

Expand Down
18 changes: 16 additions & 2 deletions src/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export class FetchRequest {
return Promise.reject(window.location.href = response.authenticationURL)
}

if (response.ok && response.isTurboStream) { response.renderTurboStream() }
if (response.ok && response.isTurboStream) {
response.renderTurboStream()
}

return response
}

Expand All @@ -38,7 +41,7 @@ export class FetchRequest {
return {
method: this.method.toUpperCase(),
headers: this.headers,
body: this.body,
body: this.formattedBody,
signal: this.signal,
credentials: 'same-origin',
redirect: 'follow'
Expand Down Expand Up @@ -101,4 +104,15 @@ export class FetchRequest {
get additionalHeaders () {
return this.options.headers || {}
}

get formattedBody () {
const bodyIsAString = Object.prototype.toString.call(this.body) === '[object String]'
const contentTypeIsJson = this.headers['Content-Type'] === 'application/json'

if (contentTypeIsJson && !bodyIsAString) {
return JSON.stringify(this.body)
}

return this.body
}
}