|
114 | 114 | <li class="group">Core</li> |
115 | 115 | <li><a href="#core">Getting started</a></li> |
116 | 116 | <li><a href="#core-query">query()</a></li> |
| 117 | + <li><a href="#core-query-modes">Query modes</a></li> |
117 | 118 | <li><a href="#core-filters">Filters</a></li> |
118 | 119 | <li><a href="#core-serialize">Serialization</a></li> |
119 | 120 | <li><a href="#core-pagination">Pagination</a></li> |
@@ -236,18 +237,107 @@ <h3>query() vs request()</h3> |
236 | 237 |
|
237 | 238 | // Low-level — explicit method and body (e.g. writes) |
238 | 239 | await client.request<User>("/users", { method: "POST", body: { name: "Ada" } });</code></pre> |
239 | | - <h4>How params reach the wire — the <code>mode</code> option</h4> |
| 240 | + </section> |
| 241 | + |
| 242 | + <section id="core-query-modes"> |
| 243 | + <h3>Query modes — <code>auto</code> vs <code>params</code></h3> |
| 244 | + <p> |
| 245 | + The <code>mode</code> option controls how <code>query()</code> puts parameters on the wire. |
| 246 | + Set it once when creating the client, or override it per request as a third argument. |
| 247 | + </p> |
| 248 | + <table> |
| 249 | + <thead><tr><th>mode</th><th>What goes on the wire</th><th>When to use</th></tr></thead> |
| 250 | + <tbody> |
| 251 | + <tr> |
| 252 | + <td><code>"auto"</code> (default)</td> |
| 253 | + <td>QUERY with JSON body → falls back to POST (JSON body) → GET (query string) on 405/501</td> |
| 254 | + <td>Most apps. Sends the richest possible request, degrades automatically.</td> |
| 255 | + </tr> |
| 256 | + <tr> |
| 257 | + <td><code>"query"</code></td> |
| 258 | + <td>Same as <code>auto</code> today — always attempts QUERY first</td> |
| 259 | + <td>When you control client and server and know QUERY is supported.</td> |
| 260 | + </tr> |
| 261 | + <tr> |
| 262 | + <td><code>"params"</code></td> |
| 263 | + <td>Plain GET with all params serialized into the query string — no body</td> |
| 264 | + <td>Servers that only speak GET, CDN-cacheable reads, or simple flat params.</td> |
| 265 | + </tr> |
| 266 | + </tbody> |
| 267 | + </table> |
| 268 | + |
| 269 | + <h4>Mode <code>"auto"</code> — QUERY-first with automatic fallback</h4> |
| 270 | + <p> |
| 271 | + This is the default. The client sends an HTTP <code>QUERY</code> request with a JSON body. |
| 272 | + If the server responds with <code>405 Method Not Allowed</code> or <code>501 Not Implemented</code>, |
| 273 | + the client automatically retries with the configured <code>fallback</code> method |
| 274 | + (<code>"POST"</code> by default). |
| 275 | + </p> |
| 276 | + <pre><code>import { createClient } from "@kevincii/http-query-core"; |
| 277 | + |
| 278 | +const client = createClient({ baseUrl: "https://api.example.com" }); |
| 279 | +// ↑ mode: "auto" and fallback: "POST" are the defaults |
| 280 | + |
| 281 | +// What the client sends on the wire: |
| 282 | +// 1st attempt → QUERY /users body: { "filter": { "age": { "gte": 18 } }, "sort": "name" } |
| 283 | +// Server returns 405 → retry automatically |
| 284 | +// 2nd attempt → POST /users body: { "filter": { "age": { "gte": 18 } }, "sort": "name" } |
| 285 | +const users = await client.query("/users", { |
| 286 | + filter: { age: { gte: 18 } }, |
| 287 | + sort: "name", |
| 288 | +}); |
| 289 | + |
| 290 | +// Override the fallback to GET (params serialized into the query string) |
| 291 | +const client2 = createClient({ baseUrl: "/api", fallback: "GET" }); |
| 292 | +// QUERY /users {...} → 405 → GET /users?filter[age][gte]=18&sort=name</code></pre> |
| 293 | + <div class="note"> |
| 294 | + <strong>Per-request override.</strong> Pass <code>{ mode: "auto" }</code> as the third argument to |
| 295 | + override the client-level mode for a single call without creating a new client. |
| 296 | + </div> |
| 297 | + <pre><code>// Client defaults to "params", but this one call uses auto |
| 298 | +await client.query("/search", { q: "Ada" }, { mode: "auto" });</code></pre> |
| 299 | + |
| 300 | + <h4>Mode <code>"params"</code> — always GET with a query string</h4> |
| 301 | + <p> |
| 302 | + Every call becomes a plain <code>GET</code> request. Parameters are serialized into the URL using |
| 303 | + bracket notation for nested objects and arrays. No request body is sent. |
| 304 | + Use this when the server only handles GET, when you need CDN-level caching, or when params are shallow enough to fit in a URL. |
| 305 | + </p> |
| 306 | + <pre><code>import { createClient } from "@kevincii/http-query-core"; |
| 307 | + |
| 308 | +const client = createClient({ baseUrl: "https://api.example.com", mode: "params" }); |
| 309 | + |
| 310 | +// GET /users?page=1&sort=name&filter[age][gte]=18&filter[country][in]=DE&filter[country][in]=AT |
| 311 | +await client.query("/users", { |
| 312 | + page: 1, |
| 313 | + sort: "name", |
| 314 | + filter: { |
| 315 | + age: { gte: 18 }, |
| 316 | + country: { in: ["DE", "AT"] }, |
| 317 | + }, |
| 318 | +}); |
| 319 | + |
| 320 | +// Tags use repeat format by default: GET /tags?tag=a&tag=b |
| 321 | +await client.query("/tags", { tag: ["a", "b"] }); |
| 322 | + |
| 323 | +// Switch to comma format for this request |
| 324 | +// GET /tags?tag=a,b |
| 325 | +await client.query("/tags", { tag: ["a", "b"] }, { |
| 326 | + mode: "params", |
| 327 | + serialize: { arrayFormat: "comma" }, |
| 328 | +});</code></pre> |
| 329 | + |
| 330 | + <h4>Choosing between the modes</h4> |
240 | 331 | <table> |
241 | | - <thead><tr><th>mode</th><th>Behavior</th></tr></thead> |
| 332 | + <thead><tr><th>Situation</th><th>Recommended mode</th></tr></thead> |
242 | 333 | <tbody> |
243 | | - <tr><td><code>"auto"</code> (default)</td><td>Send a QUERY request with a JSON body; fall back to POST (JSON body) then GET (query string) if the server rejects QUERY.</td></tr> |
244 | | - <tr><td><code>"query"</code></td><td>Always attempt the QUERY method first.</td></tr> |
245 | | - <tr><td><code>"params"</code></td><td>Always send a plain <code>GET</code> with a serialized query string — no body.</td></tr> |
| 334 | + <tr><td>General-purpose app, server may or may not support QUERY</td><td><code>"auto"</code></td></tr> |
| 335 | + <tr><td>You control both client and server, QUERY is enabled</td><td><code>"query"</code></td></tr> |
| 336 | + <tr><td>Server only accepts GET, or you need CDN caching</td><td><code>"params"</code></td></tr> |
| 337 | + <tr><td>Params are deeply nested — bracket notation gets long</td><td><code>"auto"</code> (JSON body stays clean)</td></tr> |
| 338 | + <tr><td>Params are shallow and you want bookmarkable URLs</td><td><code>"params"</code></td></tr> |
246 | 339 | </tbody> |
247 | 340 | </table> |
248 | | - <pre><code>const client = createClient({ baseUrl: "/api", mode: "params" }); |
249 | | -await client.query("/users", { filter: { age: { gte: 18 } }, country: "DE" }); |
250 | | -// GET /api/users?filter[age][gte]=18&country=DE</code></pre> |
251 | 341 | </section> |
252 | 342 |
|
253 | 343 | <section id="core-filters"> |
|
0 commit comments