-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters