exnet-routing 1.1.7 → 1.1.9
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/dist/core/_untils.js +63 -66
- package/package.json +1 -1
package/dist/core/_untils.js
CHANGED
|
@@ -27,29 +27,27 @@ exports.apiFetch = {
|
|
|
27
27
|
fetch: async (urlInput, method, body, searchParams, queryParams) => {
|
|
28
28
|
try {
|
|
29
29
|
let url = (0, exports.apiUrlBuilder)(urlInput, baseUrl, host);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
url = url.replace(placeholder, String(value));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
});
|
|
30
|
+
if (queryParams) {
|
|
31
|
+
const queryString = Object.entries(queryParams)
|
|
32
|
+
.filter(([_, value]) => value !== undefined && value !== null)
|
|
33
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
|
34
|
+
.join('&');
|
|
35
|
+
url += (url.includes('?') ? '&' : '?') + queryString;
|
|
40
36
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
37
|
+
if (searchParams) {
|
|
38
|
+
const searchParamsString = typeof searchParams === "string"
|
|
39
|
+
? searchParams
|
|
40
|
+
: Object.entries(searchParams)
|
|
41
|
+
.filter(([_, value]) => value !== undefined && value !== null)
|
|
42
|
+
.map(([key, value]) => {
|
|
43
|
+
const encodedKey = encodeURIComponent(key);
|
|
44
|
+
const encodedValue = Array.isArray(value) || typeof value === "object"
|
|
45
|
+
? encodeURIComponent(JSON.stringify(value))
|
|
46
|
+
: encodeURIComponent(String(value !== null && value !== void 0 ? value : ""));
|
|
47
|
+
return `${encodedKey}=${encodedValue}`;
|
|
48
|
+
})
|
|
49
|
+
.join("&");
|
|
50
|
+
url += (url.includes('?') ? '&' : '?') + searchParamsString;
|
|
53
51
|
}
|
|
54
52
|
const res = await (0, axios_1.default)({
|
|
55
53
|
headers: {
|
|
@@ -81,59 +79,58 @@ const isSubRoute = (value) => {
|
|
|
81
79
|
return !(0, exports.isRoute)(value);
|
|
82
80
|
};
|
|
83
81
|
exports.isSubRoute = isSubRoute;
|
|
82
|
+
const createRouteFunction = (route, apiFetch) => {
|
|
83
|
+
const createFetchFunction = (paramTypes) => {
|
|
84
|
+
return async (...args) => {
|
|
85
|
+
const params = paramTypes.reduce((acc, type, index) => {
|
|
86
|
+
if (args[index] && typeof args[index] === 'object') {
|
|
87
|
+
if ('queryParams' in args[index]) {
|
|
88
|
+
acc.queryParams = args[index].queryParams;
|
|
89
|
+
}
|
|
90
|
+
if ('searchParams' in args[index]) {
|
|
91
|
+
acc.searchParams = args[index].searchParams;
|
|
92
|
+
}
|
|
93
|
+
if ('body' in args[index]) {
|
|
94
|
+
acc.body = args[index].body;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
acc[type] = args[index] || null;
|
|
99
|
+
}
|
|
100
|
+
return acc;
|
|
101
|
+
}, { body: null, searchParams: null, queryParams: null });
|
|
102
|
+
return await apiFetch.fetch(route.url, route.method, params.body, params.searchParams, params.queryParams);
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
if (route.body) {
|
|
106
|
+
if (route.searchParams) {
|
|
107
|
+
return route.params
|
|
108
|
+
? createFetchFunction(['body', 'searchParams', 'queryParams'])
|
|
109
|
+
: createFetchFunction(['body', 'searchParams']);
|
|
110
|
+
}
|
|
111
|
+
return route.params
|
|
112
|
+
? createFetchFunction(['body', 'queryParams'])
|
|
113
|
+
: createFetchFunction(['body']);
|
|
114
|
+
}
|
|
115
|
+
if (route.searchParams) {
|
|
116
|
+
return route.params
|
|
117
|
+
? createFetchFunction(['searchParams', 'queryParams'])
|
|
118
|
+
: createFetchFunction(['searchParams']);
|
|
119
|
+
}
|
|
120
|
+
return route.params
|
|
121
|
+
? createFetchFunction(['queryParams'])
|
|
122
|
+
: createFetchFunction([]);
|
|
123
|
+
};
|
|
84
124
|
const parseApiRoute = (key, subRoute, result, hostApi, basePath, headers) => {
|
|
85
125
|
var _a;
|
|
86
126
|
exports.apiFetch.setHostApi(hostApi);
|
|
87
127
|
exports.apiFetch.setBaseUrl(basePath);
|
|
88
128
|
exports.apiFetch.setHeaders(headers);
|
|
89
129
|
if ((0, exports.isRoute)(subRoute) && key) {
|
|
90
|
-
|
|
91
|
-
if (route.body) {
|
|
92
|
-
if (route.searchParams) {
|
|
93
|
-
if (route.params) {
|
|
94
|
-
;
|
|
95
|
-
result = async (body, searchParams, queryParams) => await exports.apiFetch.fetch(route.url, route.method, body, searchParams, queryParams);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
;
|
|
99
|
-
result = async (body, searchParams) => await exports.apiFetch.fetch(route.url, route.method, body, searchParams);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
if (route.params) {
|
|
104
|
-
;
|
|
105
|
-
result = async (body, queryParams) => await exports.apiFetch.fetch(route.url, route.method, body, undefined, queryParams);
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
;
|
|
109
|
-
result = async (body) => await exports.apiFetch.fetch(route.url, route.method, body);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else if (route.searchParams) {
|
|
114
|
-
if (route.params) {
|
|
115
|
-
;
|
|
116
|
-
result = async (searchParams, queryParams) => await exports.apiFetch.fetch(route.url, route.method, undefined, searchParams, queryParams);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
;
|
|
120
|
-
result = async (searchParams) => await exports.apiFetch.fetch(route.url, route.method, undefined, searchParams);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
if (route.params) {
|
|
125
|
-
;
|
|
126
|
-
result = async (queryParams) => await exports.apiFetch.fetch(route.url, route.method, undefined, undefined, queryParams);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
;
|
|
130
|
-
result = async () => await exports.apiFetch.fetch(route.url, route.method);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
130
|
+
result = createRouteFunction(subRoute, exports.apiFetch);
|
|
133
131
|
}
|
|
134
132
|
else if ((0, exports.isSubRoute)(subRoute)) {
|
|
135
133
|
for (const [key, value] of Object.entries(subRoute)) {
|
|
136
|
-
;
|
|
137
134
|
result[key] = (0, exports.parseApiRoute)(key, value, ((_a = result[key]) !== null && _a !== void 0 ? _a : {}), hostApi, basePath, headers);
|
|
138
135
|
}
|
|
139
136
|
}
|