1
0

Merge pull request #7 from simonblund/patch-1

Fix issue when returning ReadableStreams and other strange responses
This commit is contained in:
Toby Schneider
2022-01-18 10:54:16 +01:00
committed by GitHub
2 changed files with 9 additions and 1 deletions

5
index.d.ts vendored
View File

@@ -54,6 +54,7 @@ declare class Router {
* @property {Object<string, string>} headers Object you can set response headers in * @property {Object<string, string>} headers Object you can set response headers in
* @property {number} status Return status code (default: `204`) * @property {number} status Return status code (default: `204`)
* @property {Object<string, string> | string} body Either an `object` (will be converted to JSON) or a string * @property {Object<string, string> | string} body Either an `object` (will be converted to JSON) or a string
* @property {Response} response A response object that is to be returned, this will void all other res properties and return this as is.
*/ */
/** /**
* Next Function * Next Function
@@ -291,6 +292,10 @@ type RouterResponse = {
* Return status code (default: `204`) * Return status code (default: `204`)
*/ */
status: number status: number
/**
* A response object to be directly returned to the client
*/
response: Response
/** /**
* Either an `object` (will be converted to JSON) or a string * Either an `object` (will be converted to JSON) or a string
*/ */

View File

@@ -359,6 +359,9 @@ class Router {
res.headers['Content-Type'] = 'application/json' res.headers['Content-Type'] = 'application/json'
res.body = JSON.stringify(res.body) res.body = JSON.stringify(res.body)
} }
if(res.response){
return res.response
}
return new Response(res.body, { return new Response(res.body, {
status: res.status || (res.body ? 200 : 204), status: res.status || (res.body ? 200 : 204),
headers: res.headers headers: res.headers
@@ -370,4 +373,4 @@ class Router {
} }
} }
module.exports = Router module.exports = Router