|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +# `httpRequest` |
| 6 | + |
| 7 | +### Overview |
| 8 | + |
| 9 | +`httpRequest` is a helper function that builds `io-ts` codecs specifically for HTTP |
| 10 | +requests. It defines the expected structure of path parameters, query parameters, |
| 11 | +headers, and the request body. The resulting codec flattens these distinct parts into a |
| 12 | +single object upon successful decoding. |
| 13 | + |
| 14 | +### Specification |
| 15 | + |
| 16 | +Accepts a single optional argument: an object that can contain the following optional |
| 17 | +properties: |
| 18 | + |
| 19 | +- `params`: (`object`) An object that maps path parameter names (strings matching |
| 20 | + `{name}` syntax in `httpRoute` path) to `io-ts` codecs for validation and type |
| 21 | + conversion. |
| 22 | +- `query`: (`object`) An object that maps query parameter names (strings) to `io-ts` |
| 23 | + codecs. |
| 24 | +- `headers`: (`object`) An object that maps HTTP header names (lowercase strings) to |
| 25 | + `io-ts` codecs. |
| 26 | +- `body`: (`object` | `io-ts` Codec) An object that maps field names within the request |
| 27 | + body to `io-ts` codecs. Assumes an object structure by default. (See Limitations). |
| 28 | + |
| 29 | +The function returns an `io-ts` codec. |
| 30 | + |
| 31 | +### Behavior |
| 32 | + |
| 33 | +- Decoding: Takes an input object that conforms to `GenericHttpRequest` (see below). |
| 34 | + Validates and parses the `params`, `query`, `headers`, and `body` based on the |
| 35 | + provided codecs. If successful, returns a flattened object that contains all decoded |
| 36 | + properties directly. |
| 37 | +- Encoding: Takes a flattened object (matching the decoded type). Encodes the properties |
| 38 | + back into the structured `GenericHttpRequest` format, suitable for sending as a |
| 39 | + request. |
| 40 | + |
| 41 | +### Decoded Type Structure |
| 42 | + |
| 43 | +The `t.TypeOf` of the resulting codec is a flat object that contains properties from |
| 44 | +`params`, `query`, `headers`, and `body` combined. |
| 45 | + |
| 46 | +```typescript |
| 47 | +import * as t from 'io-ts'; |
| 48 | +import { httpRequest } from '@api-ts/io-ts-http'; |
| 49 | +import { NumberFromString, DateFromISOString } from 'io-ts-types'; // Example types |
| 50 | + |
| 51 | +const ExampleRequestCodec = httpRequest({ |
| 52 | + params: { |
| 53 | + id: NumberFromString, // from path '/.../{id}' |
| 54 | + }, |
| 55 | + query: { |
| 56 | + filter: t.string, // from query '?filter=...' |
| 57 | + }, |
| 58 | + body: { |
| 59 | + content: t.string, |
| 60 | + timestamp: DateFromISOString, |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | +// The resulting decoded type: |
| 65 | +type ExampleDecoded = t.TypeOf<typeof ExampleRequestCodec>; |
| 66 | +// Equivalent to: |
| 67 | +// type ExampleDecoded = { |
| 68 | +// id: number; // from params |
| 69 | +// filter: string; // from query |
| 70 | +// content: string; // from body |
| 71 | +// timestamp: Date; // from body |
| 72 | +// }; |
| 73 | +``` |
| 74 | + |
| 75 | +### Limitations |
| 76 | + |
| 77 | +Assumes the request `body`, if present and defined via the shorthand object syntax, is |
| 78 | +an object whose properties can be flattened. For non-object bodies, see the advanced |
| 79 | +usage notes under `httpRoute`. |
| 80 | + |
| 81 | +### Usage Examples |
| 82 | + |
| 83 | +- Query Parameters Only: |
| 84 | + |
| 85 | +```typescript |
| 86 | +const RequestWithQuery = httpRequest({ |
| 87 | + query: { message: t.string, count: NumberFromString }, |
| 88 | +}); |
| 89 | +// Decoded type: { message: string; count: number } |
| 90 | +``` |
| 91 | + |
| 92 | +- Path and body parameters: |
| 93 | + |
| 94 | +```typescript |
| 95 | +const RequestWithPathAndBody = httpRequest({ |
| 96 | + params: { userId: t.string }, |
| 97 | + body: { data: t.unknown }, |
| 98 | +}); |
| 99 | +// Decoded type: { userId: string; data: unknown } |
| 100 | +``` |
0 commit comments