|
| 1 | +# Advanced HTTP route patterns |
| 2 | + |
| 3 | +Learn advanced patterns for defining HTTP routes with `io-ts-http` beyond the basic |
| 4 | +usage examples. |
| 5 | + |
| 6 | +## Work with non-object body types |
| 7 | + |
| 8 | +By default, `httpRequest` assumes the request body is a JSON object. Sometimes you need |
| 9 | +to accept other types like strings, numbers, or arrays. |
| 10 | + |
| 11 | +### Accept a string body |
| 12 | + |
| 13 | +Use `t.intersection` to combine `httpRequest` with a custom type that accepts a string |
| 14 | +body: |
| 15 | + |
| 16 | +```typescript |
| 17 | +import * as t from 'io-ts'; |
| 18 | +import { httpRoute, httpRequest } from '@api-ts/io-ts-http'; |
| 19 | + |
| 20 | +const StringBodyRoute = httpRoute({ |
| 21 | + path: '/example/{id}', |
| 22 | + method: 'POST', |
| 23 | + request: t.intersection([ |
| 24 | + httpRequest({ |
| 25 | + params: { id: t.string }, |
| 26 | + }), |
| 27 | + t.type({ |
| 28 | + body: t.string, |
| 29 | + }), |
| 30 | + ]), |
| 31 | + response: { |
| 32 | + 200: t.string, |
| 33 | + }, |
| 34 | +}); |
| 35 | + |
| 36 | +// Decoded type |
| 37 | +type DecodedType = { |
| 38 | + id: string; |
| 39 | + body: string; |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | +This approach breaks the abstraction slightly by exposing a `body` property in the |
| 44 | +decoded type, but it works effectively. |
| 45 | + |
| 46 | +### Accept an array body |
| 47 | + |
| 48 | +Similarly, accept an array body: |
| 49 | + |
| 50 | +```typescript |
| 51 | +const ArrayBodyRoute = httpRoute({ |
| 52 | + path: '/batch-process', |
| 53 | + method: 'POST', |
| 54 | + request: t.intersection([ |
| 55 | + httpRequest({}), |
| 56 | + t.type({ |
| 57 | + body: t.array(t.string), |
| 58 | + }), |
| 59 | + ]), |
| 60 | + response: { |
| 61 | + 200: t.type({ |
| 62 | + processed: t.number, |
| 63 | + }), |
| 64 | + }, |
| 65 | +}); |
| 66 | + |
| 67 | +// Decoded type |
| 68 | +type DecodedType = { |
| 69 | + body: string[]; |
| 70 | +}; |
| 71 | +``` |
| 72 | + |
| 73 | +## Create conditional request parameters |
| 74 | + |
| 75 | +Sometimes you need parameters that are required only when other parameters have specific |
| 76 | +values. |
| 77 | + |
| 78 | +### Use union types |
| 79 | + |
| 80 | +Use `t.union` with multiple `httpRequest` objects to handle conditional parameters: |
| 81 | + |
| 82 | +```typescript |
| 83 | +const SearchRoute = httpRoute({ |
| 84 | + path: '/search', |
| 85 | + method: 'GET', |
| 86 | + request: t.union([ |
| 87 | + // When searching by keyword |
| 88 | + httpRequest({ |
| 89 | + query: { |
| 90 | + type: t.literal('keyword'), |
| 91 | + keyword: t.string, |
| 92 | + }, |
| 93 | + }), |
| 94 | + // When searching by category |
| 95 | + httpRequest({ |
| 96 | + query: { |
| 97 | + type: t.literal('category'), |
| 98 | + categoryId: NumberFromString, |
| 99 | + }, |
| 100 | + }), |
| 101 | + // When searching by both |
| 102 | + httpRequest({ |
| 103 | + query: { |
| 104 | + type: t.literal('combined'), |
| 105 | + keyword: t.string, |
| 106 | + categoryId: NumberFromString, |
| 107 | + }, |
| 108 | + }), |
| 109 | + ]), |
| 110 | + response: { |
| 111 | + 200: t.array( |
| 112 | + t.type({ |
| 113 | + id: t.string, |
| 114 | + title: t.string, |
| 115 | + }), |
| 116 | + ), |
| 117 | + }, |
| 118 | +}); |
| 119 | + |
| 120 | +// Decoded type will be a union: |
| 121 | +type DecodedType = |
| 122 | + | { type: 'keyword'; keyword: string } |
| 123 | + | { type: 'category'; categoryId: number } |
| 124 | + | { type: 'combined'; keyword: string; categoryId: number }; |
| 125 | +``` |
| 126 | + |
| 127 | +## Add optional headers |
| 128 | + |
| 129 | +HTTP headers are often optional. Use the `optional` combinator to define optional |
| 130 | +headers: |
| 131 | + |
| 132 | +```typescript |
| 133 | +import { httpRequest, optional } from '@api-ts/io-ts-http'; |
| 134 | + |
| 135 | +const RequestWithOptionalHeaders = httpRoute({ |
| 136 | + path: '/resource', |
| 137 | + method: 'GET', |
| 138 | + request: httpRequest({ |
| 139 | + headers: { |
| 140 | + authorization: t.string, |
| 141 | + 'cache-control': optional(t.string), |
| 142 | + 'if-modified-since': optional(t.string), |
| 143 | + }, |
| 144 | + }), |
| 145 | + response: { |
| 146 | + 200: t.object, |
| 147 | + }, |
| 148 | +}); |
| 149 | + |
| 150 | +// Decoded type |
| 151 | +type DecodedType = { |
| 152 | + authorization: string; |
| 153 | + 'cache-control'?: string; |
| 154 | + 'if-modified-since'?: string; |
| 155 | +}; |
| 156 | +``` |
| 157 | + |
| 158 | +## Handle file uploads |
| 159 | + |
| 160 | +File uploads typically use `multipart/form-data` encoding. While `io-ts-http` doesn't |
| 161 | +directly support file uploads, you can treat the file as an opaque object in the type |
| 162 | +system and handle the file processing separately: |
| 163 | + |
| 164 | +```typescript |
| 165 | +const FileUploadRoute = httpRoute({ |
| 166 | + path: '/upload', |
| 167 | + method: 'POST', |
| 168 | + request: httpRequest({ |
| 169 | + body: { |
| 170 | + // In the type system, just indicate a file is expected |
| 171 | + // Your server framework will handle the actual file |
| 172 | + file: t.unknown, |
| 173 | + description: optional(t.string), |
| 174 | + }, |
| 175 | + }), |
| 176 | + response: { |
| 177 | + 200: t.type({ |
| 178 | + fileId: t.string, |
| 179 | + size: t.number, |
| 180 | + }), |
| 181 | + }, |
| 182 | +}); |
| 183 | +``` |
| 184 | + |
| 185 | +## Combine multiple request sources |
| 186 | + |
| 187 | +Sometimes you need to extract information from multiple sources, such as getting an ID |
| 188 | +from the path, authentication from headers, and data from the body: |
| 189 | + |
| 190 | +```typescript |
| 191 | +const ComplexRoute = httpRoute({ |
| 192 | + path: '/users/{userId}/profile', |
| 193 | + method: 'PUT', |
| 194 | + request: httpRequest({ |
| 195 | + params: { |
| 196 | + userId: NumberFromString, |
| 197 | + }, |
| 198 | + headers: { |
| 199 | + authorization: t.string, |
| 200 | + }, |
| 201 | + body: { |
| 202 | + name: t.string, |
| 203 | + email: t.string, |
| 204 | + preferences: t.type({ |
| 205 | + theme: t.union([t.literal('light'), t.literal('dark')]), |
| 206 | + notifications: t.boolean, |
| 207 | + }), |
| 208 | + }, |
| 209 | + }), |
| 210 | + response: { |
| 211 | + 200: t.type({ |
| 212 | + success: t.boolean, |
| 213 | + }), |
| 214 | + }, |
| 215 | +}); |
| 216 | + |
| 217 | +// Decoded type |
| 218 | +type DecodedType = { |
| 219 | + userId: number; |
| 220 | + authorization: string; |
| 221 | + name: string; |
| 222 | + email: string; |
| 223 | + preferences: { |
| 224 | + theme: 'light' | 'dark'; |
| 225 | + notifications: boolean; |
| 226 | + }; |
| 227 | +}; |
| 228 | +``` |
| 229 | + |
| 230 | +## Summary |
| 231 | + |
| 232 | +These advanced patterns help you define complex HTTP routes that accurately reflect your |
| 233 | +API's requirements. By combining `io-ts` with `httpRequest` and `httpRoute`, you can |
| 234 | +create type-safe APIs with sophisticated validation logic. |
0 commit comments