Skip to content

Commit

Permalink
Creating decoratorSet() to decorate res.set() for better interop …
Browse files Browse the repository at this point in the history
…with express middleware & adding a test, updating README.md, updating decorators
  • Loading branch information
avoidwork committed Oct 11, 2023
1 parent eccc701 commit 91a33f3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Woodland has 100% code coverage with its tests; the missing 0.22% is hard to rea
--------------|---------|----------|---------|---------|-------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------------------
All files | 99.78 | 76.26 | 98.52 | 100 |
woodland.cjs | 99.78 | 76.26 | 98.52 | 100 | ...214,254,275-285,314-328,...
All files | 99.78 | 76.02 | 98.57 | 100 |
woodland.cjs | 99.78 | 76.02 | 98.57 | 100 | ...214,254,275-285,314-328,...
--------------|---------|----------|---------|---------|-------------------------------
```

Expand Down Expand Up @@ -230,6 +230,9 @@ Sends a redirection response.
### res.send(body, [status = 200, headers = {}])
Sends a response. `Range` header is ignored on `stream` responses.

### res.set(headers = {})
Shorthand of `res.setHeaders()` which accepts `Object`, `Map`, or `Headers` instances.

### res.status(arg)
Sets the response `statusCode` property.

Expand Down
23 changes: 16 additions & 7 deletions dist/woodland.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 18.0.16
* @version 18.1.0
*/
'use strict';

Expand Down Expand Up @@ -474,13 +474,13 @@ class Woodland extends node_events.EventEmitter {
};
}

decoratorJson (req, res) {
decoratorJson (res) {
return (arg, status = 200, headers = {[CONTENT_TYPE]: `${APPLICATION_JSON}; charset=${UTF_8}`}) => {
res.send(JSON.stringify(arg), status, headers);
};
}

decoratorRedirect (req, res) {
decoratorRedirect (res) {
return (uri, perm = true) => {
res.send(EMPTY, perm ? 301 : 302, {[LOCATION]: uri});
};
Expand Down Expand Up @@ -529,7 +529,15 @@ class Woodland extends node_events.EventEmitter {
};
}

decoratorStatus (req, res) {
decoratorSet (res) {
return (arg = {}) => {
res.setHeaders(arg instanceof Map || arg instanceof Headers ? arg : new Headers(arg));

return res;
};
}

decoratorStatus (res) {
return (arg = 200) => {
res.statusCode = arg;

Expand All @@ -555,10 +563,11 @@ class Woodland extends node_events.EventEmitter {
res.locals = {};
res.error = this.decoratorError(req, res);
res.header = res.setHeader;
res.json = this.decoratorJson(req, res);
res.redirect = this.decoratorRedirect(req, res);
res.json = this.decoratorJson(res);
res.redirect = this.decoratorRedirect(res);
res.send = this.decoratorSend(req, res);
res.status = this.decoratorStatus(req, res);
res.set = this.decoratorSet(res);
res.status = this.decoratorStatus(res);

for (const i of this.defaultHeaders) {
res.header(i[0], i[1]);
Expand Down
23 changes: 16 additions & 7 deletions dist/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 18.0.16
* @version 18.1.0
*/
import {STATUS_CODES,METHODS}from'node:http';import {join,extname,resolve}from'node:path';import {EventEmitter}from'node:events';import {stat,readdir}from'node:fs/promises';import {etag}from'tiny-etag';import {precise}from'precise';import {lru}from'tiny-lru';import {readFileSync,createReadStream}from'node:fs';import {fileURLToPath,URL}from'node:url';import {coerce}from'tiny-coerce';import mimeDb from'mime-db';const ACCESS_CONTROL_ALLOW_CREDENTIALS = "access-control-allow-credentials";
const ACCESS_CONTROL_ALLOW_HEADERS = "access-control-allow-headers";
Expand Down Expand Up @@ -455,13 +455,13 @@ function writeHead (res, headers = {}) {
};
}

decoratorJson (req, res) {
decoratorJson (res) {
return (arg, status = 200, headers = {[CONTENT_TYPE]: `${APPLICATION_JSON}; charset=${UTF_8}`}) => {
res.send(JSON.stringify(arg), status, headers);
};
}

decoratorRedirect (req, res) {
decoratorRedirect (res) {
return (uri, perm = true) => {
res.send(EMPTY, perm ? 301 : 302, {[LOCATION]: uri});
};
Expand Down Expand Up @@ -510,7 +510,15 @@ function writeHead (res, headers = {}) {
};
}

decoratorStatus (req, res) {
decoratorSet (res) {
return (arg = {}) => {
res.setHeaders(arg instanceof Map || arg instanceof Headers ? arg : new Headers(arg));

return res;
};
}

decoratorStatus (res) {
return (arg = 200) => {
res.statusCode = arg;

Expand All @@ -536,10 +544,11 @@ function writeHead (res, headers = {}) {
res.locals = {};
res.error = this.decoratorError(req, res);
res.header = res.setHeader;
res.json = this.decoratorJson(req, res);
res.redirect = this.decoratorRedirect(req, res);
res.json = this.decoratorJson(res);
res.redirect = this.decoratorRedirect(res);
res.send = this.decoratorSend(req, res);
res.status = this.decoratorStatus(req, res);
res.set = this.decoratorSet(res);
res.status = this.decoratorStatus(res);

for (const i of this.defaultHeaders) {
res.header(i[0], i[1]);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "woodland",
"version": "18.0.16",
"version": "18.1.0",
"description": "Lightweight HTTP framework with automatic headers",
"type": "module",
"types": "types/woodland.d.ts",
Expand Down
21 changes: 15 additions & 6 deletions src/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ export class Woodland extends EventEmitter {
};
}

decoratorJson (req, res) {
decoratorJson (res) {
return (arg, status = 200, headers = {[CONTENT_TYPE]: `${APPLICATION_JSON}; charset=${UTF_8}`}) => {
res.send(JSON.stringify(arg), status, headers);
};
}

decoratorRedirect (req, res) {
decoratorRedirect (res) {
return (uri, perm = true) => {
res.send(EMPTY, perm ? 301 : 302, {[LOCATION]: uri});
};
Expand Down Expand Up @@ -293,7 +293,15 @@ export class Woodland extends EventEmitter {
};
}

decoratorStatus (req, res) {
decoratorSet (res) {
return (arg = {}) => {
res.setHeaders(arg instanceof Map || arg instanceof Headers ? arg : new Headers(arg));

return res;
};
}

decoratorStatus (res) {
return (arg = 200) => {
res.statusCode = arg;

Expand All @@ -319,10 +327,11 @@ export class Woodland extends EventEmitter {
res.locals = {};
res.error = this.decoratorError(req, res);
res.header = res.setHeader;
res.json = this.decoratorJson(req, res);
res.redirect = this.decoratorRedirect(req, res);
res.json = this.decoratorJson(res);
res.redirect = this.decoratorRedirect(res);
res.send = this.decoratorSend(req, res);
res.status = this.decoratorStatus(req, res);
res.set = this.decoratorSet(res);
res.status = this.decoratorStatus(res);

for (const i of this.defaultHeaders) {
res.header(i[0], i[1]);
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const router = woodland({

function always (req, res, next) {
res.header("x-always", "true");
res.set({"x-other-stuff": "true"});
next();
}

Expand Down

0 comments on commit 91a33f3

Please sign in to comment.