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.
- package/dist/lib/httpClient.d.ts +1 -0
- package/dist/lib/httpClient.js +18 -14
- package/dist/lib/httpClient.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/httpClient.ts +27 -26
package/dist/lib/httpClient.d.ts
CHANGED
|
@@ -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>;
|
package/dist/lib/httpClient.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
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
|
-
|
|
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":"
|
|
1
|
+
{"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../src/lib/httpClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4DAA0D;AAE1D,sDAA6E;AAC7E,sDAAmD;AACnD,gDAA0E;AAC1E,mDAAgD;AAEhD,SAAsB,WAAW,CAAC,MAAc,EAAE,GAAY,EAAG,OAAY,IAAI,EAAE,UAAgB,IAAA,iCAAsB,GAAE;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GAAG,EAAE,CAAC,CAAC;SAC7B;IACL,CAAC;CAAA;AApBD,0DAoBC"}
|
package/package.json
CHANGED
package/src/lib/httpClient.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
}
|