@@ -31,17 +31,15 @@ for typing HTTP requests. Start there for base knowledge required to use this pa
3131
3232## Introduction
3333
34- You can use @api-ts/io-ts-http to define types for APIs that can parse HTTP requests at runtime.
35- runtime. Most often, these types are defined in a sibiling package to a service, and
36- used by that service and it's clients; naming convention dictates for a service named
37- ` <service-name> ` the types package will be named ` <service-name>-types ` . Both clients
38- and services can use the types defined in ` <service-name>-types ` to send and parse
39- requests with well-defined expectations. If requests don't adhere to the defined types
40- at runtime, errors may occur when those requests parse.
34+ io-ts-http is the definition language for api-ts specifications, which define the API
35+ contract for a web sever to an arbitrary degree of precision. Web servers can use the
36+ io-ts-http spec to parse HTTP requests at runtime, and encode HTTP responses. Clients
37+ can use the io-ts-http spec to enforce API compatibility at compile time, and to encode
38+ requests to the server and decode responses.
4139
4240## Overview
4341
44- The primary function in this library is ` httpRequest ` , which is used to build codecs
42+ The primary function in this library is ` httpRequest ` . You can use this to build codecs
4543which can parse a generic HTTP request into a more refined type. The generic HTTP
4644request should conform to the following interface:
4745
@@ -120,10 +118,10 @@ the server will correctly interpret a request if the arguments typecheck.
120118
121119## Example
122120
123- Lets drill into the ` *-types ` package for an example service called ` message-user `
124- service. The top-level export for any ` *-types ` package is an
125- [ ` apiSpec ` ] ( https://github.com/BitGo/api-ts/blob/master/packages/io-ts-http/docs/apiSpec.md ) .
126- The following is a ` message-user-types ` definition :
121+ Let's define the api-ts spec for a hypothetical ` message-user ` service. The conventional
122+ top-level export is an
123+ [ ` apiSpec ` ] ( https://github.com/BitGo/api-ts/blob/master/packages/io-ts-http/docs/apiSpec.md )
124+ value; for example :
127125
128126### ` apiSpec `
129127
@@ -152,18 +150,18 @@ export const API = apiSpec({
152150});
153151```
154152
155- The ` apiSpec ` is imported, along with some named ` httpRoute ` s (` {Get|Create}Message ` , and
156- ` {Get|Create|Update|Delete}User ` ) [ which we'll discuss below] ( #httproute ) .
153+ The ` apiSpec ` is imported, along with some named ` httpRoute ` s (` {Get|Create}Message ` ,
154+ and ` {Get|Create|Update|Delete}User ` ) [ which we'll discuss below] ( #httproute ) .
157155
158156> Currently, if you add the ` @version ` JSDoc tag to the exported API spec, it will be
159157> used as the API ` version ` when generating an OpenAPI schema. Support for other tags
160158> may be added in the future.
161159
162160The top-level export for ` message-user-types ` is ` API ` , which we define as an ` apiSpec `
163- with two endpoints ` api/v1/message ` and ` api/v1/user ` . The ` api/v1/message ` endpoint responds to ` GET ` and
164- ` POST ` verbs while the second reponds to ` GET ` , ` POST ` , ` PUT ` , and ` DELETE ` verbs using
165- ` httpRoute ` s defined in ` ./routes/message ` . The following are the ` httpRoute ` s defined in
166- ` ./routes/message ` .
161+ with two endpoints ` api/v1/message ` and ` api/v1/user ` . The ` api/v1/message ` endpoint
162+ responds to ` GET ` and ` POST ` verbs while the second reponds to ` GET ` , ` POST ` , ` PUT ` , and
163+ ` DELETE ` verbs using ` httpRoute ` s defined in ` ./routes/message ` . The following are the
164+ ` httpRoute ` s defined in ` ./routes/message ` .
167165
168166### ` httpRoute `
169167
@@ -216,7 +214,8 @@ describing the types of data properties. Again, review
216214and this package.
217215
218216Then ` httpRoute ` and ` httpRequest ` are imported. We'll review the
219- [ ` httpRequest ` ] ( #httprequest ) below, but first, let's review the ` GetMessage ` ` httpRoute ` .
217+ [ ` httpRequest ` ] ( #httprequest ) below, but first, let's review the ` GetMessage `
218+ ` httpRoute ` .
220219
221220``` typescript
222221export const GetMessage = httpRoute ({
@@ -240,8 +239,9 @@ export const GetMessage = httpRoute({
240239```
241240
242241[ ` httpRoute ` ] ( https://github.com/BitGo/api-ts/blob/master/packages/io-ts-http/docs/httpRoute.md ) s
243- ` apiSpec ` s use [ ` httpRoute ` ] ( https://github.com/BitGo/api-ts/blob/master/packages/io-ts-http/docs/httpRoute.md ) s to define the ` path ` , ` method ` , ` request ` and ` response ` of a
244- route.
242+ ` apiSpec ` s use
243+ [ ` httpRoute ` ] ( https://github.com/BitGo/api-ts/blob/master/packages/io-ts-http/docs/httpRoute.md ) s
244+ to define the ` path ` , ` method ` , ` request ` and ` response ` of a route.
245245
246246#### ` path `
247247
@@ -272,24 +272,24 @@ object with a single property `error` which is a `String`.
272272
273273### ` httpRequest `
274274
275- Use ` httpRequest ` to build the expected type that you pass in a request to the route.
276- In our example ` GetMessage `
275+ Use ` httpRequest ` to build the expected type that you pass in a request to the route. In
276+ our example ` GetMessage `
277277
278278``` typescript
279- ...
280279export const GetMessage = httpRoute ({
281280 path: ' /message/{id}' ,
282281 method: ' GET' ,
283282 request: httpRequest ({
284283 params: {
285- id: t .string
284+ id: t .string ,
286285 },
287286 }),
288- ...
287+ // ...
288+ });
289289```
290290
291- ` httpRequest ` s have a total of 4 optional properties: ` params ` (shown
292- in the example), ` query ` , ` headers ` , and ` body ` .
291+ ` httpRequest ` s have a total of 4 optional properties: ` params ` (shown in the example),
292+ ` query ` , ` headers ` , and ` body ` .
293293
294294#### ` params `
295295
@@ -302,20 +302,19 @@ of our `httpRequest`.
302302#### ` query `
303303
304304` query ` is the object representing the values passed in via query parameters at the end
305- of a URL. The following example uses a new route, ` GetMessages ` , to our API that searches messages
306- related to a specific ` author ` :
305+ of a URL. The following example uses a new route, ` GetMessages ` , to our API that
306+ searches messages related to a specific ` author ` :
307307
308308``` typescript
309- ...
310309export const GetMessages = httpRoute ({
311310 path: ' /messages' ,
312311 method: ' GET' ,
313312 request: httpRequest ({
314313 query: {
315- author: t .string
314+ author: t .string ,
316315 },
317316 }),
318- ...
317+ // ...
319318});
320319```
321320
@@ -338,10 +337,10 @@ export const CreateMessage = httpRoute({
338337 method: ' POST' ,
339338 request: httpRequest ({
340339 body: {
341- message: t .string
342- }
340+ message: t .string ,
341+ },
343342 }),
344- ...
343+ // ...
345344});
346345```
347346
0 commit comments