@dkx/http-server
Advanced tools
Comparing version 0.1.6 to 1.0.0
@@ -6,1 +6,2 @@ export { Middleware, NextMiddlewareFunction, RequestState } from './middleware'; | ||
export * from './request'; | ||
export * from './testing'; |
@@ -12,2 +12,3 @@ "use strict"; | ||
__export(require("./request")); | ||
__export(require("./testing")); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { ResponseBodyBag, ResponseBodyBagAttachedListener } from './response-body-bag'; | ||
import { ResponseBodyBag } from './response-body-bag'; | ||
export declare type ResponseHeader = number | string | Array<string>; | ||
@@ -18,3 +18,3 @@ export declare type ResponseHeaders = { | ||
readonly headers: ResponseHeaders; | ||
getBody(fn: ResponseBodyBagAttachedListener): void; | ||
write(chunk: any): void; | ||
withStatus(code: number, message?: string): Response; | ||
@@ -21,0 +21,0 @@ hasHeader(name: string): boolean; |
@@ -14,4 +14,4 @@ "use strict"; | ||
} | ||
getBody(fn) { | ||
this.bodyBag.onBodyAttached(fn); | ||
write(chunk) { | ||
this.bodyBag.onBodyAttached((body) => body.write(chunk)); | ||
} | ||
@@ -18,0 +18,0 @@ withStatus(code, message = '') { |
{ | ||
"name": "@dkx/http-server", | ||
"version": "0.1.6", | ||
"version": "1.0.0", | ||
"description": "Minimalistic HTTP server", | ||
@@ -5,0 +5,0 @@ "repository": "git@gitlab.com:dkx/http/server.git", |
@@ -57,7 +57,5 @@ # DKX/Http/Server | ||
{ | ||
response.getBody((body) => { | ||
body.write('hello'); | ||
body.write(' '); | ||
body.write('world'); | ||
}); | ||
response.write('hello'); | ||
response.write(' '); | ||
response.write('world'); | ||
@@ -84,2 +82,37 @@ return next(response); | ||
## Testing middlewares | ||
Testing new middlewares is really easy with the built in helper function. It construct all the necessary objects and | ||
runs the middleware for you. | ||
```js | ||
const {testMiddleware} = require('@dkx/http-server'); | ||
function myUselessMiddleware(request, response, next) | ||
{ | ||
response = response.withHeader('X-Middleware-header', 'lorem ipsum'); | ||
return next(response); | ||
} | ||
const response = await testMiddleware(myUselessMiddleware, { | ||
method: 'POST', | ||
url: '/v1/users/5', | ||
body: 'some HTTP body', | ||
headers: { | ||
'X-My-custom-http-header': 'hello world' | ||
}, | ||
state: { | ||
msg: 'My custom shared state' | ||
} | ||
}); | ||
console.log(response.getHeader('X-Middleware-header')); // output: "lorem ipsum" | ||
``` | ||
The `testMiddleware` function can be used just with your middleware: | ||
```js | ||
testMiddleware(myUselessMiddleware); | ||
``` | ||
## Server object | ||
@@ -146,10 +179,10 @@ | ||
* **`getBody()`** | ||
* **`write()`** | ||
Method used for accessing the `ResponseBody` object, which can be used for writing the response data. | ||
Method for writing data into response body. | ||
**Arguments:** | ||
`chunk: any`: data to write. | ||
`fn: (body) => void`: callback with `ResponseBody` object. | ||
* **`withStatus()`** | ||
@@ -156,0 +189,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
31770
45
385
1
271