Skip to content

Commit 15bb02b

Browse files
Yoann-Abbesscottinet
authored andcommitted
[KZL-907] Getting started dev plugin (#276)
## What does this PR do? This PR responds to this [ticket](https://jira.kaliop.net/browse/KZL-907). Also, it increases the readability of documentation's plugin part. ### How should this be manually tested? [Netify](https://deploy-preview-276--kuzzle-doc-v2.netlify.com/plugins/1/essentials/introduction/) ### Other changes - Re-arrange plugin documentation path - Update dead link in boilerplate repository ### Boyscout Prettier
1 parent 5638bf2 commit 15bb02b

File tree

22 files changed

+709
-628
lines changed

22 files changed

+709
-628
lines changed

src/guide/1/essentials/plugins/index.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ For example, imagine you are developing a mobile application that accesses a **t
1212

1313
Kuzzle's **[Plugin Engine]({{ site_base_path }}plugins/1)** is a powerful feature that ensures that Kuzzle meets any project requirement:
1414

15-
* select from a set of prebuilt plugins (such as the [OAuth2 Authentication Plugin](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth) or the [MQTT Protocol](https://github.com/kuzzleio/protocol-mqtt)).
16-
* [create your own plugin]({{ site_base_path }}plugins/1/essentials) to meet your specific requirements.
15+
- select from a set of prebuilt plugins (such as the [OAuth2 Authentication Plugin](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth) or the [MQTT Protocol](https://github.com/kuzzleio/protocol-mqtt)).
16+
- [create your own plugin]({{ site_base_path }}plugins/1/essentials) to meet your specific requirements.
1717

1818
---
1919

2020
## Plugins
2121

2222
Plugins are used to extend Kuzzle's functionalities. They are loaded into Kuzzle during startup and share its execution thread. A plugin can implement one or multiple of the following interfaces:
2323

24-
[Hooks]({{ site_base_path }}plugins/1/essentials/hooks): adds asynchronous listeners that perform operations triggered by data events. When a listened event occurs, the data is sent to the listeners and Kuzzle continues its process without waiting for the listener to complete.
24+
[Hooks]({{ site_base_path }}plugins/1/hooks): adds asynchronous listeners that perform operations triggered by data events. When a listened event occurs, the data is sent to the listeners and Kuzzle continues its process without waiting for the listener to complete.
2525

26-
_Example - "Write a log to a third-party logging service every time a document is deleted"_. The [Logger Plugin](https://github.com/kuzzleio/kuzzle-plugin-logger) (shipped with Kuzzle) uses this feature to log all the data-related events.
26+
_Example - "Write a log to a third-party logging service every time a document is deleted"_. The [Logger Plugin](https://github.com/kuzzleio/kuzzle-plugin-logger) (shipped with Kuzzle) uses this feature to log all the data-related events.
2727

28-
[Pipes]({{ site_base_path }}plugins/1/essentials/pipes): adds synchronous listeners that perform operations triggered by data events. When a listened event occurs, the data is passed synchronously to listeners, each modifying the input data and returning the result to the next listener. Kuzzle waits until the last listener completes and returns its data. If any listener returns an error, it will interrupt the Kuzzle lifecycle, and the thrown error will be used as a response by Kuzzle.
28+
[Pipes]({{ site_base_path }}plugins/1/pipes): adds synchronous listeners that perform operations triggered by data events. When a listened event occurs, the data is passed synchronously to listeners, each modifying the input data and returning the result to the next listener. Kuzzle waits until the last listener completes and returns its data. If any listener returns an error, it will interrupt the Kuzzle lifecycle, and the thrown error will be used as a response by Kuzzle.
2929

30-
_Example - "Compare the ordered quantity with the available stock and return an error if the amount of ordered items exceeds the amount in stock"_.
30+
_Example - "Compare the ordered quantity with the available stock and return an error if the amount of ordered items exceeds the amount in stock"_.
3131

32-
[Controllers]({{ site_base_path }}plugins/1/essentials/controllers): extends Kuzzle API.
32+
[Controllers]({{ site_base_path }}plugins/1/controllers): extends Kuzzle API.
3333

34-
_Example - "Expose a `checkout` API endpoint that handles a third-party payment process"_.
34+
_Example - "Expose a `checkout` API endpoint that handles a third-party payment process"_.
3535

3636
[Strategies]({{ site_base_path }}plugins/1/essentials/strategies): add an authentication strategy to identify and authenticate users.
3737

38-
_Example - "Enable OAuth based authentication in Kuzzle"_
39-
Kuzzle ships with the [Local Strategy Plugin](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-local) and thanks to PassportJS, more than 300 authentication strategies are readily available.
38+
_Example - "Enable OAuth based authentication in Kuzzle"_
39+
Kuzzle ships with the [Local Strategy Plugin](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-local) and thanks to PassportJS, more than 300 authentication strategies are readily available.
4040

4141
## Protocols
4242

@@ -61,10 +61,8 @@ Prior to loading the plugin into Kuzzle, you will need to load all of the plugin
6161

6262
To demonstrate, we are going to install the [**Plugin Boilerplate**](https://github.com/kuzzleio/kuzzle-core-plugin-boilerplate), a plugin example that uses all features available to a plugin.
6363

64-
6564
Go to the Kuzzle installation folder and type:
6665

67-
6866
```bash
6967
# Open plugins/available folder
7068
cd plugins/available
@@ -105,13 +103,8 @@ Once Kuzzle has restarted, check the server information at `http://localhost:751
105103
"document:beforeUpdate",
106104
"core:overload"
107105
],
108-
"pipes": [
109-
"document:beforeCreate",
110-
"realtime:beforePublish"
111-
],
112-
"controllers": [
113-
"kuzzle-core-plugin-boilerplate/myNewController"
114-
],
106+
"pipes": ["document:beforeCreate", "realtime:beforePublish"],
107+
"controllers": ["kuzzle-core-plugin-boilerplate/myNewController"],
115108
"routes": [
116109
{
117110
"verb": "get",
@@ -126,9 +119,7 @@ Once Kuzzle has restarted, check the server information at `http://localhost:751
126119
"action": "myNewAction"
127120
}
128121
],
129-
"strategies": [
130-
"dummy"
131-
]
122+
"strategies": ["dummy"]
132123
}
133124
}
134125
}
@@ -138,13 +129,13 @@ Once Kuzzle has restarted, check the server information at `http://localhost:751
138129
```
139130

140131
Note that the plugin description above contains a property for each plugin component:
132+
141133
- `hooks` asynchronous operations that depend on data-related events
142134
- `pipes` synchronous operations that depend on data-related events
143135
- `controllers` list of exposed actions in the API
144136
- `routes` list of exposed actions in the **REST** API
145137
- `strategies` list of exposed authentication strategies
146138

147-
148139
---
149140

150141
## Installing protocols
@@ -179,6 +170,7 @@ ln -s ../available/protocol-mqtt .
179170
To get more insight into how plugins work, please refer to the [Plugin Reference]({{ site_base_path }}plugins/1).
180171

181172
Here is a list of official plugins:
173+
182174
- [**kuzzle-plugin-auth-passport-local**](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-local): authentication Plugin shipped with Kuzzle
183175
- [**kuzzle-plugin-logger**](https://github.com/kuzzleio/kuzzle-plugin-logger): plugin shipped with Kuzzle
184176
- [**kuzzle-plugin-auth-passport-oauth**](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth): authentication plugin

src/plugins/1/accessors/trigger/index.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ title: trigger
99

1010
Triggers a custom event.
1111

12-
This allows interactions with other plugins using [hooks]({{ site_base_path }}plugins/1/essentials/hooks/) or [pipes]({{ site_base_path }}plugins/1/essentials/pipes/).
12+
This allows interactions with other plugins using [hooks]({{ site_base_path }}plugins/1/hooks/) or [pipes]({{ site_base_path }}plugins/1/pipes/).
1313

1414
## Arguments
1515

1616
```js
17-
trigger(event, [payload])
17+
trigger(event, [payload]);
1818
```
1919

2020
<br/>
2121

22-
| Arguments | Type | Description |
23-
|-----------|------|-------------|
24-
| `event` | <pre>string</pre> | Custom event name |
25-
| `payload` | <pre>object</pre> | Event payload |
22+
| Arguments | Type | Description |
23+
| --------- | ----------------- | ----------------- |
24+
| `event` | <pre>string</pre> | Custom event name |
25+
| `payload` | <pre>object</pre> | Event payload |
2626

27-
28-
**Note:** the triggered event is renamed using the following format:<br/>`plugin-<plugin name>:<event>`.
27+
**Note:** the triggered event is renamed using the following format:<br/>`plugin-<plugin name>:<event>`.
2928

3029
## Example
3130

@@ -37,13 +36,13 @@ context.accessors.trigger('someEvent', {
3736

3837
// Listening plugin
3938
class ListeningPlugin {
40-
constructor () {
39+
constructor() {
4140
this.hooks = {
4241
'plugin-some-plugin:someEvent': 'someEventListener'
4342
};
4443
}
4544

46-
someEventListener (payload) {
45+
someEventListener(payload) {
4746
this.doSomething(payload);
4847
}
4948
}

src/plugins/1/essentials/controllers/index.md renamed to src/plugins/1/controllers/Documentation/index.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
layout: full.html.hbs
3-
title: Controllers
3+
title: Documentation
44
order: 400
55
---
66

77
# Controllers
88

99
Kuzzle's API is divided into controllers, each exposing executable actions (see [API reference]({{ site_base_path }}api/1/essentials/query-syntax)).
1010

11-
Plugins can extend Kuzzle's API by adding new controllers to it.
11+
Plugins can extend Kuzzle's API by adding new controllers to it.
1212

1313
[Security access]({{ site_base_path }}guide/1/essentials/security/) to plugin controllers must be given (or denied), using the exact same way as with native API controllers.
1414

@@ -21,7 +21,7 @@ To avoid naming conflicts, Kuzzle prefixes plugin controllers names with the plu
2121
### HTTP
2222

2323
```http
24-
URL: http://<server>:<port>/_plugin/<plugin name>/<url defined by the plugin>/<resources>
24+
URL: http://<server>:<port>/_plugin/<plugin name>/<url defined by the plugin>/<resources>
2525
Method: <verb defined by the plugin>
2626
```
2727

@@ -53,16 +53,17 @@ Kuzzle normalizes [queries]({{ site_base_path }}api/1/essentials/query-syntax) i
5353

5454
Quick summary of how queries are normalized:
5555

56-
* HTTP:
57-
* dynamic arguments provided in the URL, and query string arguments are stored in `request.input.args`
58-
* the body content is made available in `request.input.body`
59-
* if the URL contains an `index`, a `collection` or a `_id` argument, it will be stored in `request.input.resource`
60-
* request headers can be found in `request.context.connection.misc.headers`
56+
- HTTP:
6157

62-
* Other protocols:
63-
* the `body` property is stored in `request.input.body`
64-
* these root properties are available in `request.input.resource`: `index`, `collection`, `_id`
65-
* any other properties at the root of the query object will be stored in `request.input.args`
58+
- dynamic arguments provided in the URL, and query string arguments are stored in `request.input.args`
59+
- the body content is made available in `request.input.body`
60+
- if the URL contains an `index`, a `collection` or a `_id` argument, it will be stored in `request.input.resource`
61+
- request headers can be found in `request.context.connection.misc.headers`
62+
63+
- Other protocols:
64+
- the `body` property is stored in `request.input.body`
65+
- these root properties are available in `request.input.resource`: `index`, `collection`, `_id`
66+
- any other properties at the root of the query object will be stored in `request.input.args`
6667

6768
---
6869

@@ -78,7 +79,7 @@ Read more about these automatic controller events [here]({{ site_base_path }}plu
7879

7980
```javascript
8081
module.exports = class ControllerPlugin {
81-
constructor () {
82+
constructor() {
8283
/*
8384
Adds a new "newController" controller to Kuzzle's API.
8485
@@ -128,16 +129,26 @@ module.exports = class ControllerPlugin {
128129
property
129130
*/
130131
this.routes = [
131-
{verb: 'get', url: '/foo/:name', controller: 'newController', action: 'myAction'},
132-
{verb: 'post', url: '/bar', controller: 'newController', action: 'myOtherAction'}
132+
{
133+
verb: 'get',
134+
url: '/foo/:name',
135+
controller: 'newController',
136+
action: 'myAction'
137+
},
138+
{
139+
verb: 'post',
140+
url: '/bar',
141+
controller: 'newController',
142+
action: 'myOtherAction'
143+
}
133144
];
134145
}
135146

136147
/*
137148
Required plugin initialization function
138149
(see the "Plugin prerequisites" section)
139150
*/
140-
init (customConfig, context) {
151+
init(customConfig, context) {
141152
// plugin initialization
142153
}
143154

@@ -148,17 +159,20 @@ module.exports = class ControllerPlugin {
148159
This result can be of any JS type (scalar, object, array), and
149160
will be used to build a response to send to the requesting client
150161
*/
151-
actionFunction (request) {
162+
actionFunction(request) {
152163
// do action
153164

154165
// optional: set network specific headers
155166
if (request.context.protocol === 'http') {
156167
// expires in 1h
157-
request.response.setHeader('expires', new Date(Date.now() + 3600000).toUTCString());
168+
request.response.setHeader(
169+
'expires',
170+
new Date(Date.now() + 3600000).toUTCString()
171+
);
158172
}
159173

160174
// Resolve with the result content. For instance:
161-
return Promise.resolve({acknowledge: true});
175+
return Promise.resolve({ acknowledge: true });
162176
}
163177

164178
/*
@@ -168,9 +182,9 @@ module.exports = class ControllerPlugin {
168182
This result can be of any JS type (scalar, object, array), and
169183
will be used to build a response to send to the requesting client
170184
*/
171-
otherActionFunction (request) {
185+
otherActionFunction(request) {
172186
// do action
173187
return Promise.resolve(/* result content */);
174188
}
175-
}
189+
};
176190
```

src/plugins/1/controllers/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
layout: full.html.hbs
3+
title: Controllers
4+
order: 4
5+
---

0 commit comments

Comments
 (0)