@artaio/node-api 0.31.0 → 0.32.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 (49) hide show
  1. package/README.md +3 -1
  2. package/dist/lib/ArtaClient.js +13 -6
  3. package/dist/lib/MetadataTypes.d.ts +1 -93
  4. package/dist/lib/MetadataTypes.js +15 -0
  5. package/dist/lib/arta.d.ts +1 -1
  6. package/dist/lib/arta.js +2 -2
  7. package/dist/lib/endpoint/attachment.d.ts +4 -10
  8. package/dist/lib/endpoint/emailRules.d.ts +6 -12
  9. package/dist/lib/endpoint/emailSubscriptions.d.ts +6 -12
  10. package/dist/lib/endpoint/endpoint.d.ts +5 -5
  11. package/dist/lib/endpoint/endpoint.js +3 -2
  12. package/dist/lib/endpoint/hostedSessions.d.ts +12 -29
  13. package/dist/lib/endpoint/invoicePayments.d.ts +4 -15
  14. package/dist/lib/endpoint/invoices.d.ts +4 -18
  15. package/dist/lib/endpoint/keys.d.ts +6 -16
  16. package/dist/lib/endpoint/logs.d.ts +4 -20
  17. package/dist/lib/endpoint/metadata.d.ts +3 -3
  18. package/dist/lib/endpoint/organization.d.ts +2 -14
  19. package/dist/lib/endpoint/payments.d.ts +4 -12
  20. package/dist/lib/endpoint/requests.d.ts +17 -41
  21. package/dist/lib/endpoint/requests.js +17 -5
  22. package/dist/lib/endpoint/shipments.d.ts +6 -73
  23. package/dist/lib/endpoint/shipments.js +19 -13
  24. package/dist/lib/endpoint/trackings.d.ts +3 -19
  25. package/dist/lib/endpoint/uploads.d.ts +7 -18
  26. package/dist/lib/endpoint/webhookDeliveries.d.ts +4 -18
  27. package/dist/lib/endpoint/webhooks.d.ts +10 -13
  28. package/dist/lib/index.d.ts +9 -16
  29. package/dist/lib/net/FetchHttpClient.d.ts +13 -0
  30. package/dist/lib/net/FetchHttpClient.js +128 -0
  31. package/dist/lib/net/HttpClient.d.ts +3 -6
  32. package/dist/lib/types.d.ts +750 -0
  33. package/dist/lib/types.js +2 -0
  34. package/dist/package.json +15 -10
  35. package/dist/schemas/generate.d.ts +1 -0
  36. package/dist/schemas/generate.js +51 -0
  37. package/dist/schemas/index.d.ts +3204 -0
  38. package/dist/schemas/index.js +733 -0
  39. package/dist/test-integration/index.d.ts +2 -0
  40. package/dist/test-integration/index.js +7 -0
  41. package/dist/test-integration/keys.test.d.ts +1 -0
  42. package/dist/test-integration/keys.test.js +71 -0
  43. package/dist/test-integration/requests.test.d.ts +1 -0
  44. package/dist/test-integration/requests.test.js +87 -0
  45. package/dist/test-integration/shipments.test.d.ts +1 -0
  46. package/dist/test-integration/shipments.test.js +114 -0
  47. package/package.json +15 -10
  48. package/dist/lib/net/NodeHttpClient.d.ts +0 -20
  49. package/dist/lib/net/NodeHttpClient.js +0 -203
@@ -0,0 +1,2 @@
1
+ import { Arta } from '../lib/index';
2
+ export declare const arta: Arta;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.arta = void 0;
4
+ var index_1 = require("../lib/index");
5
+ exports.arta = new index_1.Arta(process.env.ARTA_API_KEY, {
6
+ host: 'api.qa.arta.io',
7
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var _1 = require(".");
40
+ var schemas_1 = require("../schemas");
41
+ describe('tests keys Arta endpoint', function () {
42
+ it('should be able CRUD api keys', function () { return __awaiter(void 0, void 0, void 0, function () {
43
+ var keys, parsedKeys, key, parsedKey, key2, parsedKey2;
44
+ return __generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, _1.arta.keys.list()];
47
+ case 1:
48
+ keys = _a.sent();
49
+ parsedKeys = (0, schemas_1.createPaginatedResponseSchema)(schemas_1.keySchema).parse(keys);
50
+ expect(parsedKeys.items.length).toBeGreaterThan(0);
51
+ return [4 /*yield*/, _1.arta.keys.create({
52
+ name: 'test key',
53
+ is_testing: true
54
+ })];
55
+ case 2:
56
+ key = _a.sent();
57
+ parsedKey = schemas_1.keySchema.parse(key);
58
+ expect(parsedKey.name).toBe('test key');
59
+ return [4 /*yield*/, _1.arta.keys.getById(parsedKey.id)];
60
+ case 3:
61
+ key2 = _a.sent();
62
+ parsedKey2 = schemas_1.keySchema.parse(key2);
63
+ expect(parsedKey2.name).toBe('test key');
64
+ return [4 /*yield*/, _1.arta.keys.remove(parsedKey.id)];
65
+ case 4:
66
+ _a.sent();
67
+ return [2 /*return*/];
68
+ }
69
+ });
70
+ }); });
71
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var _1 = require(".");
40
+ var schemas_1 = require("../schemas");
41
+ describe('tests requests Arta endpoint', function () {
42
+ it('should be able to CRUD quote requests', function () { return __awaiter(void 0, void 0, void 0, function () {
43
+ var request, parsedRequest, requests, parsedRequests, request2, parsedRequest2;
44
+ return __generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, _1.arta.requests.create({
47
+ destination: {
48
+ postal_code: '90024',
49
+ country: 'US',
50
+ },
51
+ objects: [
52
+ {
53
+ subtype: 'painting_unframed',
54
+ width: 36,
55
+ height: 24,
56
+ weight: 1,
57
+ value: 100,
58
+ value_currency: 'USD',
59
+ },
60
+ ],
61
+ origin: {
62
+ country: 'US',
63
+ postal_code: '11249',
64
+ },
65
+ })];
66
+ case 1:
67
+ request = _a.sent();
68
+ parsedRequest = schemas_1.requestSchema.parse(request);
69
+ expect(parsedRequest.status).toBe('quoted');
70
+ return [4 /*yield*/, _1.arta.requests.list()];
71
+ case 2:
72
+ requests = _a.sent();
73
+ parsedRequests = (0, schemas_1.createPaginatedResponseSchema)(schemas_1.requestListItemSchema).parse(requests);
74
+ expect(parsedRequests.items.length).toBeGreaterThanOrEqual(1);
75
+ return [4 /*yield*/, _1.arta.requests.cancel(parsedRequest.id)];
76
+ case 3:
77
+ _a.sent();
78
+ return [4 /*yield*/, _1.arta.requests.getById(parsedRequest.id)];
79
+ case 4:
80
+ request2 = _a.sent();
81
+ parsedRequest2 = schemas_1.requestSchema.parse(request2);
82
+ expect(parsedRequest2.status).toBe('cancelled');
83
+ return [2 /*return*/];
84
+ }
85
+ });
86
+ }); });
87
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var _1 = require(".");
40
+ var schemas_1 = require("../schemas");
41
+ describe.only('tests shipments Arta endpoint', function () {
42
+ var request;
43
+ beforeAll(function () { return __awaiter(void 0, void 0, void 0, function () {
44
+ return __generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, _1.arta.requests.create({
47
+ destination: {
48
+ postal_code: '10019',
49
+ country: 'US',
50
+ address_line_1: '11 W 53rd St',
51
+ contacts: [
52
+ {
53
+ 'name': 'John Doe',
54
+ 'email_address': 'johndoe@test.test',
55
+ 'phone_number': '1234567890',
56
+ }
57
+ ]
58
+ },
59
+ objects: [
60
+ {
61
+ subtype: 'painting_unframed',
62
+ width: 36,
63
+ height: 24,
64
+ weight: 1,
65
+ value: 100,
66
+ value_currency: 'USD',
67
+ },
68
+ ],
69
+ origin: {
70
+ country: 'US',
71
+ postal_code: '11249',
72
+ address_line_1: '87 Richardson St',
73
+ city: 'brooklyn',
74
+ contacts: [
75
+ {
76
+ 'name': 'John Doe',
77
+ 'email_address': 'johndoe@test.test',
78
+ 'phone_number': '1234567890',
79
+ }
80
+ ]
81
+ },
82
+ })];
83
+ case 1:
84
+ request = _a.sent();
85
+ return [2 /*return*/];
86
+ }
87
+ });
88
+ }); });
89
+ it('should be able to CRUD shipments', function () { return __awaiter(void 0, void 0, void 0, function () {
90
+ var shipment, parsedShipment, shipments, parsedShipmentList, shipmentItem, parsedShipmentItem;
91
+ return __generator(this, function (_a) {
92
+ switch (_a.label) {
93
+ case 0: return [4 /*yield*/, _1.arta.shipments.create({
94
+ quote_id: request.quotes[0].id,
95
+ })];
96
+ case 1:
97
+ shipment = _a.sent();
98
+ parsedShipment = schemas_1.shipmentSchema.parse(shipment);
99
+ expect(parsedShipment.status).toBe('pending');
100
+ return [4 /*yield*/, _1.arta.shipments.list()];
101
+ case 2:
102
+ shipments = _a.sent();
103
+ parsedShipmentList = (0, schemas_1.createPaginatedResponseSchema)(schemas_1.shipmentSchema).parse(shipments);
104
+ expect(parsedShipmentList.items.length).toBeGreaterThanOrEqual(1);
105
+ return [4 /*yield*/, _1.arta.shipments.getById(parsedShipment.id)];
106
+ case 3:
107
+ shipmentItem = _a.sent();
108
+ parsedShipmentItem = schemas_1.shipmentSchema.parse(shipmentItem);
109
+ expect(parsedShipmentItem.id).toBe(parsedShipment.id);
110
+ return [2 /*return*/];
111
+ }
112
+ });
113
+ }); });
114
+ });
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@artaio/node-api",
3
- "version": "0.31.0",
3
+ "version": "0.32.0",
4
4
  "description": "The Arta Node library provides a seamless integration to Arta API for applications running on Node.js using both Typescript or Javascript.",
5
5
  "scripts": {
6
- "build": "tsc -p tsconfig-build.json",
7
- "test": "npm run lint && jest --coverage --detectOpenHandles --verbose",
6
+ "build": "npm run build:types && tsc -p tsconfig-build.json",
7
+ "build:types": "npx ts-node ./schemas/generate.ts > lib/types.ts",
8
+ "test": "npm run lint && npm run test:unit",
9
+ "test:unit": "jest --coverage --detectOpenHandles --verbose ./test/",
10
+ "test:integration": "jest --coverage --verbose --testTimeout 30000 ./test-integration/",
8
11
  "lint": "eslint .",
9
12
  "lint:fix": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
10
13
  "_postinstall": "husky install",
11
14
  "prepublishOnly": "pinst --disable",
12
- "postpublish": "pinst --enable"
15
+ "postpublish": "pinst --enable",
16
+ "prepare": "node -e \"try { (await import('husky')).default() } catch (e) { if (e.code !== 'ERR_MODULE_NOT_FOUND') throw e }\" --input-type module"
13
17
  },
14
18
  "types": "dist/lib/index.d.ts",
15
19
  "module": "dist/lib/index.js",
@@ -34,8 +38,6 @@
34
38
  "@commitlint/cli": "^18.2.0",
35
39
  "@commitlint/config-conventional": "^18.1.0",
36
40
  "@types/jest": "^29.5.7",
37
- "@types/nock": "^11.1.0",
38
- "@types/node": "^20.8.10",
39
41
  "@typescript-eslint/eslint-plugin": "^6.9.1",
40
42
  "@typescript-eslint/parser": "^6.9.1",
41
43
  "eslint": "^8.52.0",
@@ -43,19 +45,22 @@
43
45
  "eslint-plugin-import": "^2.29.0",
44
46
  "eslint-plugin-n": "^16.2.0",
45
47
  "eslint-plugin-promise": "^6.1.1",
46
- "husky": "^8.0.3",
48
+ "husky": "^9.0.11",
47
49
  "jest": "^29.7.0",
48
- "nock": "^13.3.8",
50
+ "nock": "^14.0.0-beta.5",
49
51
  "pinst": "^3.0.0",
50
52
  "prettier": "3.0.3",
51
53
  "ts-jest": "^29.1.1",
52
- "typescript": "^5.2.2"
54
+ "ts-node": "^10.9.2",
55
+ "typescript": "^5.2.2",
56
+ "zod": "^3.22.4",
57
+ "zod-to-ts": "^1.2.0"
53
58
  },
54
59
  "bugs": {
55
60
  "url": "https://github.com/artaio/arta-node-api/issues"
56
61
  },
57
62
  "engines": {
58
- "node": ">=14.x"
63
+ "node": ">=18.x"
59
64
  },
60
65
  "license": "MIT"
61
66
  }
@@ -1,20 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import * as http from 'http';
4
- import * as https from 'https';
5
- import { HttpClient, HttpClientResponse, HttpRequestParameters } from './HttpClient';
6
- export declare class NodeHttpClientResponse implements HttpClientResponse {
7
- private readonly res;
8
- statusCode?: number;
9
- headers?: http.IncomingHttpHeaders;
10
- private rawBody;
11
- constructor(res: http.IncomingMessage);
12
- body(): Promise<string>;
13
- json<T>(): Promise<T>;
14
- }
15
- export declare class NodeHttpClient implements HttpClient {
16
- private readonly agent?;
17
- private readonly logger;
18
- constructor(agent?: http.Agent | https.Agent | undefined);
19
- request(host: string, params?: Partial<HttpRequestParameters>): Promise<HttpClientResponse>;
20
- }
@@ -1,203 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- var desc = Object.getOwnPropertyDescriptor(m, k);
16
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
- desc = { enumerable: true, get: function() { return m[k]; } };
18
- }
19
- Object.defineProperty(o, k2, desc);
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
- Object.defineProperty(o, "default", { enumerable: true, value: v });
26
- }) : function(o, v) {
27
- o["default"] = v;
28
- });
29
- var __importStar = (this && this.__importStar) || function (mod) {
30
- if (mod && mod.__esModule) return mod;
31
- var result = {};
32
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
- __setModuleDefault(result, mod);
34
- return result;
35
- };
36
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
- return new (P || (P = Promise))(function (resolve, reject) {
39
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
- step((generator = generator.apply(thisArg, _arguments || [])).next());
43
- });
44
- };
45
- var __generator = (this && this.__generator) || function (thisArg, body) {
46
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
- function verb(n) { return function (v) { return step([n, v]); }; }
49
- function step(op) {
50
- if (f) throw new TypeError("Generator is already executing.");
51
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
52
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53
- if (y = 0, t) op = [op[0] & 2, t.value];
54
- switch (op[0]) {
55
- case 0: case 1: t = op; break;
56
- case 4: _.label++; return { value: op[1], done: false };
57
- case 5: _.label++; y = op[1]; op = [0]; continue;
58
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
- default:
60
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
- if (t[2]) _.ops.pop();
65
- _.trys.pop(); continue;
66
- }
67
- op = body.call(thisArg, _);
68
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
- }
71
- };
72
- Object.defineProperty(exports, "__esModule", { value: true });
73
- exports.NodeHttpClient = exports.NodeHttpClientResponse = void 0;
74
- var http = __importStar(require("http"));
75
- var https = __importStar(require("https"));
76
- var logging_1 = require("../logging");
77
- var defaultHttpAgent = new http.Agent({ keepAlive: true });
78
- var defaultHttpsAgent = new https.Agent({ keepAlive: true });
79
- var NodeHttpClientResponse = /** @class */ (function () {
80
- function NodeHttpClientResponse(res) {
81
- this.res = res;
82
- this.rawBody = '';
83
- this.statusCode = res.statusCode;
84
- this.headers = res.headers;
85
- }
86
- NodeHttpClientResponse.prototype.body = function () {
87
- return __awaiter(this, void 0, void 0, function () {
88
- var bodyPromise;
89
- var _this = this;
90
- return __generator(this, function (_a) {
91
- switch (_a.label) {
92
- case 0:
93
- if (!(this.rawBody !== '')) return [3 /*break*/, 2];
94
- return [4 /*yield*/, Promise.resolve(this.rawBody)];
95
- case 1: return [2 /*return*/, _a.sent()];
96
- case 2:
97
- bodyPromise = new Promise(function (resolve) {
98
- _this.rawBody = '';
99
- _this.res.setEncoding('utf8');
100
- _this.res.on('data', function (chunk) {
101
- _this.rawBody += chunk;
102
- });
103
- _this.res.once('end', function () {
104
- resolve(_this.rawBody);
105
- });
106
- });
107
- return [4 /*yield*/, bodyPromise];
108
- case 3: return [2 /*return*/, _a.sent()];
109
- }
110
- });
111
- });
112
- };
113
- NodeHttpClientResponse.prototype.json = function () {
114
- return __awaiter(this, void 0, void 0, function () {
115
- var body;
116
- return __generator(this, function (_a) {
117
- switch (_a.label) {
118
- case 0: return [4 /*yield*/, this.body()];
119
- case 1:
120
- body = _a.sent();
121
- return [2 /*return*/, JSON.parse(body)];
122
- }
123
- });
124
- });
125
- };
126
- return NodeHttpClientResponse;
127
- }());
128
- exports.NodeHttpClientResponse = NodeHttpClientResponse;
129
- function initParams(params) {
130
- var defaultParams = {
131
- protocol: 'https',
132
- port: 443,
133
- path: '/',
134
- method: 'get',
135
- headers: {},
136
- requestData: '',
137
- timeout: 30000,
138
- };
139
- return __assign(__assign({}, defaultParams), params);
140
- }
141
- var NodeHttpClient = /** @class */ (function () {
142
- function NodeHttpClient(agent) {
143
- this.agent = agent;
144
- this.logger = (0, logging_1.getLogger)();
145
- }
146
- NodeHttpClient.prototype.request = function (host, params) {
147
- return __awaiter(this, void 0, void 0, function () {
148
- var _a, protocol, port, path, method, timeout, headers, requestData, isInsecureConnection, agent, requestPromise;
149
- return __generator(this, function (_b) {
150
- switch (_b.label) {
151
- case 0:
152
- _a = initParams(params), protocol = _a.protocol, port = _a.port, path = _a.path, method = _a.method, timeout = _a.timeout, headers = _a.headers, requestData = _a.requestData;
153
- isInsecureConnection = protocol === 'http';
154
- agent = this.agent;
155
- if (agent == null) {
156
- agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent;
157
- }
158
- this.logger.debug("[".concat(method, "] ").concat(protocol, "://").concat(host, ":").concat(port).concat(path));
159
- requestPromise = new Promise(function (resolve, reject) {
160
- var req = (isInsecureConnection ? http : https).request({
161
- host: host,
162
- port: port,
163
- path: path,
164
- method: method,
165
- agent: agent,
166
- headers: headers,
167
- ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
168
- });
169
- req.setTimeout(timeout, function () {
170
- var err = new Error("Request timed out to ".concat(host, ":").concat(port));
171
- req.destroy(err);
172
- reject(err);
173
- });
174
- req.on('response', function (res) {
175
- resolve(new NodeHttpClientResponse(res));
176
- });
177
- req.on('error', function (error) {
178
- reject(error);
179
- });
180
- req.once('socket', function (socket) {
181
- if (socket.connecting) {
182
- socket.once(isInsecureConnection ? 'connect' : 'secureConnect', function () {
183
- // Send payload; we're safe:
184
- req.write(requestData);
185
- req.end();
186
- });
187
- }
188
- else {
189
- // we're already connected
190
- req.write(requestData);
191
- req.end();
192
- }
193
- });
194
- });
195
- return [4 /*yield*/, requestPromise];
196
- case 1: return [2 /*return*/, _b.sent()];
197
- }
198
- });
199
- });
200
- };
201
- return NodeHttpClient;
202
- }());
203
- exports.NodeHttpClient = NodeHttpClient;