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 all 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 are not triggered** with the embedded SDK and controllers actions.

::: 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. On the contrary, controllers accessed by the external SDK through HTTP or Websocket requests will always fire events.
:::

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 `triggerEvents` to the options parameters of the controller to fire events :

```ts
await this.sdk.document.create(
"index",
'collection',
{
contentOfDocument: 'CREATED VIA CONTROLLER',
},
_idOfDocument,
{ triggerEvents: true },
);
```
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.triggerEvents) {
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 _triggerEvents = "triggerEvents\u200b";

// 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[_triggerEvents] = null;

// 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.triggerEvents = data.triggerEvents;
}

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

get triggerEvents(): boolean | undefined {
return this[_triggerEvents];
}
set triggerEvents(bool: boolean) {
this[_triggerEvents] = bool === true ? true : undefined;
}
/**
* 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 triggerEvent is enabled", async () => {
const request = new Request({
controller: "testme",
action: "succeed",
triggerEvents: 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 triggerEvent 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");
});
});
});
Loading