Skip to content

Commit c32abb4

Browse files
authored
Merge pull request #675 from kuzzleio/7.8.0-proposal
# [7.8.0](https://github.com/kuzzleio/sdk-javascript/releases/tag/7.8.0) (2021-12-20) #### New features - [ [#671](#671) ] Add Observer class ([Aschen](https://github.com/Aschen)) #### Enhancements - [ [#668](#668) ] Propagates every arguments to Kuzzle ([Aschen](https://github.com/Aschen)) ---
2 parents 7cf06a5 + e2d8302 commit c32abb4

File tree

33 files changed

+2148
-505
lines changed

33 files changed

+2148
-505
lines changed

.eslintrc-ts.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"no-shadow": "off",
1414
"@typescript-eslint/no-shadow": "error",
1515
"@typescript-eslint/no-explicit-any": 0,
16-
"@typescript-eslint/explicit-module-boundary-types": 0
16+
"@typescript-eslint/explicit-module-boundary-types": 0,
17+
"@typescript-eslint/no-empty-interface": 0
1718
}
1819
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Script to refactor inline options declaration into external interfaces
3+
* inheriting from the default "ArgsDefault" interface.
4+
*
5+
* @example
6+
*
7+
export class SomeController {
8+
helloWorld (
9+
name: string,
10+
options: {
11+
queuable?: boolean,
12+
timeout?: number,
13+
age?: number;
14+
} = {}
15+
): string {
16+
return name;
17+
}
18+
}
19+
20+
* This code will became
21+
22+
export class SomeController {
23+
helloWorld (
24+
name: string,
25+
options: ArgsSomeControllerHelloWorld = {}
26+
): string {
27+
return name;
28+
}
29+
}
30+
31+
interface ArgsSomeControllerHelloWorld extends ArgsDefault {
32+
age?: number;
33+
}
34+
35+
*/
36+
37+
/* Script Arguments ==================================================== */
38+
39+
const filePath = process.argv[2];
40+
const className = process.argv[3];
41+
42+
if (! filePath || ! className) {
43+
console.log(`Usage: node ${process.argv[1]} <file path> <class name>`);
44+
}
45+
46+
/* ===================================================================== */
47+
48+
import { Project, SyntaxKind, ParameterDeclaration } from 'ts-morph';
49+
50+
function upFirst (string) {
51+
return string.charAt(0).toUpperCase() + string.slice(1);
52+
}
53+
54+
// initialize
55+
const project = new Project({});
56+
57+
const file = project.addSourceFileAtPath(filePath);
58+
const loadedClass = file.getClassOrThrow(className);
59+
60+
for (const method of loadedClass.getMethods()) {
61+
if (method.getScope() !== 'public') {
62+
continue;
63+
}
64+
65+
// Get the parameter named "options"
66+
const options = method.getParameter('options');
67+
68+
if (options) {
69+
const argsInterface = createArgsInterface(method.getName(), options);
70+
71+
// Change the type of the "options" parameter with our newly created interface
72+
options.setType(argsInterface.getName())
73+
}
74+
}
75+
76+
77+
function createArgsInterface (methodName: string, options: ParameterDeclaration) {
78+
const argsInterface = file.addInterface({
79+
name: `Args${loadedClass.getName()}${upFirst(methodName)}`,
80+
extends: ['ArgsDefault'],
81+
isExported: true,
82+
});
83+
84+
// Get the AST node containing the type definition
85+
const optionsType = options.getTypeNode()
86+
87+
if (optionsType) {
88+
// The SyntaxList contains properties type definitions
89+
const syntaxList = optionsType.getChildSyntaxList();
90+
91+
for (const child of syntaxList.getChildren()) {
92+
93+
// Get the property name (e.g. "queuable")
94+
const name = child.getChildrenOfKind(SyntaxKind.Identifier)[0].getText();
95+
// Ignore common arguments
96+
if (['queuable', 'timeout'].includes(name)) {
97+
continue;
98+
}
99+
100+
// Is it an optional property? (e.g. "queuable?")
101+
const hasQuestionToken = Boolean(child.getChildrenOfKind(SyntaxKind.QuestionToken)[0]);
102+
103+
// Get the type of the property, the type node is located just after the "ColonToken" node
104+
const type = child.getChildrenOfKind(SyntaxKind.ColonToken)[0].getNextSibling().getText();
105+
106+
argsInterface.addProperty({ name, type, hasQuestionToken });
107+
}
108+
}
109+
110+
console.log(argsInterface.getText());
111+
112+
return argsInterface;
113+
}
114+
115+
116+
117+
async function run () {
118+
await project.save();
119+
}
120+
121+
run();

doc/7/controllers/auth/search-api-keys/snippets/search-api-keys-es.test.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

doc/7/controllers/security/get-profile-mapping/snippets/get-profile-mapping.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ try {
33

44
console.log(response);
55
/*
6-
{ mapping:
7-
{ policies:
8-
{ properties:
9-
{ restrictedTo:
10-
{ properties:
11-
{ collections:
12-
{ type: 'text',
13-
fields: { keyword: { type: 'keyword', ignore_above: 256 } } },
14-
index:
15-
{ type: 'text',
16-
fields: { keyword: { type: 'keyword', ignore_above: 256 } } } } },
17-
roleId: { type: 'keyword' } } } } }
6+
{
7+
"mapping": {
8+
"policies": {
9+
"properties": {
10+
"roleId": {
11+
"type": "keyword"
12+
},
13+
"restrictedTo": {
14+
"properties": {
15+
"collections": {
16+
"type": "text"
17+
}
18+
},
19+
"index": {
20+
"type": "text"
21+
}
22+
}
23+
}
24+
},
25+
}
26+
}
1827
*/
1928
} catch (e) {
2029
console.error(e);

doc/7/controllers/security/get-profile-mapping/snippets/get-profile-mapping.test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ name: security#getProfileMapping
22
description: get profile mapping
33
template: default
44
expected:
5-
- "mapping: { policies: { properties: \\[Object\\] } }"
5+
- "mapping: { policies: { properties: \\[Object\\]"

doc/7/controllers/security/search-api-keys/snippets/search-api-keys-es.test.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
code: true
3+
type: page
4+
title: get
5+
description: Observer get method
6+
---
7+
8+
# get
9+
10+
<SinceBadge version="7.8.0" />
11+
12+
Gets a realtime document
13+
14+
::: info
15+
This method uses the [Document.get](/sdk/js/7/controllers/document/get) method under the hood to retrieve document.
16+
:::
17+
18+
## Arguments
19+
20+
```js
21+
get (index: string, collection: string, id: string, options: any): Promise<RealtimeDocument>
22+
```
23+
24+
| Argument | Type | Description |
25+
|----------|------|-------------|
26+
| `index` | <pre>string</pre> | Index name |
27+
| `collection` | <pre>string</pre> | Collection name |
28+
| `id` | <pre>string</pre> | Document ID |
29+
| `options` | <pre>any</pre> | Additional options |
30+
31+
## Usage
32+
33+
```js
34+
const observer = new Observer(sdk);
35+
36+
const doc = await observer.get('nyc-open-data', 'yellow-taxi', 'some-id');
37+
38+
console.log(doc);
39+
/*
40+
RealtimeDocument {
41+
_id: 'some-id',
42+
_source: {
43+
name: 'aschen',
44+
age: '29',
45+
_kuzzle_info: {
46+
author: '-1',
47+
createdAt: 1638432270522,
48+
updatedAt: null,
49+
updater: null
50+
}
51+
},
52+
deleted: false
53+
}
54+
*/
55+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
code: true
3+
type: branch
4+
title: Observer
5+
description: Observer class documentation
6+
---
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
code: false
3+
type: page
4+
title: Introduction
5+
description: Observer class
6+
order: 0
7+
---
8+
9+
# Observer
10+
11+
<SinceBadge version="7.8.0" />
12+
13+
The `Observer` class allows to manipulate realtime documents.
14+
A `RealtimeDocument` is like a normal document from Kuzzle except that it is
15+
connected to the realtime engine and it's content will change with changes
16+
occuring on the database.
17+
18+
They can be retrieved using methods with the same syntax as in the Document
19+
Controller:
20+
21+
```js
22+
const docs = await observer.get('nyc-open-data', 'yellow-taxi', 'foobar');
23+
24+
const result = await observer.search('nyc-open-data', 'yellow-taxi', {
25+
query: { exists: 'licence' }
26+
});
27+
```
28+
29+
Realtime documents are resources that should be disposed via the [Observer.stop](/sdk/js/7/core-classes/observer/stop) method otherwise subscriptions will never be terminated, documents will be kept into memory, which might lead to a memory leak.
30+
31+
```js
32+
await observer.stop('nyc-open-data', 'yellow-taxi');
33+
```
34+
35+
A good frontend practice is to instantiate one observer for the actual page
36+
and/or component(s) displaying realtime documents and to dispose them when
37+
they are not displayed anymore.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
code: true
3+
type: page
4+
title: mGet
5+
description: Observer mGet method
6+
---
7+
8+
# mGet
9+
10+
<SinceBadge version="7.8.0" />
11+
12+
13+
Gets multiple realtime documents.
14+
15+
::: info
16+
This method uses the [Document.mGet](/sdk/js/7/controllers/document/m-get) method under the hood to retrieve documents.
17+
:::
18+
19+
## Arguments
20+
21+
```js
22+
mGet (index: string, collection: string, ids: string[]): Promise<{ successes: RealtimeDocument[]; errors: string[]; }>
23+
```
24+
25+
| Argument | Type | Description |
26+
|----------|------|-------------|
27+
| `index` | <pre>string</pre> | Index name |
28+
| `collection` | <pre>string</pre> | Collection name |
29+
| `ids` | <pre>string[]</pre> | Document IDs |
30+
31+
## Usage
32+
33+
```js
34+
const observer = new Observer(sdk);
35+
36+
const docs = await observer.get('nyc-open-data', 'yellow-taxi', ['foo', 'bar']);
37+
38+
console.log(docs);
39+
/*
40+
[
41+
RealtimeDocument {
42+
_id: 'foo',
43+
_source: {
44+
name: 'aschen',
45+
age: '28',
46+
_kuzzle_info: {
47+
author: '-1',
48+
createdAt: 1638432270522,
49+
updatedAt: null,
50+
updater: null
51+
}
52+
},
53+
deleted: false
54+
},
55+
RealtimeDocument {
56+
_id: 'bar',
57+
_source: {
58+
name: 'dana',
59+
age: '30',
60+
_kuzzle_info: {
61+
author: '-1',
62+
createdAt: 1638432270522,
63+
updatedAt: null,
64+
updater: null
65+
}
66+
},
67+
deleted: false
68+
}
69+
]
70+
*/
71+
```

0 commit comments

Comments
 (0)