@napp/dti-client 4.4.5 → 4.5.2
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/builder.d.ts +5 -0
- package/builder.js +10 -4
- package/bundler.js +39 -52
- package/caller.d.ts +1 -0
- package/caller.js +65 -27
- package/client.js +19 -29
- package/errorhandle.js +38 -49
- package/esm/builder.d.ts +5 -0
- package/esm/builder.js +10 -4
- package/esm/bundler.js +3 -1
- package/esm/caller.d.ts +1 -0
- package/esm/caller.js +52 -1
- package/esm/client.js +1 -0
- package/esm/route.js +2 -0
- package/esm/tree.js +4 -0
- package/package.json +2 -2
- package/route.js +2 -0
- package/tree.js +5 -2
package/builder.d.ts
CHANGED
|
@@ -10,5 +10,10 @@ export declare class DtiClientBuilder {
|
|
|
10
10
|
getBaseUrl(name: string): string | undefined;
|
|
11
11
|
header(route: DtiRoute, builder: ODtiClientHeaderBuilder): this;
|
|
12
12
|
getHeader(name: string): ODtiClientHeaderBuilder | undefined;
|
|
13
|
+
private signatureSecretResolver;
|
|
14
|
+
signatureSecret(resolver: {
|
|
15
|
+
(): Promise<string>;
|
|
16
|
+
} | undefined): this;
|
|
17
|
+
getSignatureResolver(): (() => Promise<string>) | undefined;
|
|
13
18
|
build(): DtiClient;
|
|
14
19
|
}
|
package/builder.js
CHANGED
|
@@ -4,10 +4,8 @@ exports.DtiClientBuilder = void 0;
|
|
|
4
4
|
const tree_1 = require("./tree");
|
|
5
5
|
const client_1 = require("./client");
|
|
6
6
|
class DtiClientBuilder {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this._headers = new tree_1.TreeNamer("root");
|
|
10
|
-
}
|
|
7
|
+
_baseUrls = new tree_1.TreeNamer("root");
|
|
8
|
+
_headers = new tree_1.TreeNamer("root");
|
|
11
9
|
baseUrl(route, url) {
|
|
12
10
|
this._baseUrls.set(route.getFullname(), url);
|
|
13
11
|
return this;
|
|
@@ -30,6 +28,14 @@ class DtiClientBuilder {
|
|
|
30
28
|
}
|
|
31
29
|
return undefined;
|
|
32
30
|
}
|
|
31
|
+
signatureSecretResolver = undefined;
|
|
32
|
+
signatureSecret(resolver) {
|
|
33
|
+
this.signatureSecretResolver = resolver;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
getSignatureResolver() {
|
|
37
|
+
return this.signatureSecretResolver;
|
|
38
|
+
}
|
|
33
39
|
build() {
|
|
34
40
|
return new client_1.DtiClient(this);
|
|
35
41
|
}
|
package/bundler.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.DtiClientBandler = void 0;
|
|
13
4
|
const dti_core_1 = require("@napp/dti-core");
|
|
@@ -15,10 +6,12 @@ const exception_1 = require("@napp/exception");
|
|
|
15
6
|
const cross_fetch_1 = require("cross-fetch");
|
|
16
7
|
const errorhandle_1 = require("./errorhandle");
|
|
17
8
|
class DtiClientBandler {
|
|
9
|
+
bundleMetas;
|
|
10
|
+
builder;
|
|
11
|
+
base62 = new dti_core_1.Base62();
|
|
18
12
|
constructor(bundleMetas, builder) {
|
|
19
13
|
this.bundleMetas = bundleMetas;
|
|
20
14
|
this.builder = builder;
|
|
21
|
-
this.base62 = new dti_core_1.Base62();
|
|
22
15
|
}
|
|
23
16
|
validate() {
|
|
24
17
|
for (let it of this.bundleMetas) {
|
|
@@ -58,55 +51,49 @@ class DtiClientBandler {
|
|
|
58
51
|
for (let it of this.bundleMetas) {
|
|
59
52
|
let _headers = this.builder.getHeader(it.meta.getRoute().getFullname());
|
|
60
53
|
if (_headers) {
|
|
61
|
-
headers =
|
|
54
|
+
headers = { ...headers, ..._headers };
|
|
62
55
|
}
|
|
63
56
|
}
|
|
64
57
|
headers["Content-Type"] = "application/json";
|
|
65
58
|
return headers;
|
|
66
59
|
}
|
|
67
|
-
call() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return yield this.callPost();
|
|
75
|
-
});
|
|
60
|
+
async call() {
|
|
61
|
+
this.validate();
|
|
62
|
+
let method = this.getMethod();
|
|
63
|
+
if (method === 'get') {
|
|
64
|
+
return await this.callGet();
|
|
65
|
+
}
|
|
66
|
+
return await this.callPost();
|
|
76
67
|
}
|
|
77
|
-
callGet() {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
});
|
|
68
|
+
async callGet() {
|
|
69
|
+
try {
|
|
70
|
+
let baseUrl = this.getBase();
|
|
71
|
+
let param = this.getParam();
|
|
72
|
+
let headers = this.getHeaders();
|
|
73
|
+
let p = this.base62.encode(JSON.stringify(param));
|
|
74
|
+
let q = new URLSearchParams({ p }).toString();
|
|
75
|
+
let resp = await (0, cross_fetch_1.fetch)(`${baseUrl}/__bundler_get__?${q}`, {
|
|
76
|
+
method: 'get', headers
|
|
77
|
+
});
|
|
78
|
+
return await (0, errorhandle_1.responseHandle)(resp);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
throw exception_1.Exception.from(error);
|
|
82
|
+
}
|
|
94
83
|
}
|
|
95
|
-
callPost() {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
});
|
|
84
|
+
async callPost() {
|
|
85
|
+
try {
|
|
86
|
+
let baseUrl = this.getBase();
|
|
87
|
+
let param = this.getParam();
|
|
88
|
+
let headers = this.getHeaders();
|
|
89
|
+
let resp = await (0, cross_fetch_1.fetch)(`${baseUrl}/__bundler_post__`, {
|
|
90
|
+
method: 'post', headers, body: JSON.stringify(param)
|
|
91
|
+
});
|
|
92
|
+
return await (0, errorhandle_1.responseHandle)(resp);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw exception_1.Exception.from(error);
|
|
96
|
+
}
|
|
110
97
|
}
|
|
111
98
|
}
|
|
112
99
|
exports.DtiClientBandler = DtiClientBandler;
|
package/caller.d.ts
CHANGED
package/caller.js
CHANGED
|
@@ -1,24 +1,34 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.DtiClientCaller = void 0;
|
|
13
4
|
const dti_core_1 = require("@napp/dti-core");
|
|
14
5
|
const cross_fetch_1 = require("cross-fetch");
|
|
15
6
|
const route_1 = require("./route");
|
|
16
7
|
const errorhandle_1 = require("./errorhandle");
|
|
8
|
+
function getRandomInt(min, max) {
|
|
9
|
+
const minCeiled = Math.ceil(min);
|
|
10
|
+
const maxFloored = Math.floor(max);
|
|
11
|
+
// The maximum is exclusive and the minimum is inclusive
|
|
12
|
+
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
|
|
13
|
+
}
|
|
14
|
+
async function hmacSignBase64(secret, data) {
|
|
15
|
+
const enc = new TextEncoder();
|
|
16
|
+
const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
17
|
+
const sigBuf = await crypto.subtle.sign("HMAC", key, enc.encode(data));
|
|
18
|
+
const sigBytes = new Uint8Array(sigBuf);
|
|
19
|
+
// base64 encode
|
|
20
|
+
let bin = "";
|
|
21
|
+
sigBytes.forEach(b => (bin += String.fromCharCode(b)));
|
|
22
|
+
return btoa(bin);
|
|
23
|
+
}
|
|
17
24
|
class DtiClientCaller {
|
|
25
|
+
meta;
|
|
26
|
+
builder;
|
|
27
|
+
base62 = new dti_core_1.Base62();
|
|
28
|
+
routeClient;
|
|
18
29
|
constructor(meta, builder) {
|
|
19
30
|
this.meta = meta;
|
|
20
31
|
this.builder = builder;
|
|
21
|
-
this.base62 = new dti_core_1.Base62();
|
|
22
32
|
this.routeClient = new route_1.DtiClientRoute(meta.getRoute(), builder);
|
|
23
33
|
}
|
|
24
34
|
validate(param) {
|
|
@@ -70,7 +80,7 @@ class DtiClientCaller {
|
|
|
70
80
|
let confHeaderBuilder = this.builder.getHeader(this.meta.getRoute().getFullname());
|
|
71
81
|
if (confHeaderBuilder) {
|
|
72
82
|
let confHeaders = confHeaderBuilder(this.meta);
|
|
73
|
-
headers =
|
|
83
|
+
headers = { ...headers, ...confHeaders };
|
|
74
84
|
}
|
|
75
85
|
if (m === dti_core_1.DtiMode.BFrom) {
|
|
76
86
|
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
@@ -80,29 +90,57 @@ class DtiClientCaller {
|
|
|
80
90
|
}
|
|
81
91
|
return headers;
|
|
82
92
|
}
|
|
93
|
+
async signature(data) {
|
|
94
|
+
const nonce = '' + getRandomInt(10000, 99999);
|
|
95
|
+
const timestamp = '' + Math.round(Date.now() / 1000);
|
|
96
|
+
const secretResolver = this.builder.getSignatureResolver();
|
|
97
|
+
if (secretResolver) {
|
|
98
|
+
const secret = await secretResolver();
|
|
99
|
+
if (secret) {
|
|
100
|
+
const signature = await hmacSignBase64(secret, `${timestamp}.${nonce}.${data}`);
|
|
101
|
+
return {
|
|
102
|
+
nonce, timestamp, signature
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
nonce, timestamp,
|
|
108
|
+
signature: ''
|
|
109
|
+
};
|
|
110
|
+
}
|
|
83
111
|
getUrl() {
|
|
84
112
|
return this.routeClient.buildUrl(this.meta.getPath());
|
|
85
113
|
}
|
|
86
114
|
bundler(param) {
|
|
87
115
|
return { meta: this.meta, param };
|
|
88
116
|
}
|
|
89
|
-
call(param) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return yield (0, errorhandle_1.responseHandle)(resp);
|
|
93
|
-
});
|
|
117
|
+
async call(param) {
|
|
118
|
+
let resp = await this.callRaw(param);
|
|
119
|
+
return await (0, errorhandle_1.responseHandle)(resp);
|
|
94
120
|
}
|
|
95
|
-
callRaw(param) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
121
|
+
async callRaw(param) {
|
|
122
|
+
this.validate(param);
|
|
123
|
+
let url = this.getUrl();
|
|
124
|
+
let query = this.getQeury(param);
|
|
125
|
+
let method = this.getMethod();
|
|
126
|
+
let headers = this.getHeaders(param);
|
|
127
|
+
let body = this.getBody(param);
|
|
128
|
+
const signData = this.meta.sign(param);
|
|
129
|
+
if (signData) {
|
|
130
|
+
const { nonce, signature, timestamp } = await this.signature(signData);
|
|
131
|
+
if (signature) {
|
|
132
|
+
// console.log('method', method)
|
|
133
|
+
// console.log('------------method', method, this.meta.getFullname())
|
|
134
|
+
// console.log('timestamp', timestamp)
|
|
135
|
+
// console.log('nonce', nonce)
|
|
136
|
+
// console.log('signature', signature)
|
|
137
|
+
headers["X-DTI-Timestamp"] = timestamp;
|
|
138
|
+
headers["X-DTI-Nonce"] = nonce;
|
|
139
|
+
headers["X-DTI-Signature"] = signature;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return await (0, cross_fetch_1.fetch)(url + (query ? `?${query}` : ''), {
|
|
143
|
+
method, headers, body
|
|
106
144
|
});
|
|
107
145
|
}
|
|
108
146
|
}
|
package/client.js
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.DtiClient = void 0;
|
|
13
4
|
const route_1 = require("./route");
|
|
14
5
|
const caller_1 = require("./caller");
|
|
15
6
|
const bundler_1 = require("./bundler");
|
|
16
7
|
class DtiClient {
|
|
8
|
+
builder;
|
|
17
9
|
constructor(builder) {
|
|
18
10
|
this.builder = builder;
|
|
19
11
|
}
|
|
@@ -23,26 +15,24 @@ class DtiClient {
|
|
|
23
15
|
buildUrl(route, actionPath) {
|
|
24
16
|
return new route_1.DtiClientRoute(route, this.builder).buildUrl(actionPath);
|
|
25
17
|
}
|
|
26
|
-
bundle(m1, m2, m3, m4, m5, m6) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return yield new bundler_1.DtiClientBandler(bundleMetas, this.builder).call();
|
|
45
|
-
});
|
|
18
|
+
async bundle(m1, m2, m3, m4, m5, m6) {
|
|
19
|
+
let bundleMetas = [m1];
|
|
20
|
+
if (m2) {
|
|
21
|
+
bundleMetas.push(m2);
|
|
22
|
+
}
|
|
23
|
+
if (m3) {
|
|
24
|
+
bundleMetas.push(m3);
|
|
25
|
+
}
|
|
26
|
+
if (m4) {
|
|
27
|
+
bundleMetas.push(m4);
|
|
28
|
+
}
|
|
29
|
+
if (m5) {
|
|
30
|
+
bundleMetas.push(m5);
|
|
31
|
+
}
|
|
32
|
+
if (m6) {
|
|
33
|
+
bundleMetas.push(m6);
|
|
34
|
+
}
|
|
35
|
+
return await new bundler_1.DtiClientBandler(bundleMetas, this.builder).call();
|
|
46
36
|
}
|
|
47
37
|
}
|
|
48
38
|
exports.DtiClient = DtiClient;
|
package/errorhandle.js
CHANGED
|
@@ -1,65 +1,54 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.responseHandle = void 0;
|
|
13
4
|
const exception_1 = require("@napp/exception");
|
|
14
|
-
function responseHandle(resp, parser) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (resp.ok) {
|
|
19
|
-
if (rsu) {
|
|
20
|
-
try {
|
|
21
|
-
let value = JSON.parse(rsu);
|
|
22
|
-
return value;
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
throw new exception_1.Exception(rsu, {
|
|
26
|
-
kind: 'serviceunavailable',
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return void 0;
|
|
31
|
-
}
|
|
5
|
+
async function responseHandle(resp, parser) {
|
|
6
|
+
try {
|
|
7
|
+
let rsu = await resp.text();
|
|
8
|
+
if (resp.ok) {
|
|
32
9
|
if (rsu) {
|
|
33
|
-
let err;
|
|
34
10
|
try {
|
|
35
|
-
let
|
|
36
|
-
|
|
37
|
-
let e1 = parser(errObject);
|
|
38
|
-
if (e1) {
|
|
39
|
-
err = e1;
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
err = exception_1.Exception.from(errObject);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
err = exception_1.Exception.from(errObject);
|
|
47
|
-
}
|
|
11
|
+
let value = JSON.parse(rsu);
|
|
12
|
+
return value;
|
|
48
13
|
}
|
|
49
14
|
catch (error) {
|
|
50
|
-
|
|
15
|
+
throw new exception_1.Exception(rsu, {
|
|
51
16
|
kind: 'serviceunavailable',
|
|
52
17
|
});
|
|
53
18
|
}
|
|
54
|
-
throw err;
|
|
55
19
|
}
|
|
56
|
-
|
|
57
|
-
kind: 'serviceunavailable',
|
|
58
|
-
});
|
|
20
|
+
return void 0;
|
|
59
21
|
}
|
|
60
|
-
|
|
61
|
-
|
|
22
|
+
if (rsu) {
|
|
23
|
+
let err;
|
|
24
|
+
try {
|
|
25
|
+
let errObject = JSON.parse(rsu);
|
|
26
|
+
if (parser) {
|
|
27
|
+
let e1 = parser(errObject);
|
|
28
|
+
if (e1) {
|
|
29
|
+
err = e1;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
err = exception_1.Exception.from(errObject);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
err = exception_1.Exception.from(errObject);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
err = new exception_1.Exception(rsu, {
|
|
41
|
+
kind: 'serviceunavailable',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
throw err;
|
|
62
45
|
}
|
|
63
|
-
|
|
46
|
+
throw new exception_1.Exception(`status=${resp.status}. ${resp.statusText}`, {
|
|
47
|
+
kind: 'serviceunavailable',
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw exception_1.Exception.from(error);
|
|
52
|
+
}
|
|
64
53
|
}
|
|
65
54
|
exports.responseHandle = responseHandle;
|
package/esm/builder.d.ts
CHANGED
|
@@ -10,5 +10,10 @@ export declare class DtiClientBuilder {
|
|
|
10
10
|
getBaseUrl(name: string): string | undefined;
|
|
11
11
|
header(route: DtiRoute, builder: ODtiClientHeaderBuilder): this;
|
|
12
12
|
getHeader(name: string): ODtiClientHeaderBuilder | undefined;
|
|
13
|
+
private signatureSecretResolver;
|
|
14
|
+
signatureSecret(resolver: {
|
|
15
|
+
(): Promise<string>;
|
|
16
|
+
} | undefined): this;
|
|
17
|
+
getSignatureResolver(): (() => Promise<string>) | undefined;
|
|
13
18
|
build(): DtiClient;
|
|
14
19
|
}
|
package/esm/builder.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { TreeNamer } from "./tree";
|
|
2
2
|
import { DtiClient } from "./client";
|
|
3
3
|
export class DtiClientBuilder {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this._headers = new TreeNamer("root");
|
|
7
|
-
}
|
|
4
|
+
_baseUrls = new TreeNamer("root");
|
|
5
|
+
_headers = new TreeNamer("root");
|
|
8
6
|
baseUrl(route, url) {
|
|
9
7
|
this._baseUrls.set(route.getFullname(), url);
|
|
10
8
|
return this;
|
|
@@ -27,6 +25,14 @@ export class DtiClientBuilder {
|
|
|
27
25
|
}
|
|
28
26
|
return undefined;
|
|
29
27
|
}
|
|
28
|
+
signatureSecretResolver = undefined;
|
|
29
|
+
signatureSecret(resolver) {
|
|
30
|
+
this.signatureSecretResolver = resolver;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
getSignatureResolver() {
|
|
34
|
+
return this.signatureSecretResolver;
|
|
35
|
+
}
|
|
30
36
|
build() {
|
|
31
37
|
return new DtiClient(this);
|
|
32
38
|
}
|
package/esm/bundler.js
CHANGED
|
@@ -3,10 +3,12 @@ import { Exception } from "@napp/exception";
|
|
|
3
3
|
import { fetch } from "cross-fetch";
|
|
4
4
|
import { responseHandle } from "./errorhandle";
|
|
5
5
|
export class DtiClientBandler {
|
|
6
|
+
bundleMetas;
|
|
7
|
+
builder;
|
|
8
|
+
base62 = new Base62();
|
|
6
9
|
constructor(bundleMetas, builder) {
|
|
7
10
|
this.bundleMetas = bundleMetas;
|
|
8
11
|
this.builder = builder;
|
|
9
|
-
this.base62 = new Base62();
|
|
10
12
|
}
|
|
11
13
|
validate() {
|
|
12
14
|
for (let it of this.bundleMetas) {
|
package/esm/caller.d.ts
CHANGED
package/esm/caller.js
CHANGED
|
@@ -2,11 +2,30 @@ import { DtiMode, Base62 } from "@napp/dti-core";
|
|
|
2
2
|
import { fetch } from "cross-fetch";
|
|
3
3
|
import { DtiClientRoute } from "./route";
|
|
4
4
|
import { responseHandle } from "./errorhandle";
|
|
5
|
+
function getRandomInt(min, max) {
|
|
6
|
+
const minCeiled = Math.ceil(min);
|
|
7
|
+
const maxFloored = Math.floor(max);
|
|
8
|
+
// The maximum is exclusive and the minimum is inclusive
|
|
9
|
+
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
|
|
10
|
+
}
|
|
11
|
+
async function hmacSignBase64(secret, data) {
|
|
12
|
+
const enc = new TextEncoder();
|
|
13
|
+
const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
14
|
+
const sigBuf = await crypto.subtle.sign("HMAC", key, enc.encode(data));
|
|
15
|
+
const sigBytes = new Uint8Array(sigBuf);
|
|
16
|
+
// base64 encode
|
|
17
|
+
let bin = "";
|
|
18
|
+
sigBytes.forEach(b => (bin += String.fromCharCode(b)));
|
|
19
|
+
return btoa(bin);
|
|
20
|
+
}
|
|
5
21
|
export class DtiClientCaller {
|
|
22
|
+
meta;
|
|
23
|
+
builder;
|
|
24
|
+
base62 = new Base62();
|
|
25
|
+
routeClient;
|
|
6
26
|
constructor(meta, builder) {
|
|
7
27
|
this.meta = meta;
|
|
8
28
|
this.builder = builder;
|
|
9
|
-
this.base62 = new Base62();
|
|
10
29
|
this.routeClient = new DtiClientRoute(meta.getRoute(), builder);
|
|
11
30
|
}
|
|
12
31
|
validate(param) {
|
|
@@ -68,6 +87,24 @@ export class DtiClientCaller {
|
|
|
68
87
|
}
|
|
69
88
|
return headers;
|
|
70
89
|
}
|
|
90
|
+
async signature(data) {
|
|
91
|
+
const nonce = '' + getRandomInt(10000, 99999);
|
|
92
|
+
const timestamp = '' + Math.round(Date.now() / 1000);
|
|
93
|
+
const secretResolver = this.builder.getSignatureResolver();
|
|
94
|
+
if (secretResolver) {
|
|
95
|
+
const secret = await secretResolver();
|
|
96
|
+
if (secret) {
|
|
97
|
+
const signature = await hmacSignBase64(secret, `${timestamp}.${nonce}.${data}`);
|
|
98
|
+
return {
|
|
99
|
+
nonce, timestamp, signature
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
nonce, timestamp,
|
|
105
|
+
signature: ''
|
|
106
|
+
};
|
|
107
|
+
}
|
|
71
108
|
getUrl() {
|
|
72
109
|
return this.routeClient.buildUrl(this.meta.getPath());
|
|
73
110
|
}
|
|
@@ -85,6 +122,20 @@ export class DtiClientCaller {
|
|
|
85
122
|
let method = this.getMethod();
|
|
86
123
|
let headers = this.getHeaders(param);
|
|
87
124
|
let body = this.getBody(param);
|
|
125
|
+
const signData = this.meta.sign(param);
|
|
126
|
+
if (signData) {
|
|
127
|
+
const { nonce, signature, timestamp } = await this.signature(signData);
|
|
128
|
+
if (signature) {
|
|
129
|
+
// console.log('method', method)
|
|
130
|
+
// console.log('------------method', method, this.meta.getFullname())
|
|
131
|
+
// console.log('timestamp', timestamp)
|
|
132
|
+
// console.log('nonce', nonce)
|
|
133
|
+
// console.log('signature', signature)
|
|
134
|
+
headers["X-DTI-Timestamp"] = timestamp;
|
|
135
|
+
headers["X-DTI-Nonce"] = nonce;
|
|
136
|
+
headers["X-DTI-Signature"] = signature;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
88
139
|
return await fetch(url + (query ? `?${query}` : ''), {
|
|
89
140
|
method, headers, body
|
|
90
141
|
});
|
package/esm/client.js
CHANGED
package/esm/route.js
CHANGED
package/esm/tree.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@napp/dti-client",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.2",
|
|
4
4
|
"description": "data transaction interface client library",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"author": "Farcek <farcek@gmail.com>",
|
|
25
25
|
"license": "ISC",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@napp/dti-core": "4.
|
|
27
|
+
"@napp/dti-core": "4.5.2"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@napp/exception": "^9.1.4",
|
package/route.js
CHANGED
package/tree.js
CHANGED
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TreeNamer = void 0;
|
|
4
4
|
class TreeNamer {
|
|
5
|
+
name;
|
|
6
|
+
parent;
|
|
5
7
|
constructor(name, parent) {
|
|
6
8
|
this.name = name;
|
|
7
9
|
this.parent = parent;
|
|
8
10
|
}
|
|
11
|
+
value;
|
|
12
|
+
childs;
|
|
9
13
|
set(name, value) {
|
|
10
14
|
let names = (name || '').split('.').filter(it => !!it);
|
|
11
15
|
let curr = this;
|
|
@@ -65,10 +69,9 @@ class TreeNamer {
|
|
|
65
69
|
return this.childs[name] = new TreeNamer(name, this);
|
|
66
70
|
}
|
|
67
71
|
toObj() {
|
|
68
|
-
var _a;
|
|
69
72
|
let obj = {
|
|
70
73
|
name: this.name,
|
|
71
|
-
parent:
|
|
74
|
+
parent: this.parent?.name,
|
|
72
75
|
value: this.value
|
|
73
76
|
};
|
|
74
77
|
if (this.childs) {
|