@nestia/fetcher 2.0.0-dev.20230991 → 2.0.0
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 +71 -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 +2 -2
- package/src/EncryptedFetcher.ts +76 -0
- package/src/HttpError.ts +3 -0
- package/src/IPropagation.ts +103 -0
- package/src/PlainFetcher.ts +40 -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: response.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,MAAM,GAAG,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AAI,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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestia/fetcher",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Fetcher library of Nestia SDK",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@typescript-eslint/parser": "^5.46.1",
|
|
33
33
|
"prettier": "^2.8.1",
|
|
34
34
|
"rimraf": "^3.0.2",
|
|
35
|
-
"typescript": "^5.
|
|
35
|
+
"typescript": "^5.2.2"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"typescript": ">= 4.8.0"
|
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,79 @@ 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<"GET" | "HEAD">,
|
|
123
|
+
): Promise<Output>;
|
|
124
|
+
|
|
125
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
126
|
+
connection: IConnection,
|
|
127
|
+
route: IFetchRoute<
|
|
128
|
+
"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"
|
|
129
|
+
>,
|
|
130
|
+
input?: Input,
|
|
131
|
+
stringify?: (input: Input) => string,
|
|
132
|
+
): Promise<Output>;
|
|
133
|
+
|
|
134
|
+
export async function propagate<
|
|
135
|
+
Input,
|
|
136
|
+
Output extends IPropagation<any, any>,
|
|
137
|
+
>(
|
|
138
|
+
connection: IConnection,
|
|
139
|
+
route: IFetchRoute<
|
|
140
|
+
"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"
|
|
141
|
+
>,
|
|
142
|
+
input?: Input,
|
|
143
|
+
stringify?: (input: Input) => string,
|
|
144
|
+
): Promise<Output> {
|
|
145
|
+
if (
|
|
146
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
147
|
+
connection.encryption === undefined
|
|
148
|
+
)
|
|
149
|
+
throw new Error(
|
|
150
|
+
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
151
|
+
);
|
|
152
|
+
const closure =
|
|
153
|
+
typeof connection.encryption === "function"
|
|
154
|
+
? (direction: "encode" | "decode") =>
|
|
155
|
+
(
|
|
156
|
+
headers: Record<
|
|
157
|
+
string,
|
|
158
|
+
IConnection.HeaderValue | undefined
|
|
159
|
+
>,
|
|
160
|
+
body: string,
|
|
161
|
+
) =>
|
|
162
|
+
(
|
|
163
|
+
connection.encryption as IEncryptionPassword.Closure
|
|
164
|
+
)({
|
|
165
|
+
headers,
|
|
166
|
+
body,
|
|
167
|
+
direction,
|
|
168
|
+
})
|
|
169
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
170
|
+
|
|
171
|
+
return FetcherBase.propagate({
|
|
172
|
+
className: "EncryptedFetcher",
|
|
173
|
+
encode:
|
|
174
|
+
route.request?.encrypted === true
|
|
175
|
+
? (input, headers) => {
|
|
176
|
+
const p = closure("encode")(headers, input);
|
|
177
|
+
return AesPkcs5.encrypt(
|
|
178
|
+
JSON.stringify(input),
|
|
179
|
+
p.key,
|
|
180
|
+
p.iv,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
: (input) => input,
|
|
184
|
+
decode:
|
|
185
|
+
route.response?.encrypted === true
|
|
186
|
+
? (input, headers) => {
|
|
187
|
+
const p = closure("decode")(headers, input);
|
|
188
|
+
const str = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
189
|
+
return str.length ? JSON.parse(str) : str;
|
|
190
|
+
}
|
|
191
|
+
: (input) => input,
|
|
192
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
193
|
+
}
|
|
118
194
|
}
|
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,103 @@
|
|
|
1
|
+
import { Primitive } from "./Primitive";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Propagation type.
|
|
5
|
+
*
|
|
6
|
+
* `IPropagation` is a type gathering all possible status codes and their body
|
|
7
|
+
* data types as a discriminated union type. You can specify the status code and
|
|
8
|
+
* its body data type just by using conditional statement like below.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* type Output = IPropagation<{
|
|
12
|
+
* 200: ISeller.IAuthorized;
|
|
13
|
+
* 400: TypeGuardError.IProps;
|
|
14
|
+
* >};
|
|
15
|
+
*
|
|
16
|
+
* const output: Output = await sdk.sellers.authenticate.join(input);
|
|
17
|
+
* if (output.success) {
|
|
18
|
+
* // automatically casted to "ISeller.IAuthorized" type
|
|
19
|
+
* const authorized: ISeller.IAuthorized = output.data;
|
|
20
|
+
* } else if (output.status === 400) {
|
|
21
|
+
* // automatically casted to "TypeGuardError.IProps" type
|
|
22
|
+
* const error: TypeGuardError.IProps = output.data;
|
|
23
|
+
* } else {
|
|
24
|
+
* // unknown type when out of pre-defined status codes
|
|
25
|
+
* const result: unknown = output.data;
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* For reference, this `IPropagation` type is utilized by SDK library generated by
|
|
30
|
+
* `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be `true`.
|
|
31
|
+
* In that case, SDK functions generated by `@nestia/sdk` no more returns response DTO
|
|
32
|
+
* typed data directly, but returns this `IPropagation` typed object instead.
|
|
33
|
+
*
|
|
34
|
+
* @template StatusMap Map of status code and its body data type.
|
|
35
|
+
* @template Success Default success status code.
|
|
36
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
37
|
+
*/
|
|
38
|
+
export type IPropagation<
|
|
39
|
+
StatusMap extends {
|
|
40
|
+
[P in IPropagation.Status]?: any;
|
|
41
|
+
},
|
|
42
|
+
Success extends number = 200 | 201,
|
|
43
|
+
> =
|
|
44
|
+
| {
|
|
45
|
+
[P in keyof StatusMap]: IPropagation.IBranch<
|
|
46
|
+
P extends Success ? true : false,
|
|
47
|
+
P,
|
|
48
|
+
StatusMap[P]
|
|
49
|
+
>;
|
|
50
|
+
}[keyof StatusMap]
|
|
51
|
+
| IPropagation.IBranch<false, unknown, unknown>;
|
|
52
|
+
export namespace IPropagation {
|
|
53
|
+
/**
|
|
54
|
+
* Type of configurable status codes.
|
|
55
|
+
*
|
|
56
|
+
* The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
|
|
57
|
+
* of status codes. If `5XX` is specified, it means the status code is in the
|
|
58
|
+
* range of `500` to `599`.
|
|
59
|
+
*/
|
|
60
|
+
export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Branch type of propagation.
|
|
64
|
+
*
|
|
65
|
+
* `IPropagation.IBranch` is a branch type composing `IPropagation` type,
|
|
66
|
+
* which is gathering all possible status codes and their body data types
|
|
67
|
+
* as a union type.
|
|
68
|
+
*/
|
|
69
|
+
export interface IBranch<Success extends boolean, StatusValue, BodyData> {
|
|
70
|
+
success: Success;
|
|
71
|
+
status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
|
|
72
|
+
? StatusRange<StatusValue>
|
|
73
|
+
: StatusValue extends number
|
|
74
|
+
? StatusValue
|
|
75
|
+
: never;
|
|
76
|
+
data: Primitive<BodyData>;
|
|
77
|
+
headers: Record<string, string | string[]>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Range of status codes by the first digit.
|
|
82
|
+
*/
|
|
83
|
+
export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> =
|
|
84
|
+
T extends 0
|
|
85
|
+
? IntRange<200, 299>
|
|
86
|
+
: T extends 3
|
|
87
|
+
? IntRange<300, 399>
|
|
88
|
+
: T extends 4
|
|
89
|
+
? IntRange<400, 499>
|
|
90
|
+
: IntRange<500, 599>;
|
|
91
|
+
|
|
92
|
+
type IntRange<F extends number, T extends number> = Exclude<
|
|
93
|
+
Enumerate<T>,
|
|
94
|
+
Enumerate<F>
|
|
95
|
+
>;
|
|
96
|
+
|
|
97
|
+
type Enumerate<
|
|
98
|
+
N extends number,
|
|
99
|
+
Acc extends number[] = [],
|
|
100
|
+
> = Acc["length"] extends N
|
|
101
|
+
? Acc[number]
|
|
102
|
+
: Enumerate<N, [...Acc, Acc["length"]]>;
|
|
103
|
+
}
|
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,43 @@ 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<"GET" | "HEAD">,
|
|
87
|
+
): Promise<Output>;
|
|
88
|
+
|
|
89
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
90
|
+
connection: IConnection,
|
|
91
|
+
route: IFetchRoute<
|
|
92
|
+
"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"
|
|
93
|
+
>,
|
|
94
|
+
input?: Input,
|
|
95
|
+
stringify?: (input: Input) => string,
|
|
96
|
+
): Promise<Output>;
|
|
97
|
+
|
|
98
|
+
export async function propagate<
|
|
99
|
+
Input,
|
|
100
|
+
Output extends IPropagation<any, any>,
|
|
101
|
+
>(
|
|
102
|
+
connection: IConnection,
|
|
103
|
+
route: IFetchRoute<
|
|
104
|
+
"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"
|
|
105
|
+
>,
|
|
106
|
+
input?: Input,
|
|
107
|
+
stringify?: (input: Input) => string,
|
|
108
|
+
): Promise<Output> {
|
|
109
|
+
if (
|
|
110
|
+
route.request?.encrypted === true ||
|
|
111
|
+
route.response?.encrypted === true
|
|
112
|
+
)
|
|
113
|
+
throw new Error(
|
|
114
|
+
"Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
|
|
115
|
+
);
|
|
116
|
+
return FetcherBase.propagate({
|
|
117
|
+
className: "PlainFetcher",
|
|
118
|
+
encode: (input) => input,
|
|
119
|
+
decode: (input) => input,
|
|
120
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
121
|
+
}
|
|
82
122
|
}
|