Skip to content

Commit

Permalink
Merge pull request #54 from Shudrum/payload-size-configuration
Browse files Browse the repository at this point in the history
Payload size configuration + Remove worker
  • Loading branch information
Shudrum authored Oct 13, 2019
2 parents d604c66 + b1c9c8d commit aef434c
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/_data/workers_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ toc:
description: Aggregate the result of a request onto the container's body.
- title: filter
url: /workers/filter.html
description: Filter entries from the container's body.
- title: remove
url: /workers/remove.html
description: Remove entries from the container's body.
- title: mergeBody
url: /workers/merge-body.html
Expand Down
22 changes: 22 additions & 0 deletions docs/collections/_workers/remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
layout: content
title: Workers - Remove
---

# filter(paths)

Remove the properties listed on the `paths` argument array from the container's body

_Arguments_

| Argument | Type | Description |
| :------- | :------------------------- | :--------------------------------------------------- |
| paths | **string** or **[string]** | **Required.** Paths or path to remove from the body. |

_Example_

```js
const workflow = [
remove('secret'),
];
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nodegate",
"description": "API gateway made simple, fast and easy to configure.",
"version": "1.0.0",
"version": "1.1.0",
"author": "Julien Martin <[email protected]>",
"license": "MIT",
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions services/nodegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ const express = require('express');
const { execute } = require('../entities/route');
const { getConfiguration } = require('../services/configuration');

const buildExpressApp = () => {
const buildExpressApp = (options) => {
const app = express();
const configuration = getConfiguration().express;

if (configuration.useCors) {
app.use(cors());
}
app.use(bodyParser.json());
app.use(bodyParser.json({
limit: options.payloadSizeLimit || '1mb',
}));

app.use((_, res, next) => {
res.setHeader('x-powered-by', configuration.poweredBy);
Expand All @@ -35,8 +37,8 @@ const toArray = (value) => {
return value;
};

const nodegate = () => {
const expressApp = buildExpressApp();
const nodegate = (options = {}) => {
const expressApp = buildExpressApp(options);
const beforeEach = [];
const app = (req, res, next) => {
expressApp.handle(req, res, next);
Expand Down
15 changes: 15 additions & 0 deletions test/services/nodegate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,19 @@ describe('services/nodegate', () => {
.expect(503);
});
});
describe('Options', () => {
it('should allow to limit the size of the body', async () => {
const gate = nodegate({
payloadSizeLimit: '10o',
});
gate.route({
method: 'post',
path: '/',
workflow: [],
});
await request(gate).post('/').send({
title: 'This document is too heavy to be hamdled',
}).expect(413);
});
});
});
54 changes: 54 additions & 0 deletions test/workers/remove.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const remove = require('../../workers/remove');

describe('workers/remove', () => {
it('should correctly return a function', () => {
expect(remove()).toBeInstanceOf(Function);
});
it('should remove from the container', () => {
const container = {
body: {
ships: ['enterprise'],
captains: ['Jean-Luc Picard'],
},
};
remove(['ships'])(container);
expect(container.body.ships).toBeFalsy();
expect(container.body.captains).toBeTruthy();
});
it('should remove from the container with the path as a string', () => {
const container = {
body: {
ships: ['enterprise'],
captains: ['Jean-Luc Picard'],
},
};
remove('ships')(container);
expect(container.body.ships).toBeFalsy();
expect(container.body.captains).toBeTruthy();
});
it('should remove from the container body only', () => {
const container = {
params: {
organization: 'Federation',
},
body: {
ships: ['enterprise'],
captains: ['Jean-Luc Picard'],
},
};
remove(['ships'])(container);
expect(container.params.organization).toBeTruthy();
expect(container.body.captains).toBeTruthy();
});
it('should remove nested values from the container', () => {
const container = {
body: {
ships: ['enterprise', 'NC-1701'],
captains: ['Jean-Luc Picard'],
},
};
remove(['ships[1]'])(container);
expect(container.body.ships).toBeTruthy();
expect(container.body.ships[1]).toBeUndefined();
});
});
12 changes: 12 additions & 0 deletions workers/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Weekendesk SAS.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const { omit } = require('lodash');

module.exports = (paths) => (container) => {
container.body = omit(container.body, paths);
};

0 comments on commit aef434c

Please sign in to comment.