@nestia/fetcher 2.0.0-dev.20230906 → 2.0.0-dev.20230908
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/EncryptedFetcher.d.ts +3 -0
- package/lib/EncryptedFetcher.js +39 -0
- package/lib/EncryptedFetcher.js.map +1 -1
- package/lib/HttpError.d.ts +3 -1
- package/lib/HttpError.js +3 -1
- package/lib/HttpError.js.map +1 -1
- package/lib/IPropagation.d.ts +42 -0
- package/lib/IPropagation.js +3 -0
- package/lib/IPropagation.js.map +1 -0
- package/lib/PlainFetcher.d.ts +3 -0
- package/lib/PlainFetcher.js +16 -0
- package/lib/PlainFetcher.js.map +1 -1
- package/lib/Primitive.d.ts +15 -13
- package/lib/Resolved.d.ts +46 -0
- package/lib/Resolved.js +3 -0
- package/lib/Resolved.js.map +1 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/internal/FetcherBase.d.ts +2 -0
- package/lib/internal/FetcherBase.js +153 -99
- package/lib/internal/FetcherBase.js.map +1 -1
- package/package.json +1 -1
- package/src/EncryptedFetcher.ts +74 -0
- package/src/HttpError.ts +3 -0
- package/src/IPropagation.ts +73 -0
- package/src/PlainFetcher.ts +38 -0
- package/src/Primitive.ts +20 -16
- package/src/Resolved.ts +116 -0
- package/src/index.ts +3 -1
- package/src/internal/FetcherBase.ts +87 -39
|
@@ -95,121 +95,175 @@ var FetcherBase;
|
|
|
95
95
|
var _this = this;
|
|
96
96
|
FetcherBase.fetch = function (props) {
|
|
97
97
|
return function (connection, route, input, stringify) { return __awaiter(_this, void 0, void 0, function () {
|
|
98
|
-
var
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
case
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
98
|
+
var result;
|
|
99
|
+
return __generator(this, function (_a) {
|
|
100
|
+
switch (_a.label) {
|
|
101
|
+
case 0: return [4 /*yield*/, _Propagate("fetch")(props)(connection, route, input, stringify)];
|
|
102
|
+
case 1:
|
|
103
|
+
result = _a.sent();
|
|
104
|
+
if (result.success === false)
|
|
105
|
+
throw new HttpError_1.HttpError(route.method, route.path, result.status, result.headers, result.data);
|
|
106
|
+
return [2 /*return*/, result.data];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}); };
|
|
110
|
+
};
|
|
111
|
+
FetcherBase.propagate = function (props) {
|
|
112
|
+
return function (connection, route, input, stringify) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
113
|
+
return [2 /*return*/, _Propagate("propagate")(props)(connection, route, input, stringify)];
|
|
114
|
+
}); }); };
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
var _Propagate = function (method) {
|
|
120
|
+
return function (props) {
|
|
121
|
+
return function (connection, route, input, stringify) { return __awaiter(_this, void 0, void 0, function () {
|
|
122
|
+
var headers, init, path, url, response, result, _a, type, text, _b, _c, _d;
|
|
123
|
+
var _e, _f, _g, _h, _j;
|
|
124
|
+
return __generator(this, function (_k) {
|
|
125
|
+
switch (_k.label) {
|
|
126
|
+
case 0:
|
|
127
|
+
headers = __assign({}, ((_e = connection.headers) !== null && _e !== void 0 ? _e : {}));
|
|
128
|
+
if (input !== undefined) {
|
|
129
|
+
if (((_f = route.request) === null || _f === void 0 ? void 0 : _f.type) === undefined)
|
|
130
|
+
throw new Error("Error on ".concat(props.className, ".fetch(): no content-type being configured."));
|
|
131
|
+
headers["Content-Type"] = route.request.type;
|
|
132
|
+
}
|
|
133
|
+
init = __assign(__assign({}, ((_g = connection.options) !== null && _g !== void 0 ? _g : {})), { method: route.method, headers: (function () {
|
|
134
|
+
var e_1, _a, e_2, _b;
|
|
135
|
+
var output = [];
|
|
136
|
+
try {
|
|
137
|
+
for (var _c = __values(Object.entries(headers)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
138
|
+
var _e = __read(_d.value, 2), key = _e[0], value = _e[1];
|
|
139
|
+
if (value === undefined)
|
|
140
|
+
continue;
|
|
141
|
+
else if (Array.isArray(value))
|
|
126
142
|
try {
|
|
127
|
-
|
|
143
|
+
for (var value_1 = (e_2 = void 0, __values(value)), value_1_1 = value_1.next(); !value_1_1.done; value_1_1 = value_1.next()) {
|
|
144
|
+
var v = value_1_1.value;
|
|
145
|
+
output.push([key, String(v)]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
149
|
+
finally {
|
|
150
|
+
try {
|
|
151
|
+
if (value_1_1 && !value_1_1.done && (_b = value_1.return)) _b.call(value_1);
|
|
152
|
+
}
|
|
153
|
+
finally { if (e_2) throw e_2.error; }
|
|
128
154
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
output.push([key, String(value)]);
|
|
155
|
+
else
|
|
156
|
+
output.push([key, String(value)]);
|
|
157
|
+
}
|
|
133
158
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
159
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
160
|
+
finally {
|
|
161
|
+
try {
|
|
162
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
163
|
+
}
|
|
164
|
+
finally { if (e_1) throw e_1.error; }
|
|
139
165
|
}
|
|
140
|
-
|
|
166
|
+
return output;
|
|
167
|
+
})() });
|
|
168
|
+
// CONSTRUCT BODY DATA
|
|
169
|
+
if (input !== undefined)
|
|
170
|
+
init.body = props.encode(
|
|
171
|
+
// BODY TRANSFORM
|
|
172
|
+
((_h = route.request) === null || _h === void 0 ? void 0 : _h.type) !== "text/plain"
|
|
173
|
+
? (stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input)
|
|
174
|
+
: input, headers);
|
|
175
|
+
path = connection.host[connection.host.length - 1] !== "/" &&
|
|
176
|
+
route.path[0] !== "/"
|
|
177
|
+
? "/".concat(route.path)
|
|
178
|
+
: route.path;
|
|
179
|
+
url = new URL("".concat(connection.host).concat(path));
|
|
180
|
+
return [4 /*yield*/, polyfill.get()];
|
|
181
|
+
case 1: return [4 /*yield*/, (_k.sent())(url.href, init)];
|
|
182
|
+
case 2:
|
|
183
|
+
response = _k.sent();
|
|
184
|
+
result = {
|
|
185
|
+
success: response.status === 200 ||
|
|
186
|
+
response.status === 201 ||
|
|
187
|
+
response.status == route.status,
|
|
188
|
+
status: route.status,
|
|
189
|
+
headers: response_headers_to_object(response.headers),
|
|
190
|
+
data: undefined,
|
|
191
|
+
};
|
|
192
|
+
if (!(result.success === false)) return [3 /*break*/, 4];
|
|
193
|
+
// WHEN FAILED
|
|
194
|
+
_a = result;
|
|
195
|
+
return [4 /*yield*/, response.text()];
|
|
196
|
+
case 3:
|
|
197
|
+
// WHEN FAILED
|
|
198
|
+
_a.data = _k.sent();
|
|
199
|
+
type = response.headers.get("content-type");
|
|
200
|
+
if (method !== "fetch" &&
|
|
201
|
+
type &&
|
|
202
|
+
type.indexOf("application/json") !== -1)
|
|
203
|
+
try {
|
|
204
|
+
result.data = JSON.parse(result.data);
|
|
141
205
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
?
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
response.status];
|
|
168
|
-
return [4 /*yield*/, response.text()];
|
|
169
|
-
case 3: throw new (_a.apply(HttpError_1.HttpError, _b.concat([_o.sent()])))();
|
|
170
|
-
case 4:
|
|
171
|
-
if (((_j = route.response) === null || _j === void 0 ? void 0 : _j.type) &&
|
|
172
|
-
((_l = ((_k = response.headers.get("content-type")) !== null && _k !== void 0 ? _k : route.response.type)) === null || _l === void 0 ? void 0 : _l.indexOf(route.response.type)) === -1)
|
|
173
|
-
throw new Error("Error on ".concat(props.className, ".fetch(): response type mismatched - expected ").concat(route.response.type, ", but ").concat(response.headers.get("content-type"), "."));
|
|
174
|
-
_o.label = 5;
|
|
175
|
-
case 5:
|
|
176
|
-
if (!(route.method === "HEAD")) return [3 /*break*/, 6];
|
|
177
|
-
return [2 /*return*/, undefined];
|
|
178
|
-
case 6:
|
|
179
|
-
if (!(((_m = route.response) === null || _m === void 0 ? void 0 : _m.type) === "application/json")) return [3 /*break*/, 8];
|
|
180
|
-
return [4 /*yield*/, response.text()];
|
|
181
|
-
case 7:
|
|
182
|
-
text = _o.sent();
|
|
183
|
-
return [2 /*return*/, text.length ? JSON.parse(text) : undefined];
|
|
184
|
-
case 8:
|
|
185
|
-
_d = (_c = props).decode;
|
|
186
|
-
return [4 /*yield*/, response.text()];
|
|
187
|
-
case 9: return [2 /*return*/, _d.apply(_c, [_o.sent(), response_headers_to_object(response.headers)])];
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
}); };
|
|
206
|
+
catch (_l) { }
|
|
207
|
+
return [3 /*break*/, 9];
|
|
208
|
+
case 4:
|
|
209
|
+
if (!(route.method === "HEAD")) return [3 /*break*/, 5];
|
|
210
|
+
result.data = undefined;
|
|
211
|
+
return [3 /*break*/, 9];
|
|
212
|
+
case 5:
|
|
213
|
+
if (!(((_j = route.response) === null || _j === void 0 ? void 0 : _j.type) === "application/json")) return [3 /*break*/, 7];
|
|
214
|
+
return [4 /*yield*/, response.text()];
|
|
215
|
+
case 6:
|
|
216
|
+
text = _k.sent();
|
|
217
|
+
result.data = text.length ? JSON.parse(text) : undefined;
|
|
218
|
+
return [3 /*break*/, 9];
|
|
219
|
+
case 7:
|
|
220
|
+
_b = result;
|
|
221
|
+
_d = (_c = props).decode;
|
|
222
|
+
return [4 /*yield*/, response.text()];
|
|
223
|
+
case 8:
|
|
224
|
+
_b.data = _d.apply(_c, [_k.sent(), result.headers]);
|
|
225
|
+
_k.label = 9;
|
|
226
|
+
case 9: return [2 /*return*/, result];
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}); };
|
|
230
|
+
};
|
|
191
231
|
};
|
|
192
232
|
})(FetcherBase || (exports.FetcherBase = FetcherBase = {}));
|
|
233
|
+
/**
|
|
234
|
+
* @internal
|
|
235
|
+
*/
|
|
193
236
|
var polyfill = new Singleton_1.Singleton(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
194
|
-
var _a;
|
|
195
|
-
|
|
196
|
-
|
|
237
|
+
var _a, _b;
|
|
238
|
+
var _c;
|
|
239
|
+
return __generator(this, function (_d) {
|
|
240
|
+
switch (_d.label) {
|
|
197
241
|
case 0:
|
|
198
242
|
if (!(typeof global === "object" &&
|
|
199
243
|
typeof global.process === "object" &&
|
|
200
244
|
typeof global.process.versions === "object" &&
|
|
201
|
-
typeof global.process.versions.node !== undefined)) return [3 /*break*/,
|
|
202
|
-
if (!(global.fetch === undefined)) return [3 /*break*/,
|
|
203
|
-
|
|
204
|
-
|
|
245
|
+
typeof global.process.versions.node !== undefined)) return [3 /*break*/, 5];
|
|
246
|
+
if (!(global.fetch === undefined)) return [3 /*break*/, 4];
|
|
247
|
+
if (!((_c = global.fetch) !== null && _c !== void 0)) return [3 /*break*/, 1];
|
|
248
|
+
_a = _c;
|
|
249
|
+
return [3 /*break*/, 3];
|
|
205
250
|
case 1:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
case 2:
|
|
209
|
-
|
|
251
|
+
_b = global;
|
|
252
|
+
return [4 /*yield*/, (0, import2_1.default)("node-fetch")];
|
|
253
|
+
case 2:
|
|
254
|
+
_a = (_b.fetch = (_d.sent()).default);
|
|
255
|
+
_d.label = 3;
|
|
256
|
+
case 3:
|
|
257
|
+
_a;
|
|
258
|
+
_d.label = 4;
|
|
259
|
+
case 4: return [2 /*return*/, global.fetch];
|
|
260
|
+
case 5: return [2 /*return*/, window.fetch];
|
|
210
261
|
}
|
|
211
262
|
});
|
|
212
263
|
}); });
|
|
264
|
+
/**
|
|
265
|
+
* @internal
|
|
266
|
+
*/
|
|
213
267
|
var response_headers_to_object = function (headers) {
|
|
214
268
|
var output = {};
|
|
215
269
|
headers.forEach(function (value, key) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetcherBase.js","sourceRoot":"","sources":["../../src/internal/FetcherBase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA8B;AAI9B,yCAAwC;AACxC,0CAAyC;
|
|
1
|
+
{"version":3,"file":"FetcherBase.js","sourceRoot":"","sources":["../../src/internal/FetcherBase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA8B;AAI9B,yCAAwC;AACxC,0CAAyC;AAGzC,IAAiB,WAAW,CAgK3B;AAhKD,WAAiB,WAAW;;IAaX,iBAAK,GACd,UAAC,KAAa;QACd,OAAA,UACI,UAAuB,EACvB,KAEC,EACD,KAAa,EACb,SAAoC;;;;4BAErB,qBAAM,UAAU,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAC3C,UAAU,EACV,KAAK,EACL,KAAK,EACL,SAAS,CACZ,EAAA;;wBALK,MAA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QACvB,IAAI,GAAG,KAAK,YAAY,EAAE;YACtB,MAAA,MAAM,CAAC,GAAG,qCAAV,MAAM,CAAC,GAAG,IAAM,EAAE,EAAC;YACnB,CAAA,KAAC,MAAM,CAAC,GAAG,CAAc,CAAA,CAAC,IAAI,oCACvB,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,UAAC,GAAG,IAAK,OAAA,GAAG,CAAC,IAAI,EAAE,EAAV,CAAU,CAAC,WAC9C;SACL;;YAAM,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC/B,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/EncryptedFetcher.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
2
|
import { IConnection } from "./IConnection";
|
|
3
3
|
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
+
import { IPropagation } from "./IPropagation";
|
|
4
5
|
import { Primitive } from "./Primitive";
|
|
5
6
|
import { FetcherBase } from "./internal/FetcherBase";
|
|
6
7
|
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
@@ -115,4 +116,77 @@ export namespace EncryptedFetcher {
|
|
|
115
116
|
: (input) => input,
|
|
116
117
|
})(connection, route, input, stringify);
|
|
117
118
|
}
|
|
119
|
+
|
|
120
|
+
export function propagate<Output extends IPropagation<any, any>>(
|
|
121
|
+
connection: IConnection,
|
|
122
|
+
route: IFetchRoute<"HEAD">,
|
|
123
|
+
): Promise<Output>;
|
|
124
|
+
|
|
125
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
126
|
+
connection: IConnection,
|
|
127
|
+
route: IFetchRoute<"HEAD">,
|
|
128
|
+
input?: Input,
|
|
129
|
+
stringify?: (input: Input) => string,
|
|
130
|
+
): Promise<Output>;
|
|
131
|
+
|
|
132
|
+
export async function propagate<
|
|
133
|
+
Input,
|
|
134
|
+
Output extends IPropagation<any, any>,
|
|
135
|
+
>(
|
|
136
|
+
connection: IConnection,
|
|
137
|
+
route: IFetchRoute<
|
|
138
|
+
"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"
|
|
139
|
+
>,
|
|
140
|
+
input?: Input,
|
|
141
|
+
stringify?: (input: Input) => string,
|
|
142
|
+
): Promise<Output> {
|
|
143
|
+
if (
|
|
144
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
145
|
+
connection.encryption === undefined
|
|
146
|
+
)
|
|
147
|
+
throw new Error(
|
|
148
|
+
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
149
|
+
);
|
|
150
|
+
const closure =
|
|
151
|
+
typeof connection.encryption === "function"
|
|
152
|
+
? (direction: "encode" | "decode") =>
|
|
153
|
+
(
|
|
154
|
+
headers: Record<
|
|
155
|
+
string,
|
|
156
|
+
IConnection.HeaderValue | undefined
|
|
157
|
+
>,
|
|
158
|
+
body: string,
|
|
159
|
+
) =>
|
|
160
|
+
(
|
|
161
|
+
connection.encryption as IEncryptionPassword.Closure
|
|
162
|
+
)({
|
|
163
|
+
headers,
|
|
164
|
+
body,
|
|
165
|
+
direction,
|
|
166
|
+
})
|
|
167
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
168
|
+
|
|
169
|
+
return FetcherBase.propagate({
|
|
170
|
+
className: "EncryptedFetcher",
|
|
171
|
+
encode:
|
|
172
|
+
route.request?.encrypted === true
|
|
173
|
+
? (input, headers) => {
|
|
174
|
+
const p = closure("encode")(headers, input);
|
|
175
|
+
return AesPkcs5.encrypt(
|
|
176
|
+
JSON.stringify(input),
|
|
177
|
+
p.key,
|
|
178
|
+
p.iv,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
: (input) => input,
|
|
182
|
+
decode:
|
|
183
|
+
route.response?.encrypted === true
|
|
184
|
+
? (input, headers) => {
|
|
185
|
+
const p = closure("decode")(headers, input);
|
|
186
|
+
const str = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
187
|
+
return str.length ? JSON.parse(str) : str;
|
|
188
|
+
}
|
|
189
|
+
: (input) => input,
|
|
190
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
191
|
+
}
|
|
118
192
|
}
|
package/src/HttpError.ts
CHANGED
|
@@ -29,6 +29,7 @@ export class HttpError extends Error {
|
|
|
29
29
|
| "HEAD",
|
|
30
30
|
public readonly path: string,
|
|
31
31
|
public readonly status: number,
|
|
32
|
+
public readonly headers: Record<string, string | string[]>,
|
|
32
33
|
message: string,
|
|
33
34
|
) {
|
|
34
35
|
super(message);
|
|
@@ -63,6 +64,7 @@ export class HttpError extends Error {
|
|
|
63
64
|
method: this.method,
|
|
64
65
|
path: this.path,
|
|
65
66
|
status: this.status,
|
|
67
|
+
headers: this.headers,
|
|
66
68
|
message: this.body_,
|
|
67
69
|
};
|
|
68
70
|
}
|
|
@@ -75,6 +77,7 @@ export namespace HttpError {
|
|
|
75
77
|
method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
|
|
76
78
|
path: string;
|
|
77
79
|
status: number;
|
|
80
|
+
headers: Record<string, string | string[]>;
|
|
78
81
|
message: T;
|
|
79
82
|
}
|
|
80
83
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Propagation type.
|
|
3
|
+
*
|
|
4
|
+
* @template StatusMap Map of status code and its body data type.
|
|
5
|
+
* @template Success Default success status code.
|
|
6
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
7
|
+
*/
|
|
8
|
+
export type IPropagation<
|
|
9
|
+
StatusMap extends {
|
|
10
|
+
[P in IPropagation.Status]?: any;
|
|
11
|
+
},
|
|
12
|
+
Success extends number = 200 | 201,
|
|
13
|
+
> =
|
|
14
|
+
| {
|
|
15
|
+
[P in keyof StatusMap]: IPropagation.IBranch<
|
|
16
|
+
P extends Success ? true : false,
|
|
17
|
+
P,
|
|
18
|
+
StatusMap[P]
|
|
19
|
+
>;
|
|
20
|
+
}[keyof StatusMap]
|
|
21
|
+
| IPropagation.IBranch<false, number, any>;
|
|
22
|
+
export namespace IPropagation {
|
|
23
|
+
/**
|
|
24
|
+
* Type of configurable status codes.
|
|
25
|
+
*
|
|
26
|
+
* The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
|
|
27
|
+
* of status codes. If `5XX` is specified, it means the status code is in the
|
|
28
|
+
* range of `500` to `599`.
|
|
29
|
+
*/
|
|
30
|
+
export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Branch type of propagation.
|
|
34
|
+
*
|
|
35
|
+
* `IPropagation.IBranch` is a branch type composing `IPropagation` type,
|
|
36
|
+
* which is gathering all possible status codes and their body data types
|
|
37
|
+
* as a union type.
|
|
38
|
+
*/
|
|
39
|
+
export interface IBranch<Success extends boolean, StatusValue, BodyData> {
|
|
40
|
+
success: Success;
|
|
41
|
+
status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
|
|
42
|
+
? StatusRange<StatusValue>
|
|
43
|
+
: StatusValue extends number
|
|
44
|
+
? StatusValue
|
|
45
|
+
: never;
|
|
46
|
+
data: BodyData;
|
|
47
|
+
headers: Record<string, string | string[]>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Range of status codes by the first digit.
|
|
52
|
+
*/
|
|
53
|
+
export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> =
|
|
54
|
+
T extends 0
|
|
55
|
+
? IntRange<200, 299>
|
|
56
|
+
: T extends 3
|
|
57
|
+
? IntRange<300, 399>
|
|
58
|
+
: T extends 4
|
|
59
|
+
? IntRange<400, 499>
|
|
60
|
+
: IntRange<500, 599>;
|
|
61
|
+
|
|
62
|
+
type IntRange<F extends number, T extends number> = Exclude<
|
|
63
|
+
Enumerate<T>,
|
|
64
|
+
Enumerate<F>
|
|
65
|
+
>;
|
|
66
|
+
|
|
67
|
+
type Enumerate<
|
|
68
|
+
N extends number,
|
|
69
|
+
Acc extends number[] = [],
|
|
70
|
+
> = Acc["length"] extends N
|
|
71
|
+
? Acc[number]
|
|
72
|
+
: Enumerate<N, [...Acc, Acc["length"]]>;
|
|
73
|
+
}
|
package/src/PlainFetcher.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Primitive } from "./Primitive";
|
|
|
3
3
|
|
|
4
4
|
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
5
5
|
import { FetcherBase } from "./internal/FetcherBase";
|
|
6
|
+
import { IPropagation } from "./IPropagation";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Utility class for `fetch` functions used in `@nestia/sdk`.
|
|
@@ -79,4 +80,41 @@ export namespace PlainFetcher {
|
|
|
79
80
|
decode: (input) => input,
|
|
80
81
|
})(connection, route, input, stringify);
|
|
81
82
|
}
|
|
83
|
+
|
|
84
|
+
export function propagate<Output extends IPropagation<any, any>>(
|
|
85
|
+
connection: IConnection,
|
|
86
|
+
route: IFetchRoute<"HEAD">,
|
|
87
|
+
): Promise<Output>;
|
|
88
|
+
|
|
89
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
90
|
+
connection: IConnection,
|
|
91
|
+
route: IFetchRoute<"HEAD">,
|
|
92
|
+
input?: Input,
|
|
93
|
+
stringify?: (input: Input) => string,
|
|
94
|
+
): Promise<Output>;
|
|
95
|
+
|
|
96
|
+
export async function propagate<
|
|
97
|
+
Input,
|
|
98
|
+
Output extends IPropagation<any, any>,
|
|
99
|
+
>(
|
|
100
|
+
connection: IConnection,
|
|
101
|
+
route: IFetchRoute<
|
|
102
|
+
"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"
|
|
103
|
+
>,
|
|
104
|
+
input?: Input,
|
|
105
|
+
stringify?: (input: Input) => string,
|
|
106
|
+
): Promise<Output> {
|
|
107
|
+
if (
|
|
108
|
+
route.request?.encrypted === true ||
|
|
109
|
+
route.response?.encrypted === true
|
|
110
|
+
)
|
|
111
|
+
throw new Error(
|
|
112
|
+
"Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
|
|
113
|
+
);
|
|
114
|
+
return FetcherBase.propagate({
|
|
115
|
+
className: "PlainFetcher",
|
|
116
|
+
encode: (input) => input,
|
|
117
|
+
decode: (input) => input,
|
|
118
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
119
|
+
}
|
|
82
120
|
}
|
package/src/Primitive.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Primitive type.
|
|
2
|
+
* Primitive type of JSON.
|
|
3
3
|
*
|
|
4
|
-
* `Primitive
|
|
5
|
-
* primitive type.
|
|
4
|
+
* `Primitive<T>` is a TMP (Type Meta Programming) type which converts
|
|
5
|
+
* its argument as a primitive type within framework JSON.
|
|
6
6
|
*
|
|
7
|
-
* If the target argument is a built-in class
|
|
8
|
-
* the `valueOf()` method like the `String` or `Number`, its return type would
|
|
9
|
-
* `string` or `number`.
|
|
7
|
+
* If the target argument is a built-in class which returns its origin primitive type
|
|
8
|
+
* through the `valueOf()` method like the `String` or `Number`, its return type would
|
|
9
|
+
* be the `string` or `number`. Otherwise, the built-in class does not have the
|
|
10
|
+
* `valueOf()` method, the return type would be an empty object (`{}`).
|
|
10
11
|
*
|
|
11
|
-
* Otherwise, the target argument is a type of custom class, all of its custom method
|
|
12
|
-
* be erased and its prototype would be changed to the primitive `object`.
|
|
13
|
-
* type of the TMP type finally be the primitive object.
|
|
12
|
+
* Otherwise, the target argument is a type of custom class, all of its custom method
|
|
13
|
+
* would be erased and its prototype would be changed to the primitive `object`.
|
|
14
|
+
* Therefore, return type of the TMP type finally be the primitive object.
|
|
14
15
|
*
|
|
15
16
|
* In addition, if the target argument is a type of custom class and it has a special
|
|
16
17
|
* method `toJSON()`, return type of this `Primitive` would be not `Primitive<Instance>`
|
|
@@ -23,10 +24,11 @@
|
|
|
23
24
|
* `String` | `string`
|
|
24
25
|
* `Class` | `object`
|
|
25
26
|
* `Class` with `toJSON()` | `Primitive<ReturnType<Class.toJSON>>`
|
|
27
|
+
* Native Class | `{}`
|
|
26
28
|
* Others | No change
|
|
27
29
|
*
|
|
28
30
|
* @template Instance Target argument type.
|
|
29
|
-
* @author
|
|
31
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
30
32
|
* @author Kyungsu Kang - https://github.com/kakasoo
|
|
31
33
|
* @author Michael - https://github.com/8471919
|
|
32
34
|
*/
|
|
@@ -38,8 +40,12 @@ type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
|
|
|
38
40
|
|
|
39
41
|
type PrimitiveMain<Instance> = Instance extends [never]
|
|
40
42
|
? never // (special trick for jsonable | null) type
|
|
41
|
-
: ValueOf<Instance> extends
|
|
43
|
+
: ValueOf<Instance> extends bigint
|
|
44
|
+
? never
|
|
45
|
+
: ValueOf<Instance> extends boolean | number | string
|
|
42
46
|
? ValueOf<Instance>
|
|
47
|
+
: Instance extends Function
|
|
48
|
+
? never
|
|
43
49
|
: ValueOf<Instance> extends object
|
|
44
50
|
? Instance extends object
|
|
45
51
|
? Instance extends NativeClass
|
|
@@ -59,9 +65,7 @@ type PrimitiveObject<Instance extends object> = Instance extends Array<infer T>
|
|
|
59
65
|
? PrimitiveTuple<Instance>
|
|
60
66
|
: PrimitiveMain<T>[]
|
|
61
67
|
: {
|
|
62
|
-
[P in keyof Instance]: Instance[P]
|
|
63
|
-
? never
|
|
64
|
-
: PrimitiveMain<Instance[P]>;
|
|
68
|
+
[P in keyof Instance]: PrimitiveMain<Instance[P]>;
|
|
65
69
|
};
|
|
66
70
|
|
|
67
71
|
type PrimitiveTuple<T extends readonly any[]> = T extends []
|
|
@@ -115,8 +119,8 @@ type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
|
|
|
115
119
|
: false;
|
|
116
120
|
|
|
117
121
|
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
118
|
-
? Object extends IValueOf<infer
|
|
119
|
-
? Instance extends
|
|
122
|
+
? Object extends IValueOf<infer U>
|
|
123
|
+
? Instance extends U
|
|
120
124
|
? false
|
|
121
125
|
: true // not Primitive, but Object
|
|
122
126
|
: false // cannot be
|