Skip to content

Commit

Permalink
💥 Changed template functionality to support params
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecarr committed Nov 19, 2021
1 parent 7336eff commit 8429209
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,32 @@ You can create error templates using the exported `withTemplate` function:
const { withTemplate } = require("@moducate/houston");
const app = require("express")();

const [withNotFound, rawNotFound] = withTemplate({ type: "https://example.com/not-found", status: 404 });
const [withUserNotFound, rawUserNotFound] = withTemplate<{ userId: number }>(({ userId }) => ({
type: "https://example.com/user-not-found",
status: 404,
instance: `/users/${userId}`,
}));

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

const res = rawNotFound({ status: 401 });
const res = rawUserNotFound({ userId: 1 });
// => 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 }
// => { "type": "https://example.com/user-not-found", "status": 404, "instance": "/users/1" }
```

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

Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ export function withError (res: ServerResponse, err: Partial<HoustonError>, opts
}

/**
* Generates an RFC 7807 compliant error template which can be invoked to
* make errors with common values.
* Generates functions which can be invoked to produce errors from a
* predefined template.
*
* @param template The template's values.
* @param template A function which accepts the template's parameters
* and produces an error.
* @param opts Additional options for Houston.
* @returns A `withError` function which can be invoked to apply the error
* template to a `http.ServerResponse` object, and a `raw` function to
* generate a plain object from the template (without touching the `http`
* module).
*/
export function withTemplate (template: Partial<HoustonError>, opts?: Partial<Options>): [(res: ServerResponse, err?: Partial<HoustonError>, opts?: Partial<Options>) => void, (err?: Partial<HoustonError>) => Partial<HoustonError>] {
return [(res, err, _opts) => withError(res, { ...template, ...err }, { ...opts, ..._opts }), err => ({ ...template, ...err })]
export function withTemplate <T = {}>(template: (params: T) => Partial<HoustonError>, opts?: Partial<Options>): [(res: ServerResponse, params: T) => void, (params: T) => Partial<HoustonError>] {
return [(res, params) => withError(res, { ...template(params) }, { ...opts }), params => ({ ...template(params) })]
}
10 changes: 3 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,16 @@ describe('withError', () => {

describe('withTemplate', () => {
it('should return a withError function', () => {
const [withNotFound] = withTemplate({ status: 404 })
const [withNotFound] = withTemplate(() => ({ status: 404 }))
expect(typeof withNotFound).toStrictEqual('function')
})

it('should merge the defaults with the supplied error', () => {
const [withNotFound] = withTemplate({ status: 404 })
const res = new MockRes()
withNotFound(res)

withNotFound(res, {})
expect(res.statusCode).toStrictEqual(404)
})

it('should return a raw function', () => {
const [, withNotFound] = withTemplate({ status: 404 })
const [, withNotFound] = withTemplate<{ status: number }>(x => x)
expect(typeof withNotFound).toStrictEqual('function')

const res = withNotFound({ status: 200 })
Expand Down

0 comments on commit 8429209

Please sign in to comment.