-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add JSON parsing examples #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8625f5c
feat: Add JSON parsing examples
youngjungithub b85867b
chore: use JSONFromString()
youngjungithub eb63dde
chore: add new line
youngjungithub 88d072b
chore: nix flake
youngjungithub 49c0f49
chore: skill issues
youngjungithub 4f5a3b4
chore: real skill issues
youngjungithub 941e1f1
chore: sus code
youngjungithub acb177c
Update httpRequest.ts
youngjungithub cf06230
nix flake
youngjungithub b1b3067
feat: parse, don't vaidate
youngjungithub 0cf9942
chore: nix
youngjungithub e64f7db
chore: nix
youngjungithub 6214c8f
chore: nix
youngjungithub 5653fd9
fix: apply doc feedback
youngjungithub f228a9a
fix: example code
youngjungithub be189c6
chore: add
youngjungithub 934a562
fix: apply feedback
youngjungithub 0529117
fix: apply feedback
youngjungithub 3ac6957
fix: apply feedback
youngjungithub 06ce95e
fix: title
youngjungithub e485b8a
fix: apply feedback
youngjungithub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| { | ||
| "label": "How-To Guide", | ||
| "label": "How-To Guides", | ||
| "position": 3, | ||
| "link": { | ||
| "type": "generated-index" | ||
| "type": "generated-index", | ||
| "description": "5 minutes to practice the most important api-ts concepts." | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Decoding JSON from Headers, Query Parameters, and URL Parameters | ||
|
|
||
| Though we know headers, url parameters, and query parameters will be received as a | ||
| `string` or `string[]` value, due to a limitation in api-ts, `httpRequest` only accepts | ||
| codecs that decode values starting from the `unknown` type. Consequently, decoding a | ||
| header, url parameter, or query parameter with a codec like `JsonFromString`, which can | ||
| only decode values typed as `string`, will produce a error like: | ||
|
|
||
| ``` | ||
| Type 'Type<Json, string, string>' is not assignable to type 'Mixed'. | ||
| Types of property 'validate' are incompatible. | ||
| Type 'Validate<string, Json>' is not assignable to type 'Validate<unknown, any>'. | ||
| Type 'unknown' is not assignable to type 'string'. | ||
| ``` | ||
|
|
||
| There's a straightforward pattern you can use when you have a value typed as `unknown` | ||
| but need to decode it with a codec that can only decode a narrower type. This pattern is | ||
| called <em>codec chaining</em>: | ||
|
|
||
| ```typescript | ||
| declare const JsonFromString: t.Type<Json, string, string>; | ||
| declare const t.string: t.Type<string, string, unknown>; | ||
|
|
||
| const myCodec: t.Type<Json, string, unknown> = t.string.pipe(JsonFromString); | ||
| ``` | ||
|
|
||
| Here, `t.string` decodes a value from `unknown` to `string`, and then `JsonFromString` | ||
| decodes the same value from `string` to `Json`. | ||
|
|
||
| For example: | ||
|
|
||
| ```typescript | ||
| import * as t from 'io-ts'; | ||
| import { nonEmptyArray, JsonFromString, NumberFromString } from 'io-ts-types'; | ||
| import { httpRequest, optional } from '@api-ts/io-ts-http'; | ||
|
|
||
| // Define the Filter type for the JSON string | ||
| const Filter = t.type({ | ||
| category: t.string, | ||
| tags: t.array(t.string), | ||
| price: t.type({ | ||
| min: t.number, | ||
| max: t.number, | ||
| }), | ||
| }); | ||
|
|
||
| // Define the SearchRequest codec | ||
| const SearchRequest = httpRequest({ | ||
| params: { | ||
| userId: NumberFromString, | ||
| }, | ||
| query: { | ||
| q: t.string, | ||
| filter: t.string.pipe(JsonFromString).pipe(Filter), | ||
| tags: nonEmptyArray(t.string), | ||
| sort: optional(t.string), | ||
| }, | ||
| headers: { | ||
| authorization: t.string, | ||
| }, | ||
| }); | ||
|
|
||
| // Example request object | ||
| const example = { | ||
| params: { | ||
| userId: '84938492', | ||
| }, | ||
| query: { | ||
| q: 'test', | ||
| filter: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: this is a great example! |
||
| '{"category":"books","tags":["crypto","trading"],"price":{"min":10,"max":50}}', | ||
| tags: ['tag1', 'tag2', 'tag3'], | ||
| sort: 'price', | ||
| }, | ||
| headers: { | ||
| authorization: 'Bearer token', | ||
| }, | ||
| }; | ||
|
|
||
| // Decode the example | ||
| const decoded = SearchRequest.decode(example); | ||
| if (decoded._tag === 'Right') { | ||
| console.log(decoded); | ||
| /* | ||
| Expected decoded output | ||
| { | ||
| userId: 84938492, | ||
| q: 'test', | ||
| filter: { | ||
| category: 'books', | ||
| tags: ['crypto', 'trading'], | ||
| price: { min: 10, max: 50 }, | ||
| }, | ||
| tags: ['tag1', 'tag2', 'tag3'], | ||
| sort: 'price', | ||
| authorization: 'Bearer token', | ||
| }; | ||
| */ | ||
| } else { | ||
| console.error('Decoding failed:', decoded.left); | ||
| } | ||
| ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: Love it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.