-
Notifications
You must be signed in to change notification settings - Fork 2
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
4 changed files
with
80 additions
and
5 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,32 @@ | ||
let plugins = []; | ||
|
||
function register(plugin) { | ||
//@TODO: validate the plugin | ||
plugins.push(plugin); | ||
} | ||
|
||
function getPlugins() { | ||
return plugins; | ||
} | ||
|
||
async function executePreRequest(event) { | ||
let response = null; | ||
//@TODO: only loop through plugins with preRequest. | ||
//@TODO: allow multiple reponses, currently last one wins. | ||
for (let i = 0; i < plugins.length; i++) { | ||
let plugin = plugins[i]; | ||
if (plugin.preRequest) { | ||
response = await plugin.preRequest(event); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
|
||
async function executePostRequest() { | ||
|
||
} | ||
|
||
module.exports.register = register; | ||
module.exports.getPlugins = getPlugins; | ||
module.exports.executePreRequest = executePreRequest; |
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,35 @@ | ||
const plugins = require('../src/plugins'); | ||
|
||
test('Plugin registration', () => { | ||
plugins.register({name: 'foo'}); | ||
plugins.register({name: 'bar'}); | ||
|
||
const pluginList = plugins.getPlugins(); | ||
expect(pluginList).toHaveLength(2); | ||
}); | ||
|
||
test('Do not execute non-existent preRequest', async () => { | ||
plugins.register({name: 'Test'}); | ||
|
||
const response = await plugins.executePreRequest({test: 'test'}); | ||
expect(response).toBeNull(); | ||
}); | ||
|
||
test('Execute preRequest', async () => { | ||
const statusCode = 200; | ||
const body = 'foo'; | ||
|
||
plugins.register({ | ||
name: 'Hello World', | ||
preRequest: async function(event) { | ||
return { | ||
statusCode: 200, | ||
body: event.test, | ||
} | ||
}, | ||
}); | ||
|
||
const response = await plugins.executePreRequest({test: body}); | ||
expect(response.body).toEqual(body); | ||
expect(response.statusCode).toEqual(statusCode); | ||
}); |