@nestia/fetcher 1.2.0 → 1.2.1
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/lib/Fetcher.js +38 -40
- package/lib/Fetcher.js.map +1 -1
- package/lib/IEncryptionPassword.d.ts +0 -10
- package/package.json +1 -1
- package/src/Fetcher.ts +42 -50
- package/src/IEncryptionPassword.ts +0 -16
package/lib/Fetcher.js
CHANGED
|
@@ -73,10 +73,11 @@ var Fetcher = /** @class */ (function () {
|
|
|
73
73
|
function Fetcher() {
|
|
74
74
|
}
|
|
75
75
|
Fetcher.fetch = function (connection, encrypted, method, path, input, stringify) {
|
|
76
|
+
var _a;
|
|
76
77
|
return __awaiter(this, void 0, void 0, function () {
|
|
77
|
-
var init,
|
|
78
|
-
return __generator(this, function (
|
|
79
|
-
switch (
|
|
78
|
+
var init, url, response, text, content, ret;
|
|
79
|
+
return __generator(this, function (_b) {
|
|
80
|
+
switch (_b.label) {
|
|
80
81
|
case 0:
|
|
81
82
|
if (encrypted.request === true || encrypted.response === true)
|
|
82
83
|
if (connection.encryption === undefined)
|
|
@@ -86,21 +87,25 @@ var Fetcher = /** @class */ (function () {
|
|
|
86
87
|
headers: encrypted.request === false &&
|
|
87
88
|
input !== undefined &&
|
|
88
89
|
typeof input === "object"
|
|
89
|
-
? __assign(__assign({}, connection.headers), { "Content-Type": "application/json" }) : connection.headers,
|
|
90
|
+
? __assign(__assign({}, connection.headers), { "Content-Type": "application/json" }) : (_a = connection.headers) !== null && _a !== void 0 ? _a : {},
|
|
90
91
|
};
|
|
92
|
+
if (encrypted.request)
|
|
93
|
+
init.headers["Content-Type"] =
|
|
94
|
+
"text/plain";
|
|
91
95
|
// REQUEST BODY (WITH ENCRYPTION)
|
|
92
|
-
if (input !== undefined)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
if (input !== undefined)
|
|
97
|
+
init.body = (function () {
|
|
98
|
+
var json = (stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input);
|
|
99
|
+
if (!encrypted.request)
|
|
100
|
+
return json;
|
|
101
|
+
var password = connection.encryption instanceof Function
|
|
102
|
+
? connection.encryption({
|
|
103
|
+
headers: init.headers,
|
|
104
|
+
body: json,
|
|
105
|
+
}, true)
|
|
98
106
|
: connection.encryption;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
init.body = body_1;
|
|
103
|
-
}
|
|
107
|
+
return AesPkcs5_1.AesPkcs5.encrypt(json, password.key, password.iv);
|
|
108
|
+
})();
|
|
104
109
|
//----
|
|
105
110
|
// RESPONSE MESSAGE
|
|
106
111
|
//----
|
|
@@ -110,13 +115,13 @@ var Fetcher = /** @class */ (function () {
|
|
|
110
115
|
path = "/" + path;
|
|
111
116
|
url = new URL("".concat(connection.host).concat(path));
|
|
112
117
|
return [4 /*yield*/, polyfill.get()];
|
|
113
|
-
case 1: return [4 /*yield*/, (
|
|
118
|
+
case 1: return [4 /*yield*/, (_b.sent())(url.href, init)];
|
|
114
119
|
case 2:
|
|
115
|
-
response =
|
|
120
|
+
response = _b.sent();
|
|
116
121
|
return [4 /*yield*/, response.text()];
|
|
117
122
|
case 3:
|
|
118
|
-
|
|
119
|
-
if (!
|
|
123
|
+
text = _b.sent();
|
|
124
|
+
if (!text)
|
|
120
125
|
return [2 /*return*/, undefined];
|
|
121
126
|
// CHECK THE STATUS CODE
|
|
122
127
|
if ((encrypted.status !== undefined &&
|
|
@@ -124,16 +129,19 @@ var Fetcher = /** @class */ (function () {
|
|
|
124
129
|
(encrypted.status === undefined &&
|
|
125
130
|
response.status !== 200 &&
|
|
126
131
|
response.status !== 201))
|
|
127
|
-
throw new HttpError_1.HttpError(method, path, response.status,
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
throw new HttpError_1.HttpError(method, path, response.status, text);
|
|
133
|
+
content = !encrypted.response
|
|
134
|
+
? text
|
|
135
|
+
: (function () {
|
|
136
|
+
var password = connection.encryption instanceof Function
|
|
137
|
+
? connection.encryption({
|
|
138
|
+
headers: headers_to_object(response.headers),
|
|
139
|
+
body: text,
|
|
140
|
+
}, false)
|
|
141
|
+
: connection.encryption;
|
|
142
|
+
return AesPkcs5_1.AesPkcs5.decrypt(text, password.key, password.iv);
|
|
143
|
+
})();
|
|
144
|
+
ret = content;
|
|
137
145
|
try {
|
|
138
146
|
// PARSE RESPONSE BODY
|
|
139
147
|
ret = JSON.parse(ret);
|
|
@@ -145,7 +153,7 @@ var Fetcher = /** @class */ (function () {
|
|
|
145
153
|
Object.assign(connection.headers, ret.__set_headers__);
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
|
-
catch (
|
|
156
|
+
catch (_c) { }
|
|
149
157
|
// RETURNS
|
|
150
158
|
return [2 /*return*/, ret];
|
|
151
159
|
}
|
|
@@ -175,16 +183,6 @@ var polyfill = new Singleton_1.Singleton(function () { return __awaiter(void 0,
|
|
|
175
183
|
}
|
|
176
184
|
});
|
|
177
185
|
}); });
|
|
178
|
-
function is_disabled(password, headers, body, encoded) {
|
|
179
|
-
if (password.disabled === undefined)
|
|
180
|
-
return false;
|
|
181
|
-
if (typeof password.disabled === "function")
|
|
182
|
-
return password.disabled({
|
|
183
|
-
headers: headers.get(),
|
|
184
|
-
body: body,
|
|
185
|
-
}, encoded);
|
|
186
|
-
return password.disabled;
|
|
187
|
-
}
|
|
188
186
|
function headers_to_object(headers) {
|
|
189
187
|
var output = {};
|
|
190
188
|
headers.forEach(function (value, key) { return (output[key] = value); });
|
package/lib/Fetcher.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Fetcher.js","sourceRoot":"","sources":["../src/Fetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,o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Q;oBAC1B,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ;oBAClC,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,KAAK,QAAQ;oBAC3C,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,KAAK,SAAS,CAAA,EAHjD,wBAGiD;qBAE7C,CAAA,MAAM,CAAC,KAAK,KAAK,SAAS,CAAA,EAA1B,wBAA0B;gBAC1B,KAAA,MAAM,CAAA;gBAAW,qBAAM,IAAA,iBAAO,EAAC,YAAY,CAAC,EAAA;;gBAA5C,GAAO,KAAK,GAAI,CAAC,SAA2B,CAAS,CAAC,OAAO,CAAC;;oBAClE,sBAAQ,MAAc,CAAC,KAAK,EAAC;oBAEjC,sBAAO,MAAM,CAAC,KAAK,EAAC;;;KACvB,CAAC,CAAC;AAEH,SAAS,iBAAiB,CAAC,OAAgB;IACvC,IAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,OAAO,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG,IAAK,OAAA,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,EAArB,CAAqB,CAAC,CAAC;IACvD,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -17,16 +17,6 @@ export interface IEncryptionPassword {
|
|
|
17
17
|
* Initialization vector.
|
|
18
18
|
*/
|
|
19
19
|
iv: string;
|
|
20
|
-
/**
|
|
21
|
-
* Disable encryption to let content as plain.
|
|
22
|
-
*
|
|
23
|
-
* When you configure this `disabled` variable to be `false`, encryption and decryption
|
|
24
|
-
* algorithm would be disabled. Therefore, content like request or response body
|
|
25
|
-
* would be considered as a plain text instead.
|
|
26
|
-
*
|
|
27
|
-
* Default is `false`.
|
|
28
|
-
*/
|
|
29
|
-
disabled?: boolean | ((param: IEncryptionPassword.IParameter, encoded: boolean) => boolean);
|
|
30
20
|
}
|
|
31
21
|
export declare namespace IEncryptionPassword {
|
|
32
22
|
/**
|
package/package.json
CHANGED
package/src/Fetcher.ts
CHANGED
|
@@ -87,29 +87,35 @@ export class Fetcher {
|
|
|
87
87
|
...connection.headers,
|
|
88
88
|
"Content-Type": "application/json",
|
|
89
89
|
}
|
|
90
|
-
: connection.headers,
|
|
90
|
+
: connection.headers ?? {},
|
|
91
91
|
};
|
|
92
|
+
if (encrypted.request)
|
|
93
|
+
(init.headers as Record<string, string>)["Content-Type"] =
|
|
94
|
+
"text/plain";
|
|
92
95
|
|
|
93
96
|
// REQUEST BODY (WITH ENCRYPTION)
|
|
94
|
-
if (input !== undefined)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
if (input !== undefined)
|
|
98
|
+
init.body = (() => {
|
|
99
|
+
const json: string = (stringify ?? JSON.stringify)(input);
|
|
100
|
+
if (!encrypted.request) return json;
|
|
101
|
+
|
|
99
102
|
const password:
|
|
100
103
|
| IEncryptionPassword
|
|
101
104
|
| IEncryptionPassword.Closure =
|
|
102
105
|
connection.encryption instanceof Function
|
|
103
106
|
? connection.encryption!(
|
|
104
|
-
{
|
|
107
|
+
{
|
|
108
|
+
headers: init.headers as Record<
|
|
109
|
+
string,
|
|
110
|
+
string
|
|
111
|
+
>,
|
|
112
|
+
body: json,
|
|
113
|
+
},
|
|
105
114
|
true,
|
|
106
115
|
)
|
|
107
116
|
: connection.encryption!;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
init.body = body;
|
|
112
|
-
}
|
|
117
|
+
return AesPkcs5.encrypt(json, password.key, password.iv);
|
|
118
|
+
})();
|
|
113
119
|
|
|
114
120
|
//----
|
|
115
121
|
// RESPONSE MESSAGE
|
|
@@ -125,8 +131,8 @@ export class Fetcher {
|
|
|
125
131
|
|
|
126
132
|
// DO FETCH
|
|
127
133
|
const response: Response = await (await polyfill.get())(url.href, init);
|
|
128
|
-
|
|
129
|
-
if (!
|
|
134
|
+
const text: string = await response.text();
|
|
135
|
+
if (!text) return undefined!;
|
|
130
136
|
|
|
131
137
|
// CHECK THE STATUS CODE
|
|
132
138
|
if (
|
|
@@ -136,29 +142,33 @@ export class Fetcher {
|
|
|
136
142
|
response.status !== 200 &&
|
|
137
143
|
response.status !== 201)
|
|
138
144
|
)
|
|
139
|
-
throw new HttpError(method, path, response.status,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
145
|
+
throw new HttpError(method, path, response.status, text);
|
|
146
|
+
|
|
147
|
+
const content: string = !encrypted.response
|
|
148
|
+
? text
|
|
149
|
+
: (() => {
|
|
150
|
+
const password:
|
|
151
|
+
| IEncryptionPassword
|
|
152
|
+
| IEncryptionPassword.Closure =
|
|
153
|
+
connection.encryption instanceof Function
|
|
154
|
+
? connection.encryption!(
|
|
155
|
+
{
|
|
156
|
+
headers: headers_to_object(
|
|
157
|
+
response.headers,
|
|
158
|
+
),
|
|
159
|
+
body: text,
|
|
160
|
+
},
|
|
161
|
+
false,
|
|
162
|
+
)
|
|
163
|
+
: connection.encryption!;
|
|
164
|
+
return AesPkcs5.decrypt(text, password.key, password.iv);
|
|
165
|
+
})();
|
|
156
166
|
|
|
157
167
|
//----
|
|
158
168
|
// OUTPUT
|
|
159
169
|
//----
|
|
160
170
|
let ret: { __set_headers__: Record<string, any> } & Primitive<Output> =
|
|
161
|
-
|
|
171
|
+
content as any;
|
|
162
172
|
try {
|
|
163
173
|
// PARSE RESPONSE BODY
|
|
164
174
|
ret = JSON.parse(ret as any);
|
|
@@ -227,24 +237,6 @@ const polyfill = new Singleton(async (): Promise<typeof fetch> => {
|
|
|
227
237
|
return window.fetch;
|
|
228
238
|
});
|
|
229
239
|
|
|
230
|
-
function is_disabled(
|
|
231
|
-
password: IEncryptionPassword,
|
|
232
|
-
headers: Singleton<Record<string, string>>,
|
|
233
|
-
body: string,
|
|
234
|
-
encoded: boolean,
|
|
235
|
-
): boolean {
|
|
236
|
-
if (password.disabled === undefined) return false;
|
|
237
|
-
if (typeof password.disabled === "function")
|
|
238
|
-
return password.disabled(
|
|
239
|
-
{
|
|
240
|
-
headers: headers.get(),
|
|
241
|
-
body,
|
|
242
|
-
},
|
|
243
|
-
encoded,
|
|
244
|
-
);
|
|
245
|
-
return password.disabled;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
240
|
function headers_to_object(headers: Headers): Record<string, string> {
|
|
249
241
|
const output: Record<string, string> = {};
|
|
250
242
|
headers.forEach((value, key) => (output[key] = value));
|
|
@@ -18,22 +18,6 @@ export interface IEncryptionPassword {
|
|
|
18
18
|
* Initialization vector.
|
|
19
19
|
*/
|
|
20
20
|
iv: string;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Disable encryption to let content as plain.
|
|
24
|
-
*
|
|
25
|
-
* When you configure this `disabled` variable to be `false`, encryption and decryption
|
|
26
|
-
* algorithm would be disabled. Therefore, content like request or response body
|
|
27
|
-
* would be considered as a plain text instead.
|
|
28
|
-
*
|
|
29
|
-
* Default is `false`.
|
|
30
|
-
*/
|
|
31
|
-
disabled?:
|
|
32
|
-
| boolean
|
|
33
|
-
| ((
|
|
34
|
-
param: IEncryptionPassword.IParameter,
|
|
35
|
-
encoded: boolean,
|
|
36
|
-
) => boolean);
|
|
37
21
|
}
|
|
38
22
|
export namespace IEncryptionPassword {
|
|
39
23
|
/**
|