Skip to content

Append query params to url if provided #28

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 6 commits into from
Aug 1, 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
45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,44 @@ async myMethod () {
}
```

#### Request body
#### Request Options

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.
You can pass options to a request as the last argument. For example:

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

...

async myMethod () {
const response = await post('localhost:3000/my_endpoint', { body: { name: 'Request.JS' } })
if (response.ok) {
...
}
}
```javascript
post("/my_endpoint", {
body: {},
contentType: "application/json",
headers: {},
query: {},
responseKind: "html"
})
```

### responseKind
##### body

You can provide this option to specify which kind of response will be accepted. Default is `html`.
This is the `body` for POST requests. You can pass in a Javascript object, FormData, Files, strings, etc.

Options are `html`, `turbo-stream`, `json`.
Request.js will automatically JSON stringify the `body` if the content type is `application/json`.

### contentType
##### contentType

When provided this value will be sent in the `Content-Type` header. When not provided Request.JS will send nothing when the `body` of the request is `null` or an instance of `FormData`, when the `body` is an instance of a `File` then the type of the file will be sent and `application/json` will be sent if none of the prior conditions matches.

##### headers

Adds additional headers to the request. `X-CSRF-Token` and `Content-Type` are automatically included.

##### query

Appends query parameters to the URL. Query params in the URL are preserved and merged with the query options.

##### responseKind

Specifies which response format will be accepted. Default is `html`.

Options are `html`, `turbo-stream`, `json`.

#### Turbo Streams

Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the `window.Turbo` global variable:
Expand Down
20 changes: 19 additions & 1 deletion src/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getCookie, compact, metaContent } from './lib/utils'
export class FetchRequest {
constructor (method, url, options = {}) {
this.method = method
this.url = url
this.options = options
this.originalUrl = url
}

async perform () {
Expand Down Expand Up @@ -93,6 +93,24 @@ export class FetchRequest {
return this.options.body
}

get query () {
const originalQuery = (this.originalUrl.split('?')[1] || '').split('#')[0]
const params = new URLSearchParams(originalQuery)

if (this.options.query) {
for (const [key, value] of Object.entries(this.options.query)) {
params.append(key, value)
}
}

const query = params.toString()
return (query.length > 0 ? `?${query}` : '')
}

get url () {
return this.originalUrl.split('?')[0] + this.query
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably wouldn't matter for ajax calls, but a side effect of this is if the url has an anchor in it (for whatever reason), it would be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I didn't see any reason to keep the anchor for AJAX requests. I don't know of any use case for it server-side actually.

}

get responseKind () {
return this.options.responseKind || 'html'
}
Expand Down