Skip to content

Commit d7b04c2

Browse files
Merge pull request #11 from jacobwgillespie/v2
2 parents addb0da + 3bb3ad4 commit d7b04c2

File tree

2 files changed

+207
-129
lines changed

2 files changed

+207
-129
lines changed

Diff for: README.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
Zap is a lightweight HTTP server framework for Node.
44

5+
- [Installation](#installation)
6+
- [Usage](#usage)
7+
- [API](#api)
8+
- [Recipes](#recipes)
9+
- [Credits](#credits)
10+
- [License](#license)
11+
512
## Installation
613

714
Install with your favorite package manager:
815

916
```shell
10-
$ yarn add zap
1117
$ pnpm add zap
18+
$ yarn add zap
1219
$ npm install zap
1320
```
1421

@@ -27,6 +34,70 @@ const server = http.createServer(serve(app))
2734
server.listen(3000)
2835
```
2936

37+
## API
38+
39+
### `serve(handler, options)`
40+
41+
Constructs a new `http.RequestListener` out of a `Handler`.
42+
43+
### `router(...routes)`
44+
45+
Constructs a new `Handler` out of a list of `RouteHandlers`.
46+
47+
### `route(method, path, handler)`
48+
49+
Constructs a `RouteHandler` that matches a given method (`GET`, `POST`, etc) and path.
50+
51+
### Body parsers
52+
53+
- `buffer(req, options)` - read the request body as a `Buffer`
54+
- `text(req, options)` - read the request body as a string
55+
- `json(req, options)` - read the request body as parsed JSON
56+
57+
### Request helpers
58+
59+
- `getHeader(req, header)` - returns the requested header if it was provided
60+
- `fromRequest(fn)` - wraps a function in the form `(req: ServerRequest, ...rest) => any` to return an equivalent function that caches its results for the provided request
61+
62+
### Response helpers
63+
64+
- Ordinarily you would return a `ResponseBodyType` from a `Handler` function
65+
- `send(res, statusCode, body)` - a response with a given status code
66+
- `notFound()` - a 404 response
67+
- `redirect(location, statusCode)` - a redirect to another location (default status code 303)
68+
- `httpError(code, message, metadata)` - an error response with a given code, message, and optional metadata
69+
70+
## Recipes
71+
72+
### Validating body schema
73+
74+
You can use a function that throws an `httpError` to provide type-safe body payload parsing:
75+
76+
```typescript
77+
async function parseBody(req: ServerRequest) {
78+
const body = await json(req)
79+
if (!validate(body)) throw httpError(400, 'invalid body')
80+
return body
81+
}
82+
83+
route('POST', '/example', (req) => {
84+
const body = await parseBody(req)
85+
// body is now typed according to your parseBody return type
86+
})
87+
```
88+
89+
### Error handling
90+
91+
The `serve()` function options accept an `errorHandler` that will replace `zap`'s built-in error handler. This allows you to report errors to services like Sentry, format the response sent to the user, etc.
92+
93+
```typescript
94+
serve(handler, {
95+
errorHandler: (_, res, error) => {
96+
send(res, 500, {message: 'Internal server error', details: formatError(error)})
97+
},
98+
})
99+
```
100+
30101
## Credits
31102

32103
Special thanks to [@nornagon](https://github.com/nornagon) for the `zap` package name. For versions of this module published before `v1.0.0`, see [nornagon/node-zap](https://github.com/nornagon/node-zap).

0 commit comments

Comments
 (0)