@verii/vnf-nodejs-wallet-sdk 1.2.0-pre.1777246370 → 1.2.0-pre.1777246593
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,10 +1,21 @@
|
|
|
1
|
+
import type { HttpClientInitOptions } from '@verii/http-client';
|
|
1
2
|
import NetworkService from '../../../domain/infrastructure/network/NetworkService';
|
|
2
3
|
import Response from './Response';
|
|
3
4
|
import Request from './Request';
|
|
5
|
+
type NetworkServiceImplOptions = Pick<HttpClientInitOptions, 'isTest'>;
|
|
4
6
|
export default class NetworkServiceImpl implements NetworkService {
|
|
7
|
+
private readonly createHttpClient;
|
|
8
|
+
constructor(options?: NetworkServiceImplOptions);
|
|
5
9
|
sendRequestRaw(request: Request): Promise<Response>;
|
|
6
10
|
sendRequest(request: Request): Promise<Response>;
|
|
7
11
|
logRequest(request: Request): void;
|
|
12
|
+
private sendGetRequest;
|
|
13
|
+
private sendPostRequest;
|
|
14
|
+
private parseResponse;
|
|
15
|
+
private parsePayload;
|
|
8
16
|
private normalizeError;
|
|
17
|
+
private normalizeResponseError;
|
|
9
18
|
private isJsonContentType;
|
|
19
|
+
private headerValue;
|
|
10
20
|
}
|
|
21
|
+
export {};
|
|
@@ -12,40 +12,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const
|
|
15
|
+
const http_client_1 = require("@verii/http-client");
|
|
16
|
+
const node_crypto_1 = require("node:crypto");
|
|
16
17
|
const VCLError_1 = __importDefault(require("../../../../api/entities/error/VCLError"));
|
|
17
18
|
const HelperFunctions_1 = require("../../../utils/HelperFunctions");
|
|
18
19
|
const VCLLog_1 = __importDefault(require("../../../utils/VCLLog"));
|
|
19
20
|
const Response_1 = __importDefault(require("./Response"));
|
|
20
21
|
const HttpMethod_1 = require("./HttpMethod");
|
|
22
|
+
const TRACE_ID_HEADER = 'x-trace-id';
|
|
23
|
+
const TRACE_ID_PREFIX = 'vnf-sdk_';
|
|
21
24
|
class NetworkServiceImpl {
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.createHttpClient = (0, http_client_1.initHttpClient)(Object.assign({ isTest: process.env.NODE_ENV === 'test', traceIdHeader: TRACE_ID_HEADER }, options));
|
|
27
|
+
}
|
|
22
28
|
sendRequestRaw(request) {
|
|
23
29
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
30
|
const MAX_AGE = 60 * 60 * 24;
|
|
25
|
-
|
|
26
|
-
let commonHeaders = request.headers;
|
|
27
|
-
if (request.useCaches) {
|
|
28
|
-
commonHeaders = Object.assign(Object.assign({}, request.headers), { 'Cache-Control': `public, max-age=${MAX_AGE}` });
|
|
29
|
-
}
|
|
30
|
-
switch (request.method) {
|
|
31
|
-
case HttpMethod_1.HttpMethod.GET:
|
|
32
|
-
handler = () => axios_1.default.create(Object.assign({}, axios_1.default.defaults)).get(request.endpoint, {
|
|
33
|
-
headers: commonHeaders,
|
|
34
|
-
});
|
|
35
|
-
break;
|
|
36
|
-
case HttpMethod_1.HttpMethod.POST:
|
|
37
|
-
handler = () => axios_1.default
|
|
38
|
-
.create(Object.assign({}, axios_1.default.defaults))
|
|
39
|
-
.post(request.endpoint, request.body, {
|
|
40
|
-
headers: Object.assign(Object.assign({}, commonHeaders), { 'Content-Type': request.contentType }),
|
|
41
|
-
});
|
|
42
|
-
break;
|
|
43
|
-
default:
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
31
|
+
const commonHeaders = buildRequestHeaders(request, MAX_AGE);
|
|
46
32
|
try {
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
switch (request.method) {
|
|
34
|
+
case HttpMethod_1.HttpMethod.GET:
|
|
35
|
+
return yield this.sendGetRequest(request, commonHeaders);
|
|
36
|
+
case HttpMethod_1.HttpMethod.POST:
|
|
37
|
+
return yield this.sendPostRequest(request, commonHeaders);
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Unsupported HTTP method: ${request.method}`);
|
|
40
|
+
}
|
|
49
41
|
}
|
|
50
42
|
catch (error) {
|
|
51
43
|
throw this.normalizeError(error);
|
|
@@ -61,24 +53,64 @@ class NetworkServiceImpl {
|
|
|
61
53
|
logRequest(request) {
|
|
62
54
|
VCLLog_1.default.info(request, 'Network request');
|
|
63
55
|
}
|
|
56
|
+
sendGetRequest(request, headers) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
var _a, _b;
|
|
59
|
+
const httpClient = this.createHttpClient({
|
|
60
|
+
log: VCLLog_1.default,
|
|
61
|
+
traceId: (_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a[TRACE_ID_HEADER]) !== null && _b !== void 0 ? _b : generateTraceId(),
|
|
62
|
+
});
|
|
63
|
+
const response = yield httpClient.get(request.endpoint, {
|
|
64
|
+
headers,
|
|
65
|
+
});
|
|
66
|
+
return this.parseResponse(response);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
sendPostRequest(request, headers) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
var _a, _b;
|
|
72
|
+
const httpClient = this.createHttpClient({
|
|
73
|
+
log: VCLLog_1.default,
|
|
74
|
+
traceId: (_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a[TRACE_ID_HEADER]) !== null && _b !== void 0 ? _b : generateTraceId(),
|
|
75
|
+
});
|
|
76
|
+
const response = yield httpClient.post(request.endpoint, request.body, {
|
|
77
|
+
headers: Object.assign(Object.assign({}, headers), { 'content-type': request.contentType }),
|
|
78
|
+
});
|
|
79
|
+
return this.parseResponse(response);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
parseResponse(response) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const payload = yield this.parsePayload(response);
|
|
85
|
+
return new Response_1.default(payload, response.statusCode);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
parsePayload(response) {
|
|
89
|
+
var _a;
|
|
90
|
+
const contentType = this.headerValue((_a = response.resHeaders) === null || _a === void 0 ? void 0 : _a['content-type']);
|
|
91
|
+
return this.isJsonContentType(contentType)
|
|
92
|
+
? response.json()
|
|
93
|
+
: response.text();
|
|
94
|
+
}
|
|
64
95
|
normalizeError(error) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!response) {
|
|
68
|
-
return VCLError_1.default.fromError(error);
|
|
96
|
+
if ((error === null || error === void 0 ? void 0 : error.body) !== undefined && (error === null || error === void 0 ? void 0 : error.statusCode) != null) {
|
|
97
|
+
return this.normalizeResponseError(error.body, error.statusCode, error.headers);
|
|
69
98
|
}
|
|
70
|
-
|
|
71
|
-
|
|
99
|
+
return VCLError_1.default.fromError(error);
|
|
100
|
+
}
|
|
101
|
+
normalizeResponseError(payload, statusCode, headers) {
|
|
102
|
+
if (this.isJsonContentType(this.headerValue(headers === null || headers === void 0 ? void 0 : headers['content-type']))) {
|
|
103
|
+
const normalizedError = VCLError_1.default.fromPayloadJson(payload);
|
|
72
104
|
if (normalizedError.statusCode == null) {
|
|
73
|
-
normalizedError.statusCode =
|
|
105
|
+
normalizedError.statusCode = statusCode;
|
|
74
106
|
}
|
|
75
107
|
return normalizedError;
|
|
76
108
|
}
|
|
77
|
-
const textPayload = (0, HelperFunctions_1.toNullableString)(
|
|
109
|
+
const textPayload = (0, HelperFunctions_1.toNullableString)(payload);
|
|
78
110
|
return new VCLError_1.default({
|
|
79
111
|
payload: textPayload,
|
|
80
|
-
message:
|
|
81
|
-
statusCode
|
|
112
|
+
message: `Request failed with status code ${statusCode}`,
|
|
113
|
+
statusCode,
|
|
82
114
|
});
|
|
83
115
|
}
|
|
84
116
|
isJsonContentType(contentType) {
|
|
@@ -89,6 +121,16 @@ class NetworkServiceImpl {
|
|
|
89
121
|
return (normalizedContentType.includes('application/json') ||
|
|
90
122
|
normalizedContentType.includes('+json'));
|
|
91
123
|
}
|
|
124
|
+
headerValue(value) {
|
|
125
|
+
return Array.isArray(value) ? value.join(',') : (value !== null && value !== void 0 ? value : undefined);
|
|
126
|
+
}
|
|
92
127
|
}
|
|
93
128
|
exports.default = NetworkServiceImpl;
|
|
129
|
+
const buildRequestHeaders = (request, maxAge) => {
|
|
130
|
+
var _a;
|
|
131
|
+
return (Object.assign(Object.assign({}, ((_a = request.headers) !== null && _a !== void 0 ? _a : {})), (request.useCaches
|
|
132
|
+
? { 'Cache-Control': `public, max-age=${maxAge}` }
|
|
133
|
+
: {})));
|
|
134
|
+
};
|
|
135
|
+
const generateTraceId = () => `${TRACE_ID_PREFIX}${(0, node_crypto_1.randomBytes)(6).toString('base64url')}`;
|
|
94
136
|
//# sourceMappingURL=NetworkServiceImpl.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NetworkServiceImpl.js","sourceRoot":"","sources":["../../../../../src/impl/data/infrastructure/network/NetworkServiceImpl.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"NetworkServiceImpl.js","sourceRoot":"","sources":["../../../../../src/impl/data/infrastructure/network/NetworkServiceImpl.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,oDAAoD;AAEpD,6CAA0C;AAE1C,uFAA+D;AAE/D,oEAAkE;AAClE,mEAA2C;AAC3C,0DAAkC;AAElC,6CAA0C;AAG1C,MAAM,eAAe,GAAG,YAAY,CAAC;AACrC,MAAM,eAAe,GAAG,UAAU,CAAC;AAEnC,MAAqB,kBAAkB;IAGnC,YAAY,UAAqC,EAAE;QAC/C,IAAI,CAAC,gBAAgB,GAAG,IAAA,4BAAc,kBAClC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,EACvC,aAAa,EAAE,eAAe,IAC3B,OAAO,EACZ,CAAC;IACP,CAAC;IAEK,cAAc,CAAC,OAAgB;;YACjC,MAAM,OAAO,G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C,CAAC;IACzE,CAAC;CACJ;AAnJD,qCAmJC;AAED,MAAM,mBAAmB,GAAG,CAAC,OAAgB,EAAE,MAAc,EAAE,EAAE;;IAAC,OAAA,iCAC3D,CAAC,MAAA,OAAO,CAAC,OAAO,mCAAI,EAAE,CAAC,GACvB,CAAC,OAAO,CAAC,SAAS;QACjB,CAAC,CAAC,EAAE,eAAe,EAAE,mBAAmB,MAAM,EAAE,EAAE;QAClD,CAAC,CAAC,EAAE,CAAC,EACX,CAAA;CAAA,CAAC;AAEH,MAAM,eAAe,GAAG,GAAG,EAAE,CACzB,GAAG,eAAe,GAAG,IAAA,yBAAW,EAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verii/vnf-nodejs-wallet-sdk",
|
|
3
|
-
"version": "1.2.0-pre.
|
|
3
|
+
"version": "1.2.0-pre.1777246593",
|
|
4
4
|
"description": "VNF Wallet SDK Nodejs",
|
|
5
5
|
"repository": "https://github.com/LFDT-Verii/core",
|
|
6
6
|
"author": "Andres Olave",
|
|
@@ -16,15 +16,15 @@
|
|
|
16
16
|
"build": "tsc",
|
|
17
17
|
"build:watch": "tsc --watch",
|
|
18
18
|
"prepublishOnly": "tsc --declaration",
|
|
19
|
-
"test": "node --enable-source-maps --import=tsx --test --test-reporter=spec --test-reporter-destination=stdout '**/*.test.ts' '**/*.test.tsx'",
|
|
20
|
-
"test:ci": "node --enable-source-maps --import=tsx --test --experimental-test-coverage --test-reporter=spec --test-reporter=junit --test-reporter-destination=stdout --test-reporter-destination=test-results.junit.xml --test-reporter=lcov --test-reporter-destination=lcov.info '**/*.test.ts' '**/*.test.tsx'"
|
|
19
|
+
"test": "cross-env NODE_ENV=test node --enable-source-maps --import=tsx --test --test-reporter=spec --test-reporter-destination=stdout '**/*.test.ts' '**/*.test.tsx'",
|
|
20
|
+
"test:ci": "cross-env NODE_ENV=test node --enable-source-maps --import=tsx --test --experimental-test-coverage --test-reporter=spec --test-reporter=junit --test-reporter-destination=stdout --test-reporter-destination=test-results.junit.xml --test-reporter=lcov --test-reporter-destination=lcov.info '**/*.test.ts' '**/*.test.tsx'"
|
|
21
21
|
},
|
|
22
22
|
"engines": {
|
|
23
23
|
"node": ">= 22.22.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@verii/
|
|
27
|
-
"
|
|
26
|
+
"@verii/http-client": "1.2.0-pre.1777246593",
|
|
27
|
+
"@verii/vc-checks": "1.2.0-pre.1777246593",
|
|
28
28
|
"canonicalize": "^2.1.0",
|
|
29
29
|
"jose": "^5.9.6",
|
|
30
30
|
"pino": "^10.0.0",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/node": "24.12.2",
|
|
35
|
-
"@verii/test-regexes": "1.2.0-pre.
|
|
35
|
+
"@verii/test-regexes": "1.2.0-pre.1777246593",
|
|
36
36
|
"eslint": "9.39.4",
|
|
37
37
|
"eslint-config-prettier": "10.1.8",
|
|
38
38
|
"eslint-import-resolver-typescript": "4.4.4",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"lib"
|
|
56
56
|
]
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "b28187707886028c7a0e2ba40d3f933b29d01da1"
|
|
59
59
|
}
|