Skip to content

Commit 4963f1b

Browse files
author
HTTP Query Contributor
committed
add more doc to modes
1 parent 82e1388 commit 4963f1b

6 files changed

Lines changed: 220 additions & 17 deletions

File tree

‎packages/client/README.md‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ DX-focused entry point of the [`@kevincii/http-query`](https://github.com/Kevinc
55
ecosystem. Re-exports the full [`@kevincii/http-query-core`](https://github.com/Kevinci/http-query/tree/main/packages/core)
66
API and ships a preconfigured default client.
77

8-
> **Browser support:** As of 2026 browsers cannot send the `QUERY` method via
9-
> `fetch`, so the client automatically falls back to `POST` (or `GET`, which is
10-
> serialized to a query string). In Node.js (≥20) `QUERY` is sent natively.
8+
> **Browser & QUERY support:** `QUERY` is a valid `fetch` method —
9+
> `fetch(url, { method: "QUERY", body })` works in current browsers (it is neither a
10+
> forbidden nor a normalized method) and in Node.js (≥20). The practical caveats:
11+
> cross-origin QUERY always triggers a CORS preflight (it is not safelisted), browsers
12+
> don't cache QUERY responses yet, HTML `<form>`s fall back to GET, and many
13+
> servers/proxies/CDNs don't handle QUERY yet. That's why the client keeps an automatic
14+
> `POST` (or `GET`, serialized to a query string) fallback — send QUERY where it's
15+
> supported, degrade gracefully everywhere else.
1116
1217
## Install
1318

@@ -35,6 +40,68 @@ const page = await api.query<User[]>("/users", {
3540
const data = await query("/users", { sort: "name" });
3641
```
3742

43+
## Query modes — `auto` vs `params`
44+
45+
The `mode` option controls how `query()` puts parameters on the wire. Set it once on the client or override per request.
46+
47+
### `"auto"` (default) — QUERY-first with automatic fallback
48+
49+
Sends an HTTP `QUERY` request with a JSON body. If the server responds with `405` or `501`, the client automatically retries with the fallback method (`"POST"` by default, then `"GET"` with a serialized query string).
50+
51+
```ts
52+
const client = createBrowserClient({ baseUrl: "https://api.example.com" });
53+
// mode: "auto", fallback: "POST" are the defaults
54+
55+
// 1st try → QUERY /users body: { "filter": { "age": { "gte": 18 } }, "sort": "name" }
56+
// Server returns 405 → retry automatically
57+
// 2nd try → POST /users body: { "filter": { "age": { "gte": 18 } }, "sort": "name" }
58+
const users = await client.query<User[]>("/users", {
59+
filter: { age: { gte: 18 } },
60+
sort: "name",
61+
});
62+
```
63+
64+
Use `"auto"` for most apps — it sends the richest possible request and degrades gracefully.
65+
66+
### `"params"` — always GET with a query string
67+
68+
Every call becomes a plain `GET`. Parameters are serialized into the URL using bracket notation. No body is sent. Use this for servers that only handle GET, CDN-cacheable reads, or shallow params.
69+
70+
```ts
71+
const client = createBrowserClient({ baseUrl: "https://api.example.com", mode: "params" });
72+
73+
// GET /users?page=1&sort=name&filter[age][gte]=18&filter[country][in]=DE&filter[country][in]=AT
74+
await client.query<User[]>("/users", {
75+
page: 1,
76+
sort: "name",
77+
filter: {
78+
age: { gte: 18 },
79+
country: { in: ["DE", "AT"] },
80+
},
81+
});
82+
```
83+
84+
### Per-request override
85+
86+
Pass `mode` as part of the third argument to override the client default for a single call:
87+
88+
```ts
89+
// Client defaults to "params", but this one call uses QUERY/POST
90+
await client.query("/search", { q: "Ada" }, { mode: "auto" });
91+
92+
// Client defaults to "auto", but this call forces a plain GET
93+
await client.query("/config", { locale: "de" }, { mode: "params" });
94+
```
95+
96+
### When to use which
97+
98+
| Situation | Mode |
99+
|---|---|
100+
| General-purpose app, server may not support QUERY | `"auto"` |
101+
| Server only accepts GET, or you need CDN caching | `"params"` |
102+
| Deeply nested filters (bracket notation gets long) | `"auto"` (JSON body stays clean) |
103+
| Shallow params, bookmarkable URLs | `"params"` |
104+
38105
## Features
39106

40107
- **Type-safe `query()`** — pass a params object, get a typed response.

‎packages/client/package.json‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
"module": "dist/index.mjs",
3131
"types": "dist/index.d.ts",
3232
"sideEffects": false,
33-
"files": ["dist", "LICENSE", "README.md"],
33+
"files": [
34+
"dist",
35+
"LICENSE",
36+
"README.md"
37+
],
3438
"scripts": {
3539
"build": "tsup",
3640
"test": "vitest",
@@ -57,5 +61,8 @@
5761
"tsup": "^8.0.0",
5862
"typescript": "^5.4.0",
5963
"vitest": "^1.6.0"
64+
},
65+
"publishConfig": {
66+
"access": "public"
6067
}
6168
}

‎packages/core/package.json‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
"module": "dist/index.mjs",
3131
"types": "dist/index.d.ts",
3232
"sideEffects": false,
33-
"files": ["dist", "LICENSE", "README.md"],
33+
"files": [
34+
"dist",
35+
"LICENSE",
36+
"README.md"
37+
],
3438
"scripts": {
3539
"build": "tsup",
3640
"test": "vitest",
@@ -51,5 +55,8 @@
5155
"tsup": "^8.0.0",
5256
"typescript": "^5.4.0",
5357
"vitest": "^1.6.0"
58+
},
59+
"publishConfig": {
60+
"access": "public"
5461
}
5562
}

‎packages/docs/index.html‎

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<li class="group">Core</li>
115115
<li><a href="#core">Getting started</a></li>
116116
<li><a href="#core-query">query()</a></li>
117+
<li><a href="#core-query-modes">Query modes</a></li>
117118
<li><a href="#core-filters">Filters</a></li>
118119
<li><a href="#core-serialize">Serialization</a></li>
119120
<li><a href="#core-pagination">Pagination</a></li>
@@ -236,18 +237,107 @@ <h3>query() vs request()</h3>
236237

237238
// Low-level — explicit method and body (e.g. writes)
238239
await client.request&lt;User&gt;("/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&amp;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&amp;sort=name&amp;filter[age][gte]=18&amp;filter[country][in]=DE&amp;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&amp;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>
240331
<table>
241-
<thead><tr><th>mode</th><th>Behavior</th></tr></thead>
332+
<thead><tr><th>Situation</th><th>Recommended mode</th></tr></thead>
242333
<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>
246339
</tbody>
247340
</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&amp;country=DE</code></pre>
251341
</section>
252342

253343
<section id="core-filters">

‎packages/next/package.json‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@
1313
"directory": "packages/next"
1414
},
1515
"homepage": "https://github.com/Kevinci/http-query/tree/main/packages/next#readme",
16-
"keywords": ["http", "query", "next", "nextjs", "rsc", "route-handler", "server", "typescript"],
16+
"keywords": [
17+
"http",
18+
"query",
19+
"next",
20+
"nextjs",
21+
"rsc",
22+
"route-handler",
23+
"server",
24+
"typescript"
25+
],
1726
"type": "module",
1827
"main": "dist/index.cjs",
1928
"module": "dist/index.mjs",
2029
"types": "dist/index.d.ts",
2130
"sideEffects": false,
22-
"files": ["dist", "LICENSE", "README.md"],
31+
"files": [
32+
"dist",
33+
"LICENSE",
34+
"README.md"
35+
],
2336
"scripts": {
2437
"build": "tsup",
2538
"test": "vitest",
@@ -49,5 +62,8 @@
4962
"tsup": "^8.0.0",
5063
"typescript": "^5.4.0",
5164
"vitest": "^1.6.0"
65+
},
66+
"publishConfig": {
67+
"access": "public"
5268
}
5369
}

‎packages/react/package.json‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@
1313
"directory": "packages/react"
1414
},
1515
"homepage": "https://github.com/Kevinci/http-query/tree/main/packages/react#readme",
16-
"keywords": ["http", "query", "react", "hooks", "fetch", "typescript", "cache", "pagination"],
16+
"keywords": [
17+
"http",
18+
"query",
19+
"react",
20+
"hooks",
21+
"fetch",
22+
"typescript",
23+
"cache",
24+
"pagination"
25+
],
1726
"type": "module",
1827
"main": "dist/index.cjs",
1928
"module": "dist/index.mjs",
2029
"types": "dist/index.d.ts",
2130
"sideEffects": false,
22-
"files": ["dist", "LICENSE", "README.md"],
31+
"files": [
32+
"dist",
33+
"LICENSE",
34+
"README.md"
35+
],
2336
"scripts": {
2437
"build": "tsup",
2538
"test": "vitest",
@@ -51,5 +64,8 @@
5164
"tsup": "^8.0.0",
5265
"typescript": "^5.4.0",
5366
"vitest": "^1.6.0"
67+
},
68+
"publishConfig": {
69+
"access": "public"
5470
}
5571
}

0 commit comments

Comments
 (0)