Skip to content

Commit

Permalink
💥 Changed withTemplate to return tuple for raw error generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecarr committed Nov 18, 2021
1 parent ba7fce7 commit 307267d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
"files": [
"dist"
],
"engines": {
"node": ">=10.24"
},
"scripts": {
"build": "siroc build",
"changelog": "gitmoji-changelog",
Expand Down Expand Up @@ -60,6 +57,9 @@
"ts-jest": "^27.0.7",
"typescript": "^4.4.4"
},
"engines": {
"node": ">=10.24"
},
"size-limit": [
{
"name": "withError",
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export function withError (res: ServerResponse, err: Partial<HoustonError>): voi
*
* @param template The template's values.
* @returns A `withError` function which can be invoked to apply the error
* template to a `http.ServerResponse` object.
* 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>): (res: ServerResponse, err?: Partial<HoustonError>) => void {
return (res, err) => withError(res, { ...template, ...err })
export function withTemplate (template: Partial<HoustonError>): [(res: ServerResponse, err?: Partial<HoustonError>) => void, (err?: Partial<HoustonError>) => Partial<HoustonError>] {
return [(res, err) => withError(res, { ...template, ...err }), err => ({ ...template, ...err })]
}
14 changes: 11 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,24 @@ describe('withError', () => {
})

describe('withTemplate', () => {
it('should return a function', () => {
const withNotFound = withTemplate({ status: 404 })
it('should return a withError function', () => {
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 [withNotFound] = withTemplate({ status: 404 })
const res = new MockRes()
withNotFound(res)

expect(res.statusCode).toStrictEqual(404)
})

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

const res = withNotFound({ status: 200 })
expect(res.status).toStrictEqual(200)
})
})

0 comments on commit 307267d

Please sign in to comment.