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

Port #284 to 3-dev #309

Merged
merged 3 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions src/sdk/js/6/core-classes/base-controller/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
code: true
type: branch
title: BaseController
description: BaseController object documentation
order: 500
---
13 changes: 13 additions & 0 deletions src/sdk/js/6/core-classes/base-controller/introduction/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
code: true
type: page
title: Introduction
description: BaseController class
order: 0
---

# BaseController

Base class for a [custom SDK controller](/sdk/js/6/essentials/extend-sdk/#define-a-custom-sdk-controller).

Custom SDK controllers have to extend the `BaseController` class to be added with the [Kuzzle.useController](/sdk/js/6/core-classes/kuzzle/use-controller) method.
16 changes: 16 additions & 0 deletions src/sdk/js/6/core-classes/base-controller/properties/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
code: true
type: page
title: Properties
description: BaseController Properties
---

# Properties

| Property name | Type | Description |
| -------------------- | -------- | --------------------------------------- |
| `name` | <pre>string</pre> | Controller name |
| `kuzzle` | <a href="/sdk/js/6/core-classes/kuzzle/constructor"><pre>Kuzzle</pre></a> | Kuzzle SDK instance |

**Note:**
- The `name` property will be injected in the request sent by the [BaseController.query](/sdk/js/6/core-classes/base-controller/query) method if the `controller` property is not set.
55 changes: 55 additions & 0 deletions src/sdk/js/6/core-classes/base-controller/query/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
code: true
type: page
title: query
description: Wrapper around the Kuzzle.query method
---

# query

Base method used to send queries to a Kuzzle controller, following the [API Documentation](/core/1/api/essentials/connecting-to-kuzzle).

This method injects the controller name into the the request and forwards it to the original [Kuzzle.query](/sdk/js/6/core-classes/kuzzle/query) method.

## Arguments

```javascript
query (request, [options]);
```

<br/>

| Argument | Type | Description |
| -------------- | --------- | ------------- |
| `request` | <pre>object</pre> | API request |
| `options` | <pre>object</pre> | Optional query options |

### request

All properties necessary for the Kuzzle API can be added in the request object.
The following properties are the most common.

| Property | Type | Description |
| -------------- | --------- | ------------- |
| `action` | <pre>string</pre> | Action name (required) |
| `controller` | <pre>string</pre> | Controller name |
| `body` | <pre>object</pre> | Query body for this action |
| `index` | <pre>string</pre> | Index name for this action |
| `collection` | <pre>string</pre> | Collection name for this action |
| `_id` | <pre>string</pre> | id for this action |
| `volatile` | <pre>object</pre> | Additional information to send to Kuzzle |

**Note:**
- If the `controller` property is not set, the controller [name property](/sdk/js/6/core-classes/base-controller/properties) will be used

### options

Additional query options

| Property | Type<br/>(default) | Description |
| -------------- | --------- | ------------- |
| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again |

## Resolves

Resolve to the raw Kuzzle API response. See the [API Documentation](/core/1/api/essentials/connecting-to-kuzzle).
2 changes: 1 addition & 1 deletion src/sdk/js/6/core-classes/kuzzle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ code: true
type: branch
title: Kuzzle
description: Kuzzle object documentation
order: 500
order: 510
---
2 changes: 1 addition & 1 deletion src/sdk/js/6/core-classes/kuzzle/query/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ query(request, [options]);

| Argument | Type | Description |
| --------- | ----------------- | ---------------------- |
| `request` | <pre>object</pre> | API request options |
| `request` | <pre>object</pre> | API request |
| `options` | <pre>object</pre> | Optional query options |

### request
Expand Down
34 changes: 34 additions & 0 deletions src/sdk/js/6/core-classes/kuzzle/use-controller/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
code: true
type: page
title: useController
description: Adds a new controller to the SDK
---

# useController

Adds a new controller to the SDK.

*See also:*
- *[Extend the SDK](/sdk/js/6/essentials/extend-sdk)*

## Arguments

```javascript
useController (ControllerClass, accessor);
```

<br/>

| Argument | Type | Description |
| -------------- | --------- | ------------- |
| `ControllerClass` | <pre>Class</pre> | Controller class. Must inherit from [BaseController](/sdk/js/6/core-classes/base-controller) |
| `accessor` | <pre>string</pre> | Accessor name for the controller in the Kuzzle object |

## Returns

Returns the Kuzzle object.

## Usage

<<< ./snippets/use-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class TaxiController extends BaseController {
constructor (kuzzle) {
super(kuzzle, 'my-plugin/taxi');
}

enroll () {
return this.query({
action: 'enroll'
});
}
}

const kuzzle = new Kuzzle(
new WebSocket('kuzzle')
);

// Add the custom SDK controller
kuzzle.useController(TaxiController, 'taxi');

const run = async () => {
try {
await kuzzle.connect();

// Call the custom SDK controller action
console.log(await kuzzle.taxi.enroll());

console.log('Success');
} catch (error) {
console.error(error);
} finally {
kuzzle.disconnect();
}
};

run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: kuzzle#useController
description: Adds a new controller to the SDK
hooks:
before:
after:
template: controller
expected: Success

sdk: js
version: 6