Skip to content

Commit

Permalink
postRequest plugin callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmac committed Feb 12, 2024
1 parent 0b5aa35 commit fb35973
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ function getPlugins() {
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);
response = await plugin.preRequest(event, response);
}
}

return response;
}

async function executePostRequest() {
async function executePostRequest(event, response) {
//@TODO: only loop through plugins with postRequest.
for (let i = 0; i < plugins.length; i++) {
let plugin = plugins[i];
if (plugin.postRequest) {
response = await plugin.postRequest(event, response);
}
}

return response;
}

module.exports.register = register;
module.exports.getPlugins = getPlugins;
module.exports.executePreRequest = executePreRequest;
module.exports.executePreRequest = executePreRequest;
module.exports.executePostRequest = executePostRequest;
48 changes: 46 additions & 2 deletions tests/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ test('Execute preRequest', async () => {
const body = 'foo';

plugins.register({
name: 'Hello World',
preRequest: async function(event) {
name: 'preRequest',
preRequest: async function(event, response) {
return {
statusCode: 200,
body: event.test,
Expand All @@ -32,4 +32,48 @@ test('Execute preRequest', async () => {
const response = await plugins.executePreRequest({test: body});
expect(response.body).toEqual(body);
expect(response.statusCode).toEqual(statusCode);
});

test('Execute multiple preRequest', async () => {
const body = 'foo';

plugins.register({
name: '1',
preRequest: async function(event, response) {
return {
statusCode: 200,
body: event.test,
}
},
});

plugins.register({
name: '2',
preRequest: async function(event, response) {
if (response?.statusCode) {
response.statusCode = response.statusCode + 1;
}
return response;
},
});

const response = await plugins.executePreRequest({test: body});
expect(response.body).toEqual(body);
expect(response.statusCode).toEqual(201);
});

test('Execute postRequest', async () => {
plugins.register({
name: 'postRequest',
postRequest: async function(event, response) {
return {
statusCode: response.statusCode + 1,
body: 'Foo'
}
}
});

const response = await plugins.executePostRequest({test: 'foo'}, {statusCode: 200, body: 'Test'});
expect(response.body).toEqual('Foo');
expect(response.statusCode).toEqual(201);
});

0 comments on commit fb35973

Please sign in to comment.