Skip to content

Commit

Permalink
feat(onUse): add connect adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbonnet committed Mar 3, 2021
1 parent 36ec9c5 commit 14438ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ export { isStream } from './src/isStream'
export { keyTransform } from './src/keyTransform'
export { log } from './src/log'
export { matchUrl } from './src/matchUrl'
export { setHref } from './src/setHref'
export { use } from './src/use'
export { onRequest } from './src/onRequest'
export { onUpgrade } from './src/onUpgrade'
export { onUse } from './src/onUse'
export { parseBody } from './src/parseBody'
export { parseBodyJson } from './src/parseBodyJson'
export { parseNumber } from './src/parseNumber'
Expand All @@ -38,9 +37,11 @@ export { routeUrl } from './src/routeUrl'
export { session } from './src/session'
export { sessionStoreMap } from './src/sessionStoreMap'
export { setCookie } from './src/setCookie'
export { setHref } from './src/setHref'
export { setSessionValue } from './src/setSessionValue'
export { stop } from './src/stop'
export { toReadableStream } from './src/toReadableStream'
export { use } from './src/use'
export { withJson } from './src/withJson'
export { writeBody } from './src/writeBody'
export { writeCompressibleBody } from './src/writeCompressibleBody'
Expand Down
28 changes: 28 additions & 0 deletions src/onUse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { respond } from './tools/respond'

export function onUse(handle) {
/*
Adapts a Serware `handler` to the [`connect`](https://github.com/senchalabs/connect) API.
Note that the `handler` second argument, `next`, calls the next middleware, preventing any changes to the response object. Nothing is returned by
*/
return async function (request, response, next) {
request.respond = function (options) {
if (options == null) {
return response
}
if (options.respond != null) {
// If request object is provided, return its response object
return options.respond()
}
return respond(response, options)
}
await handle(request, (request) => {
if (request != null) {
throw new Error(
'Calls to "next" should be performed without any argument.',
)
}
next()
})
}
}

0 comments on commit 14438ad

Please sign in to comment.