skyeye-svc-common-utils 1.0.62 → 1.0.63

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,3 +1,4 @@
1
+ import { Response } from 'node-fetch';
1
2
  import { UserInfo } from '../interfaces';
2
3
  export declare function httpRequest(method: string, url: string, data?: any, headers?: any): Promise<Response>;
3
4
  export declare function sendInternalHttpRequest(userInfo: UserInfo, client: string, endpoint: string, data?: any, method?: string, select?: string[]): Promise<any>;
@@ -8,26 +8,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.sendInternalHttpRequest = exports.httpRequest = void 0;
16
+ const node_fetch_1 = __importDefault(require("node-fetch"));
13
17
  const commonUtils_1 = require("../utils/commonUtils");
14
18
  const commonUtils_2 = require("../utils/commonUtils");
15
19
  const appConst_1 = require("../utils/appConst");
16
- const fetch = require('node-fetch');
20
+ const logger_1 = require("../utils/logger/logger");
17
21
  function httpRequest(method, url, data = null, headers = (0, appConst_1.jsonContentTypeHeaders)()) {
18
22
  return __awaiter(this, void 0, void 0, function* () {
19
- let init = {};
20
- init = data === null ? {
21
- method: method,
22
- cache: "no-cache",
23
- headers: headers
24
- } : {
25
- method: method,
26
- cache: "no-cache",
27
- headers: headers,
28
- body: JSON.stringify(data)
23
+ const init = {
24
+ method,
25
+ headers,
26
+ body: data === null ? undefined : JSON.stringify(data)
29
27
  };
30
- return fetch(url, init);
28
+ return (0, node_fetch_1.default)(url, init);
31
29
  });
32
30
  }
33
31
  exports.httpRequest = httpRequest;
@@ -42,9 +40,15 @@ function sendInternalHttpRequest(userInfo, client, endpoint, data = null, method
42
40
  let token = `${appConst_1.commonAppConst.httpHeader.BEARER} ${yield (0, commonUtils_2.generateJWT)(userInfo)}`;
43
41
  headers[appConst_1.commonAppConst.httpHeader.AUTHORIZATION] = token;
44
42
  }
45
- return httpRequest(method, url, data, headers).then(response => {
43
+ let response;
44
+ try {
45
+ response = yield httpRequest(method, url, data, headers);
46
46
  return response.json();
47
- });
47
+ }
48
+ catch (err) {
49
+ logger_1.logger.error(`Error while sending internal http request, receiving response: ${response}`);
50
+ throw new Error(`${err}`);
51
+ }
48
52
  });
49
53
  }
50
54
  exports.sendInternalHttpRequest = sendInternalHttpRequest;
@@ -1 +1 @@
1
- {"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../src/lib/httpClient.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,sDAA6E;AAC7E,sDAAmD;AACnD,gDAA0E;AAC1E,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAEpC,SAAsB,WAAW,CAAC,MAAc,EAAE,GAAY,EAAG,OAAY,IAAI,EAAE,UAAgB,IAAA,iCAAsB,GAAE;;QACzH,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,IAAI,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,CAAE;YACpB,MAAM,EAAE,MAAM;YACd,KAAK,EAAE,UAAU;YACjB,OAAO,EAAE,OAAO;SACnB,CAAC,CAAC,CAAC;YACF,MAAM,EAAE,MAAM;YACd,KAAK,EAAE,UAAU;YACjB,OAAO,EAAE,OAAO;YAChB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC;QAEF,OAAO,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC1B,CAAC;CAAA;AAdD,kCAcC;AAED,SAAsB,uBAAuB,CAAC,QAAiB,EAAE,MAAc,EAAE,QAAgB,EAAE,OAAY,IAAI,EAAE,MAAe,EAAE,MAAgB;;QACpJ,MAAM,GAAG,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,yBAAc,CAAC,UAAU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,SAAsB,uBAAuB,CAAC,QAAiB,EAAE,MAAc,EAAE,QAAgB,EAAE,OAAY,IAAI,EAAE,MAAe,EAAE,MAAgB;;QAClJ,MAAM,GAAG,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,yBAAc,CAAC,UAAU,CAAC,IAAI,CAAC;QAClD,MAAM,cAAc,GAAG,MAAM,IAAA,+BAAiB,EAAC,MAAM,CAAC,CAAA;QACtD,MAAM,GAAG,GAAG,cAAc,GAAG,QAAQ,CAAC;QACtC,IAAI,OAAO,GAAO,IAAA,iCAAsB,GAAE,CAAC;QAC3C,IAAG,QAAQ,EAAC;YACR,QAAQ,GAAG,MAAM,IAAA,gCAAkB,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;YACrD,IAAI,KAAK,GAAW,GAAG,yBAAc,CAAC,UAAU,CAAC,MAAM,IAAI,MAAM,IAAA,yBAAW,EAAC,QAAQ,CAAC,EAAE,CAAC;YACzF,OAAO,CAAC,yBAAc,CAAC,UAAU,CAAC,aAAa,CAAC,GAAG,KAAK,CAAC;SAC5D;QAED,IAAI,QAAkB,CAAC;QACvB,IAAI;YACA,QAAQ,GAAG,MAAM,WAAW,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;YACzD,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;SAC1B;QACD,OAAO,GAAG,EAAE;YACR,eAAM,CAAC,KAAK,CAAC,kEAAkE,QAAQ,EAAE,CAAC,CAAC;YAC3F,MAAM,IAAI,KAAK,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC;SAC7B;IACL,CAAC;CAAA;AApBD,0DAoBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skyeye-svc-common-utils",
3
- "version": "1.0.62",
3
+ "version": "1.0.63",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,36 +1,37 @@
1
+ import fetch, { Response, RequestInit } from 'node-fetch';
1
2
  import { UserInfo } from '../interfaces';
2
3
  import { getInternalClient, userInfoSimplifier } from '../utils/commonUtils';
3
4
  import { generateJWT } from '../utils/commonUtils';
4
5
  import {commonAppConst, jsonContentTypeHeaders } from '../utils/appConst';
5
- const fetch = require('node-fetch');
6
+ import { logger } from '../utils/logger/logger';
6
7
 
7
8
  export async function httpRequest(method: string, url : string, data: any = null, headers : any = jsonContentTypeHeaders()): Promise<Response> {
8
- let init = {};
9
- init = data === null ? {
10
- method: method,
11
- cache: "no-cache",
12
- headers: headers
13
- } : {
14
- method: method,
15
- cache: "no-cache",
16
- headers: headers,
17
- body: JSON.stringify(data)
18
- };
19
-
20
- return fetch(url, init);
9
+ const init: RequestInit = {
10
+ method,
11
+ headers,
12
+ body: data === null ? undefined : JSON.stringify(data)
13
+ }
14
+ return fetch(url, init);
21
15
  }
22
16
 
23
17
  export async function sendInternalHttpRequest(userInfo:UserInfo, client: string, endpoint: string, data: any = null, method?: string, select?:string[]) {
24
- method = method ?? commonAppConst.httpMethod.POST;
25
- const internalClient = await getInternalClient(client)
26
- const url = internalClient + endpoint;
27
- let headers:any = jsonContentTypeHeaders();
28
- if(userInfo){
29
- userInfo = await userInfoSimplifier(userInfo, select)
30
- let token: string = `${commonAppConst.httpHeader.BEARER} ${await generateJWT(userInfo)}`;
31
- headers[commonAppConst.httpHeader.AUTHORIZATION] = token;
32
- }
33
- return httpRequest(method, url, data, headers).then(response => {
34
- return response.json()
35
- })
18
+ method = method ?? commonAppConst.httpMethod.POST;
19
+ const internalClient = await getInternalClient(client)
20
+ const url = internalClient + endpoint;
21
+ let headers:any = jsonContentTypeHeaders();
22
+ if(userInfo){
23
+ userInfo = await userInfoSimplifier(userInfo, select)
24
+ let token: string = `${commonAppConst.httpHeader.BEARER} ${await generateJWT(userInfo)}`;
25
+ headers[commonAppConst.httpHeader.AUTHORIZATION] = token;
26
+ }
27
+
28
+ let response: Response;
29
+ try {
30
+ response = await httpRequest(method, url, data, headers);
31
+ return response.json();
32
+ }
33
+ catch (err) {
34
+ logger.error(`Error while sending internal http request, receiving response: ${response}`);
35
+ throw new Error(`${err}`);
36
+ }
36
37
  }