Skip to content

Commit 49288ba

Browse files
authored
Merge pull request #662 from kuzzleio/7.7.5-proposal
# [7.7.5](https://github.com/kuzzleio/sdk-javascript/releases/tag/7.7.5) (2021-09-06) #### Bug fixes - [ [#660](#660) ] Fix upsert options.default ([Aschen](https://github.com/Aschen)) #### Enhancements - [ [#661](#661) ] Allows to define custom headers for HTTP protocol ([Aschen](https://github.com/Aschen)) ---
2 parents 1900bf0 + 70c44a8 commit 49288ba

File tree

9 files changed

+134
-31
lines changed

9 files changed

+134
-31
lines changed

doc/7/controllers/document/upsert/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Additional query options
3030

3131
| Options | Type<br/>(default) | Description |
3232
| ----------------- | -------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
33-
| `defaults` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
33+
| `default` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
3434
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
3535
| `retryOnConflict` | <pre>int</pre><br/>(`10`) | The number of times the database layer should retry in case of version conflict |
3636
| `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> |

doc/7/protocols/http/constructor/index.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ Http(host, [options]);
1919
<br/>
2020

2121
| Argument | Type | Description |
22-
| --------- | ----------------- | ---------------------------- |
22+
|-----------|-------------------|------------------------------|
2323
| `host` | <pre>string</pre> | Kuzzle server hostname or IP |
2424
| `options` | <pre>object</pre> | Http connection options |
2525

2626
### options
2727

2828
Http protocol connection options.
2929

30-
| Property | Type<br/>(default) | Description |
31-
| --------------- | -------------------------------- | ----------------------------------- |
32-
| `port` | <pre>number</pre><br/>(`7512`) | Kuzzle server port |
33-
| `sslConnection` | <pre>boolean</pre><br/>(`false`) | Use SSL to connect to Kuzzle server <DeprecatedBadge version="7.4.0"/> |
34-
| `ssl` | <pre>boolean</pre><br/>(`false`) | Use SSL to connect to Kuzzle server. Defaults to `true` for ports 443 and 7443. |
35-
| `customRoutes` | <pre>object</pre><br/>(`{}`) | Add custom routes <SinceBadge version="6.2.0"/> |
36-
| `timeout` | <pre>number</pre><br/>(`0`) | Connection timeout in milliseconds (`0` means no timeout)<SinceBadge version="6.2.1"/> |
30+
| Property | Type<br/>(default) | Description |
31+
|-----------------|----------------------------------|----------------------------------------------------------------------------------------|
32+
| `customRoutes` | <pre>object</pre><br/>(`{}`) | Add custom routes <SinceBadge version="6.2.0"/> |
33+
| `headers` | <pre>object</pre><br/>(`{}`) | Default headers sent with each HTTP request <SinceBadge version="7.7.5"/> |
34+
| `port` | <pre>number</pre><br/>(`7512`) | Kuzzle server port |
35+
| `sslConnection` | <pre>boolean</pre><br/>(`false`) | Use SSL to connect to Kuzzle server <DeprecatedBadge version="7.4.0"/> |
36+
| `ssl` | <pre>boolean</pre><br/>(`false`) | Use SSL to connect to Kuzzle server. Defaults to `true` for ports 443 and 7443. |
37+
| `timeout` | <pre>number</pre><br/>(`0`) | Connection timeout in milliseconds (`0` means no timeout) <SinceBadge version="6.2.1"/> |
3738

3839
**Note:**
3940

doc/7/protocols/http/constructor/snippets/constructor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ const customRoutes = {
1212
}
1313
};
1414

15+
const headers = {
16+
'Accept-Encoding': 'gzip, deflate'
17+
};
18+
1519
const options = {
1620
customRoutes,
21+
headers,
1722
sslConnection: false
1823
};
1924

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "7.7.4",
3+
"version": "7.7.5",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/controllers/Document.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class DocumentController extends BaseController {
250250
silent: options.silent,
251251
source: options.source,
252252
};
253-
253+
254254
return this.query(request, options)
255255
.then(response => response.result);
256256
}
@@ -397,6 +397,7 @@ export class DocumentController extends BaseController {
397397
body: { documents },
398398
action: 'mCreate',
399399
silent: options.silent,
400+
strict: options.strict,
400401
};
401402

402403
return this.query(request, options)
@@ -469,6 +470,7 @@ export class DocumentController extends BaseController {
469470
body: { documents },
470471
action: 'mCreateOrReplace',
471472
silent: options.silent,
473+
strict: options.strict,
472474
};
473475

474476
return this.query(request, options)
@@ -528,6 +530,7 @@ export class DocumentController extends BaseController {
528530
body: { ids },
529531
action: 'mDelete',
530532
silent: options.silent,
533+
strict: options.strict,
531534
};
532535

533536
return this.query(request, options)
@@ -645,6 +648,7 @@ export class DocumentController extends BaseController {
645648
body: { documents },
646649
action: 'mReplace',
647650
silent: options.silent,
651+
strict: options.strict,
648652
};
649653

650654
return this.query(request, options)
@@ -722,6 +726,7 @@ export class DocumentController extends BaseController {
722726
body: { documents },
723727
action: 'mUpdate',
724728
silent: options.silent,
729+
strict: options.strict,
725730
};
726731

727732
return this.query(request, options)
@@ -730,7 +735,7 @@ export class DocumentController extends BaseController {
730735

731736
/**
732737
* Applies partial updates to multiple documents.
733-
*
738+
*
734739
* If a document doesn't already exist, a new document is created.
735740
* @see https://docs.kuzzle.io/sdk/js/7/controllers/document/m-upsert/
736741
*
@@ -742,6 +747,7 @@ export class DocumentController extends BaseController {
742747
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
743748
* - `silent` If true, then Kuzzle will not generate notifications
744749
* - `retryOnConflict` Number of times the database layer should retry in case of version conflict
750+
* - `strict` If true, an error will occur if a document was not updated
745751
*
746752
* @returns An object containing 2 arrays: "successes" and "errors"
747753
*/
@@ -767,6 +773,7 @@ export class DocumentController extends BaseController {
767773
refresh?: 'wait_for',
768774
silent?: boolean,
769775
retryOnConflict?: number,
776+
strict?: boolean
770777
} = {}
771778
): Promise<{
772779
/**
@@ -797,6 +804,7 @@ export class DocumentController extends BaseController {
797804
body: { documents },
798805
action: 'mUpsert',
799806
silent: options.silent,
807+
strict: options.strict,
800808
};
801809

802810
return this.query(request, options)
@@ -1038,7 +1046,7 @@ export class DocumentController extends BaseController {
10381046
* @param _id Unique document identifier
10391047
* @param changes Partial content of the document to update
10401048
* @param [options]
1041-
* - `defaults` Fields to add to the document if it gets created
1049+
* - `default` Fields to add to the document if it gets created
10421050
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
10431051
* - `silent` If true, then Kuzzle will not generate notifications
10441052
* - `retryOnConflict` Number of times the database layer should retry in case of version conflict
@@ -1052,7 +1060,7 @@ export class DocumentController extends BaseController {
10521060
_id: string,
10531061
changes: JSONObject,
10541062
options: {
1055-
defaults?: JSONObject;
1063+
default?: JSONObject;
10561064
refresh?: string,
10571065
silent?: boolean,
10581066
retryOnConflict?: boolean,
@@ -1064,7 +1072,7 @@ export class DocumentController extends BaseController {
10641072
index,
10651073
collection,
10661074
_id,
1067-
body: { changes, defaults: options.defaults },
1075+
body: { changes, default: options.default },
10681076
action: 'upsert',
10691077
source: options.source,
10701078
silent: options.silent,

src/protocols/Http.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
1414
private _routes: HttpRoutes;
1515
private _timeout: number;
1616
private _customRoutes: HttpRoutes;
17+
private _defaultHeaders: JSONObject;
1718

1819
/**
1920
* @param host Kuzzle server hostname or IP
2021
* @param options Http connection options
2122
* - `customRoutes` Add custom routes
23+
* - `headers` Default headers sent with each HTTP request (default: `{}`)
2224
* - `port` Kuzzle server port (default: `7512`)
2325
* - `ssl` Use SSL to connect to Kuzzle server. Default `false` unless port is 443 or 7443.
2426
* - `timeout` Connection timeout in milliseconds (default: `0`)
@@ -33,7 +35,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
3335
sslConnection?: boolean;
3436
ssl?: boolean;
3537
customRoutes?: HttpRoutes;
36-
timeout?: number
38+
timeout?: number,
39+
headers?: JSONObject,
3740
} = {}
3841
) {
3942
super(host, options, 'http');
@@ -48,6 +51,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
4851

4952
this._customRoutes = options.customRoutes || {};
5053

54+
this._defaultHeaders = options.headers || {};
55+
5156
for (const controller of Object.keys(this._customRoutes)) {
5257
const definition = this._customRoutes[controller];
5358

@@ -113,7 +118,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
113118
return Promise.resolve();
114119
}
115120

116-
return this._sendHttpRequest({method: 'GET', path: '/_publicApi'})
121+
const publicApiRequest = {
122+
method: 'GET',
123+
path: '/_publicApi',
124+
payload: {
125+
headers: this._defaultHeaders,
126+
}
127+
};
128+
return this._sendHttpRequest(publicApiRequest)
117129
.then(({ result, error }) => {
118130
if (! error) {
119131
this._routes = this._constructRoutes(result);
@@ -133,7 +145,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
133145
} else if (error.status === 404) {
134146
// fallback to server:info route
135147
// server:publicApi is only available since Kuzzle 1.9.0
136-
return this._sendHttpRequest({method: 'GET', path: '/'})
148+
const serverInfoRequest = {
149+
method: 'GET',
150+
path: '/',
151+
payload: {
152+
headers: this._defaultHeaders,
153+
}
154+
};
155+
return this._sendHttpRequest(serverInfoRequest)
137156
.then(({ result: res, error: err }) => {
138157
if (! err) {
139158
this._routes = this._constructRoutes(res.serverInfo.kuzzle.api.routes);
@@ -207,7 +226,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
207226
collection: undefined,
208227
controller: undefined,
209228
headers: {
210-
'Content-Type': 'application/json'
229+
'Content-Type': 'application/json',
230+
...this._defaultHeaders,
211231
},
212232
index: undefined,
213233
meta: undefined,
@@ -311,11 +331,15 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
311331
if (formattedRequest) {
312332
this._sendHttpRequest(formattedRequest)
313333
.then(response => this.emit(formattedRequest.payload.requestId, response))
314-
.catch(error => this.emit(formattedRequest.payload.requestId, {error}));
334+
.catch(error => this.emit(formattedRequest.payload.requestId, { error }));
315335
}
316336
}
317337

318-
_sendHttpRequest ({method, path, payload = {headers: undefined, body:undefined}}) {
338+
_sendHttpRequest ({ method, path, payload }: {
339+
method: string,
340+
path: string,
341+
payload: JSONObject
342+
}) {
319343
if (typeof XMLHttpRequest === 'undefined') {
320344
// NodeJS implementation, using http.request:
321345

@@ -326,12 +350,12 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
326350
path = `/${path}`;
327351
}
328352
const url = `${this.protocol}://${this.host}:${this.port}${path}`;
329-
const headers = payload.headers || {};
330-
headers['Content-Length'] = Buffer.byteLength(payload.body || '');
353+
const headers = payload && payload.headers || {};
354+
headers['Content-Length'] = Buffer.byteLength(payload && payload.body || '');
331355

332356
return httpClient.request(url, method, {
333-
headers,
334-
body: payload.body,
357+
body: payload && payload.body,
358+
headers: headers,
335359
timeout: this._timeout
336360
})
337361
.then(response => {
@@ -362,8 +386,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
362386
// Authorize the reception of cookies
363387
xhr.withCredentials = this.cookieSupport;
364388

365-
for (const header of Object.keys(payload.headers || {})) {
366-
xhr.setRequestHeader(header, payload.headers[header]);
389+
for (const [header, value] of Object.entries(payload && payload.headers || {})) {
390+
xhr.setRequestHeader(header, value as string);
367391
}
368392

369393
xhr.onload = () => {
@@ -376,7 +400,7 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
376400
}
377401
};
378402

379-
xhr.send(payload.body);
403+
xhr.send(payload && payload.body);
380404
});
381405
}
382406

0 commit comments

Comments
 (0)