autoinspector 2.1.0 → 2.2.0

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.
Files changed (44) hide show
  1. package/constants/http.ts +6 -0
  2. package/dist/constants/http.d.ts +5 -0
  3. package/dist/constants/http.js +8 -0
  4. package/dist/package.json +7 -5
  5. package/dist/resources/Autoinspector.d.ts +1 -0
  6. package/dist/resources/Autoinspector.js +15 -1
  7. package/dist/resources/Bike.js +2 -0
  8. package/dist/resources/Car.js +2 -0
  9. package/dist/resources/Custom.js +2 -0
  10. package/dist/resources/Goods.d.ts +1 -1
  11. package/dist/resources/Goods.js +3 -1
  12. package/dist/resources/HTTPClient.d.ts +3 -2
  13. package/dist/resources/HTTPClient.js +21 -6
  14. package/dist/resources/Inspections.d.ts +4 -3
  15. package/dist/resources/Inspections.js +4 -1
  16. package/dist/resources/Machinery.js +2 -0
  17. package/dist/resources/Moto.js +2 -0
  18. package/dist/resources/People.js +2 -0
  19. package/dist/resources/Webhooks.d.ts +2 -2
  20. package/dist/types/autoinspector.d.ts +2 -1
  21. package/dist/types/bike.d.ts +2 -2
  22. package/dist/types/car.d.ts +4 -4
  23. package/dist/types/consumer.d.ts +1 -1
  24. package/dist/types/goods.d.ts +2 -2
  25. package/dist/types/http.d.ts +6 -2
  26. package/dist/types/inspection.d.ts +49 -13
  27. package/dist/types/machinery.d.ts +2 -2
  28. package/dist/types/people.d.ts +2 -2
  29. package/dist/types/productMethods.d.ts +2 -2
  30. package/dist/types/webhooks.d.ts +9 -2
  31. package/dist/utils/backoff-delay.d.ts +2 -0
  32. package/dist/utils/backoff-delay.js +15 -0
  33. package/dist/utils/build-oauth-header.d.ts +3 -0
  34. package/dist/utils/build-oauth-header.js +11 -0
  35. package/dist/utils/http-retry-handler.d.ts +2 -0
  36. package/dist/utils/http-retry-handler.js +18 -0
  37. package/dist/utils/idempotency.d.ts +3 -0
  38. package/dist/utils/idempotency.js +10 -0
  39. package/package.json +7 -5
  40. package/utils/backoff-delay.ts +18 -0
  41. package/utils/build-oauth-header.ts +9 -0
  42. package/utils/http-retry-handler.ts +21 -0
  43. package/utils/idempotency.ts +7 -0
  44. package/.env +0 -6
@@ -0,0 +1,6 @@
1
+ export const BASE_DELAY = 50; // 1 second
2
+ export const DEFAULT_MAX_BACKOFF_TIME = 3000; // 3 seconds
3
+ export const DEFAULT_MAX_RETRIES_DEFAULT = 3;
4
+ export const DEFAULT_TIMEOUT = 120000;
5
+
6
+ export const INTERNAL_SERVER_ERROR_STATUS = 500;
@@ -0,0 +1,5 @@
1
+ export declare const BASE_DELAY = 50;
2
+ export declare const DEFAULT_MAX_BACKOFF_TIME = 3000;
3
+ export declare const DEFAULT_MAX_RETRIES_DEFAULT = 3;
4
+ export declare const DEFAULT_TIMEOUT = 120000;
5
+ export declare const INTERNAL_SERVER_ERROR_STATUS = 500;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.INTERNAL_SERVER_ERROR_STATUS = exports.DEFAULT_TIMEOUT = exports.DEFAULT_MAX_RETRIES_DEFAULT = exports.DEFAULT_MAX_BACKOFF_TIME = exports.BASE_DELAY = void 0;
4
+ exports.BASE_DELAY = 50; // 1 second
5
+ exports.DEFAULT_MAX_BACKOFF_TIME = 3000; // 3 seconds
6
+ exports.DEFAULT_MAX_RETRIES_DEFAULT = 3;
7
+ exports.DEFAULT_TIMEOUT = 120000;
8
+ exports.INTERNAL_SERVER_ERROR_STATUS = 500;
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autoinspector",
3
- "version": "2.0.1",
3
+ "version": "2.2.0",
4
4
  "description": "Autoinspector API wrapper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,8 @@
8
8
  "test": "jest --config ./jest.config.js",
9
9
  "build": "tsc",
10
10
  "semantic-release": "semantic-release",
11
- "commit": "git-cz"
11
+ "commit": "git-cz",
12
+ "link": "npm run build && npm link"
12
13
  },
13
14
  "repository": {
14
15
  "type": "git",
@@ -27,9 +28,11 @@
27
28
  "homepage": "https://github.com/autoinspector-global/autoinspector-node#readme",
28
29
  "dependencies": {
29
30
  "axios": "^0.25.0",
31
+ "axios-retry": "^3.3.1",
30
32
  "form-data": "^4.0.0",
31
33
  "isomorphic-form-data": "^2.0.0",
32
- "typescript": "^4.5.4"
34
+ "typescript": "^4.5.4",
35
+ "uuid": "^9.0.0"
33
36
  },
34
37
  "devDependencies": {
35
38
  "@types/jest": "^27.5.0",
@@ -44,8 +47,7 @@
44
47
  "eslint-plugin-prettier": "^4.0.0",
45
48
  "jest": "^27.5.1",
46
49
  "prettier": "^2.5.1",
47
- "semantic-release": "^19.0.2",
48
- "ts-jest": "^28.0.0"
50
+ "semantic-release": "^19.0.2"
49
51
  },
50
52
  "config": {
51
53
  "commitizen": {
@@ -4,6 +4,7 @@ import { Inspections } from './Inspections';
4
4
  import { OAuth } from './OAuth';
5
5
  import { Template } from './Template';
6
6
  import { Webhooks } from './Webhooks';
7
+ export * from '../types/webhooks';
7
8
  /**
8
9
  * @classdesc Represents the Autoinspector SDK. It allows the user to make every call to the API with a single function.
9
10
  * @class
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
2
12
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
14
  };
5
15
  Object.defineProperty(exports, "__esModule", { value: true });
6
16
  exports.Autoinspector = void 0;
17
+ const http_1 = require("../constants/http");
7
18
  const package_json_1 = __importDefault(require("../package.json"));
8
19
  const HTTPClient_1 = require("./HTTPClient");
9
20
  const Image_1 = require("./Image");
@@ -11,6 +22,7 @@ const Inspections_1 = require("./Inspections");
11
22
  const OAuth_1 = require("./OAuth");
12
23
  const Template_1 = require("./Template");
13
24
  const Webhooks_1 = require("./Webhooks");
25
+ __exportStar(require("../types/webhooks"), exports);
14
26
  /**
15
27
  * @classdesc Represents the Autoinspector SDK. It allows the user to make every call to the API with a single function.
16
28
  * @class
@@ -32,7 +44,9 @@ class Autoinspector {
32
44
  'x-api-key': input.apikey,
33
45
  'User-Agent': 'autoinspector-node-sdk/' + package_json_1.default.version,
34
46
  },
35
- timeout: input.timeout || 80000,
47
+ maxRetries: input.maxRetries || http_1.DEFAULT_MAX_RETRIES_DEFAULT,
48
+ timeout: input.timeout,
49
+ maxBackoffTime: input.maxBackoffTime,
36
50
  pathPrefix: '/v1',
37
51
  });
38
52
  this.oauth = new OAuth_1.OAuth(httpClient, input.oauthCredentials || {});
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Bike = void 0;
13
+ const idempotency_1 = require("../utils/idempotency");
13
14
  class Bike {
14
15
  constructor(httpClient) {
15
16
  this.httpClient = httpClient;
@@ -19,6 +20,7 @@ class Bike {
19
20
  return yield this.httpClient.makeRequest({
20
21
  method: 'POST',
21
22
  path: `/inspection/bike`,
23
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
22
24
  body,
23
25
  });
24
26
  });
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Car = void 0;
13
+ const idempotency_1 = require("../utils/idempotency");
13
14
  class Car {
14
15
  constructor(httpClient) {
15
16
  this.httpClient = httpClient;
@@ -19,6 +20,7 @@ class Car {
19
20
  return yield this.httpClient.makeRequest({
20
21
  method: 'POST',
21
22
  path: `/inspection/car`,
23
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
22
24
  body,
23
25
  });
24
26
  });
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Custom = void 0;
13
+ const idempotency_1 = require("../utils/idempotency");
13
14
  class Custom {
14
15
  constructor(httpClient) {
15
16
  this.httpClient = httpClient;
@@ -20,6 +21,7 @@ class Custom {
20
21
  method: 'POST',
21
22
  path: `/inspection/custom`,
22
23
  body,
24
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
23
25
  });
24
26
  });
25
27
  }
@@ -18,6 +18,6 @@ export declare class Goods {
18
18
  * data or an Error with the problem.
19
19
  */
20
20
  create(body: ICreateGoodsInspection): Promise<ICreateInspectionGoodsOutput>;
21
- push(inspectionId: string, goods: IGood[]): Promise<ICreateInspectionGoodsOutput>;
21
+ addGoods(inspectionId: string, goods: IGood[]): Promise<ICreateInspectionGoodsOutput>;
22
22
  update(productId: string, good: Partial<Omit<IGood, 'type' | 'category'>>): Promise<ICreateInspectionGoodsOutput>;
23
23
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Goods = void 0;
4
+ const idempotency_1 = require("../utils/idempotency");
4
5
  const Helper_1 = require("./Helper");
5
6
  class Goods {
6
7
  constructor(httpClient) {
@@ -23,10 +24,11 @@ class Goods {
23
24
  return this.httpClient.makeRequest({
24
25
  method: 'POST',
25
26
  path: `/inspection/goods`,
27
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
26
28
  body,
27
29
  });
28
30
  }
29
- push(inspectionId, goods) {
31
+ addGoods(inspectionId, goods) {
30
32
  return this.httpClient.makeRequest({
31
33
  method: 'POST',
32
34
  path: `/inspection/goods/${inspectionId}`,
@@ -5,14 +5,15 @@ import { IHTTPClient, IMakeRequest } from '../types/http';
5
5
  */
6
6
  export declare class HTTPClient {
7
7
  private headers;
8
- private timeout;
9
8
  private baseURL;
10
9
  private pathPrefix;
10
+ private instance;
11
+ private timeout;
11
12
  /**
12
13
  * Create HTTPClient instance to start making request in another class.
13
14
  * @constructor
14
15
  * @param input - An object with common parameters that we need to set in the http client third party library.
15
- * @param {Object} input.headers - An object with the headers to send in each makeRequest() call.
16
+ * @param {Object} input'''.headers - An object with the headers to send in each makeRequest() call.
16
17
  * @param {Object} input.timeout - The timeout that each request will have. That means, the time in milliseconds maximum that request will wait until cancel it.
17
18
  * @param {Object} input.baseURL - The baseURL to make the requests.
18
19
  */
@@ -5,6 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HTTPClient = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
+ const axios_retry_1 = __importDefault(require("axios-retry"));
9
+ const http_1 = require("../constants/http");
10
+ const http_retry_handler_1 = require("../utils/http-retry-handler");
8
11
  /**
9
12
  * @classdesc Represents the class that implements some HTTP client third library. It's an extra layer for in case if we need to change the implementation be one hundred percent sure we can do it without problems.
10
13
  * @class
@@ -14,16 +17,28 @@ class HTTPClient {
14
17
  * Create HTTPClient instance to start making request in another class.
15
18
  * @constructor
16
19
  * @param input - An object with common parameters that we need to set in the http client third party library.
17
- * @param {Object} input.headers - An object with the headers to send in each makeRequest() call.
20
+ * @param {Object} input'''.headers - An object with the headers to send in each makeRequest() call.
18
21
  * @param {Object} input.timeout - The timeout that each request will have. That means, the time in milliseconds maximum that request will wait until cancel it.
19
22
  * @param {Object} input.baseURL - The baseURL to make the requests.
20
23
  */
21
24
  constructor(input) {
22
25
  this.headers = {};
23
26
  this.headers = input.headers;
24
- this.timeout = input.timeout;
25
27
  this.baseURL = input.baseURL;
28
+ this.timeout = input.timeout || http_1.DEFAULT_TIMEOUT;
26
29
  this.pathPrefix = input.pathPrefix;
30
+ const httpClient = axios_1.default.create();
31
+ (0, axios_retry_1.default)(httpClient, {
32
+ retryDelay: (0, http_retry_handler_1.httpRetryHandler)(input),
33
+ retries: input.maxRetries,
34
+ retryCondition: (error) => {
35
+ if (axios_1.default.isAxiosError(error)) {
36
+ return false;
37
+ }
38
+ return true;
39
+ },
40
+ });
41
+ this.instance = httpClient;
27
42
  }
28
43
  /**
29
44
  * Make a request to a specific endpoint.
@@ -44,28 +59,28 @@ class HTTPClient {
44
59
  };
45
60
  switch (input.method) {
46
61
  case 'GET':
47
- return axios_1.default
62
+ return this.instance
48
63
  .get(path, options)
49
64
  .then((res) => {
50
65
  return res.data;
51
66
  })
52
67
  .catch((err) => this.handleError(err));
53
68
  case 'PUT':
54
- return axios_1.default
69
+ return this.instance
55
70
  .put(path, input.body, options)
56
71
  .then((res) => {
57
72
  return res.data;
58
73
  })
59
74
  .catch((err) => this.handleError(err));
60
75
  case 'POST':
61
- return axios_1.default
76
+ return this.instance
62
77
  .post(path, input.body, options)
63
78
  .then((res) => {
64
79
  return res.data;
65
80
  })
66
81
  .catch((err) => this.handleError(err));
67
82
  case 'DELETE':
68
- return axios_1.default
83
+ return this.instance
69
84
  .post(path, input.body, options)
70
85
  .then((res) => {
71
86
  return res.data;
@@ -1,6 +1,6 @@
1
1
  import { IAPISucessResponse } from '../types/api';
2
2
  import { IConsumer } from '../types/consumer';
3
- import { IFinishInspection, IIInspectionCommonParamsV2, IInspection } from '../types/inspection';
3
+ import { IFinishInspection, IInspection, IInspectionCommonParamsV2 } from '../types/inspection';
4
4
  import { IPagination, IPaginationResponse } from '../types/pagination';
5
5
  import { IProducer } from '../types/producer';
6
6
  import { HTTPClient } from './HTTPClient';
@@ -24,11 +24,12 @@ export declare class Inspections extends Products {
24
24
  * Get a specific inspection object.
25
25
  * @param input - An object that contains the essential information for upload the image.
26
26
  * @param {String} input.inspectionId - Represents the id of the inspection that you want to retrieve.
27
+ * @param {String} input.accessToken - Represents an oauth access token.
27
28
  * @return {Promise} - Returns a Promise that, when fulfilled, will either return an JSON Object with the requested
28
29
  * data or an Error with the problem.
29
30
  */
30
- retrieve(inspectionId: string): Promise<IInspection>;
31
- update(inspectionId: string, inspection: Partial<Pick<IIInspectionCommonParamsV2<IProducer, Partial<IConsumer>>, 'inputs' | 'consumer' | 'metadata'>>): Promise<IAPISucessResponse>;
31
+ retrieve(inspectionId: string, accessToken?: string): Promise<IInspection>;
32
+ update(inspectionId: string, inspection: Partial<Pick<IInspectionCommonParamsV2<IProducer, Partial<IConsumer>>, 'inputs' | 'consumer' | 'metadata'>>): Promise<IAPISucessResponse>;
32
33
  /**
33
34
  * Get a list of n inspections with pagination.
34
35
  * @param input - An object that contains filters for list the inspections.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Inspections = void 0;
4
+ const build_oauth_header_1 = require("../utils/build-oauth-header");
4
5
  const Helper_1 = require("./Helper");
5
6
  const Products_1 = require("./Products");
6
7
  /**
@@ -30,13 +31,15 @@ class Inspections extends Products_1.Products {
30
31
  * Get a specific inspection object.
31
32
  * @param input - An object that contains the essential information for upload the image.
32
33
  * @param {String} input.inspectionId - Represents the id of the inspection that you want to retrieve.
34
+ * @param {String} input.accessToken - Represents an oauth access token.
33
35
  * @return {Promise} - Returns a Promise that, when fulfilled, will either return an JSON Object with the requested
34
36
  * data or an Error with the problem.
35
37
  */
36
- retrieve(inspectionId) {
38
+ retrieve(inspectionId, accessToken) {
37
39
  return this.httpClient.makeRequest({
38
40
  method: 'GET',
39
41
  path: `/inspection/${inspectionId}`,
42
+ headers: (0, build_oauth_header_1.buildOAuthHeader)(accessToken),
40
43
  });
41
44
  }
42
45
  update(inspectionId, inspection) {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Machinery = void 0;
4
+ const idempotency_1 = require("../utils/idempotency");
4
5
  /**
5
6
  * @classdesc Represents the class that handle all the requests related to an inspection of type machinery.
6
7
  * @class
@@ -27,6 +28,7 @@ class Machinery {
27
28
  method: 'POST',
28
29
  path: `/inspection/machinery`,
29
30
  body,
31
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
30
32
  });
31
33
  }
32
34
  update(productId, machinery) {
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Moto = void 0;
13
+ const idempotency_1 = require("../utils/idempotency");
13
14
  class Moto {
14
15
  constructor(httpClient) {
15
16
  this.httpClient = httpClient;
@@ -19,6 +20,7 @@ class Moto {
19
20
  return yield this.httpClient.makeRequest({
20
21
  method: 'POST',
21
22
  path: `/inspection/moto`,
23
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
22
24
  body,
23
25
  });
24
26
  });
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.People = void 0;
4
+ const idempotency_1 = require("../utils/idempotency");
4
5
  class People {
5
6
  constructor(httpClient) {
6
7
  this.httpClient = httpClient;
@@ -22,6 +23,7 @@ class People {
22
23
  return this.httpClient.makeRequest({
23
24
  method: 'POST',
24
25
  path: `/inspection/people`,
26
+ headers: (0, idempotency_1.generateIdempotencyHeader)(),
25
27
  body,
26
28
  });
27
29
  }
@@ -1,4 +1,4 @@
1
- import { IWebhook } from '../types/webhooks';
1
+ import { AutoinspectorWebhook } from '../types/webhooks';
2
2
  export declare class Webhooks {
3
- constructEvent(body: any, requestSignature: string, webhookSecret: string): IWebhook;
3
+ constructEvent(body: any, requestSignature: string, webhookSecret: string): AutoinspectorWebhook;
4
4
  }
@@ -1,5 +1,6 @@
1
+ import { IHttpClientOpts } from './http';
1
2
  import { IOAuth20Credentials } from './oauth20';
2
- export interface IAutoinspector {
3
+ export interface IAutoinspector extends Partial<IHttpClientOpts> {
3
4
  /**
4
5
  * The apikey for authenticate to Autoinspector API.
5
6
  */
@@ -1,10 +1,10 @@
1
- import { ICreateInspectionProducer, IIInspectionCommonParamsV2 } from './inspection';
1
+ import { ICreateInspectionProducer, IInspectionCommonParamsV2 } from './inspection';
2
2
  export interface IBike {
3
3
  make: string;
4
4
  model: string;
5
5
  price: number;
6
6
  serialNumber: string;
7
7
  }
8
- export interface ICreateBikeInspection extends IIInspectionCommonParamsV2<ICreateInspectionProducer> {
8
+ export interface ICreateBikeInspection extends IInspectionCommonParamsV2<ICreateInspectionProducer> {
9
9
  bike?: Partial<IBike>;
10
10
  }
@@ -1,5 +1,5 @@
1
1
  import { Colors } from './colors';
2
- import { ICreateInspectionProducer, IIInspectionCommonParamsV2 } from './inspection';
2
+ import { ICreateInspectionProducer, IInspectionCommonParamsV2 } from './inspection';
3
3
  export interface ICar {
4
4
  /**
5
5
  * Plate: The plate of the vehicle.
@@ -34,8 +34,8 @@ export interface ICar {
34
34
  */
35
35
  chassis?: string;
36
36
  }
37
- export interface ICreateCarInspection extends IIInspectionCommonParamsV2<ICreateInspectionProducer> {
38
- car?: ICar;
37
+ export interface ICreateCarInspection extends IInspectionCommonParamsV2<ICreateInspectionProducer> {
38
+ car?: Partial<ICar>;
39
39
  }
40
40
  export declare type InspectionCompletedBy = 'client' | 'expiration';
41
41
  export interface IValidation {
@@ -45,4 +45,4 @@ export interface IValidation {
45
45
  type: string;
46
46
  }
47
47
  export declare type InspectionType = 'goods' | 'people' | 'vehicle' | 'machinery' | 'car' | 'moto';
48
- export declare type ICreateInspectionParamsV2 = IIInspectionCommonParamsV2<ICreateInspectionProducer>;
48
+ export declare type ICreateInspectionParamsV2 = IInspectionCommonParamsV2<ICreateInspectionProducer>;
@@ -1,4 +1,4 @@
1
- export declare type Sex = 0 | 1 | 2 | 9;
1
+ export declare type Sex = 'male' | 'female' | 'not_applicable';
2
2
  export interface IConsumer {
3
3
  firstName: string;
4
4
  lastName: string;
@@ -1,5 +1,5 @@
1
1
  import { IConfiguration, IConfigurationCommonValidations } from './configuration';
2
- import { ICreateInspectionProducer, IIInspectionCommonParamsV2 } from './inspection';
2
+ import { ICreateInspectionProducer, IInspectionCommonParamsV2 } from './inspection';
3
3
  export interface IGoodConfiguration extends IConfigurationCommonValidations {
4
4
  /**
5
5
  * object: `Validation that verifies the serial number that appears in the serialNumber photo is the same as declared.`
@@ -40,6 +40,6 @@ export interface IGood {
40
40
  /**
41
41
  * Represents the object to be sended when create an inspection of type vehicle.
42
42
  */
43
- export interface ICreateGoodsInspection extends IIInspectionCommonParamsV2<ICreateInspectionProducer> {
43
+ export interface ICreateGoodsInspection extends IInspectionCommonParamsV2<ICreateInspectionProducer> {
44
44
  goods?: Partial<IGood>[];
45
45
  }
@@ -14,9 +14,13 @@ export interface IMakeRequest {
14
14
  headers?: IHeaders;
15
15
  withoutPredefinedHeaders?: boolean;
16
16
  }
17
- export interface IHTTPClient {
18
- headers: IHeaders;
17
+ export interface IHttpClientOpts {
18
+ maxBackoffTime: number;
19
19
  timeout: number;
20
+ maxRetries: number;
21
+ }
22
+ export interface IHTTPClient extends Partial<IHttpClientOpts> {
23
+ headers: IHeaders;
20
24
  baseURL: string;
21
25
  pathPrefix: string;
22
26
  }
@@ -1,4 +1,5 @@
1
1
  import { IConsumer } from './consumer';
2
+ import { ITemplateInputTypes } from './template';
2
3
  export declare type InspectionCompletedBy = 'client' | 'expiration';
3
4
  export interface IValidation {
4
5
  confidence: number;
@@ -6,7 +7,7 @@ export interface IValidation {
6
7
  _id: string;
7
8
  type: string;
8
9
  }
9
- export declare type InspectionType = 'goods' | 'people' | 'machinery' | 'car' | 'moto' | 'custom';
10
+ export declare type InspectionType = 'goods' | 'people' | 'machinery' | 'car' | 'moto' | 'custom' | 'bike';
10
11
  export declare type InspectionVeredict = 'approved' | 'disapproved' | 'not_defined' | 'not_provided';
11
12
  export declare type ImageGeneratedBy = 'configuration' | 'damage_declaration' | 'full_control';
12
13
  export declare type InspectionStatus = 'created' | 'started' | 'completed' | 'blocked' | 'processing';
@@ -41,17 +42,15 @@ export declare type IInputFile = {
41
42
  filename: string;
42
43
  identifier: string;
43
44
  };
44
- export declare type IInputValue = {
45
- value: any;
45
+ export interface IInputValue {
46
46
  identifier: string;
47
- contentType?: string;
48
- filename?: string;
49
- };
47
+ value: any;
48
+ }
50
49
  export declare type DeliveryChannels = 'email' | 'wsp';
51
50
  export declare type Delivery = {
52
51
  disabled: true;
53
52
  } | {
54
- channel: 'wsp';
53
+ channel: 'wsp' | 'sms';
55
54
  destination: string;
56
55
  countryISO: string;
57
56
  disabled: false;
@@ -60,19 +59,19 @@ export declare type Delivery = {
60
59
  destination: string;
61
60
  disabled: false;
62
61
  };
63
- export interface IUpdateInspection extends Pick<IIInspectionCommonParamsV2, 'inputs' | 'consumer'> {
62
+ export interface IUpdateInspection extends Pick<IInspectionCommonParamsV2, 'inputs' | 'consumer'> {
64
63
  inspectionId: string;
65
64
  }
66
65
  export interface IInspectionMetadata {
67
66
  [key: string]: string;
68
67
  }
69
- export interface IIInspectionCommonParamsV2<P = IProducer, C = IConsumer> {
68
+ export interface IInspectionCommonParamsV2<P = IProducer, C = IConsumer> {
70
69
  callbackURL?: string;
71
70
  delivery: Delivery;
72
71
  locale: 'es_AR' | 'es_MX' | 'es_CL' | 'es_UY' | 'es_PE' | string;
73
72
  inputs?: IInputValue[];
74
- consumer?: C;
75
- producer: P;
73
+ consumer?: Partial<C>;
74
+ producer?: P;
76
75
  templateId: string;
77
76
  initialStatus?: 'created' | 'started';
78
77
  metadata?: IInspectionMetadata;
@@ -94,9 +93,25 @@ export interface ICreateInspectionOutputCommon {
94
93
  export interface IGetInspection {
95
94
  inspectionId: string;
96
95
  }
97
- export interface IProduct {
96
+ export declare type InspectionTargets = 'product' | 'identity' | 'custom';
97
+ export interface IInspectionSection {
98
98
  images: IImage[];
99
- extraImages: string[];
99
+ extraImages: {
100
+ target: InspectionTargets;
101
+ uploadedAt: string;
102
+ side: string;
103
+ _id: string;
104
+ src: string;
105
+ }[];
106
+ inputs: {
107
+ value?: any;
108
+ label: string;
109
+ type: ITemplateInputTypes;
110
+ identifier: string;
111
+ }[];
112
+ }
113
+ export interface IProduct extends IInspectionSection {
114
+ type: InspectionType;
100
115
  _id: string;
101
116
  }
102
117
  export interface ICreateInspectionProducer {
@@ -111,7 +126,26 @@ export interface IProducer {
111
126
  export interface IImageToken {
112
127
  token: string;
113
128
  }
129
+ export interface IInspectionIdentity extends IInspectionSection {
130
+ address: string;
131
+ city: string;
132
+ country: string;
133
+ state: string;
134
+ phone: string;
135
+ sex: string;
136
+ birthdate: string;
137
+ email: string;
138
+ identification: string;
139
+ lastName: string;
140
+ firstName: string;
141
+ }
142
+ export declare type InspectionTemplateTypes = 'built-in' | 'created';
114
143
  export interface IInspection {
144
+ template: {
145
+ name: string;
146
+ type: InspectionTemplateTypes;
147
+ };
148
+ testing: boolean;
115
149
  veredict: InspectionVeredict;
116
150
  type: InspectionType;
117
151
  status: InspectionStatus;
@@ -119,7 +153,9 @@ export interface IInspection {
119
153
  products: IProduct[];
120
154
  _id: string;
121
155
  producer: IProducer;
156
+ custom: IInspectionSection;
122
157
  metadata: IInspectionMetadata;
158
+ identity: IInspectionIdentity;
123
159
  }
124
160
  export interface IInspectionHandler {
125
161
  }
@@ -1,6 +1,6 @@
1
1
  import { Colors } from './colors';
2
2
  import { IConfiguration, IConfigurationCommonValidations } from './configuration';
3
- import { ICreateInspectionProducer, IIInspectionCommonParamsV2 } from './inspection';
3
+ import { ICreateInspectionProducer, IInspectionCommonParamsV2 } from './inspection';
4
4
  export declare type MachineryPurpose = 'Agrícola' | 'Vial' | 'Industrial';
5
5
  export declare type MachineryUse = 'OFICIAL' | 'PRIVADO' | 'PÚBLICO';
6
6
  export declare type MachineryType = 'tractor' | 'combine_harvester' | 'sprayer' | 'seeder' | 'fumigator' | 'baler' | 'round_baler' | 'paver' | 'leveler' | 'crane' | 'excavator' | 'carts' | 'grader' | 'loader' | 'scrapers' | 'compacting_machine' | 'machine_soil_treatment' | 'forklift' | 'motor_vehicle_with_coupling_device' | 'fertilizer' | 'backhoe' | 'bulldozer' | 'compactor' | 'hidrogua' | 'trencher' | 'mixer' | 'hopper' | 'mechanical_shovel';
@@ -52,6 +52,6 @@ export interface IMachinery {
52
52
  */
53
53
  type: MachineryType;
54
54
  }
55
- export interface ICreateMachineryInspection extends IIInspectionCommonParamsV2<ICreateInspectionProducer> {
55
+ export interface ICreateMachineryInspection extends IInspectionCommonParamsV2<ICreateInspectionProducer> {
56
56
  machinery?: Partial<IMachinery>;
57
57
  }
@@ -1,5 +1,5 @@
1
1
  import { IConfiguration, IConfigurationCommonValidations } from './configuration';
2
- import { ICreateInspectionProducer, IIInspectionCommonParamsV2 } from './inspection';
2
+ import { ICreateInspectionProducer, IInspectionCommonParamsV2 } from './inspection';
3
3
  export declare type PeopleType = 'person';
4
4
  export interface IPeopleConfiguration extends IConfigurationCommonValidations {
5
5
  /**
@@ -39,6 +39,6 @@ export interface IPeople {
39
39
  /**
40
40
  * Represents the object to be sended when create an inspection of type vehicle.
41
41
  */
42
- export interface ICreatePeopleInspection extends IIInspectionCommonParamsV2<ICreateInspectionProducer> {
42
+ export interface ICreatePeopleInspection extends IInspectionCommonParamsV2<ICreateInspectionProducer> {
43
43
  people: IPeople;
44
44
  }
@@ -1,5 +1,5 @@
1
- import { ICreateInspectionProducer, IIInspectionCommonParamsV2 } from './inspection';
2
- export interface IProductMethods<C extends IIInspectionCommonParamsV2<ICreateInspectionProducer>, U extends object = any> {
1
+ import { ICreateInspectionProducer, IInspectionCommonParamsV2 } from './inspection';
2
+ export interface IProductMethods<C extends IInspectionCommonParamsV2<ICreateInspectionProducer>, U extends object = any> {
3
3
  create(input: C): Promise<any>;
4
4
  update?(productId: string, update: U): Promise<any>;
5
5
  }
@@ -1,5 +1,5 @@
1
1
  import { IConsumer } from './consumer';
2
- import { IImageDamage, InspectionStatus, InspectionVeredict, IProduct, IValidation } from './inspection';
2
+ import { IImageDamage, InspectionStatus, InspectionType, InspectionVeredict, IProduct, IValidation } from './inspection';
3
3
  export declare type IWebhookEvents = 'inspection_started' | 'image_processed' | 'inspection_blocked' | 'inspection_completed' | 'inspection_reviewed';
4
4
  export interface IWebhookProducer {
5
5
  companyId: string;
@@ -8,6 +8,8 @@ export interface IWebhookProducer {
8
8
  export interface IWebhookEvent<P, E extends IWebhookEvents> {
9
9
  event: E;
10
10
  payload: {
11
+ type: InspectionType;
12
+ _id: string;
11
13
  producer: IWebhookProducer;
12
14
  metadata: {
13
15
  [key: string]: string;
@@ -47,6 +49,11 @@ export interface IWebhookInspectionBlocked extends IWebhookEvent<{
47
49
  }, 'inspection_blocked'> {
48
50
  }
49
51
  export interface IWebhookInspectionCompleted extends IWebhookEvent<{
52
+ template: {
53
+ name: string;
54
+ _id: string;
55
+ type: string;
56
+ };
50
57
  status: 'completed';
51
58
  products: IProduct[];
52
59
  identity: IConsumer & {
@@ -60,4 +67,4 @@ export interface IWebhookInspectionCompleted extends IWebhookEvent<{
60
67
  veredict: InspectionVeredict;
61
68
  }, 'inspection_completed'> {
62
69
  }
63
- export declare type IWebhook = IWebhookInspectionStarted | IWebhookImageProcessed | IWebhookInspectionBlocked | IWebhookInspectionCompleted;
70
+ export declare type AutoinspectorWebhook = IWebhookInspectionStarted | IWebhookImageProcessed | IWebhookInspectionBlocked | IWebhookInspectionCompleted;
@@ -0,0 +1,2 @@
1
+ import { IHttpClientOpts } from '../types/http';
2
+ export declare const calculateBackoffDelay: (retryCount: number, opts: IHttpClientOpts) => number;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateBackoffDelay = void 0;
4
+ const http_1 = require("../constants/http");
5
+ const getRandomInt = (max) => Math.floor(Math.random() * Math.floor(max));
6
+ const calculateBackoffDelay = (retryCount, opts) => {
7
+ let baseDelay = 325;
8
+ if (retryCount < 1) {
9
+ baseDelay = http_1.BASE_DELAY;
10
+ }
11
+ const retries = Math.min(retryCount, opts.maxRetries);
12
+ const ceil = Math.min((1 << retries) * baseDelay, opts.maxBackoffTime);
13
+ return ceil / 2 + getRandomInt(ceil / 2 + 1);
14
+ };
15
+ exports.calculateBackoffDelay = calculateBackoffDelay;
@@ -0,0 +1,3 @@
1
+ export declare const buildOAuthHeader: (accessToken?: string | undefined) => {
2
+ Authorization?: string | undefined;
3
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildOAuthHeader = void 0;
4
+ const buildOAuthHeader = (accessToken) => {
5
+ return Object.assign({}, (!!accessToken
6
+ ? {
7
+ Authorization: `Bearer ${accessToken}`,
8
+ }
9
+ : {}));
10
+ };
11
+ exports.buildOAuthHeader = buildOAuthHeader;
@@ -0,0 +1,2 @@
1
+ import { IHttpClientOpts } from '../types/http';
2
+ export declare const httpRetryHandler: (opts: Partial<IHttpClientOpts>) => (retryCount: number, error: Error) => number;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.httpRetryHandler = void 0;
4
+ const http_1 = require("../constants/http");
5
+ const backoff_delay_1 = require("./backoff-delay");
6
+ const httpRetryHandler = (opts) => {
7
+ return (retryCount, error) => {
8
+ if (error) {
9
+ console.log('[AUTOINSPECTOR SDK] An error occurred:', error);
10
+ }
11
+ return (0, backoff_delay_1.calculateBackoffDelay)(retryCount, {
12
+ maxBackoffTime: opts.maxBackoffTime || http_1.DEFAULT_MAX_BACKOFF_TIME,
13
+ maxRetries: opts.maxRetries || http_1.DEFAULT_MAX_RETRIES_DEFAULT,
14
+ timeout: opts.timeout || http_1.DEFAULT_TIMEOUT,
15
+ });
16
+ };
17
+ };
18
+ exports.httpRetryHandler = httpRetryHandler;
@@ -0,0 +1,3 @@
1
+ export declare const generateIdempotencyHeader: () => {
2
+ 'Idempotency-Key': any;
3
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateIdempotencyHeader = void 0;
4
+ const { v4: uuidv4 } = require('uuid');
5
+ const generateIdempotencyHeader = () => {
6
+ return {
7
+ 'Idempotency-Key': uuidv4(),
8
+ };
9
+ };
10
+ exports.generateIdempotencyHeader = generateIdempotencyHeader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autoinspector",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Autoinspector API wrapper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,8 @@
8
8
  "test": "jest --config ./jest.config.js",
9
9
  "build": "tsc",
10
10
  "semantic-release": "semantic-release",
11
- "commit": "git-cz"
11
+ "commit": "git-cz",
12
+ "link": "npm run build && npm link"
12
13
  },
13
14
  "repository": {
14
15
  "type": "git",
@@ -27,9 +28,11 @@
27
28
  "homepage": "https://github.com/autoinspector-global/autoinspector-node#readme",
28
29
  "dependencies": {
29
30
  "axios": "^0.25.0",
31
+ "axios-retry": "^3.3.1",
30
32
  "form-data": "^4.0.0",
31
33
  "isomorphic-form-data": "^2.0.0",
32
- "typescript": "^4.5.4"
34
+ "typescript": "^4.5.4",
35
+ "uuid": "^9.0.0"
33
36
  },
34
37
  "devDependencies": {
35
38
  "@types/jest": "^27.5.0",
@@ -44,8 +47,7 @@
44
47
  "eslint-plugin-prettier": "^4.0.0",
45
48
  "jest": "^27.5.1",
46
49
  "prettier": "^2.5.1",
47
- "semantic-release": "^19.0.2",
48
- "ts-jest": "^28.0.0"
50
+ "semantic-release": "^19.0.2"
49
51
  },
50
52
  "config": {
51
53
  "commitizen": {
@@ -0,0 +1,18 @@
1
+ import { BASE_DELAY } from '../constants/http';
2
+ import { IHttpClientOpts } from '../types/http';
3
+
4
+ const getRandomInt = (max: number) => Math.floor(Math.random() * Math.floor(max));
5
+
6
+ export const calculateBackoffDelay = (retryCount: number, opts: IHttpClientOpts) => {
7
+ let baseDelay = 325;
8
+
9
+ if (retryCount < 1) {
10
+ baseDelay = BASE_DELAY;
11
+ }
12
+
13
+ const retries: number = Math.min(retryCount, opts.maxRetries);
14
+
15
+ const ceil = Math.min((1 << retries) * baseDelay, opts.maxBackoffTime);
16
+
17
+ return ceil / 2 + getRandomInt(ceil / 2 + 1);
18
+ };
@@ -0,0 +1,9 @@
1
+ export const buildOAuthHeader = (accessToken?: string) => {
2
+ return {
3
+ ...(!!accessToken
4
+ ? {
5
+ Authorization: `Bearer ${accessToken}`,
6
+ }
7
+ : {}),
8
+ };
9
+ };
@@ -0,0 +1,21 @@
1
+ import {
2
+ DEFAULT_MAX_BACKOFF_TIME,
3
+ DEFAULT_MAX_RETRIES_DEFAULT,
4
+ DEFAULT_TIMEOUT,
5
+ } from '../constants/http';
6
+ import { IHttpClientOpts } from '../types/http';
7
+ import { calculateBackoffDelay } from './backoff-delay';
8
+
9
+ export const httpRetryHandler = (opts: Partial<IHttpClientOpts>) => {
10
+ return (retryCount: number, error: Error): number => {
11
+ if (error) {
12
+ console.log('[AUTOINSPECTOR SDK] An error occurred:', error);
13
+ }
14
+
15
+ return calculateBackoffDelay(retryCount, {
16
+ maxBackoffTime: opts.maxBackoffTime || DEFAULT_MAX_BACKOFF_TIME,
17
+ maxRetries: opts.maxRetries || DEFAULT_MAX_RETRIES_DEFAULT,
18
+ timeout: opts.timeout || DEFAULT_TIMEOUT,
19
+ });
20
+ };
21
+ };
@@ -0,0 +1,7 @@
1
+ const { v4: uuidv4 } = require('uuid');
2
+
3
+ export const generateIdempotencyHeader = () => {
4
+ return {
5
+ 'Idempotency-Key': uuidv4(),
6
+ };
7
+ };
package/.env DELETED
@@ -1,6 +0,0 @@
1
- AUTOINSPECTOR_API_BASE_URL=http://localhost:9001/api
2
- AUTOINSPECTOR_API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MTQ3ODQzYmI5YTcwODAwNTUxODc4YmQiLCJpYXQiOjE2NDAyODI5NjcsImV4cCI6MzMxNjYzMjUzNjd9.Ikc2kIZRja8WBQ3QzWo7ztMAgFk-EFNyifjw_58sg3g
3
- CI_CAR_TEMPLATE_ID=627157f565b79931f3d1dbac
4
- CI_MACHINERY_TEMPLATE_ID=6274475b1996d60ec2803218
5
- CI_PEOPLE_TEMPLATE_ID=6274475b1996d60ec2803218
6
- CI_MOTO_TEMPLATE_ID=6274475b1996d60ec2803218