Commit 928a5fd
authored
Append query params to url if provided (#28)
If a user wants to request a url with query params, they have to build the URL with query params on their own with `URLSearchParams` or something similar.
For example, a dynamic country / state select you might do the following to update the state select field after choosing a country:
```javascript
async updateStateSelect(event) {
const value = event.target.selectedOptions[0].value
const query = new URLSearchParams({ country: value })
const response = await get(`/addresses/states?${query}`, {
responseKind: "turbo-stream"
})
}
```
This PR adds a `query` option that will automatically be appended to the URL if provided. The above example can then become the following:
```javascript
async updateStateSelect(event) {
const response = await get("/addresses/states", {
query: { country: event.target.selectedOptions[0].value },
responseKind: "turbo-stream"
})
}
```
Both examples will make requests to `/addresses/states?country=us`.
I also updated the README to document all the request options a bit clearer (and documented `query`). Originally, I named the option `params` but thought `query` was clearer.1 parent 1e04b60 commit 928a5fd
2 files changed
+47
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
72 | 69 | | |
73 | 70 | | |
74 | | - | |
| 71 | + | |
75 | 72 | | |
76 | | - | |
| 73 | + | |
77 | 74 | | |
78 | | - | |
| 75 | + | |
79 | 76 | | |
80 | | - | |
| 77 | + | |
81 | 78 | | |
82 | 79 | | |
83 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
84 | 95 | | |
85 | 96 | | |
86 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
96 | 114 | | |
97 | 115 | | |
98 | 116 | | |
| |||
0 commit comments