@nestia/fetcher 3.3.2 → 3.4.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/MigrateFetcher.d.ts +10 -0
- package/lib/MigrateFetcher.js +139 -0
- package/lib/MigrateFetcher.js.map +1 -0
- package/package.json +5 -2
- package/src/MigrateFetcher.ts +70 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IMigrateRoute } from "@samchon/openapi";
|
|
2
|
+
import { IConnection } from "./IConnection";
|
|
3
|
+
export declare namespace MigrateFetcher {
|
|
4
|
+
interface IProps {
|
|
5
|
+
route: IMigrateRoute;
|
|
6
|
+
connection: IConnection;
|
|
7
|
+
arguments: any[];
|
|
8
|
+
}
|
|
9
|
+
function request(props: IProps): Promise<any>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
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
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
+
if (!m) return o;
|
|
52
|
+
var i = m.call(o), r, ar = [], e;
|
|
53
|
+
try {
|
|
54
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
+
}
|
|
56
|
+
catch (error) { e = { error: error }; }
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
+
}
|
|
61
|
+
finally { if (e) throw e.error; }
|
|
62
|
+
}
|
|
63
|
+
return ar;
|
|
64
|
+
};
|
|
65
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
exports.MigrateFetcher = void 0;
|
|
67
|
+
var PlainFetcher_1 = require("./PlainFetcher");
|
|
68
|
+
var MigrateFetcher;
|
|
69
|
+
(function (MigrateFetcher) {
|
|
70
|
+
function request(props) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
+
var length;
|
|
73
|
+
var _a, _b, _c, _d;
|
|
74
|
+
return __generator(this, function (_e) {
|
|
75
|
+
length = props.route.parameters.length +
|
|
76
|
+
(props.route.query ? 1 : 0) +
|
|
77
|
+
(props.route.body ? 1 : 0);
|
|
78
|
+
if (props.arguments.length !== length)
|
|
79
|
+
throw new Error("Error on MigrateFetcher.request(): arguments length is not matched with the route (expected: ".concat(length, ", actual: ").concat(props.arguments.length, ")."));
|
|
80
|
+
else if (((_a = props.route.body) === null || _a === void 0 ? void 0 : _a["x-nestia-encrypted"]) === true ||
|
|
81
|
+
((_b = props.route.success) === null || _b === void 0 ? void 0 : _b["x-nestia-encrypted"]) === true)
|
|
82
|
+
throw new Error("Error on MigrateFetcher.request(): encrypted API is not supported yet.");
|
|
83
|
+
return [2 /*return*/, PlainFetcher_1.PlainFetcher.fetch(props.connection, {
|
|
84
|
+
method: props.route.method.toUpperCase(),
|
|
85
|
+
path: getPath(props),
|
|
86
|
+
template: props.route.path,
|
|
87
|
+
status: null,
|
|
88
|
+
request: props.route.body
|
|
89
|
+
? {
|
|
90
|
+
encrypted: false,
|
|
91
|
+
type: props.route.body.type,
|
|
92
|
+
}
|
|
93
|
+
: null,
|
|
94
|
+
response: {
|
|
95
|
+
encrypted: false,
|
|
96
|
+
type: (_d = (_c = props.route.success) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : "application/json",
|
|
97
|
+
},
|
|
98
|
+
}, props.route.body ? props.arguments.at(-1) : undefined)];
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
MigrateFetcher.request = request;
|
|
103
|
+
function getPath(props) {
|
|
104
|
+
var path = props.route.emendedPath;
|
|
105
|
+
props.route.parameters.forEach(function (p, i) {
|
|
106
|
+
path = path.replace(":".concat(p.key), props.arguments[i]);
|
|
107
|
+
});
|
|
108
|
+
if (props.route.query)
|
|
109
|
+
path += getQueryPath(props.arguments[props.route.parameters.length]);
|
|
110
|
+
return path;
|
|
111
|
+
}
|
|
112
|
+
function getQueryPath(query) {
|
|
113
|
+
var e_1, _a;
|
|
114
|
+
var variables = new URLSearchParams();
|
|
115
|
+
var _loop_1 = function (key, value) {
|
|
116
|
+
if (undefined === value)
|
|
117
|
+
return "continue";
|
|
118
|
+
else if (Array.isArray(value))
|
|
119
|
+
value.forEach(function (elem) { return variables.append(key, String(elem)); });
|
|
120
|
+
else
|
|
121
|
+
variables.set(key, String(value));
|
|
122
|
+
};
|
|
123
|
+
try {
|
|
124
|
+
for (var _b = __values(Object.entries(query)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
125
|
+
var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
|
|
126
|
+
_loop_1(key, value);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
130
|
+
finally {
|
|
131
|
+
try {
|
|
132
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
133
|
+
}
|
|
134
|
+
finally { if (e_1) throw e_1.error; }
|
|
135
|
+
}
|
|
136
|
+
return 0 === variables.size ? "" : "?".concat(variables.toString());
|
|
137
|
+
}
|
|
138
|
+
})(MigrateFetcher || (exports.MigrateFetcher = MigrateFetcher = {}));
|
|
139
|
+
//# sourceMappingURL=MigrateFetcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MigrateFetcher.js","sourceRoot":"","sources":["../src/MigrateFetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,+CAA8C;AAE9C,IAAiB,cAAc,CAgE9B;AAhED,WAAiB,cAAc;IAO7B,SAAsB,OAAO,CAAC,KAAa;;;;;gBACnC,MAAM,GACV,KAAK,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM;oBAC7B,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC3B,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBAC7B,IAAI,KAAK,CAAC,SAAS,CAAC,MAAM,KAAK,MAAM;oBACnC,MAAM,IAAI,KAAK,CACb,uGAAgG,MAAM,uBAAa,KAAK,CAAC,SAAS,CAAC,MAAM,OAAI,CAC9I,CAAC;qBACC,IACH,CAAA,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,0CAAG,oBAAoB,CAAC,MAAK,IAAI;oBACjD,CAAA,MAAA,KAAK,CAAC,KAAK,CAAC,OAAO,0CAAG,oBAAoB,CAAC,MAAK,IAAI;oBAEpD,MAAM,IA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AAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,SAAS,YAAY,CAAC,KAA0B;;QAC9C,IAAM,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;gCAC5B,GAAG,EAAE,KAAK;YACpB,IAAI,SAAS,KAAK,KAAK;;iBAClB,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBAC3B,KAAK,CAAC,OAAO,CAAC,UAAC,IAAS,IAAK,OAAA,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,EAAnC,CAAmC,CAAC,CAAC;;gBAC/D,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;;;YAJzC,KAA2B,IAAA,KAAA,SAAA,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA,gBAAA;gBAArC,IAAA,KAAA,mBAAY,EAAX,GAAG,QAAA,EAAE,KAAK,QAAA;wBAAV,GAAG,EAAE,KAAK;aAImB;;;;;;;;;QACzC,OAAO,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAI,SAAS,CAAC,QAAQ,EAAE,CAAE,CAAC;IAChE,CAAC;AACH,CAAC,EAhEgB,cAAc,8BAAd,cAAc,QAgE9B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestia/fetcher",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Fetcher library of Nestia SDK",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -41,5 +41,8 @@
|
|
|
41
41
|
"package.json",
|
|
42
42
|
"lib",
|
|
43
43
|
"src"
|
|
44
|
-
]
|
|
44
|
+
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@samchon/openapi": "^0.2.2"
|
|
47
|
+
}
|
|
45
48
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { IMigrateRoute } from "@samchon/openapi";
|
|
2
|
+
|
|
3
|
+
import { IConnection } from "./IConnection";
|
|
4
|
+
import { PlainFetcher } from "./PlainFetcher";
|
|
5
|
+
|
|
6
|
+
export namespace MigrateFetcher {
|
|
7
|
+
export interface IProps {
|
|
8
|
+
route: IMigrateRoute;
|
|
9
|
+
connection: IConnection;
|
|
10
|
+
arguments: any[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function request(props: IProps): Promise<any> {
|
|
14
|
+
const length: number =
|
|
15
|
+
props.route.parameters.length +
|
|
16
|
+
(props.route.query ? 1 : 0) +
|
|
17
|
+
(props.route.body ? 1 : 0);
|
|
18
|
+
if (props.arguments.length !== length)
|
|
19
|
+
throw new Error(
|
|
20
|
+
`Error on MigrateFetcher.request(): arguments length is not matched with the route (expected: ${length}, actual: ${props.arguments.length}).`,
|
|
21
|
+
);
|
|
22
|
+
else if (
|
|
23
|
+
props.route.body?.["x-nestia-encrypted"] === true ||
|
|
24
|
+
props.route.success?.["x-nestia-encrypted"] === true
|
|
25
|
+
)
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Error on MigrateFetcher.request(): encrypted API is not supported yet.`,
|
|
28
|
+
);
|
|
29
|
+
return PlainFetcher.fetch(
|
|
30
|
+
props.connection,
|
|
31
|
+
{
|
|
32
|
+
method: props.route.method.toUpperCase() as "POST",
|
|
33
|
+
path: getPath(props),
|
|
34
|
+
template: props.route.path,
|
|
35
|
+
status: null,
|
|
36
|
+
request: props.route.body
|
|
37
|
+
? {
|
|
38
|
+
encrypted: false,
|
|
39
|
+
type: props.route.body.type,
|
|
40
|
+
}
|
|
41
|
+
: null,
|
|
42
|
+
response: {
|
|
43
|
+
encrypted: false,
|
|
44
|
+
type: props.route.success?.type ?? "application/json",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
props.route.body ? props.arguments.at(-1) : undefined,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getPath(props: Pick<IProps, "arguments" | "route">): string {
|
|
52
|
+
let path: string = props.route.emendedPath;
|
|
53
|
+
props.route.parameters.forEach((p, i) => {
|
|
54
|
+
path = path.replace(`:${p.key}`, props.arguments[i]);
|
|
55
|
+
});
|
|
56
|
+
if (props.route.query)
|
|
57
|
+
path += getQueryPath(props.arguments[props.route.parameters.length]);
|
|
58
|
+
return path;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getQueryPath(query: Record<string, any>): string {
|
|
62
|
+
const variables = new URLSearchParams();
|
|
63
|
+
for (const [key, value] of Object.entries(query))
|
|
64
|
+
if (undefined === value) continue;
|
|
65
|
+
else if (Array.isArray(value))
|
|
66
|
+
value.forEach((elem: any) => variables.append(key, String(elem)));
|
|
67
|
+
else variables.set(key, String(value));
|
|
68
|
+
return 0 === variables.size ? "" : `?${variables.toString()}`;
|
|
69
|
+
}
|
|
70
|
+
}
|