Skip to content

Commit

Permalink
Worker "executeIf" added
Browse files Browse the repository at this point in the history
  • Loading branch information
Shudrum committed Oct 7, 2020
1 parent 9d58c7c commit 19a7688
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/_data/workers_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ toc:
- title: Aggregate
url: /workers/aggregate.html
description: Aggregate a request response into the container's body.
- title: ExecuteIf
url: /workers/execute-if.html
description: Execute a workflow if a condition is met.
- title: Filter
url: /workers/filter.html
description: Filter entries from the container's body.
Expand Down
60 changes: 60 additions & 0 deletions docs/collections/_workers/execute-if.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
layout: documentation
title: Workers - ExecuteIf
---

# ExecuteIf

> Execute a workflow if a condition is met.
## executeIf(condition, workflow)

Execute a workflow if a condition is true.

### Arguments

| Argument | Type | Description |
| :-------- | :----------- | :------------------------------------------------------ |
| condition | **function** | **Required.** Condition needed to execute the workflow. |
| workflow | **array** | **Required.** Workflow to execute. |

- The `condition` function receive two arguments: `container` and `request`. It must return either
`true` or `false`,
- The `workflow` is simply an array of workers.

## Examples

In this example, we only want to fetch some data from our API if the weather is sunny:

```js
gateway.route({
method: 'get',
path: '/:username',
workflow: [
aggregate('get', 'https://myapi.com/weather'),
executeIf(({ body }) => body.weather === 'sun', [
aggregate('get', 'https://myapi.com/activities'),
]),
],
});
```

The response of the request can be, if sunny:

```json
{
"weather": "sun",
"activities": [
"football",
"kayak"
]
}
```

But, if the weather is not sunny:

```json
{
"weather": "rain"
}
```
59 changes: 59 additions & 0 deletions test/workers/executeIf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const executeIf = require('../../workers/executeIf');

describe('workers/executeIf', () => {
it('should correctly return a function', () => {
expect(executeIf()).toBeInstanceOf(Function);
});
it('should execute the condition', () => {
expect.assertions(1);
executeIf(() => { expect(true).toBeTruthy(); }, [])();
});
it('should call the executeChunk function if the condition is true', () => {
expect.assertions(1);
executeIf(() => true, [])({}, {}, () => { expect(true).toBeTruthy(); });
});
it('should NOT call the executeChunk function if the condition is true', () => {
expect.assertions(0);
executeIf(() => false, [])({}, {}, () => { expect(true).toBeTruthy(); });
});
it('should pass the container to the condition parameter', () => {
expect.assertions(1);
executeIf(({ body }) => body.captain === 'Jean-Luc', [])(
{ body: { captain: 'Jean-Luc' } },
{},
() => { expect(true).toBeTruthy(); },
);
});
it('should pass the request to the condition parameter', () => {
expect.assertions(1);
executeIf((_, { headers }) => headers.authorization === 'Picard-Gamma-6-0-7-3', [])(
{},
{ headers: { authorization: 'Picard-Gamma-6-0-7-3' } },
() => { expect(true).toBeTruthy(); },
);
});
it('should pass the needed parameters to the function executeChunk', () => {
expect.assertions(3);
const workflow = ['step1', 'step2'];
const container = { body: {} };
const request = { headers: {} };
executeIf(() => true, workflow)(
container,
request,
(workflowParameter, containerParameter, requestParameter) => {
expect(workflowParameter).toBe(workflow);
expect(containerParameter).toBe(container);
expect(requestParameter).toBe(request);
},
);
});
it('should await for the execution of the executeChunk', async () => {
let value = 0;
const wait = () => new Promise((resolve) => setTimeout(() => {
value = 1;
resolve();
}), 50);
await executeIf(() => true, [])({}, {}, wait);
expect(value).toEqual(1);
});
});
1 change: 1 addition & 0 deletions test/workers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const workers = require('../../workers');
describe('workers/index', () => {
it('should correctly expose all the workers', () => {
expect(workers.aggregate).toBeInstanceOf(Function);
expect(workers.executeIf).toBeInstanceOf(Function);
expect(workers.filter).toBeInstanceOf(Function);
expect(workers.forwardedHost).toBeInstanceOf(Function);
expect(workers.routeMatch).toBeInstanceOf(Function);
Expand Down
10 changes: 10 additions & 0 deletions workers/executeIf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 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.
*/

module.exports = (condition, workflow) => async (container, request, executeChunk) => {
if (condition(container, request)) await executeChunk(workflow, container, request);
};
2 changes: 2 additions & 0 deletions workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const aggregate = require('./aggregate');
const executeIf = require('./executeIf');
const filter = require('./filter');
const forwardedHost = require('./forwardedHost');
const keepHeaders = require('./keepHeaders');
Expand All @@ -19,6 +20,7 @@ const waitFor = require('./waitFor');

module.exports = {
aggregate,
executeIf,
filter,
forwardedHost,
keepHeaders,
Expand Down

0 comments on commit 19a7688

Please sign in to comment.