Socket
Socket
Sign inDemoInstall

express-briefcase

Package Overview
Dependencies
63
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.0 to 4.0.1

appveyor.yml

4

CHANGELOG.md
## Change Log
### v4.0.1 on 2021-01-06
- AppVeyor integration
### v4.0.0 on 2021-01-06
- Initial release
{
"name": "express-briefcase",
"version": "4.0.0",
"version": "4.0.1",
"description": "Include metadata in Express.js response json such as errors, warnings and pagination",

@@ -8,7 +8,22 @@ "main": "index.js",

"test": "npx jest",
"coverage": "npx jest --coverage"
"coverage": "npx jest --coverage",
"generate-types": "sh ./scripts/generate-types.sh"
},
"keywords": [],
"author": "",
"repository": {
"type": "git",
"url": "git+https://github.com/kensnyder/express-briefcase.git"
},
"keywords": [
"express",
"express.js",
"metadata",
"response",
"pagination"
],
"author": "kendsnyder@gmail.com",
"license": "ISC",
"bugs": {
"url": "https://github.com/kensnyder/express-briefcase/issues"
},
"homepage": "https://github.com/kensnyder/express-briefcase#readme",
"dependencies": {

@@ -15,0 +30,0 @@ "express": "4.x"

85

README.md
# 💼 express-briefcase
[![Build Status](https://travis-ci.com/kensnyder/express-briefcase.svg?branch=master&v=1.0.0)](https://travis-ci.com/kensnyder/express-briefcase)
[![Code Coverage](https://codecov.io/gh/kensnyder/express-briefcase/branch/master/graph/badge.svg?v=1.0.0)](https://codecov.io/gh/kensnyder/express-briefcase)
[![ISC License](https://img.shields.io/npm/l/express-briefcase.svg?v=1.0.0)](https://opensource.org/licenses/ISC)
[![NPM Link](https://img.shields.io/npm/v/express-briefcase?v=4.0.1)](https://npmjs.com/package/express-briefcase)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/kensnyder/express-briefcase?branch=master&svg=true&v=4.0.1)](https://ci.appveyor.com/project/kensnyder/express-briefcase/branch/master)
[![Code Coverage](https://codecov.io/gh/kensnyder/express-briefcase/branch/master/graph/badge.svg?v=4.0.1)](https://codecov.io/gh/kensnyder/express-briefcase)
[![ISC License](https://img.shields.io/npm/l/express-briefcase.svg?v=4.0.1)](https://opensource.org/licenses/ISC)

@@ -29,9 +30,11 @@ Include metadata in Express.js response json such as errors, warnings and pagination

const app = express();
app.use(briefcase(shell => {
app.use(
briefcase(shell => {
shell.requestReferenceId = uuid.v4();
if (shell.statusClass === '5xx') {
shell.apology = 'Sorry about that!';
}
shell.apology = 'Sorry about that!';
}
return shell;
}));
})
);
```

@@ -45,7 +48,10 @@

const app = express();
app.use(briefcase(function(shell) {
// inside your customizer function, "this" is express's "res" object
app.use(
briefcase(function (shell) {
// Inside your customizer function, "this" is express's "res" object
// Note that we must use the "function" keyword, not an arrow function
shell.statusMessage = this.statusMessage;
return shell;
}));
})
);
```

@@ -66,4 +72,4 @@

app.get('/user/:id', async (req, res) => {
const user = await getUser(req.params.id);
res.decoratedJson(user);
const user = await getUser(req.params.id);
res.decoratedJson(user);
});

@@ -93,3 +99,3 @@ ```

------------------------
---

@@ -100,9 +106,9 @@ #### Using `res.error()`

app.get('/user/:id', async (req, res) => {
const user = await getUser(req.params.id);
if (user === null) {
// Note: error can be a string or instance of Error
res.error('User not found');
res.status(404);
}
res.decoratedJson(user);
const user = await getUser(req.params.id);
if (user === null) {
// Note: error can be a string or instance of Error
res.error('User not found');
res.status(404);
}
res.decoratedJson(user);
});

@@ -128,3 +134,3 @@ ```

---------------------------
---

@@ -136,3 +142,3 @@ #### Using `res.devError()`

-----------------------
---

@@ -143,7 +149,7 @@ #### Using `res.warn()`

app.get('/user/:id', async (req, res) => {
if (req.query.role) {
res.warn('Passing role to /user/:id is deprecated.');
}
const user = await getUser(req.params.id);
res.decoratedJson(user);
if (req.query.role) {
res.warn('Passing role to /user/:id is deprecated.');
}
const user = await getUser(req.params.id);
res.decoratedJson(user);
});

@@ -173,3 +179,3 @@ ```

----------------------
---

@@ -180,5 +186,5 @@ #### Using `res.new()`

app.post('/user', async (req, res) => {
const { newId } = await createUser(req.body);
res.new({ id: newId, url: `/users/${newId}` });
res.decoratedJson();
const { newId } = await createUser(req.body);
res.new({ id: newId, url: `/users/${newId}` });
res.decoratedJson();
});

@@ -207,3 +213,3 @@ ```

------------------------
---

@@ -214,9 +220,9 @@ #### Using `res.total()`

app.get('/users/search', async (req, res) => {
const { list, total } = await getUsers({
name: req.query.name,
page: req.query.page,
limit: 25,
});
res.total({ total, page: req.query.page, perPage: 25 });
res.decoratedJson(user);
const { list, total } = await getUsers({
name: req.query.name,
page: req.query.page,
limit: 25,
});
res.total({ total, page: req.query.page, perPage: 25 });
res.decoratedJson(user);
});

@@ -275,2 +281,1 @@ ```

Open Source under the [ISC License](https://opensource.org/licenses/ISC).
/**
*
* @param data
* Send the given payload, wrapped with metadata including date, errors, warnings, and pagination
* @param {*} [data=null] The payload to send
* @returns {Response}
* @chainable
*/

@@ -10,2 +12,3 @@ function decoratedJson(data = null) {

date: this.locals._startedAt || new Date(),
// in CloudFlare workers, "took" will always be 0, so omit in that case
took: took > 0 ? took : undefined,

@@ -25,4 +28,5 @@ status,

this.json(shell);
return this;
}
module.exports = decoratedJson;

@@ -0,1 +1,7 @@

/**
* Add error message only if NODE_ENV is "development"
* @param {String|Error} message
* @returns {Response}
* @chainable
*/
function devError(message) {

@@ -2,0 +8,0 @@ if (process.env.NODE_ENV !== 'development') {

@@ -0,1 +1,7 @@

/**
* Add error message
* @param {String|Error} message The error string (or the Error whose message property to use)
* @returns {Response}
* @chainable
*/
function error(message) {

@@ -2,0 +8,0 @@ if (message instanceof Error) {

@@ -0,1 +1,8 @@

/**
* Return information about a newly created record and set status to 201.
* @param {String|Number} id The id of the new record
* @param {String} [url] The API URL to fetch that new resource
* @returns {Response}
* @chainable
*/
function newRecord({ id, url = undefined }) {

@@ -2,0 +9,0 @@ this.locals._new = { id, url };

@@ -0,1 +1,10 @@

/**
* Calculate pagination metadata based on current request
* For GET/HEAD it also calls res.links({ prev, next }) to add a Link http header
* @param {Number} total The total number of records if a limit were not applied
* @param {Number} perPage The number of items per page (falls back to req.query.limit then req.body.limit then 10)
* @param {Number} page The current page number (falls back to req.query.page then req.body.page then 1)
* @returns {Response}
* @chainable
*/
function total({ total, perPage = undefined, page = undefined }) {

@@ -2,0 +11,0 @@ if (perPage === undefined) {

@@ -0,1 +1,7 @@

/**
* Add warning message
* @param {String|Error} message The warning string (or the Error whose message property to use)
* @returns {Response}
* @chainable
*/
function warn(message) {

@@ -2,0 +8,0 @@ if (message instanceof Error) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc