Skip to content

Commit 3786e1f

Browse files
author
Adrien Maret
authored
Add query typing (#707)
A new BaseRequest interface can be extended to define the request type.
1 parent d96970b commit 3786e1f

File tree

9 files changed

+78
-16
lines changed

9 files changed

+78
-16
lines changed

doc/7/essentials/strong-typing/index.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,53 @@ The returned type is `KDocument<DeviceContent>` and it contains a `_source` prop
3232

3333
::: info
3434
By default, a generic document content with only a strongly defined `_kuzzle_info` property is returned.
35-
:::
35+
:::
36+
37+
## Kuzzle.query method
38+
39+
<SinceBadge version="auto-version"/>
40+
41+
The [Kuzzle.query](/sdk/js/7/core-classes/kuzzle/query) method can accept 2 optional types.
42+
43+
Those types are used to strongly type both the request payload and the response result for each API action used.
44+
45+
```js
46+
query<TRequest extends BaseRequest, TResult> (
47+
req: TRequest,
48+
opts: JSONObject = {},
49+
): Promise<ResponsePayload<TResult>>;
50+
```
51+
52+
You can define the `TRequest` type by extending the `BaseRequest` type. It corresponds to the payload sent to Kuzzle API.
53+
54+
```js
55+
interface NotificationSmsRequest extends BaseRequest {
56+
body: {
57+
phone: string;
58+
message: string;
59+
}
60+
}
61+
```
62+
63+
The `TResult` is just a definition of the expected result for the API action.
64+
65+
```js
66+
interface NotificationSmsResult {
67+
smsCount: number;
68+
}
69+
```
70+
71+
The complete usage with strong typing will be:
72+
73+
```js
74+
const { result } = await sdk.query<NotificationSmsRequest, NotificationSmsResult>({
75+
controller: 'notification',
76+
action: 'sms',
77+
body: {
78+
phone: '+33629951621',
79+
message: 'Hello, world',
80+
}
81+
});
82+
83+
result.smsCount; // number
84+
```

src/Kuzzle.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Deprecation } from './utils/Deprecation';
1515
import { uuidv4 } from './utils/uuidv4';
1616
import { proxify } from './utils/proxify';
1717
import { debug } from './utils/debug';
18-
import { JSONObject } from './types';
18+
import { BaseRequest, JSONObject } from './types';
1919
import { RequestPayload } from './types/RequestPayload';
2020
import { ResponsePayload } from './types/ResponsePayload';
2121
import { RequestTimeoutError } from './RequestTimeoutError';
@@ -766,7 +766,10 @@ export class Kuzzle extends KuzzleEventEmitter {
766766
* @param req
767767
* @param opts - Optional arguments
768768
*/
769-
query (req: RequestPayload = {}, opts: JSONObject = {}): Promise<ResponsePayload> {
769+
query<TRequest extends BaseRequest, TResult> (
770+
req: TRequest,
771+
opts: JSONObject = {},
772+
): Promise<ResponsePayload<TResult>> {
770773
if (typeof req !== 'object' || Array.isArray(req)) {
771774
throw new Error(`Kuzzle.query: Invalid request: ${JSON.stringify(req)}`);
772775
}
@@ -857,7 +860,7 @@ Discarded request: ${JSON.stringify(request)}`));
857860
requestTimeout,
858861
request,
859862
options
860-
).then((response: ResponsePayload) => {
863+
).then((response: ResponsePayload<TResult>) => {
861864
debug('RESPONSE', response);
862865

863866
return this.deprecationHandler.logDeprecation(response);

src/controllers/Base.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Kuzzle } from '../Kuzzle';
22
import { JSONObject } from '../types';
3-
import { RequestPayload } from '../types/RequestPayload';
43

54
export class BaseController {
65
private _name: string;
@@ -36,7 +35,7 @@ export class BaseController {
3635
* @param request
3736
* @param options
3837
*/
39-
query (request: RequestPayload = {}, options: any = {}): Promise<JSONObject> {
38+
query (request: any, options: any = {}): Promise<JSONObject> {
4039
request.controller = request.controller || this.name;
4140

4241
return this._kuzzle.query(request, options);

src/core/searchResult/SearchResultBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { JSONObject } from '../../types';
1+
import { BaseRequest, JSONObject } from '../../types';
22
import { RequestPayload } from '../../types/RequestPayload';
33
import { Kuzzle } from '../../Kuzzle';
44

@@ -60,7 +60,7 @@ export class SearchResultBase<T> implements SearchResult<T> {
6060

6161
constructor (
6262
kuzzle: Kuzzle,
63-
request: RequestPayload = {},
63+
request: BaseRequest,
6464
options: JSONObject = {},
6565
result: any = {}
6666
) {

src/types/BaseRequest.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { JSONObject } from './JSONObject';
2+
3+
export interface BaseRequest {
4+
controller: string;
5+
6+
action: string;
7+
8+
body?: JSONObject;
9+
}

src/types/RequestPayload.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { JSONObject } from './JSONObject';
55
*
66
* @see https://docs.kuzzle.io/core/2/api/payloads/request
77
*/
8-
export type RequestPayload = {
8+
export interface RequestPayload {
99
/**
1010
* API controller name
1111
*/
12-
controller?: string;
12+
controller: string;
1313

1414
/**
1515
* API action name
1616
*/
17-
action?: string;
17+
action: string;
1818

1919
/**
2020
* Index name
@@ -52,4 +52,4 @@ export type RequestPayload = {
5252
requestId?: string;
5353

5454
[key: string]: any;
55-
}
55+
}

src/types/ResponsePayload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { JSONObject } from './JSONObject';
55
*
66
* @see https://docs.kuzzle.io/core/2/api/payloads/response
77
*/
8-
export type ResponsePayload = {
8+
export interface ResponsePayload<TResult = JSONObject> {
99
/**
1010
* API controller name
1111
*/
@@ -86,7 +86,7 @@ export type ResponsePayload = {
8686
/**
8787
* API action result
8888
*/
89-
result: any;
89+
result: TResult;
9090

9191
/**
9292
* HTTP status code

src/types/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ export * from './mResponses';
2222

2323
export * from './KDocument';
2424

25-
export * from './RequestPayload';
25+
export * from './RequestPayload';
26+
27+
export * from './BaseRequest';

test/kuzzle/query.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe('Kuzzle query management', () => {
174174
};
175175

176176
kuzzle.volatile = volatile;
177-
kuzzle.query();
177+
kuzzle.query({});
178178
should(kuzzle._timeoutRequest).be.calledOnce();
179179
should(kuzzle._timeoutRequest).be.calledWithMatch(kuzzle._requestTimeout, {volatile: kuzzle.volatile});
180180
});

0 commit comments

Comments
 (0)