Skip to content

Commit

Permalink
📄 Added Fastify example (with source)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecarr committed Nov 18, 2021
1 parent 89fbdb3 commit 1ca4696
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 21 deletions.
57 changes: 36 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ import { withError } from "@moducate/houston";
const { withError } = require("@moducate/houston");
```

## 📄 Templates

You can create error templates using the exported `withTemplate` function:

```js
const { withTemplate } = require("@moducate/houston");
const app = require("express")();

const withNotFound = withTemplate({ type: "https://example.com/not-found", status: 404 });

app.get("/not-found", (_, res) => {
return withNotFound(res); // The second parameter is optional when using templates
});
```

## 🏷 MIME Type

The exported `mime` constant can be used to obtain the media/MIME type for RFC 7807 JSON errors.

```js
const { mime } = require("@moducate/houston");

console.log(mime);
// => 'application/problem+json'
```

This is what the `Content-Type` header of the response supplied to `withError` is set to.

## 💡 Examples

> 📁 Full source code for these examples can be found in the `examples` directory.
Expand All @@ -61,34 +89,21 @@ export default function handler(req, res) {
}
```

## 📄 Templates
### Fastify

You can create error templates using the exported `withTemplate` function:
> ⚠ Fastify uses a custom `Reply` class, rather than `http.ServerResponse`.
>
> This means you need to supply `reply.raw` (instead of `reply`) to `withError()`.
```js
const { withTemplate } = require("@moducate/houston");
const app = require("express")();

const withNotFound = withTemplate({ type: "https://example.com/not-found", status: 404 });
const { withError } = require("@moducate/houston");
const app = require("fastify")();

app.get("/not-found", (_, res) => {
return withNotFound(res); // The second parameter is optional when using templates
app.get("/not-found", (_, reply) => {
return withError(reply.raw, { type: "https://example.com/not-found", status: 404 });
});
```

## 🏷 MIME Type

The exported `mime` constant can be used to obtain the media/MIME type for RFC 7807 JSON errors.

```js
const { mime } = require("@moducate/houston");

console.log(mime);
// => 'application/problem+json'
```

This is what the `Content-Type` header of the response supplied to `withError` is set to.

## ⚖ License

Houston is licensed under the [`MIT License`](LICENSE).
8 changes: 8 additions & 0 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "fastify",
"private": true,
"dependencies": {
"fastify": "^3.24.0",
"@moducate/houston": "../.."
}
}
37 changes: 37 additions & 0 deletions examples/fastify/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { withError } = require('@moducate/houston')
const app = require('fastify')()

const books = [
{
id: '1',
name: 'The Da Vinci Code',
author: 'Dan Brown',
publisher: 'Transworld'
},
{
id: '2',
name: 'Harry Potter and the Deathly Hallows',
author: 'J.K. Rowling',
publisher: 'Bloomsbury'
}
]

app.get('/books', () => {
return { books }
})

app.get('/books/:id', ({ params }, res) => {
const book = books.find(x => x.id === params.id)
if (book) {
return book
}
return withError(res.raw, {
type: 'https://express.example.com/not-found',
status: 404,
instance: `/books/${params.id}`,
title: 'Book not found.',
detail: `No book was found with the ID '${params.id}'.`
})
})

app.listen(3000)
Loading

0 comments on commit 1ca4696

Please sign in to comment.