Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(funnel): allow pluginRequest to trigger events #2542

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions doc/2/guides/develop-on-kuzzle/event-system/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,25 @@ await app.trigger('app-name/file-available', fileUrl);
::: warning
If an internal event is triggered, the payload must be the same as the original event.
:::

**Events can be triggered** with SDK and controllers actions.
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved

::: warning
By default, actions executed through the embedded SDK or controllers won't trigger any events and thus no pipe or hooks will be called.
:::

This behaviour is set in order to prevent an infinite loop in which a pipe calls a controller generating an event calling this same pipe again and again.

It is nonetheless possible to pass the flag `allowTrigggerEvents` to the options parameters of the controller to allow events firing :
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved

```ts
await this.sdk.document.create(
"index",
'collection',
{
contentOfDocument: 'CREATED VIA CONTROLLER',
},
_idOfDocument,
{ allowTriggerEvents: true },
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved
);
```
3 changes: 3 additions & 0 deletions lib/api/funnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ class Funnel {
*/
async executePluginRequest(request) {
try {
if (request.input.allowTriggerEvents) {
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved
return await this.processRequest(request);
}
return await doAction(this.getController(request), request);
} catch (e) {
this.handleErrorDump(e);
Expand Down
10 changes: 9 additions & 1 deletion lib/api/request/requestInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const _body = "body\u200b";
const _headers = "headers\u200b";
const _controller = "controller\u200b";
const _action = "action\u200b";
const _allowTriggerEvents = "allowTriggerEvents\u200b";
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved

// any property not listed here will be copied into
// RequestInput.args
Expand Down Expand Up @@ -155,6 +156,7 @@ export class RequestInput {
this[_body] = null;
this[_controller] = null;
this[_action] = null;
this[_allowTriggerEvents] = null;
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved

// default value to null for former "resources" to avoid breaking
this.args = {};
Expand All @@ -181,6 +183,7 @@ export class RequestInput {
this.body = data.body;
this.controller = data.controller;
this.action = data.action;
this.allowTriggerEvents = data.allowTriggerEvents;
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -263,7 +266,12 @@ export class RequestInput {
this[_action] = assert.assertString("action", str);
}
}

get allowTriggerEvents(): boolean | undefined {
return this[_allowTriggerEvents];
}
set allowTriggerEvents(bool: boolean) {
this[_allowTriggerEvents] = bool === true ? true : undefined;
}
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved
/**
* Request body.
* In Http it's the request body parsed.
Expand Down
28 changes: 27 additions & 1 deletion test/api/funnel/executePluginRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const should = require("should");
const sinon = require("sinon");

const KuzzleMock = require("../../mocks/kuzzle.mock");
const { MockNativeController } = require("../../mocks/controller.mock");
const FunnelController = require("../../../lib/api/funnel");
Expand Down Expand Up @@ -102,4 +101,31 @@ describe("funnel.executePluginRequest", () => {
done(e);
});
});

it("should trigger pipes if allowTriggerEvent is enabled", async () => {
const request = new Request({
controller: "testme",
action: "succeed",
allowTriggerEvents: true,
});

return funnel.executePluginRequest(request).then((response) => {
should(response).be.exactly(request);
should(kuzzle.pipe).calledWith("testme:beforeSucceed");
should(kuzzle.pipe).calledWith("testme:afterSucceed");
});
});

it("should not trigger pipes if allowTriggerEvent is disabled", async () => {
const request = new Request({
controller: "testme",
action: "succeed",
});

return funnel.executePluginRequest(request).then((response) => {
should(response).be.exactly(request);
should(kuzzle.pipe).not.calledWith("testme:beforeSucceed");
should(kuzzle.pipe).not.calledWith("testme:afterSucceed");
});
});
QuentinRousselet marked this conversation as resolved.
Show resolved Hide resolved
});
Loading