Skip to content

Commit 7e12c23

Browse files
authored
Merge 8b869af into 04c5ca9
2 parents 04c5ca9 + 8b869af commit 7e12c23

File tree

7 files changed

+175
-14
lines changed

7 files changed

+175
-14
lines changed

doc/7/core-classes/kuzzle/constructor/index.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ It can be one of the following available protocols:
3636

3737
Kuzzle SDK instance options.
3838

39-
| Property | Type<br/>(default) | Description |
40-
| ----------------- | -------------------------------- | ------------------------------------------------------------------ |
41-
| `autoQueue` | <pre>boolean</pre><br/>(`false`) | Automatically queue all requests during offline mode |
42-
| `autoReplay` | <pre>boolean</pre><br/>(`false`) | Automatically replay queued requests on a `reconnected` event |
43-
| `autoResubscribe` | <pre>boolean</pre><br/>(`true`) | Automatically renew all subscriptions on a `reconnected` event |
44-
| `eventTimeout` | <pre>number</pre><br/>(`200`) | Time (in ms) during which a similar event is ignored |
45-
| `offlineMode` | <pre>string</pre><br/>(`manual`) | Offline mode configuration. Can be `manual` or `auto` |
46-
| `queueTTL` | <pre>number</pre><br/>(`120000`) | Time a queued request is kept during offline mode, in milliseconds |
47-
| `queueMaxSize` | <pre>number</pre><br/>(`500`) | Number of maximum requests kept during offline mode |
48-
| `replayInterval` | <pre>number</pre><br/>(`10`) | Delay between each replayed requests, in milliseconds |
49-
| `tokenExpiredInterval` | <pre>number</pre><br/>(`1000`) | Time (in ms) during which a TokenExpired event is ignored |
50-
| `volatile` | <pre>object</pre><br/>(`{}`) | Common volatile data, will be sent to all future requests |
39+
| Property | Type<br/>(default) | Description |
40+
| ---------------------- | -------------------------------- | ------------------------------------------------------------------------ |
41+
| `autoQueue` | <pre>boolean</pre><br/>(`false`) | Automatically queue all requests during offline mode |
42+
| `autoReplay` | <pre>boolean</pre><br/>(`false`) | Automatically replay queued requests on a `reconnected` event |
43+
| `autoResubscribe` | <pre>boolean</pre><br/>(`true`) | Automatically renew all subscriptions on a `reconnected` event |
44+
| `eventTimeout` | <pre>number</pre><br/>(`200`) | Time (in ms) during which a similar event is ignored |
45+
| `deprecationWarning` | <pre>boolean</pre><br />(`true`) | Show deprecation warning in development (hidden either way in production)|
46+
| `offlineMode` | <pre>string</pre><br/>(`manual`) | Offline mode configuration. Can be `manual` or `auto` |
47+
| `queueTTL` | <pre>number</pre><br/>(`120000`) | Time a queued request is kept during offline mode, in milliseconds |
48+
| `queueMaxSize` | <pre>number</pre><br/>(`500`) | Number of maximum requests kept during offline mode |
49+
| `replayInterval` | <pre>number</pre><br/>(`10`) | Delay between each replayed requests, in milliseconds |
50+
| `tokenExpiredInterval` | <pre>number</pre><br/>(`1000`) | Time (in ms) during which a TokenExpired event is ignored |
51+
| `volatile` | <pre>object</pre><br/>(`{}`) | Common volatile data, will be sent to all future requests |
5152

5253
## Return
5354

src/Kuzzle.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ServerController } from './controllers/Server';
1111
import { SecurityController } from './controllers/Security';
1212
import { MemoryStorageController } from './controllers/MemoryStorage';
1313

14+
import { Deprecation } from './utils/Deprecation';
1415
import { uuidv4 } from './utils/uuidv4';
1516
import { proxify } from './utils/proxify';
1617
import { JSONObject } from './types';
@@ -61,6 +62,10 @@ export class Kuzzle extends KuzzleEventEmitter {
6162
* Common volatile data that will be sent to all future requests.
6263
*/
6364
public volatile: JSONObject;
65+
/**
66+
* Handle deprecation warning in development mode (hidden in production)
67+
*/
68+
public deprecationHandler: Deprecation;
6469

6570
public auth: AuthController;
6671
public bulk: any;
@@ -164,6 +169,11 @@ export class Kuzzle extends KuzzleEventEmitter {
164169
* If set to `auto`, the `autoQueue` and `autoReplay` are also set to `true`
165170
*/
166171
offlineMode?: 'auto';
172+
/**
173+
* Show deprecation warning in development mode (hidden either way in production)
174+
* Default: `true`
175+
*/
176+
deprecationWarning?: boolean;
167177
} = {}
168178
) {
169179
super();
@@ -208,6 +218,10 @@ export class Kuzzle extends KuzzleEventEmitter {
208218
? options.volatile
209219
: {};
210220

221+
this.deprecationHandler = new Deprecation(
222+
typeof options.deprecationWarning === 'boolean' ? options.deprecationWarning : true
223+
);
224+
211225
// controllers
212226
this.useController(AuthController, 'auth');
213227
this.useController(BulkController, 'bulk');
@@ -586,7 +600,8 @@ export class Kuzzle extends KuzzleEventEmitter {
586600
Discarded request: ${JSON.stringify(request)}`));
587601
}
588602

589-
return this.protocol.query(request, options);
603+
return this.protocol.query(request, options)
604+
.then((response: ResponsePayload) => this.deprecationHandler.logDeprecation(response));
590605
}
591606

592607
/**

src/controllers/Base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { Kuzzle } from '../Kuzzle';
12
import { JSONObject } from '../types';
23
import { RequestPayload } from '../types/RequestPayload';
34

45
export class BaseController {
56
private _name: string;
6-
private _kuzzle: any;
7+
private _kuzzle: Kuzzle;
78

89
/**
910
* @param {Kuzzle} kuzzle - Kuzzle SDK object.

src/types/ResponsePayload.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,27 @@ export type ResponsePayload = {
3131
*/
3232
_id?: string;
3333

34+
/**
35+
* Array of deprecation warnings (hidden if NODE_ENV=production)
36+
*/
37+
deprecations?: Array<{
38+
39+
/**
40+
* Deprecation description
41+
*/
42+
message: string;
43+
44+
/**
45+
* Deprecated since this version
46+
*/
47+
version: string;
48+
}>;
49+
3450
/**
3551
* API error
3652
*/
3753
error?: {
54+
3855
/**
3956
* Error human readable identifier
4057
*/

src/utils/Deprecation.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ResponsePayload } from 'src/types/ResponsePayload';
2+
3+
export class Deprecation {
4+
5+
private _deprecationWarning: boolean;
6+
7+
constructor (deprecationWarning: boolean) {
8+
this._deprecationWarning =
9+
process.env.NODE_ENV !== 'production' ? deprecationWarning : false;
10+
}
11+
12+
/**
13+
* Warn the developer that he is using a deprecated action (disabled if NODE_ENV=production)
14+
*
15+
* @param response Result of a query to the API
16+
*
17+
* @returns Same as response param, just like a middleware
18+
*/
19+
logDeprecation (response: ResponsePayload) {
20+
if ( this._deprecationWarning
21+
&& response.deprecations
22+
&& response.deprecations.length
23+
) {
24+
for (const deprecation of response.deprecations) {
25+
// eslint-disable-next-line no-console
26+
console.warn(deprecation.message);
27+
}
28+
}
29+
return response;
30+
}
31+
}

test/kuzzle/query.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,14 @@ describe('Kuzzle query management', () => {
242242

243243
should(request).be.eql({ controller: 'server', action: 'now' });
244244
});
245+
246+
it('should call logDeprecation with the response', async () => {
247+
kuzzle.deprecationHandler.logDeprecation = sinon.stub().returns(response);
248+
await kuzzle.query(query);
249+
250+
should(kuzzle.deprecationHandler.logDeprecation)
251+
.be.calledOnce()
252+
.be.calledWith(response);
253+
});
245254
});
246255
});

test/utils/Deprecation.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const should = require('should');
2+
const sinon = require('sinon');
3+
const { Deprecation } = require('../../src/utils/Deprecation');
4+
5+
describe('Deprecation', () => {
6+
let deprecationHandler, response;
7+
const sandbox = sinon.createSandbox();
8+
const NODE_ENV = process.env.NODE_ENV;
9+
10+
beforeEach(()=>{
11+
sandbox.stub(console, 'warn');
12+
process.env.NODE_ENV = 'development';
13+
deprecationHandler = new Deprecation(true);
14+
15+
response = {
16+
action: 'test',
17+
collection: 'collection',
18+
controller: 'controller',
19+
deprecations: [
20+
{
21+
message: 'Use this route instead: http://kuzzle:7512/test/succeed',
22+
version: '6.6.6'
23+
}
24+
],
25+
error: null,
26+
index: null,
27+
node: 'nodeJS',
28+
requestId: 'idididididid',
29+
result: true,
30+
status: 200,
31+
volatile: null
32+
};
33+
});
34+
35+
afterEach(()=>{
36+
sandbox.restore();
37+
});
38+
39+
it('should warn the developer that he is using a deprecated action', () => {
40+
deprecationHandler.logDeprecation(response);
41+
42+
should(console.warn)
43+
.be.calledOnce()
44+
.be.calledWith(response.deprecations[0].message);
45+
});
46+
47+
it('should return the same response as it has received', () => {
48+
should(deprecationHandler.logDeprecation(response)).match(response);
49+
});
50+
51+
it('should handle multiple deprecations', () => {
52+
response.deprecations.push(response);
53+
54+
deprecationHandler.logDeprecation(response);
55+
56+
should(console.warn).be.calledTwice();
57+
});
58+
59+
it('should not warn the developer if he refused to', () => {
60+
deprecationHandler = new Deprecation(false);
61+
62+
deprecationHandler.logDeprecation(response);
63+
64+
should(console.warn).not.have.been.called();
65+
});
66+
67+
it('should not warn the developer if there is no deprecation', () => {
68+
response.deprecations = [];
69+
70+
deprecationHandler.logDeprecation(response);
71+
72+
should(console.warn).not.have.been.called();
73+
});
74+
75+
it('should not warn the developer in production', () => {
76+
process.env.NODE_ENV = 'production';
77+
deprecationHandler = new Deprecation(true);
78+
79+
deprecationHandler.logDeprecation(response);
80+
81+
should(console.warn).not.have.been.called();
82+
});
83+
84+
after(() => {
85+
process.env.NODE_ENV = NODE_ENV;
86+
});
87+
});

0 commit comments

Comments
 (0)