kuzzle-device-manager 0.4.10 → 0.4.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { DocumentController, EmbeddedSDK, JSONObject } from 'kuzzle';
1
+ import { DocumentController, Kuzzle as KuzzleSDK, JSONObject } from 'kuzzle';
2
2
  import { BatchWriter } from './BatchWriter';
3
3
  /**
4
4
  * Overload of the document controller.
@@ -21,7 +21,9 @@ import { BatchWriter } from './BatchWriter';
21
21
  */
22
22
  export declare class BatchController extends DocumentController {
23
23
  writer: BatchWriter;
24
- constructor(sdk: EmbeddedSDK, writer: BatchWriter);
24
+ constructor(sdk: KuzzleSDK, writer: BatchWriter);
25
+ protected extractErrorById(id: string, errors: any[]): any;
26
+ protected extractErrorByContent(content: JSONObject, errors: any[]): any;
25
27
  create(index: string, collection: string, content: JSONObject, _id?: string, options?: JSONObject): Promise<any>;
26
28
  replace(index: string, collection: string, _id: string, content: JSONObject, options?: JSONObject): Promise<any>;
27
29
  createOrReplace(index: string, collection: string, _id: string, content: JSONObject, options?: JSONObject): Promise<any>;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.BatchController = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
4
9
  const kuzzle_1 = require("kuzzle");
5
10
  /**
6
11
  * Overload of the document controller.
@@ -26,35 +31,59 @@ class BatchController extends kuzzle_1.DocumentController {
26
31
  super(sdk);
27
32
  this.writer = writer;
28
33
  }
34
+ extractErrorById(id, errors) {
35
+ return errors.find(({ document }) => document._id === id);
36
+ }
37
+ extractErrorByContent(content, errors) {
38
+ return errors.find(e => {
39
+ const responseDoc = (0, json_stable_stringify_1.default)(lodash_1.default.omit(e.document._source, ['_kuzzle_info']));
40
+ const originDoc = (0, json_stable_stringify_1.default)(content);
41
+ return responseDoc === originDoc;
42
+ });
43
+ }
29
44
  async create(index, collection, content, _id, options) {
30
- const { idx, promise } = this.writer.addCreate(index, collection, content, _id, options);
31
- const { successes } = await promise.promise;
32
- return successes[idx];
45
+ try {
46
+ const { idx, promise } = this.writer.addCreate(index, collection, content, _id, options);
47
+ const { successes, errors } = await promise.promise;
48
+ if (errors.length > 0) {
49
+ const error = this.extractErrorByContent(content, errors);
50
+ if (error) {
51
+ throw new kuzzle_1.BadRequestError(`Cannot create document in "${index}":"${collection}" : ${error.reason}`);
52
+ }
53
+ }
54
+ return successes[idx];
55
+ }
56
+ catch (exception) {
57
+ const error = this.extractErrorByContent(content, exception.errors);
58
+ if (error) {
59
+ throw new kuzzle_1.BadRequestError(`Cannot create document in "${index}":"${collection}" : ${error.reason}`);
60
+ }
61
+ }
33
62
  }
34
63
  async replace(index, collection, _id, content, options) {
35
64
  const { idx, promise } = this.writer.addReplace(index, collection, content, _id, options);
36
65
  const { successes, errors } = await promise.promise;
37
- const error = errors.find(({ _id: id }) => id === _id);
66
+ const error = this.extractErrorById(_id, errors);
38
67
  if (error) {
39
- throw new kuzzle_1.BadRequestError(`Cannot replace document "${_id}": ${error}`);
68
+ throw new kuzzle_1.BadRequestError(`Cannot replace document "${index}":"${collection}":"${_id}": ${error.reason}`);
40
69
  }
41
70
  return successes[idx];
42
71
  }
43
72
  async createOrReplace(index, collection, _id, content, options) {
44
73
  const { idx, promise } = this.writer.addCreateOrReplace(index, collection, content, _id, options);
45
74
  const { successes, errors } = await promise.promise;
46
- const error = errors.find(({ document }) => document._id === _id);
75
+ const error = this.extractErrorById(_id, errors);
47
76
  if (error) {
48
- throw new kuzzle_1.BadRequestError(`Cannot create or replace document "${_id}": ${error}`);
77
+ throw new kuzzle_1.BadRequestError(`Cannot create or replace document "${index}":"${collection}":"${_id}": ${error.reason}`);
49
78
  }
50
79
  return successes[idx];
51
80
  }
52
81
  async update(index, collection, _id, changes, options) {
53
82
  const { idx, promise } = this.writer.addUpdate(index, collection, changes, _id, options);
54
83
  const { successes, errors } = await promise.promise;
55
- const error = errors.find(({ _id: id }) => id === _id);
84
+ const error = this.extractErrorById(_id, errors);
56
85
  if (error) {
57
- throw new kuzzle_1.BadRequestError(`Cannot update document "${_id}": ${error}`);
86
+ throw new kuzzle_1.BadRequestError(`Cannot update document "${index}":"${collection}":"${_id}": ${error.reason}`);
58
87
  }
59
88
  return successes[idx];
60
89
  }
@@ -63,7 +92,7 @@ class BatchController extends kuzzle_1.DocumentController {
63
92
  const { successes } = await promise.promise;
64
93
  const document = successes.find(({ _id }) => _id === id);
65
94
  if (!document) {
66
- throw new kuzzle_1.NotFoundError(`Document ${id} not found`, 'services.storage.not_found');
95
+ throw new kuzzle_1.NotFoundError(`Document "${index}":"${collection}":"${id}" not found`, 'services.storage.not_found');
67
96
  }
68
97
  return document;
69
98
  }
@@ -77,7 +106,7 @@ class BatchController extends kuzzle_1.DocumentController {
77
106
  const { successes, errors } = await promise.promise;
78
107
  const error = errors.find(({ _id }) => _id === id);
79
108
  if (error) {
80
- throw new kuzzle_1.NotFoundError(`Cannot delete document ${id}: ${error}`);
109
+ throw new kuzzle_1.NotFoundError(`Cannot delete document "${index}":"${collection}":"${id}" : ${error.reason}`);
81
110
  }
82
111
  return successes[idx];
83
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kuzzle-device-manager",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
4
4
  "description": "Manage your IoT devices and assets. Choose a provisionning strategy, receive and decode payload, handle your IoT business logic.",
5
5
  "author": "The Kuzzle Team (support@kuzzle.io)",
6
6
  "repository": {