|
| 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 | +``` |
0 commit comments