-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
released v0.2.0 - feature: added ThrowableRouter
- Loading branch information
Showing
12 changed files
with
123 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class StatusError extends Error { | ||
constructor(status = 500, message = 'Internal Error.') { | ||
super(message) | ||
this.name = 'StatusError' | ||
this.status = status | ||
} | ||
} | ||
|
||
module.exports = { StatusError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { StatusError } = require('./StatusError') | ||
|
||
const message = 'You messed up!' | ||
|
||
describe('class/StatusError', () => { | ||
describe(`StatusError(status = '500', message = 'Internal Error')`, () => { | ||
it('returns a JSON Response with { message } and status', async () => { | ||
const error = new StatusError(400, 'Bad Request') | ||
|
||
expect(error instanceof Error).toBe(true) | ||
expect(error.status).toBe(400) | ||
expect(error.name).toBe('StatusError') | ||
expect(error.message).toBe('Bad Request') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
...require('./StatusError'), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
describe('itty-router-extras', () => { | ||
it('returns all exports', async () => { | ||
const exports = require('./index') | ||
const expectedExports = [ | ||
'StatusError', | ||
'withContent', | ||
'withCookies', | ||
'withParams', | ||
'error', | ||
'json', | ||
'missing', | ||
'status', | ||
'text', | ||
'ThrowableRouter', | ||
] | ||
|
||
for (const e of expectedExports) { | ||
if (!exports.hasOwnProperty(e)) { | ||
console.log('missing export:', e) | ||
} | ||
expect(exports.hasOwnProperty(e)).toBe(true) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,14 @@ | ||
const { json } = require('../response') | ||
'use strict' | ||
|
||
// WRAPPER FUNCTION FOR THROWABLE HANDLERS | ||
const throwable = (options = {}) => fn => async (request, ...rest) => { | ||
const { | ||
error = {}, | ||
cors = true, | ||
} = options | ||
const { message, status = 500 } = error | ||
const { Router } = require('itty-router') | ||
const { error } = require('../response') | ||
|
||
try { | ||
let response = await fn(request, ...rest) | ||
|
||
if (cors && response && response instanceof Response) { | ||
addCorsHeaders(request)(response) | ||
} | ||
|
||
return response | ||
} catch (err) { | ||
let response = json({ | ||
message: message || err.message, | ||
stack: err.stack, | ||
status: err.status || status | ||
}, { status: err.status || status }) | ||
|
||
if (cors) { | ||
addCorsHeaders(request)(response) | ||
} | ||
|
||
return response | ||
} | ||
} | ||
|
||
const ThrowableRouter = (options = {}) => { | ||
const { middleware = [], ...other } = options | ||
|
||
return new Proxy(Router(options), { | ||
get: (obj, prop) => | ||
(route, ...handlers) => { | ||
let options = typeof handlers[handlers.length - 1] === 'object' && handlers.pop() || {} | ||
|
||
return obj[prop](route, withParams, ...middleware, ...handlers.map(throwable(other))) | ||
} | ||
const ThrowableRouter = (options = {}) => | ||
new Proxy(Router(options), { | ||
get: (obj, prop) => (...args) => | ||
prop === 'handle' | ||
? obj[prop](...args).catch(err => error(err.status || 500, err.message)) | ||
: obj[prop](...args) | ||
}) | ||
} | ||
|
||
module.exports = { ThrowableRouter } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { ThrowableRouter } = require('./ThrowableRouter') | ||
|
||
describe('router/ThrowableRouter', () => { | ||
describe(`ThrowableRouter(options = {})`, () => { | ||
it('is an itty proxy', async () => { | ||
const origin = {} | ||
const router = ThrowableRouter(origin) | ||
|
||
router.get('/foo', () => {}) | ||
|
||
expect(typeof origin.r).toBe('object') | ||
expect(origin.r.length).toBe(1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters