Skip to content

Commit

Permalink
πŸ“ Updated README for new templating system
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecarr committed Nov 18, 2021
1 parent 307267d commit 5e0e566
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,30 @@ You can create error templates using the exported `withTemplate` function:
const { withTemplate } = require("@moducate/houston");
const app = require("express")();

const withNotFound = withTemplate({ type: "https://example.com/not-found", status: 404 });
const [withNotFound, rawNotFound] = 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
});

const res = rawNotFound({ status: 401 });
// => The second function returned by withTemplate transforms and returns an object (decoupled from http.ServerResponse)
// => The supplied status (401) will override the template's status (404)

console.log(JSON.stringify(res));
// => { "type": "https://example.com/not-found", "status": 401 }
```

If you are not needing to use both functions, you can use this handy shorthand to obtain one of them:

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

const [withNotFound] = withTemplate({ type: "https://example.com/not-found", status: 404 });
// => Returns the function that transforms a http.ServerResponse

const [, withNotFound] = withTemplate({ type: "https://example.com/not-found", status: 404 });
// => Returns the raw function for transforming an object
```

## 🏷 MIME Type
Expand Down

0 comments on commit 5e0e566

Please sign in to comment.