Skip to content

Commit b2b13eb

Browse files
docs: add Diátaxis taxonomy labels to documentation categories
- Update sidebars.js with explicit category definitions - Modify how-to guides category description - Create reference/explanation category scaffolding - Align documentation structure with Diátaxis framework
1 parent f4f39b7 commit b2b13eb

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"label": "Explanation",
3+
"documentation_type": "explanation",
4+
"position": 5,
5+
"link": {
6+
"type": "generated-index",
7+
"description": "Conceptual guides explaining api-ts architecture and design decisions."
8+
}
9+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Decoding JSON from Headers, Query Parameters, and URL Parameters
2+
3+
Though we know headers, URL parameters, and query parameters will be received as a
4+
`string` or `string[]` value, due to a limitation in api-ts, `httpRequest` only accepts
5+
codecs that decode values starting from the `unknown` type. Consequently, decoding a
6+
header, URL parameter, or query parameter with a codec like `JsonFromString`, which can
7+
only decode values typed as `string`, will produce a error like:
8+
9+
```
10+
Type 'Type<Json, string, string>' is not assignable to type 'Mixed'.
11+
Types of property 'validate' are incompatible.
12+
Type 'Validate<string, Json>' is not assignable to type 'Validate<unknown, any>'.
13+
Type 'unknown' is not assignable to type 'string'.
14+
```
15+
16+
There's a straightforward pattern you can use when you have a value typed as `unknown`
17+
but need to decode it with a codec that can only decode a narrower type. This pattern is
18+
called <em>codec chaining</em>:
19+
20+
```typescript
21+
declare const JsonFromString: t.Type<Json, string, string>;
22+
declare const t.string: t.Type<string, string, unknown>;
23+
24+
const myCodec: t.Type<Json, string, unknown> = t.string.pipe(JsonFromString);
25+
```
26+
27+
Here, `t.string` decodes a value from `unknown` to `string`, and then `JsonFromString`
28+
decodes the same value from `string` to `Json`.
29+
30+
For example:
31+
32+
```typescript
33+
import * as t from 'io-ts';
34+
import { nonEmptyArray, JsonFromString, NumberFromString } from 'io-ts-types';
35+
import { httpRequest, optional } from '@api-ts/io-ts-http';
36+
37+
// Define the Filter type for the JSON string
38+
const Filter = t.type({
39+
category: t.string,
40+
tags: t.array(t.string),
41+
price: t.type({
42+
min: t.number,
43+
max: t.number,
44+
}),
45+
});
46+
47+
// Define the SearchRequest codec
48+
const SearchRequest = httpRequest({
49+
params: {
50+
userId: NumberFromString,
51+
},
52+
query: {
53+
q: t.string,
54+
filter: t.string.pipe(JsonFromString).pipe(Filter),
55+
tags: nonEmptyArray(t.string),
56+
sort: optional(t.string),
57+
},
58+
headers: {
59+
authorization: t.string,
60+
},
61+
});
62+
63+
// Example request object
64+
const example = {
65+
params: {
66+
userId: '84938492',
67+
},
68+
query: {
69+
q: 'test',
70+
filter:
71+
'{"category":"books","tags":["crypto","trading"],"price":{"min":10,"max":50}}',
72+
tags: ['tag1', 'tag2', 'tag3'],
73+
sort: 'price',
74+
},
75+
headers: {
76+
authorization: 'Bearer token',
77+
},
78+
};
79+
80+
// Decode the example
81+
const decoded = SearchRequest.decode(example);
82+
if (decoded._tag === 'Right') {
83+
console.log(decoded);
84+
/*
85+
Expected decoded output
86+
{
87+
userId: 84938492,
88+
q: 'test',
89+
filter: {
90+
category: 'books',
91+
tags: ['crypto', 'trading'],
92+
price: { min: 10, max: 50 },
93+
},
94+
tags: ['tag1', 'tag2', 'tag3'],
95+
sort: 'price',
96+
authorization: 'Bearer token',
97+
};
98+
*/
99+
} else {
100+
console.error('Decoding failed:', decoded.left);
101+
}
102+
```

website/docs/how-to-guides/_category_.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"label": "How-To Guides",
3+
"documentation_type": "how-to_guide",
34
"position": 3,
45
"link": {
56
"type": "generated-index",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"label": "Reference",
3+
"documentation_type": "reference",
4+
"position": 4,
5+
"link": {
6+
"type": "generated-index",
7+
"description": "Technical reference documentation for api-ts components"
8+
}
9+
}

website/docs/tutorial-basics/_category_.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"label": "Tutorial - Basics",
3+
"documentation_type": "tutorial",
34
"position": 2,
45
"link": {
56
"type": "generated-index",

0 commit comments

Comments
 (0)