@snail-js/api 0.1.14 → 0.1.15
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/README.md +223 -73
- package/README_EN.md +592 -0
- package/dist/cache/index.d.ts +6 -2
- package/dist/cache/indexDBCache.d.ts +7 -22
- package/dist/cache/localstorageCache.d.ts +7 -22
- package/dist/cache/memoryCache.d.ts +8 -23
- package/dist/core/index.d.ts +4 -1
- package/dist/core/snailApi.d.ts +22 -0
- package/dist/core/snailMethod.d.ts +54 -0
- package/dist/core/snailServer.d.ts +34 -0
- package/dist/core/snailSse.d.ts +20 -0
- package/dist/decorators/api.d.ts +9 -9
- package/dist/decorators/{param.d.ts → args.d.ts} +6 -0
- package/dist/decorators/cache.d.ts +11 -6
- package/dist/decorators/sse.d.ts +2 -3
- package/dist/decorators/strategy.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/snail-api.js +1125 -531
- package/dist/snail-api.umd.cjs +1126 -532
- package/dist/typings/api.option.d.ts +16 -0
- package/dist/typings/apiProxy.d.ts +5 -4
- package/dist/typings/cache.management.option.d.ts +16 -10
- package/dist/typings/cache.type.d.ts +17 -2
- package/dist/typings/index.d.ts +2 -1
- package/dist/typings/request.method.d.ts +9 -8
- package/dist/typings/response.data.d.ts +7 -3
- package/dist/typings/snail.method.d.ts +11 -0
- package/dist/typings/snail.option.d.ts +5 -1
- package/dist/typings/sse.d.ts +5 -1
- package/dist/typings/versioning.option.d.ts +2 -2
- package/dist/utils/function.d.ts +29 -8
- package/dist/versioning/index.d.ts +1 -0
- package/dist/versioning/versioning.d.ts +10 -5
- package/package.json +2 -1
- package/dist/core/snail.d.ts +0 -32
- package/dist/typings/api.config.d.ts +0 -9
package/dist/snail-api.umd.cjs
CHANGED
|
@@ -1465,7 +1465,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1465
1465
|
setImmediate: _setImmediate,
|
|
1466
1466
|
asap
|
|
1467
1467
|
};
|
|
1468
|
-
function AxiosError
|
|
1468
|
+
function AxiosError(message, code, config, request, response) {
|
|
1469
1469
|
Error.call(this);
|
|
1470
1470
|
if (Error.captureStackTrace) {
|
|
1471
1471
|
Error.captureStackTrace(this, this.constructor);
|
|
@@ -1482,7 +1482,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1482
1482
|
this.status = response.status ? response.status : null;
|
|
1483
1483
|
}
|
|
1484
1484
|
}
|
|
1485
|
-
utils$1.inherits(AxiosError
|
|
1485
|
+
utils$1.inherits(AxiosError, Error, {
|
|
1486
1486
|
toJSON: function toJSON() {
|
|
1487
1487
|
return {
|
|
1488
1488
|
// Standard
|
|
@@ -1503,7 +1503,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1503
1503
|
};
|
|
1504
1504
|
}
|
|
1505
1505
|
});
|
|
1506
|
-
const prototype$1 = AxiosError
|
|
1506
|
+
const prototype$1 = AxiosError.prototype;
|
|
1507
1507
|
const descriptors = {};
|
|
1508
1508
|
[
|
|
1509
1509
|
"ERR_BAD_OPTION_VALUE",
|
|
@@ -1522,16 +1522,16 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1522
1522
|
].forEach((code) => {
|
|
1523
1523
|
descriptors[code] = { value: code };
|
|
1524
1524
|
});
|
|
1525
|
-
Object.defineProperties(AxiosError
|
|
1525
|
+
Object.defineProperties(AxiosError, descriptors);
|
|
1526
1526
|
Object.defineProperty(prototype$1, "isAxiosError", { value: true });
|
|
1527
|
-
AxiosError
|
|
1527
|
+
AxiosError.from = (error, code, config, request, response, customProps) => {
|
|
1528
1528
|
const axiosError = Object.create(prototype$1);
|
|
1529
1529
|
utils$1.toFlatObject(error, axiosError, function filter(obj) {
|
|
1530
1530
|
return obj !== Error.prototype;
|
|
1531
1531
|
}, (prop) => {
|
|
1532
1532
|
return prop !== "isAxiosError";
|
|
1533
1533
|
});
|
|
1534
|
-
AxiosError
|
|
1534
|
+
AxiosError.call(axiosError, error.message, code, config, request, response);
|
|
1535
1535
|
axiosError.cause = error;
|
|
1536
1536
|
axiosError.name = error.name;
|
|
1537
1537
|
customProps && Object.assign(axiosError, customProps);
|
|
@@ -1557,7 +1557,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1557
1557
|
const predicates = utils$1.toFlatObject(utils$1, {}, null, function filter(prop) {
|
|
1558
1558
|
return /^is[A-Z]/.test(prop);
|
|
1559
1559
|
});
|
|
1560
|
-
function toFormData
|
|
1560
|
+
function toFormData(obj, formData, options) {
|
|
1561
1561
|
if (!utils$1.isObject(obj)) {
|
|
1562
1562
|
throw new TypeError("target must be an object");
|
|
1563
1563
|
}
|
|
@@ -1584,7 +1584,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1584
1584
|
return value.toISOString();
|
|
1585
1585
|
}
|
|
1586
1586
|
if (!useBlob && utils$1.isBlob(value)) {
|
|
1587
|
-
throw new AxiosError
|
|
1587
|
+
throw new AxiosError("Blob is not supported. Use a Buffer instead.");
|
|
1588
1588
|
}
|
|
1589
1589
|
if (utils$1.isArrayBuffer(value) || utils$1.isTypedArray(value)) {
|
|
1590
1590
|
return useBlob && typeof Blob === "function" ? new Blob([value]) : Buffer.from(value);
|
|
@@ -1663,7 +1663,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1663
1663
|
}
|
|
1664
1664
|
function AxiosURLSearchParams(params, options) {
|
|
1665
1665
|
this._pairs = [];
|
|
1666
|
-
params && toFormData
|
|
1666
|
+
params && toFormData(params, this, options);
|
|
1667
1667
|
}
|
|
1668
1668
|
const prototype = AxiosURLSearchParams.prototype;
|
|
1669
1669
|
prototype.append = function append(name, value) {
|
|
@@ -1805,7 +1805,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1805
1805
|
...platform$1
|
|
1806
1806
|
};
|
|
1807
1807
|
function toURLEncodedForm(data, options) {
|
|
1808
|
-
return toFormData
|
|
1808
|
+
return toFormData(data, new platform.classes.URLSearchParams(), Object.assign({
|
|
1809
1809
|
visitor: function(value, key, path, helpers) {
|
|
1810
1810
|
if (platform.isNode && utils$1.isBuffer(value)) {
|
|
1811
1811
|
this.append(key, value.toString("base64"));
|
|
@@ -1909,7 +1909,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1909
1909
|
}
|
|
1910
1910
|
if ((isFileList2 = utils$1.isFileList(data)) || contentType.indexOf("multipart/form-data") > -1) {
|
|
1911
1911
|
const _FormData = this.env && this.env.FormData;
|
|
1912
|
-
return toFormData
|
|
1912
|
+
return toFormData(
|
|
1913
1913
|
isFileList2 ? { "files[]": data } : data,
|
|
1914
1914
|
_FormData && new _FormData(),
|
|
1915
1915
|
this.formSerializer
|
|
@@ -1937,7 +1937,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1937
1937
|
} catch (e) {
|
|
1938
1938
|
if (strictJSONParsing) {
|
|
1939
1939
|
if (e.name === "SyntaxError") {
|
|
1940
|
-
throw AxiosError
|
|
1940
|
+
throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response);
|
|
1941
1941
|
}
|
|
1942
1942
|
throw e;
|
|
1943
1943
|
}
|
|
@@ -2065,7 +2065,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2065
2065
|
});
|
|
2066
2066
|
});
|
|
2067
2067
|
}
|
|
2068
|
-
|
|
2068
|
+
class AxiosHeaders {
|
|
2069
2069
|
constructor(headers) {
|
|
2070
2070
|
headers && this.set(headers);
|
|
2071
2071
|
}
|
|
@@ -2220,9 +2220,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2220
2220
|
utils$1.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
|
|
2221
2221
|
return this;
|
|
2222
2222
|
}
|
|
2223
|
-
}
|
|
2224
|
-
AxiosHeaders
|
|
2225
|
-
utils$1.reduceDescriptors(AxiosHeaders
|
|
2223
|
+
}
|
|
2224
|
+
AxiosHeaders.accessor(["Content-Type", "Content-Length", "Accept", "Accept-Encoding", "User-Agent", "Authorization"]);
|
|
2225
|
+
utils$1.reduceDescriptors(AxiosHeaders.prototype, ({ value }, key) => {
|
|
2226
2226
|
let mapped = key[0].toUpperCase() + key.slice(1);
|
|
2227
2227
|
return {
|
|
2228
2228
|
get: () => value,
|
|
@@ -2231,11 +2231,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2231
2231
|
}
|
|
2232
2232
|
};
|
|
2233
2233
|
});
|
|
2234
|
-
utils$1.freezeMethods(AxiosHeaders
|
|
2234
|
+
utils$1.freezeMethods(AxiosHeaders);
|
|
2235
2235
|
function transformData(fns, response) {
|
|
2236
2236
|
const config = this || defaults;
|
|
2237
2237
|
const context = response || config;
|
|
2238
|
-
const headers = AxiosHeaders
|
|
2238
|
+
const headers = AxiosHeaders.from(context.headers);
|
|
2239
2239
|
let data = context.data;
|
|
2240
2240
|
utils$1.forEach(fns, function transform(fn) {
|
|
2241
2241
|
data = fn.call(config, data, headers.normalize(), response ? response.status : void 0);
|
|
@@ -2243,14 +2243,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2243
2243
|
headers.normalize();
|
|
2244
2244
|
return data;
|
|
2245
2245
|
}
|
|
2246
|
-
function isCancel
|
|
2246
|
+
function isCancel(value) {
|
|
2247
2247
|
return !!(value && value.__CANCEL__);
|
|
2248
2248
|
}
|
|
2249
|
-
function CanceledError
|
|
2250
|
-
AxiosError
|
|
2249
|
+
function CanceledError(message, config, request) {
|
|
2250
|
+
AxiosError.call(this, message == null ? "canceled" : message, AxiosError.ERR_CANCELED, config, request);
|
|
2251
2251
|
this.name = "CanceledError";
|
|
2252
2252
|
}
|
|
2253
|
-
utils$1.inherits(CanceledError
|
|
2253
|
+
utils$1.inherits(CanceledError, AxiosError, {
|
|
2254
2254
|
__CANCEL__: true
|
|
2255
2255
|
});
|
|
2256
2256
|
function settle(resolve, reject, response) {
|
|
@@ -2258,9 +2258,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2258
2258
|
if (!response.status || !validateStatus || validateStatus(response.status)) {
|
|
2259
2259
|
resolve(response);
|
|
2260
2260
|
} else {
|
|
2261
|
-
reject(new AxiosError
|
|
2261
|
+
reject(new AxiosError(
|
|
2262
2262
|
"Request failed with status code " + response.status,
|
|
2263
|
-
[AxiosError
|
|
2263
|
+
[AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],
|
|
2264
2264
|
response.config,
|
|
2265
2265
|
response.request,
|
|
2266
2266
|
response
|
|
@@ -2419,8 +2419,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2419
2419
|
}
|
|
2420
2420
|
return requestedURL;
|
|
2421
2421
|
}
|
|
2422
|
-
const headersToObject = (thing) => thing instanceof AxiosHeaders
|
|
2423
|
-
function mergeConfig
|
|
2422
|
+
const headersToObject = (thing) => thing instanceof AxiosHeaders ? { ...thing } : thing;
|
|
2423
|
+
function mergeConfig(config1, config2) {
|
|
2424
2424
|
config2 = config2 || {};
|
|
2425
2425
|
const config = {};
|
|
2426
2426
|
function getMergedValue(target, source, prop, caseless) {
|
|
@@ -2498,9 +2498,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2498
2498
|
return config;
|
|
2499
2499
|
}
|
|
2500
2500
|
const resolveConfig = (config) => {
|
|
2501
|
-
const newConfig = mergeConfig
|
|
2501
|
+
const newConfig = mergeConfig({}, config);
|
|
2502
2502
|
let { data, withXSRFToken, xsrfHeaderName, xsrfCookieName, headers, auth } = newConfig;
|
|
2503
|
-
newConfig.headers = headers = AxiosHeaders
|
|
2503
|
+
newConfig.headers = headers = AxiosHeaders.from(headers);
|
|
2504
2504
|
newConfig.url = buildURL(buildFullPath(newConfig.baseURL, newConfig.url), config.params, config.paramsSerializer);
|
|
2505
2505
|
if (auth) {
|
|
2506
2506
|
headers.set(
|
|
@@ -2533,7 +2533,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2533
2533
|
return new Promise(function dispatchXhrRequest(resolve, reject) {
|
|
2534
2534
|
const _config = resolveConfig(config);
|
|
2535
2535
|
let requestData = _config.data;
|
|
2536
|
-
const requestHeaders = AxiosHeaders
|
|
2536
|
+
const requestHeaders = AxiosHeaders.from(_config.headers).normalize();
|
|
2537
2537
|
let { responseType, onUploadProgress, onDownloadProgress } = _config;
|
|
2538
2538
|
let onCanceled;
|
|
2539
2539
|
let uploadThrottled, downloadThrottled;
|
|
@@ -2551,7 +2551,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2551
2551
|
if (!request) {
|
|
2552
2552
|
return;
|
|
2553
2553
|
}
|
|
2554
|
-
const responseHeaders = AxiosHeaders
|
|
2554
|
+
const responseHeaders = AxiosHeaders.from(
|
|
2555
2555
|
"getAllResponseHeaders" in request && request.getAllResponseHeaders()
|
|
2556
2556
|
);
|
|
2557
2557
|
const responseData = !responseType || responseType === "text" || responseType === "json" ? request.responseText : request.response;
|
|
@@ -2589,11 +2589,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2589
2589
|
if (!request) {
|
|
2590
2590
|
return;
|
|
2591
2591
|
}
|
|
2592
|
-
reject(new AxiosError
|
|
2592
|
+
reject(new AxiosError("Request aborted", AxiosError.ECONNABORTED, config, request));
|
|
2593
2593
|
request = null;
|
|
2594
2594
|
};
|
|
2595
2595
|
request.onerror = function handleError() {
|
|
2596
|
-
reject(new AxiosError
|
|
2596
|
+
reject(new AxiosError("Network Error", AxiosError.ERR_NETWORK, config, request));
|
|
2597
2597
|
request = null;
|
|
2598
2598
|
};
|
|
2599
2599
|
request.ontimeout = function handleTimeout() {
|
|
@@ -2602,9 +2602,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2602
2602
|
if (_config.timeoutErrorMessage) {
|
|
2603
2603
|
timeoutErrorMessage = _config.timeoutErrorMessage;
|
|
2604
2604
|
}
|
|
2605
|
-
reject(new AxiosError
|
|
2605
|
+
reject(new AxiosError(
|
|
2606
2606
|
timeoutErrorMessage,
|
|
2607
|
-
transitional.clarifyTimeoutError ? AxiosError
|
|
2607
|
+
transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
|
|
2608
2608
|
config,
|
|
2609
2609
|
request
|
|
2610
2610
|
));
|
|
@@ -2636,7 +2636,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2636
2636
|
if (!request) {
|
|
2637
2637
|
return;
|
|
2638
2638
|
}
|
|
2639
|
-
reject(!cancel || cancel.type ? new CanceledError
|
|
2639
|
+
reject(!cancel || cancel.type ? new CanceledError(null, config, request) : cancel);
|
|
2640
2640
|
request.abort();
|
|
2641
2641
|
request = null;
|
|
2642
2642
|
};
|
|
@@ -2647,7 +2647,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2647
2647
|
}
|
|
2648
2648
|
const protocol = parseProtocol(_config.url);
|
|
2649
2649
|
if (protocol && platform.protocols.indexOf(protocol) === -1) {
|
|
2650
|
-
reject(new AxiosError
|
|
2650
|
+
reject(new AxiosError("Unsupported protocol " + protocol + ":", AxiosError.ERR_BAD_REQUEST, config));
|
|
2651
2651
|
return;
|
|
2652
2652
|
}
|
|
2653
2653
|
request.send(requestData || null);
|
|
@@ -2663,12 +2663,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2663
2663
|
aborted = true;
|
|
2664
2664
|
unsubscribe();
|
|
2665
2665
|
const err = reason instanceof Error ? reason : this.reason;
|
|
2666
|
-
controller.abort(err instanceof AxiosError
|
|
2666
|
+
controller.abort(err instanceof AxiosError ? err : new CanceledError(err instanceof Error ? err.message : err));
|
|
2667
2667
|
}
|
|
2668
2668
|
};
|
|
2669
2669
|
let timer = timeout && setTimeout(() => {
|
|
2670
2670
|
timer = null;
|
|
2671
|
-
onabort(new AxiosError
|
|
2671
|
+
onabort(new AxiosError(`timeout ${timeout} of ms exceeded`, AxiosError.ETIMEDOUT));
|
|
2672
2672
|
}, timeout);
|
|
2673
2673
|
const unsubscribe = () => {
|
|
2674
2674
|
if (signals) {
|
|
@@ -2791,7 +2791,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2791
2791
|
isFetchSupported && ((res) => {
|
|
2792
2792
|
["text", "arrayBuffer", "blob", "formData", "stream"].forEach((type) => {
|
|
2793
2793
|
!resolvers[type] && (resolvers[type] = utils$1.isFunction(res[type]) ? (res2) => res2[type]() : (_, config) => {
|
|
2794
|
-
throw new AxiosError
|
|
2794
|
+
throw new AxiosError(`Response type '${type}' is not supported`, AxiosError.ERR_NOT_SUPPORT, config);
|
|
2795
2795
|
});
|
|
2796
2796
|
});
|
|
2797
2797
|
})(new Response());
|
|
@@ -2903,7 +2903,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2903
2903
|
return await new Promise((resolve, reject) => {
|
|
2904
2904
|
settle(resolve, reject, {
|
|
2905
2905
|
data: responseData,
|
|
2906
|
-
headers: AxiosHeaders
|
|
2906
|
+
headers: AxiosHeaders.from(response.headers),
|
|
2907
2907
|
status: response.status,
|
|
2908
2908
|
statusText: response.statusText,
|
|
2909
2909
|
config,
|
|
@@ -2914,13 +2914,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2914
2914
|
unsubscribe && unsubscribe();
|
|
2915
2915
|
if (err && err.name === "TypeError" && /fetch/i.test(err.message)) {
|
|
2916
2916
|
throw Object.assign(
|
|
2917
|
-
new AxiosError
|
|
2917
|
+
new AxiosError("Network Error", AxiosError.ERR_NETWORK, config, request),
|
|
2918
2918
|
{
|
|
2919
2919
|
cause: err.cause || err
|
|
2920
2920
|
}
|
|
2921
2921
|
);
|
|
2922
2922
|
}
|
|
2923
|
-
throw AxiosError
|
|
2923
|
+
throw AxiosError.from(err, err && err.code, config, request);
|
|
2924
2924
|
}
|
|
2925
2925
|
});
|
|
2926
2926
|
const knownAdapters = {
|
|
@@ -2953,7 +2953,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2953
2953
|
if (!isResolvedHandle(nameOrAdapter)) {
|
|
2954
2954
|
adapter = knownAdapters[(id = String(nameOrAdapter)).toLowerCase()];
|
|
2955
2955
|
if (adapter === void 0) {
|
|
2956
|
-
throw new AxiosError
|
|
2956
|
+
throw new AxiosError(`Unknown adapter '${id}'`);
|
|
2957
2957
|
}
|
|
2958
2958
|
}
|
|
2959
2959
|
if (adapter) {
|
|
@@ -2966,7 +2966,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2966
2966
|
([id, state]) => `adapter ${id} ` + (state === false ? "is not supported by the environment" : "is not available in the build")
|
|
2967
2967
|
);
|
|
2968
2968
|
let s = length ? reasons.length > 1 ? "since :\n" + reasons.map(renderReason).join("\n") : " " + renderReason(reasons[0]) : "as no adapter specified";
|
|
2969
|
-
throw new AxiosError
|
|
2969
|
+
throw new AxiosError(
|
|
2970
2970
|
`There is no suitable adapter to dispatch the request ` + s,
|
|
2971
2971
|
"ERR_NOT_SUPPORT"
|
|
2972
2972
|
);
|
|
@@ -2980,12 +2980,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
2980
2980
|
config.cancelToken.throwIfRequested();
|
|
2981
2981
|
}
|
|
2982
2982
|
if (config.signal && config.signal.aborted) {
|
|
2983
|
-
throw new CanceledError
|
|
2983
|
+
throw new CanceledError(null, config);
|
|
2984
2984
|
}
|
|
2985
2985
|
}
|
|
2986
2986
|
function dispatchRequest(config) {
|
|
2987
2987
|
throwIfCancellationRequested(config);
|
|
2988
|
-
config.headers = AxiosHeaders
|
|
2988
|
+
config.headers = AxiosHeaders.from(config.headers);
|
|
2989
2989
|
config.data = transformData.call(
|
|
2990
2990
|
config,
|
|
2991
2991
|
config.transformRequest
|
|
@@ -3001,10 +3001,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3001
3001
|
config.transformResponse,
|
|
3002
3002
|
response
|
|
3003
3003
|
);
|
|
3004
|
-
response.headers = AxiosHeaders
|
|
3004
|
+
response.headers = AxiosHeaders.from(response.headers);
|
|
3005
3005
|
return response;
|
|
3006
3006
|
}, function onAdapterRejection(reason) {
|
|
3007
|
-
if (!isCancel
|
|
3007
|
+
if (!isCancel(reason)) {
|
|
3008
3008
|
throwIfCancellationRequested(config);
|
|
3009
3009
|
if (reason && reason.response) {
|
|
3010
3010
|
reason.response.data = transformData.call(
|
|
@@ -3012,13 +3012,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3012
3012
|
config.transformResponse,
|
|
3013
3013
|
reason.response
|
|
3014
3014
|
);
|
|
3015
|
-
reason.response.headers = AxiosHeaders
|
|
3015
|
+
reason.response.headers = AxiosHeaders.from(reason.response.headers);
|
|
3016
3016
|
}
|
|
3017
3017
|
}
|
|
3018
3018
|
return Promise.reject(reason);
|
|
3019
3019
|
});
|
|
3020
3020
|
}
|
|
3021
|
-
const VERSION
|
|
3021
|
+
const VERSION = "1.7.9";
|
|
3022
3022
|
const validators$1 = {};
|
|
3023
3023
|
["object", "boolean", "number", "function", "string", "symbol"].forEach((type, i) => {
|
|
3024
3024
|
validators$1[type] = function validator2(thing) {
|
|
@@ -3028,13 +3028,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3028
3028
|
const deprecatedWarnings = {};
|
|
3029
3029
|
validators$1.transitional = function transitional(validator2, version, message) {
|
|
3030
3030
|
function formatMessage(opt, desc) {
|
|
3031
|
-
return "[Axios v" + VERSION
|
|
3031
|
+
return "[Axios v" + VERSION + "] Transitional option '" + opt + "'" + desc + (message ? ". " + message : "");
|
|
3032
3032
|
}
|
|
3033
3033
|
return (value, opt, opts) => {
|
|
3034
3034
|
if (validator2 === false) {
|
|
3035
|
-
throw new AxiosError
|
|
3035
|
+
throw new AxiosError(
|
|
3036
3036
|
formatMessage(opt, " has been removed" + (version ? " in " + version : "")),
|
|
3037
|
-
AxiosError
|
|
3037
|
+
AxiosError.ERR_DEPRECATED
|
|
3038
3038
|
);
|
|
3039
3039
|
}
|
|
3040
3040
|
if (version && !deprecatedWarnings[opt]) {
|
|
@@ -3057,7 +3057,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3057
3057
|
};
|
|
3058
3058
|
function assertOptions(options, schema, allowUnknown) {
|
|
3059
3059
|
if (typeof options !== "object") {
|
|
3060
|
-
throw new AxiosError
|
|
3060
|
+
throw new AxiosError("options must be an object", AxiosError.ERR_BAD_OPTION_VALUE);
|
|
3061
3061
|
}
|
|
3062
3062
|
const keys = Object.keys(options);
|
|
3063
3063
|
let i = keys.length;
|
|
@@ -3068,12 +3068,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3068
3068
|
const value = options[opt];
|
|
3069
3069
|
const result = value === void 0 || validator2(value, opt, options);
|
|
3070
3070
|
if (result !== true) {
|
|
3071
|
-
throw new AxiosError
|
|
3071
|
+
throw new AxiosError("option " + opt + " must be " + result, AxiosError.ERR_BAD_OPTION_VALUE);
|
|
3072
3072
|
}
|
|
3073
3073
|
continue;
|
|
3074
3074
|
}
|
|
3075
3075
|
if (allowUnknown !== true) {
|
|
3076
|
-
throw new AxiosError
|
|
3076
|
+
throw new AxiosError("Unknown option " + opt, AxiosError.ERR_BAD_OPTION);
|
|
3077
3077
|
}
|
|
3078
3078
|
}
|
|
3079
3079
|
}
|
|
@@ -3082,7 +3082,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3082
3082
|
validators: validators$1
|
|
3083
3083
|
};
|
|
3084
3084
|
const validators = validator.validators;
|
|
3085
|
-
|
|
3085
|
+
class Axios {
|
|
3086
3086
|
constructor(instanceConfig) {
|
|
3087
3087
|
this.defaults = instanceConfig;
|
|
3088
3088
|
this.interceptors = {
|
|
@@ -3125,7 +3125,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3125
3125
|
} else {
|
|
3126
3126
|
config = configOrUrl || {};
|
|
3127
3127
|
}
|
|
3128
|
-
config = mergeConfig
|
|
3128
|
+
config = mergeConfig(this.defaults, config);
|
|
3129
3129
|
const { transitional, paramsSerializer, headers } = config;
|
|
3130
3130
|
if (transitional !== void 0) {
|
|
3131
3131
|
validator.assertOptions(transitional, {
|
|
@@ -3161,7 +3161,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3161
3161
|
delete headers[method];
|
|
3162
3162
|
}
|
|
3163
3163
|
);
|
|
3164
|
-
config.headers = AxiosHeaders
|
|
3164
|
+
config.headers = AxiosHeaders.concat(contextHeaders, headers);
|
|
3165
3165
|
const requestInterceptorChain = [];
|
|
3166
3166
|
let synchronousRequestInterceptors = true;
|
|
3167
3167
|
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
|
|
@@ -3215,14 +3215,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3215
3215
|
return promise;
|
|
3216
3216
|
}
|
|
3217
3217
|
getUri(config) {
|
|
3218
|
-
config = mergeConfig
|
|
3218
|
+
config = mergeConfig(this.defaults, config);
|
|
3219
3219
|
const fullPath = buildFullPath(config.baseURL, config.url);
|
|
3220
3220
|
return buildURL(fullPath, config.params, config.paramsSerializer);
|
|
3221
3221
|
}
|
|
3222
|
-
}
|
|
3222
|
+
}
|
|
3223
3223
|
utils$1.forEach(["delete", "get", "head", "options"], function forEachMethodNoData(method) {
|
|
3224
|
-
Axios
|
|
3225
|
-
return this.request(mergeConfig
|
|
3224
|
+
Axios.prototype[method] = function(url, config) {
|
|
3225
|
+
return this.request(mergeConfig(config || {}, {
|
|
3226
3226
|
method,
|
|
3227
3227
|
url,
|
|
3228
3228
|
data: (config || {}).data
|
|
@@ -3232,7 +3232,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3232
3232
|
utils$1.forEach(["post", "put", "patch"], function forEachMethodWithData(method) {
|
|
3233
3233
|
function generateHTTPMethod(isForm) {
|
|
3234
3234
|
return function httpMethod(url, data, config) {
|
|
3235
|
-
return this.request(mergeConfig
|
|
3235
|
+
return this.request(mergeConfig(config || {}, {
|
|
3236
3236
|
method,
|
|
3237
3237
|
headers: isForm ? {
|
|
3238
3238
|
"Content-Type": "multipart/form-data"
|
|
@@ -3242,10 +3242,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3242
3242
|
}));
|
|
3243
3243
|
};
|
|
3244
3244
|
}
|
|
3245
|
-
Axios
|
|
3246
|
-
Axios
|
|
3245
|
+
Axios.prototype[method] = generateHTTPMethod();
|
|
3246
|
+
Axios.prototype[method + "Form"] = generateHTTPMethod(true);
|
|
3247
3247
|
});
|
|
3248
|
-
|
|
3248
|
+
class CancelToken {
|
|
3249
3249
|
constructor(executor) {
|
|
3250
3250
|
if (typeof executor !== "function") {
|
|
3251
3251
|
throw new TypeError("executor must be a function.");
|
|
@@ -3278,7 +3278,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3278
3278
|
if (token.reason) {
|
|
3279
3279
|
return;
|
|
3280
3280
|
}
|
|
3281
|
-
token.reason = new CanceledError
|
|
3281
|
+
token.reason = new CanceledError(message, config, request);
|
|
3282
3282
|
resolvePromise(token.reason);
|
|
3283
3283
|
});
|
|
3284
3284
|
}
|
|
@@ -3331,7 +3331,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3331
3331
|
*/
|
|
3332
3332
|
static source() {
|
|
3333
3333
|
let cancel;
|
|
3334
|
-
const token = new
|
|
3334
|
+
const token = new CancelToken(function executor(c) {
|
|
3335
3335
|
cancel = c;
|
|
3336
3336
|
});
|
|
3337
3337
|
return {
|
|
@@ -3339,16 +3339,16 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3339
3339
|
cancel
|
|
3340
3340
|
};
|
|
3341
3341
|
}
|
|
3342
|
-
}
|
|
3343
|
-
function spread
|
|
3342
|
+
}
|
|
3343
|
+
function spread(callback) {
|
|
3344
3344
|
return function wrap(arr) {
|
|
3345
3345
|
return callback.apply(null, arr);
|
|
3346
3346
|
};
|
|
3347
3347
|
}
|
|
3348
|
-
function isAxiosError
|
|
3348
|
+
function isAxiosError(payload) {
|
|
3349
3349
|
return utils$1.isObject(payload) && payload.isAxiosError === true;
|
|
3350
3350
|
}
|
|
3351
|
-
const HttpStatusCode
|
|
3351
|
+
const HttpStatusCode = {
|
|
3352
3352
|
Continue: 100,
|
|
3353
3353
|
SwitchingProtocols: 101,
|
|
3354
3354
|
Processing: 102,
|
|
@@ -3413,102 +3413,115 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3413
3413
|
NotExtended: 510,
|
|
3414
3414
|
NetworkAuthenticationRequired: 511
|
|
3415
3415
|
};
|
|
3416
|
-
Object.entries(HttpStatusCode
|
|
3417
|
-
HttpStatusCode
|
|
3416
|
+
Object.entries(HttpStatusCode).forEach(([key, value]) => {
|
|
3417
|
+
HttpStatusCode[value] = key;
|
|
3418
3418
|
});
|
|
3419
3419
|
function createInstance(defaultConfig) {
|
|
3420
|
-
const context = new Axios
|
|
3421
|
-
const instance = bind(Axios
|
|
3422
|
-
utils$1.extend(instance, Axios
|
|
3420
|
+
const context = new Axios(defaultConfig);
|
|
3421
|
+
const instance = bind(Axios.prototype.request, context);
|
|
3422
|
+
utils$1.extend(instance, Axios.prototype, context, { allOwnKeys: true });
|
|
3423
3423
|
utils$1.extend(instance, context, null, { allOwnKeys: true });
|
|
3424
3424
|
instance.create = function create(instanceConfig) {
|
|
3425
|
-
return createInstance(mergeConfig
|
|
3425
|
+
return createInstance(mergeConfig(defaultConfig, instanceConfig));
|
|
3426
3426
|
};
|
|
3427
3427
|
return instance;
|
|
3428
3428
|
}
|
|
3429
3429
|
const axios = createInstance(defaults);
|
|
3430
|
-
axios.Axios = Axios
|
|
3431
|
-
axios.CanceledError = CanceledError
|
|
3432
|
-
axios.CancelToken = CancelToken
|
|
3433
|
-
axios.isCancel = isCancel
|
|
3434
|
-
axios.VERSION = VERSION
|
|
3435
|
-
axios.toFormData = toFormData
|
|
3436
|
-
axios.AxiosError = AxiosError
|
|
3430
|
+
axios.Axios = Axios;
|
|
3431
|
+
axios.CanceledError = CanceledError;
|
|
3432
|
+
axios.CancelToken = CancelToken;
|
|
3433
|
+
axios.isCancel = isCancel;
|
|
3434
|
+
axios.VERSION = VERSION;
|
|
3435
|
+
axios.toFormData = toFormData;
|
|
3436
|
+
axios.AxiosError = AxiosError;
|
|
3437
3437
|
axios.Cancel = axios.CanceledError;
|
|
3438
|
-
axios.all = function
|
|
3438
|
+
axios.all = function all(promises) {
|
|
3439
3439
|
return Promise.all(promises);
|
|
3440
3440
|
};
|
|
3441
|
-
axios.spread = spread
|
|
3442
|
-
axios.isAxiosError = isAxiosError
|
|
3443
|
-
axios.mergeConfig = mergeConfig
|
|
3444
|
-
axios.AxiosHeaders = AxiosHeaders
|
|
3441
|
+
axios.spread = spread;
|
|
3442
|
+
axios.isAxiosError = isAxiosError;
|
|
3443
|
+
axios.mergeConfig = mergeConfig;
|
|
3444
|
+
axios.AxiosHeaders = AxiosHeaders;
|
|
3445
3445
|
axios.formToJSON = (thing) => formDataToJSON(utils$1.isHTMLForm(thing) ? new FormData(thing) : thing);
|
|
3446
3446
|
axios.getAdapter = adapters.getAdapter;
|
|
3447
|
-
axios.HttpStatusCode = HttpStatusCode
|
|
3447
|
+
axios.HttpStatusCode = HttpStatusCode;
|
|
3448
3448
|
axios.default = axios;
|
|
3449
|
-
const {
|
|
3450
|
-
Axios,
|
|
3451
|
-
AxiosError,
|
|
3452
|
-
CanceledError,
|
|
3453
|
-
isCancel,
|
|
3454
|
-
CancelToken,
|
|
3455
|
-
VERSION,
|
|
3456
|
-
all,
|
|
3457
|
-
Cancel,
|
|
3458
|
-
isAxiosError,
|
|
3459
|
-
spread,
|
|
3460
|
-
toFormData,
|
|
3461
|
-
AxiosHeaders,
|
|
3462
|
-
HttpStatusCode,
|
|
3463
|
-
formToJSON,
|
|
3464
|
-
getAdapter,
|
|
3465
|
-
mergeConfig
|
|
3466
|
-
} = axios;
|
|
3467
3449
|
class MemoryCache {
|
|
3468
3450
|
constructor(ttl) {
|
|
3469
|
-
|
|
3451
|
+
// private cache: Record<string, CacheSetData> = {};
|
|
3452
|
+
__publicField(this, "CacheMap", /* @__PURE__ */ new Map());
|
|
3470
3453
|
__publicField(this, "ttl");
|
|
3471
3454
|
this.ttl = ttl;
|
|
3472
3455
|
}
|
|
3473
3456
|
async get(key) {
|
|
3474
|
-
const cacheRecord = this.cache;
|
|
3475
3457
|
return new Promise((resolve, reject) => {
|
|
3476
3458
|
try {
|
|
3477
|
-
const cacheItem =
|
|
3459
|
+
const cacheItem = this.CacheMap.get(key);
|
|
3478
3460
|
if (!cacheItem) {
|
|
3479
|
-
return resolve({
|
|
3461
|
+
return resolve({
|
|
3462
|
+
error: new Error("[MemoryCache]未找到缓存,将执行请求"),
|
|
3463
|
+
data: null
|
|
3464
|
+
});
|
|
3480
3465
|
}
|
|
3481
3466
|
const { data, exp } = cacheItem;
|
|
3482
3467
|
if (exp < Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3)) {
|
|
3483
3468
|
this.delete(key);
|
|
3484
|
-
return resolve({
|
|
3469
|
+
return resolve({
|
|
3470
|
+
error: new Error("[MemoryCache]缓存已过期,将执行请求"),
|
|
3471
|
+
data: null
|
|
3472
|
+
});
|
|
3485
3473
|
}
|
|
3486
3474
|
resolve({ error: null, data });
|
|
3487
3475
|
} catch (error) {
|
|
3488
|
-
|
|
3476
|
+
console.error("[MemoryCache]获取缓存错误:", error);
|
|
3477
|
+
reject(error);
|
|
3489
3478
|
}
|
|
3490
3479
|
});
|
|
3491
3480
|
}
|
|
3492
3481
|
async set(key, value) {
|
|
3493
3482
|
return new Promise((resolve, reject) => {
|
|
3494
3483
|
try {
|
|
3495
|
-
this.
|
|
3484
|
+
this.CacheMap.set(key, {
|
|
3496
3485
|
data: value,
|
|
3497
3486
|
exp: Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + this.ttl
|
|
3498
|
-
};
|
|
3499
|
-
resolve(
|
|
3487
|
+
});
|
|
3488
|
+
resolve(true);
|
|
3500
3489
|
} catch (error) {
|
|
3501
|
-
|
|
3490
|
+
console.error("[MemoryCache]设置缓存错误:", error);
|
|
3491
|
+
reject(error);
|
|
3502
3492
|
}
|
|
3503
3493
|
});
|
|
3504
3494
|
}
|
|
3505
3495
|
async delete(key) {
|
|
3506
3496
|
return new Promise((resolve, reject) => {
|
|
3507
3497
|
try {
|
|
3508
|
-
|
|
3509
|
-
resolve(
|
|
3498
|
+
this.CacheMap.delete(key);
|
|
3499
|
+
resolve(true);
|
|
3510
3500
|
} catch (error) {
|
|
3511
|
-
|
|
3501
|
+
console.error("[MemoryCache]删除缓存错误:", error);
|
|
3502
|
+
reject(error);
|
|
3503
|
+
}
|
|
3504
|
+
});
|
|
3505
|
+
}
|
|
3506
|
+
async clear() {
|
|
3507
|
+
return new Promise((resolve, reject) => {
|
|
3508
|
+
try {
|
|
3509
|
+
this.CacheMap.clear();
|
|
3510
|
+
resolve(true);
|
|
3511
|
+
} catch (error) {
|
|
3512
|
+
console.error("[MemoryCache]清空缓存错误:", error);
|
|
3513
|
+
reject(error);
|
|
3514
|
+
}
|
|
3515
|
+
});
|
|
3516
|
+
}
|
|
3517
|
+
async keys() {
|
|
3518
|
+
return new Promise((resolve, reject) => {
|
|
3519
|
+
try {
|
|
3520
|
+
const keys = Array.from(this.CacheMap.keys());
|
|
3521
|
+
resolve(keys);
|
|
3522
|
+
} catch (error) {
|
|
3523
|
+
console.error("[MemoryCache]获取缓存键错误:", error);
|
|
3524
|
+
reject(error);
|
|
3512
3525
|
}
|
|
3513
3526
|
});
|
|
3514
3527
|
}
|
|
@@ -3523,12 +3536,18 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3523
3536
|
try {
|
|
3524
3537
|
const cacheItem = localStorage.getItem(key);
|
|
3525
3538
|
if (!cacheItem) {
|
|
3526
|
-
return resolve({
|
|
3539
|
+
return resolve({
|
|
3540
|
+
error: new Error(`未找到${key}的缓存`),
|
|
3541
|
+
data: null
|
|
3542
|
+
});
|
|
3527
3543
|
}
|
|
3528
3544
|
const { data, exp } = JSON.parse(cacheItem);
|
|
3529
3545
|
if (exp < Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3)) {
|
|
3530
3546
|
this.delete(key);
|
|
3531
|
-
return resolve({
|
|
3547
|
+
return resolve({
|
|
3548
|
+
error: new Error(`${key}的缓存已过期`),
|
|
3549
|
+
data: null
|
|
3550
|
+
});
|
|
3532
3551
|
}
|
|
3533
3552
|
resolve({ error: null, data });
|
|
3534
3553
|
} catch (error) {
|
|
@@ -3546,8 +3565,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3546
3565
|
exp: Math.ceil((/* @__PURE__ */ new Date()).getTime() / 1e3) + this.ttl
|
|
3547
3566
|
})
|
|
3548
3567
|
);
|
|
3549
|
-
resolve(
|
|
3568
|
+
resolve(true);
|
|
3550
3569
|
} catch (error) {
|
|
3570
|
+
console.error("[LocalStorage]插入数据错误:", error);
|
|
3551
3571
|
reject({ error, data: null });
|
|
3552
3572
|
}
|
|
3553
3573
|
});
|
|
@@ -3556,12 +3576,34 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3556
3576
|
return new Promise((resolve, reject) => {
|
|
3557
3577
|
try {
|
|
3558
3578
|
localStorage.removeItem(key);
|
|
3559
|
-
resolve(
|
|
3579
|
+
resolve(true);
|
|
3560
3580
|
} catch (error) {
|
|
3561
|
-
|
|
3581
|
+
console.error("[LocalStorage]删除数据错误:", error);
|
|
3582
|
+
reject(error);
|
|
3562
3583
|
}
|
|
3563
3584
|
});
|
|
3564
3585
|
}
|
|
3586
|
+
async clear() {
|
|
3587
|
+
return new Promise((resolve, reject) => {
|
|
3588
|
+
try {
|
|
3589
|
+
localStorage.clear();
|
|
3590
|
+
resolve(true);
|
|
3591
|
+
} catch (error) {
|
|
3592
|
+
console.error("[LocalStorage]清空数据错误:", error);
|
|
3593
|
+
reject(false);
|
|
3594
|
+
}
|
|
3595
|
+
});
|
|
3596
|
+
}
|
|
3597
|
+
async keys() {
|
|
3598
|
+
return new Promise((resolve) => {
|
|
3599
|
+
const keys = [];
|
|
3600
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
3601
|
+
const key = localStorage.key(i);
|
|
3602
|
+
if (key) keys.push(key);
|
|
3603
|
+
}
|
|
3604
|
+
resolve(keys);
|
|
3605
|
+
});
|
|
3606
|
+
}
|
|
3565
3607
|
}
|
|
3566
3608
|
const INDEXDB_DATABASE_NAME = "SNAIL_CACHE";
|
|
3567
3609
|
const INDEXDB_VERSION = 3;
|
|
@@ -3631,13 +3673,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3631
3673
|
key
|
|
3632
3674
|
});
|
|
3633
3675
|
select.onsuccess = () => {
|
|
3634
|
-
resolve(
|
|
3676
|
+
resolve(true);
|
|
3635
3677
|
};
|
|
3636
3678
|
select.onerror = (event) => {
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
data: null
|
|
3640
|
-
});
|
|
3679
|
+
console.error(`[indexDB]插入数据错误:${event.target}`);
|
|
3680
|
+
reject(event);
|
|
3641
3681
|
};
|
|
3642
3682
|
});
|
|
3643
3683
|
}
|
|
@@ -3648,27 +3688,49 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3648
3688
|
}
|
|
3649
3689
|
const select = this.db.transaction(INDEXDB_OBJECT_STORE_NAME, "readwrite").objectStore(INDEXDB_OBJECT_STORE_NAME).delete(key);
|
|
3650
3690
|
select.onsuccess = () => {
|
|
3651
|
-
resolve(
|
|
3691
|
+
resolve(true);
|
|
3652
3692
|
};
|
|
3653
3693
|
select.onerror = (event) => {
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3694
|
+
console.error(`[indexDB]删除数据错误:${event.target}`);
|
|
3695
|
+
reject(event);
|
|
3696
|
+
};
|
|
3697
|
+
});
|
|
3698
|
+
}
|
|
3699
|
+
async clear() {
|
|
3700
|
+
return new Promise(async (resolve, reject) => {
|
|
3701
|
+
if (!this.db) await this.init();
|
|
3702
|
+
const transaction = this.db.transaction(INDEXDB_OBJECT_STORE_NAME, "readwrite");
|
|
3703
|
+
const request = transaction.objectStore(INDEXDB_OBJECT_STORE_NAME).clear();
|
|
3704
|
+
request.onsuccess = () => resolve(true);
|
|
3705
|
+
request.onerror = (event) => {
|
|
3706
|
+
console.error(`[indexDB]清空数据错误:${event.target}`);
|
|
3707
|
+
reject(false);
|
|
3708
|
+
};
|
|
3709
|
+
});
|
|
3710
|
+
}
|
|
3711
|
+
async keys() {
|
|
3712
|
+
return new Promise(async (resolve, reject) => {
|
|
3713
|
+
if (!this.db) await this.init();
|
|
3714
|
+
const transaction = this.db.transaction(INDEXDB_OBJECT_STORE_NAME, "readonly");
|
|
3715
|
+
const request = transaction.objectStore(INDEXDB_OBJECT_STORE_NAME).getAllKeys();
|
|
3716
|
+
request.onsuccess = () => resolve(request.result.map((key) => key.toString()));
|
|
3717
|
+
request.onerror = (event) => {
|
|
3718
|
+
console.error(`[indexDB]获取键名错误:${event.target}`);
|
|
3719
|
+
reject([]);
|
|
3658
3720
|
};
|
|
3659
3721
|
});
|
|
3660
3722
|
}
|
|
3661
3723
|
}
|
|
3662
|
-
var
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
|
|
3666
|
-
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
return
|
|
3671
|
-
})(
|
|
3724
|
+
var RequestMethodEnum = /* @__PURE__ */ ((RequestMethodEnum2) => {
|
|
3725
|
+
RequestMethodEnum2["GET"] = "Get";
|
|
3726
|
+
RequestMethodEnum2["HEAD"] = "Head";
|
|
3727
|
+
RequestMethodEnum2["POST"] = "Post";
|
|
3728
|
+
RequestMethodEnum2["PUT"] = "Put";
|
|
3729
|
+
RequestMethodEnum2["DELETE"] = "delete";
|
|
3730
|
+
RequestMethodEnum2["PATCH"] = "Patch";
|
|
3731
|
+
RequestMethodEnum2["OPTIONS"] = "Options";
|
|
3732
|
+
return RequestMethodEnum2;
|
|
3733
|
+
})(RequestMethodEnum || {});
|
|
3672
3734
|
var VersioningType = /* @__PURE__ */ ((VersioningType2) => {
|
|
3673
3735
|
VersioningType2[VersioningType2["Uri"] = 0] = "Uri";
|
|
3674
3736
|
VersioningType2[VersioningType2["Header"] = 1] = "Header";
|
|
@@ -3680,36 +3742,70 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3680
3742
|
CacheType2[CacheType2["Memory"] = 0] = "Memory";
|
|
3681
3743
|
CacheType2[CacheType2["IndexDB"] = 1] = "IndexDB";
|
|
3682
3744
|
CacheType2[CacheType2["LocalStorage"] = 2] = "LocalStorage";
|
|
3745
|
+
CacheType2[CacheType2["Custom"] = 3] = "Custom";
|
|
3683
3746
|
return CacheType2;
|
|
3684
3747
|
})(CacheType || {});
|
|
3748
|
+
class CacheStorageAdapter {
|
|
3749
|
+
}
|
|
3685
3750
|
class Strategy {
|
|
3686
3751
|
}
|
|
3687
|
-
class
|
|
3752
|
+
class SseEventListener {
|
|
3688
3753
|
constructor() {
|
|
3689
3754
|
__publicField(this, "eventName");
|
|
3690
3755
|
__publicField(this, "emit");
|
|
3691
3756
|
__publicField(this, "options");
|
|
3692
3757
|
}
|
|
3693
3758
|
}
|
|
3694
|
-
|
|
3695
|
-
|
|
3696
|
-
|
|
3759
|
+
class SseOptions {
|
|
3760
|
+
constructor() {
|
|
3761
|
+
__publicField(this, "withCredentials");
|
|
3762
|
+
__publicField(this, "version");
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
3766
|
+
EventType2["Success"] = "success";
|
|
3767
|
+
EventType2["Error"] = "error";
|
|
3768
|
+
EventType2["hitCache"] = "hitCache";
|
|
3769
|
+
EventType2["Finish"] = "finish";
|
|
3770
|
+
return EventType2;
|
|
3771
|
+
})(EventType || {});
|
|
3772
|
+
function createCache(optios) {
|
|
3773
|
+
const { type } = optios;
|
|
3774
|
+
if (type === CacheType.Custom) {
|
|
3775
|
+
const adapter = optios.adapter;
|
|
3776
|
+
return new adapter(optios.options);
|
|
3697
3777
|
}
|
|
3698
3778
|
if (type === CacheType.LocalStorage) {
|
|
3699
|
-
|
|
3779
|
+
const { ttl: ttl2 } = optios;
|
|
3780
|
+
return new LocalStorageCache(ttl2);
|
|
3700
3781
|
}
|
|
3701
3782
|
if (type === CacheType.IndexDB) {
|
|
3702
|
-
const
|
|
3783
|
+
const { ttl: ttl2 } = optios;
|
|
3784
|
+
const db = new IndexDBCache(ttl2);
|
|
3703
3785
|
db.init();
|
|
3704
3786
|
return db;
|
|
3705
3787
|
}
|
|
3706
|
-
|
|
3788
|
+
const { ttl } = optios;
|
|
3789
|
+
return new MemoryCache(ttl);
|
|
3707
3790
|
}
|
|
3708
|
-
const
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3791
|
+
const REQUEST_ARGS_KEY = Symbol("SNAIL_REQUEST_ARGS_KEY");
|
|
3792
|
+
const Params = (key) => createArgsDecorator("params", key);
|
|
3793
|
+
const Query = (key) => createArgsDecorator("querys", key);
|
|
3794
|
+
const Data = (key) => createArgsDecorator("data", key);
|
|
3795
|
+
const createArgsDecorator = (type, key) => {
|
|
3796
|
+
return (target, propertyKey, parameterIndex) => {
|
|
3797
|
+
const args = Reflect.getMetadata(REQUEST_ARGS_KEY, target, propertyKey) || [];
|
|
3798
|
+
args.push({ index: parameterIndex, type, key });
|
|
3799
|
+
Reflect.defineMetadata(REQUEST_ARGS_KEY, args, target, propertyKey);
|
|
3800
|
+
};
|
|
3712
3801
|
};
|
|
3802
|
+
async function generateCacheKey(methodName, version, request) {
|
|
3803
|
+
const { method, url, params, headers } = request;
|
|
3804
|
+
const hash = await generateShortUniqueHash(
|
|
3805
|
+
`${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`
|
|
3806
|
+
);
|
|
3807
|
+
return `${methodName}${version ? `[v${version}]` : ""}[${method}]${url}-${hash}`;
|
|
3808
|
+
}
|
|
3713
3809
|
async function generateShortUniqueHash(str) {
|
|
3714
3810
|
const encoder = new TextEncoder();
|
|
3715
3811
|
const data = encoder.encode(str);
|
|
@@ -3724,42 +3820,73 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3724
3820
|
return `${key}=${record[key]}`;
|
|
3725
3821
|
}).sort((a, b) => a.localeCompare(b)).join("&");
|
|
3726
3822
|
}
|
|
3727
|
-
|
|
3728
|
-
[
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
}
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
return {
|
|
3745
|
-
params: {
|
|
3746
|
-
[key]: version
|
|
3747
|
-
}
|
|
3748
|
-
};
|
|
3749
|
-
},
|
|
3750
|
-
[VersioningType.Custom]: (version, versioning) => {
|
|
3751
|
-
return versioning.extractor({
|
|
3752
|
-
version
|
|
3753
|
-
});
|
|
3754
|
-
}
|
|
3823
|
+
function replacePlaceholders(route, values) {
|
|
3824
|
+
const placeholderRegex = /:([a-zA-Z0-9_]+)/g;
|
|
3825
|
+
const newRoute = route.replace(placeholderRegex, (match, key) => {
|
|
3826
|
+
if (values.hasOwnProperty(key)) {
|
|
3827
|
+
return values[key];
|
|
3828
|
+
} else {
|
|
3829
|
+
console.error(`路由[${route}]中的占位符${match},没有对应的值`);
|
|
3830
|
+
console.error(`请在该路由对应的方法中添加@Params('${key}')装饰器`);
|
|
3831
|
+
throw new TypeError(`route params error in ${route}`);
|
|
3832
|
+
}
|
|
3833
|
+
});
|
|
3834
|
+
return newRoute;
|
|
3835
|
+
}
|
|
3836
|
+
const resolveUrl = (url) => {
|
|
3837
|
+
if (!url) return "";
|
|
3838
|
+
if (url.startsWith("/")) return url;
|
|
3839
|
+
return `/${url}`;
|
|
3755
3840
|
};
|
|
3756
|
-
function
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3841
|
+
async function applyStrategies(data, strategies, type = "request") {
|
|
3842
|
+
return await strategies.reduce(
|
|
3843
|
+
async (result, strategy) => {
|
|
3844
|
+
let handler;
|
|
3845
|
+
const instance = new strategy();
|
|
3846
|
+
if (type == "response") {
|
|
3847
|
+
handler = instance.applyResponse;
|
|
3848
|
+
}
|
|
3849
|
+
handler = instance.applyRequest;
|
|
3850
|
+
const currentResult = handler(await result);
|
|
3851
|
+
if (!currentResult) return result;
|
|
3852
|
+
return currentResult;
|
|
3853
|
+
},
|
|
3854
|
+
Promise.resolve(data)
|
|
3761
3855
|
);
|
|
3762
3856
|
}
|
|
3857
|
+
const SnailPass = (...args) => {
|
|
3858
|
+
};
|
|
3859
|
+
const buildRequestArgs = (target, propertyKey, args) => {
|
|
3860
|
+
const requestArgs = { params: {}, data: {}, querys: {} };
|
|
3861
|
+
const argsConfigs = Reflect.getMetadata(REQUEST_ARGS_KEY, target, propertyKey) || [];
|
|
3862
|
+
console.log("argsConfigs:", argsConfigs);
|
|
3863
|
+
argsConfigs.forEach(({ index, type, key }) => {
|
|
3864
|
+
const value = args[index];
|
|
3865
|
+
if (type === "params" && !key && typeof value === "object") {
|
|
3866
|
+
requestArgs.params = { ...requestArgs.params, ...value };
|
|
3867
|
+
}
|
|
3868
|
+
if (type === "params" && key) {
|
|
3869
|
+
requestArgs.params[key] = value;
|
|
3870
|
+
}
|
|
3871
|
+
if (type === "querys" && !key && typeof value === "object") {
|
|
3872
|
+
requestArgs.querys = { ...requestArgs.params, ...value };
|
|
3873
|
+
}
|
|
3874
|
+
if (type === "querys" && key) {
|
|
3875
|
+
requestArgs.querys[key] = value;
|
|
3876
|
+
}
|
|
3877
|
+
if (type === "data" && !key && typeof value === "object") {
|
|
3878
|
+
requestArgs.data = { ...requestArgs.data, ...value };
|
|
3879
|
+
}
|
|
3880
|
+
if (type === "data" && key) {
|
|
3881
|
+
requestArgs.data[key] = value;
|
|
3882
|
+
}
|
|
3883
|
+
});
|
|
3884
|
+
return requestArgs;
|
|
3885
|
+
};
|
|
3886
|
+
function isSpecialResponse(response) {
|
|
3887
|
+
const contentType = response.headers["content-type"];
|
|
3888
|
+
return contentType && contentType.includes("application/json");
|
|
3889
|
+
}
|
|
3763
3890
|
const METHOD_KEY = Symbol("SNAIL_METHOD_KEY");
|
|
3764
3891
|
const API_CONFIG_KEY = Symbol("SNAIL_API_CONFIG_KEY");
|
|
3765
3892
|
const Api = (url = "", config) => {
|
|
@@ -3768,23 +3895,34 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3768
3895
|
};
|
|
3769
3896
|
};
|
|
3770
3897
|
const createMethodDecorator = (method) => {
|
|
3771
|
-
return (path = "") => {
|
|
3898
|
+
return (path = "", name) => {
|
|
3772
3899
|
return (target, propertyKey) => {
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3900
|
+
const methodOptions = Reflect.getMetadata(
|
|
3901
|
+
METHOD_KEY,
|
|
3902
|
+
target,
|
|
3903
|
+
propertyKey
|
|
3904
|
+
);
|
|
3905
|
+
if (methodOptions) {
|
|
3906
|
+
console.error("only one method decorator for a request function!");
|
|
3907
|
+
console.error(`you must chose only one method decorator[@${method} or @${methodOptions.method}]`);
|
|
3908
|
+
throw new TypeError(`Multiple decorators are used for the same request function`);
|
|
3776
3909
|
}
|
|
3777
|
-
Reflect.
|
|
3910
|
+
Reflect.defineMetadata(
|
|
3911
|
+
METHOD_KEY,
|
|
3912
|
+
{ method, path, name },
|
|
3913
|
+
target,
|
|
3914
|
+
propertyKey
|
|
3915
|
+
);
|
|
3778
3916
|
};
|
|
3779
3917
|
};
|
|
3780
3918
|
};
|
|
3781
|
-
const Get = createMethodDecorator(
|
|
3782
|
-
const Post = createMethodDecorator(
|
|
3783
|
-
const Put = createMethodDecorator(
|
|
3784
|
-
const Delete = createMethodDecorator(
|
|
3785
|
-
const Patch = createMethodDecorator(
|
|
3786
|
-
const Options = createMethodDecorator(
|
|
3787
|
-
const Head = createMethodDecorator(
|
|
3919
|
+
const Get = createMethodDecorator(RequestMethodEnum.GET);
|
|
3920
|
+
const Post = createMethodDecorator(RequestMethodEnum.POST);
|
|
3921
|
+
const Put = createMethodDecorator(RequestMethodEnum.PUT);
|
|
3922
|
+
const Delete = createMethodDecorator(RequestMethodEnum.DELETE);
|
|
3923
|
+
const Patch = createMethodDecorator(RequestMethodEnum.PATCH);
|
|
3924
|
+
const Options = createMethodDecorator(RequestMethodEnum.OPTIONS);
|
|
3925
|
+
const Head = createMethodDecorator(RequestMethodEnum.HEAD);
|
|
3788
3926
|
const SERVER_CONFIG_KEY = Symbol("SANIL_SERVER_CONFIG_KEY");
|
|
3789
3927
|
const Server = (config) => {
|
|
3790
3928
|
return (target) => {
|
|
@@ -3798,16 +3936,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3798
3936
|
Reflect.defineMetadata(key, strategies, target);
|
|
3799
3937
|
};
|
|
3800
3938
|
};
|
|
3801
|
-
const REQUEST_ARGS_KEY = Symbol("SNAIL_REQUEST_ARGS_KEY");
|
|
3802
|
-
const Params = (key) => createArgsDecorator("params", key);
|
|
3803
|
-
const Data = (key) => createArgsDecorator("data", key);
|
|
3804
|
-
const createArgsDecorator = (type, key) => {
|
|
3805
|
-
return (target, propertyKey, parameterIndex) => {
|
|
3806
|
-
const args = Reflect.getMetadata(REQUEST_ARGS_KEY, target, propertyKey) || [];
|
|
3807
|
-
args.push({ index: parameterIndex, type, key });
|
|
3808
|
-
Reflect.defineMetadata(REQUEST_ARGS_KEY, args, target, propertyKey);
|
|
3809
|
-
};
|
|
3810
|
-
};
|
|
3811
3939
|
const VERSIONING_KEY = Symbol("SNAIL_VERSIONING_KEY");
|
|
3812
3940
|
const VERSION_KEY = Symbol("SNAIL_VERSION_KEY");
|
|
3813
3941
|
const Versioning = (options) => {
|
|
@@ -3820,389 +3948,844 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
3820
3948
|
Reflect.defineMetadata(VERSION_KEY, version, target, propertyKey);
|
|
3821
3949
|
};
|
|
3822
3950
|
};
|
|
3823
|
-
const
|
|
3824
|
-
const
|
|
3951
|
+
const NO_CACHE_KEY = Symbol("SNALI_NO_CACHE_KEY");
|
|
3952
|
+
const CACHE_EXPIRE_SOURCE_KEY = Symbol("SNALI_CACHE_EXPIRE_SOURCE_KEY");
|
|
3953
|
+
const NoCache = () => {
|
|
3825
3954
|
return (target, propertyKey) => {
|
|
3826
|
-
|
|
3827
|
-
|
|
3955
|
+
if (propertyKey) {
|
|
3956
|
+
Reflect.defineMetadata(NO_CACHE_KEY, true, target, propertyKey);
|
|
3957
|
+
return;
|
|
3958
|
+
}
|
|
3959
|
+
Reflect.defineMetadata(NO_CACHE_KEY, true, target);
|
|
3828
3960
|
};
|
|
3829
3961
|
};
|
|
3830
|
-
const
|
|
3831
|
-
const DOWNLOAD_PROGRESS_KEY = Symbol("SNAIL_DOWNLOAD_PROGRESS_KEY");
|
|
3832
|
-
const EVENT_SOURCE_OPTION_KEY = Symbol("SNAIL_EVENT_SOURCE_OPTION_KEY");
|
|
3833
|
-
const Sse = (path, options) => {
|
|
3962
|
+
const HitSource = (...sources) => {
|
|
3834
3963
|
return (target, propertyKey) => {
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
|
|
3964
|
+
if (propertyKey) {
|
|
3965
|
+
Reflect.defineMetadata(
|
|
3966
|
+
CACHE_EXPIRE_SOURCE_KEY,
|
|
3967
|
+
sources,
|
|
3968
|
+
target,
|
|
3969
|
+
propertyKey
|
|
3970
|
+
);
|
|
3971
|
+
return;
|
|
3972
|
+
}
|
|
3973
|
+
Reflect.defineMetadata(CACHE_EXPIRE_SOURCE_KEY, sources, target);
|
|
3844
3974
|
};
|
|
3845
3975
|
};
|
|
3846
|
-
const
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3976
|
+
const versionHandlers = {
|
|
3977
|
+
[VersioningType.Uri]: (version, versioning) => {
|
|
3978
|
+
const prefix = versioning.prefix || "v";
|
|
3979
|
+
return {
|
|
3980
|
+
type: VersioningType.Uri,
|
|
3981
|
+
result: `${prefix}${version}`
|
|
3982
|
+
};
|
|
3983
|
+
},
|
|
3984
|
+
[VersioningType.Header]: (version, versioning) => {
|
|
3985
|
+
const headers = {
|
|
3986
|
+
[versioning.header || "version"]: version
|
|
3987
|
+
};
|
|
3988
|
+
return {
|
|
3989
|
+
type: VersioningType.Header,
|
|
3990
|
+
result: headers
|
|
3991
|
+
};
|
|
3992
|
+
},
|
|
3993
|
+
[VersioningType.Query]: (version, versioning) => {
|
|
3994
|
+
const key = versioning.key || "v";
|
|
3995
|
+
return {
|
|
3996
|
+
type: VersioningType.Query,
|
|
3997
|
+
result: {
|
|
3998
|
+
[key]: version
|
|
3999
|
+
}
|
|
4000
|
+
};
|
|
4001
|
+
},
|
|
4002
|
+
[VersioningType.Custom]: (version, versioning) => {
|
|
4003
|
+
return versioning.extractor({
|
|
4004
|
+
version
|
|
3854
4005
|
});
|
|
3855
|
-
|
|
3856
|
-
};
|
|
3857
|
-
};
|
|
3858
|
-
const EVENT_SOURCE_OPEN_KEY = Symbol("EVENT_SOURCE_OPEN_KEY");
|
|
3859
|
-
const OnSseOpen = () => {
|
|
3860
|
-
return (target, propertyKey) => {
|
|
3861
|
-
Reflect.defineMetadata(EVENT_SOURCE_OPEN_KEY, target[propertyKey], target);
|
|
3862
|
-
};
|
|
3863
|
-
};
|
|
3864
|
-
const EVENT_SOURCE_ERROR_KEY = Symbol("EVENT_SOURCE_ERROR_KEY");
|
|
3865
|
-
const OnSseError = () => {
|
|
3866
|
-
return (target, propertyKey) => {
|
|
3867
|
-
Reflect.defineMetadata(EVENT_SOURCE_ERROR_KEY, target[propertyKey], target);
|
|
3868
|
-
};
|
|
4006
|
+
}
|
|
3869
4007
|
};
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
4008
|
+
function applyVersioning(version, versioning) {
|
|
4009
|
+
if (!versioning) return void 0;
|
|
4010
|
+
const handler = versionHandlers[versioning.type];
|
|
4011
|
+
return handler(
|
|
4012
|
+
version,
|
|
4013
|
+
versioning
|
|
4014
|
+
);
|
|
4015
|
+
}
|
|
4016
|
+
const UPLOAD_PROGRESS_KEY = Symbol("SNAIL_UPLOAD_PROGRESS_KEY");
|
|
4017
|
+
const DOWNLOAD_PROGRESS_KEY = Symbol("SNAIL_DOWNLOAD_PROGRESS_KEY");
|
|
4018
|
+
class SnailMethod {
|
|
4019
|
+
constructor(apiInstance, target, propertyKey, args) {
|
|
4020
|
+
// 私有属性
|
|
4021
|
+
// private serverInstance: SnailServer;
|
|
4022
|
+
__publicField(this, "Name");
|
|
4023
|
+
__publicField(this, "apiInstance");
|
|
4024
|
+
__publicField(this, "target");
|
|
3874
4025
|
__publicField(this, "strategies", []);
|
|
3875
|
-
__publicField(this, "
|
|
3876
|
-
__publicField(this, "
|
|
3877
|
-
__publicField(this, "
|
|
3878
|
-
__publicField(this, "
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
this
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
4026
|
+
__publicField(this, "Request");
|
|
4027
|
+
__publicField(this, "Response");
|
|
4028
|
+
__publicField(this, "Error");
|
|
4029
|
+
__publicField(this, "eventMap");
|
|
4030
|
+
__publicField(this, "onceWrapperMap");
|
|
4031
|
+
__publicField(this, "propertyKey");
|
|
4032
|
+
__publicField(this, "Url", "");
|
|
4033
|
+
__publicField(this, "Path", "");
|
|
4034
|
+
__publicField(this, "Method", RequestMethodEnum.GET);
|
|
4035
|
+
__publicField(this, "Version");
|
|
4036
|
+
__publicField(this, "Args", []);
|
|
4037
|
+
__publicField(this, "send", async () => {
|
|
4038
|
+
this.enableLog() && console.log("send:", this.name);
|
|
4039
|
+
const [serverName] = this.apiInstance.name.split(".");
|
|
4040
|
+
const axios2 = AxiosInstanceMap.get(serverName);
|
|
4041
|
+
if (!axios2) throw new Error("AxiosInstance not created");
|
|
4042
|
+
const strategies = this.getStrategies();
|
|
4043
|
+
this.Request = await applyStrategies(this.Request, strategies, "request");
|
|
4044
|
+
this.Request = this.applyProgress(this.Request);
|
|
4045
|
+
const { data, querys, params } = buildRequestArgs(
|
|
4046
|
+
this.target,
|
|
4047
|
+
this.propertyKey,
|
|
4048
|
+
this.Args
|
|
4049
|
+
);
|
|
4050
|
+
console.log("apiurl:", this.apiInstance.url);
|
|
4051
|
+
const newUrl = replacePlaceholders(this.Url, params);
|
|
4052
|
+
this.Request = {
|
|
4053
|
+
...this.Request,
|
|
4054
|
+
url: newUrl,
|
|
4055
|
+
params: {
|
|
4056
|
+
...this.Request.params,
|
|
4057
|
+
...querys
|
|
4058
|
+
},
|
|
4059
|
+
data
|
|
4060
|
+
};
|
|
4061
|
+
this.enableLog() && console.log("request:", this.Request);
|
|
4062
|
+
const isNoCache = this.isNoCache();
|
|
4063
|
+
this.enableLog() && console.log("method is noCache:", isNoCache);
|
|
4064
|
+
if (!isNoCache) {
|
|
4065
|
+
const { data: data2 } = await this.getCacheData(serverName);
|
|
4066
|
+
this.enableLog() && console.log("getCacheData:", data2);
|
|
4067
|
+
if (data2) {
|
|
4068
|
+
this.emit("hitCache", data2);
|
|
4069
|
+
this.emit("success", data2);
|
|
4070
|
+
this.emit("finish", data2);
|
|
4071
|
+
return data2;
|
|
4072
|
+
}
|
|
4073
|
+
}
|
|
4074
|
+
const requester = AxiosInstanceMap.get(serverName);
|
|
4075
|
+
if (!requester) throw new Error("AxiosInstance not created");
|
|
4076
|
+
try {
|
|
4077
|
+
const rawResponse = await requester.request(this.Request);
|
|
4078
|
+
const isSpecial = isSpecialResponse(rawResponse);
|
|
4079
|
+
const response = await applyStrategies(
|
|
4080
|
+
rawResponse,
|
|
4081
|
+
strategies,
|
|
4082
|
+
"response"
|
|
3902
4083
|
);
|
|
3903
|
-
if (!
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
const strategies = this.getStrategies(target, propertyKey);
|
|
3919
|
-
const params = this.buildRequestArgs(target, propertyKey, args);
|
|
3920
|
-
request = { ...request, ...params };
|
|
3921
|
-
enableLog && console.log("构建请求参数:", request);
|
|
3922
|
-
const requestStrategies = strategies.filter(
|
|
3923
|
-
(strategy) => strategy.applyRequest
|
|
3924
|
-
);
|
|
3925
|
-
request = await this.applyStrategies(
|
|
3926
|
-
request,
|
|
3927
|
-
requestStrategies,
|
|
3928
|
-
"request"
|
|
3929
|
-
);
|
|
3930
|
-
const hitSource = this.getHitSource(target, propertyKey);
|
|
3931
|
-
enableLog && console.log("hitSource:", hitSource);
|
|
3932
|
-
if (typeof hitSource == "string") {
|
|
3933
|
-
await this.setHitSource(request, hitSource);
|
|
3934
|
-
}
|
|
3935
|
-
if (hitSource !== null) {
|
|
3936
|
-
const cachedResponse = await this.getCache(request, strategies);
|
|
3937
|
-
if (cachedResponse) {
|
|
3938
|
-
enableLog && console.warn("数据从缓存获取");
|
|
3939
|
-
return this.handleResponse(cachedResponse, true);
|
|
3940
|
-
}
|
|
3941
|
-
}
|
|
3942
|
-
const onUploadProgress = Reflect.getMetadata(
|
|
3943
|
-
UPLOAD_PROGRESS_KEY,
|
|
3944
|
-
target,
|
|
3945
|
-
propertyKey
|
|
3946
|
-
);
|
|
3947
|
-
const onDownloadProgress = Reflect.getMetadata(
|
|
3948
|
-
DOWNLOAD_PROGRESS_KEY,
|
|
3949
|
-
target,
|
|
3950
|
-
propertyKey
|
|
3951
|
-
);
|
|
3952
|
-
try {
|
|
3953
|
-
enableLog && console.log("send request:", request);
|
|
3954
|
-
const response = await this.axiosInstance({
|
|
3955
|
-
...request,
|
|
3956
|
-
onUploadProgress,
|
|
3957
|
-
onDownloadProgress
|
|
3958
|
-
});
|
|
3959
|
-
const responseStrategies = strategies.filter(
|
|
3960
|
-
(strategy) => strategy.applyResponse
|
|
3961
|
-
);
|
|
3962
|
-
const strategyResponse = await this.applyStrategies(
|
|
3963
|
-
response,
|
|
3964
|
-
responseStrategies,
|
|
3965
|
-
"response"
|
|
3966
|
-
);
|
|
3967
|
-
hitSource !== null && this.setCache(request, response);
|
|
3968
|
-
this.expireCache(propertyKey);
|
|
3969
|
-
return this.handleResponse(strategyResponse, false);
|
|
3970
|
-
} catch (error) {
|
|
3971
|
-
return this.handleError(error);
|
|
3972
|
-
}
|
|
3973
|
-
};
|
|
4084
|
+
if (!isNoCache) {
|
|
4085
|
+
!isSpecial && await this.setCacheData(serverName, response);
|
|
4086
|
+
}
|
|
4087
|
+
this.Response = response;
|
|
4088
|
+
this.applyHitSource(serverName);
|
|
4089
|
+
this.emit("success", response.data);
|
|
4090
|
+
this.emit("finish", response.data);
|
|
4091
|
+
if (isSpecial) return response;
|
|
4092
|
+
return response.data;
|
|
4093
|
+
} catch (error) {
|
|
4094
|
+
this.emit("error", error);
|
|
4095
|
+
this.emit("finish", error);
|
|
4096
|
+
console.error(error);
|
|
4097
|
+
this.Error = error;
|
|
4098
|
+
return error;
|
|
3974
4099
|
}
|
|
3975
4100
|
});
|
|
4101
|
+
this.apiInstance = apiInstance;
|
|
4102
|
+
this.target = target;
|
|
4103
|
+
this.propertyKey = propertyKey.toString();
|
|
4104
|
+
this.Args = args ?? [];
|
|
4105
|
+
this.init();
|
|
4106
|
+
console.log("url:", this.Url);
|
|
4107
|
+
this.eventMap = /* @__PURE__ */ new Map();
|
|
4108
|
+
this.onceWrapperMap = /* @__PURE__ */ new Map();
|
|
4109
|
+
this.eventMap.set("success", /* @__PURE__ */ new Set());
|
|
4110
|
+
this.eventMap.set("hitCache", /* @__PURE__ */ new Set());
|
|
4111
|
+
this.eventMap.set("error", /* @__PURE__ */ new Set());
|
|
4112
|
+
this.eventMap.set("finish", /* @__PURE__ */ new Set());
|
|
4113
|
+
}
|
|
4114
|
+
init() {
|
|
4115
|
+
const methodOptions = this.getMethodOptions();
|
|
4116
|
+
if (!methodOptions) {
|
|
4117
|
+
throw new Error("Create SnailMethod must be used for decoration");
|
|
4118
|
+
}
|
|
4119
|
+
const { path, method, name } = methodOptions;
|
|
4120
|
+
this.Name = name ?? this.propertyKey;
|
|
4121
|
+
this.Path = path;
|
|
4122
|
+
this.Method = method;
|
|
4123
|
+
this.createRequest();
|
|
4124
|
+
this.initUrl();
|
|
4125
|
+
this.initVersion();
|
|
4126
|
+
}
|
|
4127
|
+
initUrl() {
|
|
4128
|
+
const url = this.Path == "" ? this.apiInstance.url || "" : `${this.apiInstance.url || ""}${resolveUrl(this.Path)}`;
|
|
4129
|
+
this.Url = url;
|
|
4130
|
+
}
|
|
4131
|
+
initVersion() {
|
|
4132
|
+
const version = Reflect.getMetadata(
|
|
4133
|
+
VERSION_KEY,
|
|
4134
|
+
this.target,
|
|
4135
|
+
this.propertyKey
|
|
4136
|
+
);
|
|
4137
|
+
this.Version = version ?? this.apiInstance.version;
|
|
4138
|
+
if (!this.Version) return;
|
|
4139
|
+
const [serverName] = this.apiInstance.name.split(".");
|
|
4140
|
+
const versioningOptions = VersioningMap.get(serverName);
|
|
4141
|
+
if (!versioningOptions) {
|
|
4142
|
+
console.warn("Version Manager not configured");
|
|
4143
|
+
console.log(
|
|
4144
|
+
"Please use @Versioning() to configure the version manager on the ` SnailServer ` class"
|
|
4145
|
+
);
|
|
4146
|
+
return;
|
|
4147
|
+
}
|
|
4148
|
+
const versioningResult = applyVersioning(this.Version, versioningOptions);
|
|
4149
|
+
if (!versioningResult) {
|
|
4150
|
+
console.warn("Apply Versioning error");
|
|
4151
|
+
return;
|
|
4152
|
+
}
|
|
4153
|
+
const { type, result } = versioningResult;
|
|
4154
|
+
if (type === VersioningType.Uri) {
|
|
4155
|
+
this.Url = `${resolveUrl(result)}${resolveUrl(this.Url)}`;
|
|
4156
|
+
}
|
|
4157
|
+
if (type === VersioningType.Header) {
|
|
4158
|
+
this.Request.headers = {
|
|
4159
|
+
...this.Request.headers,
|
|
4160
|
+
...result
|
|
4161
|
+
};
|
|
4162
|
+
}
|
|
4163
|
+
if (type === VersioningType.Query) {
|
|
4164
|
+
this.Request.params = {
|
|
4165
|
+
...this.Request.params,
|
|
4166
|
+
...result
|
|
4167
|
+
};
|
|
4168
|
+
}
|
|
3976
4169
|
}
|
|
3977
|
-
|
|
3978
|
-
const
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
}
|
|
3988
|
-
|
|
3989
|
-
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
4170
|
+
getExpireSources() {
|
|
4171
|
+
const invalidSources = ExpireSourceMap.get(this.name) ?? /* @__PURE__ */ new Set();
|
|
4172
|
+
if (invalidSources.size === 0) return [];
|
|
4173
|
+
return Array.from(invalidSources);
|
|
4174
|
+
}
|
|
4175
|
+
createRequest() {
|
|
4176
|
+
const request = {
|
|
4177
|
+
url: this.Url,
|
|
4178
|
+
method: this.Method,
|
|
4179
|
+
timeout: this.apiInstance.timeout,
|
|
4180
|
+
data: {},
|
|
4181
|
+
params: {},
|
|
4182
|
+
headers: {}
|
|
4183
|
+
};
|
|
4184
|
+
this.Request = request;
|
|
4185
|
+
return request;
|
|
4186
|
+
}
|
|
4187
|
+
onSuccess(handler) {
|
|
4188
|
+
this.on("success", handler);
|
|
4189
|
+
}
|
|
4190
|
+
onError(handler) {
|
|
4191
|
+
this.on("error", handler);
|
|
4192
|
+
}
|
|
4193
|
+
onHitCache(handler) {
|
|
4194
|
+
this.on("hitCache", handler);
|
|
4195
|
+
}
|
|
4196
|
+
onFinish(handler) {
|
|
4197
|
+
this.on("finish", handler);
|
|
4198
|
+
}
|
|
4199
|
+
on(eventName, handler) {
|
|
4200
|
+
if (typeof handler !== "function") {
|
|
4201
|
+
throw new TypeError("Handler must be a function");
|
|
4202
|
+
}
|
|
4203
|
+
const handlers = this.eventMap.get(eventName) || /* @__PURE__ */ new Set();
|
|
4204
|
+
handlers.add(handler);
|
|
4205
|
+
this.eventMap.set(eventName, handlers);
|
|
4206
|
+
}
|
|
4207
|
+
once(eventName, handler) {
|
|
4208
|
+
const onceHandler = (data) => {
|
|
4209
|
+
try {
|
|
4210
|
+
handler.apply(this, [data]);
|
|
4211
|
+
} finally {
|
|
4212
|
+
this.off(eventName, onceHandler);
|
|
4213
|
+
this.onceWrapperMap.delete(handler);
|
|
3993
4214
|
}
|
|
4215
|
+
};
|
|
4216
|
+
this.onceWrapperMap.set(handler, onceHandler);
|
|
4217
|
+
this.on(eventName, onceHandler);
|
|
4218
|
+
}
|
|
4219
|
+
emit(eventName, ...args) {
|
|
4220
|
+
const handlers = this.eventMap.get(eventName);
|
|
4221
|
+
if (!handlers || handlers.size === 0) return false;
|
|
4222
|
+
handlers.forEach((handler) => {
|
|
4223
|
+
Promise.resolve().then(() => {
|
|
4224
|
+
handler.apply(this, args);
|
|
4225
|
+
});
|
|
3994
4226
|
});
|
|
3995
|
-
return
|
|
4227
|
+
return true;
|
|
3996
4228
|
}
|
|
3997
|
-
|
|
3998
|
-
const
|
|
3999
|
-
|
|
4229
|
+
off(eventName, handler) {
|
|
4230
|
+
const handlers = this.eventMap.get(eventName);
|
|
4231
|
+
if (!handlers) return;
|
|
4232
|
+
if (handlers.has(handler)) {
|
|
4233
|
+
handlers.delete(handler);
|
|
4234
|
+
}
|
|
4235
|
+
if (handlers.size === 0) {
|
|
4236
|
+
this.eventMap.delete(eventName);
|
|
4237
|
+
}
|
|
4238
|
+
}
|
|
4239
|
+
async getCacheData(serverName) {
|
|
4240
|
+
const methodKey = await generateCacheKey(
|
|
4241
|
+
this.name,
|
|
4242
|
+
this.version,
|
|
4243
|
+
this.Request
|
|
4244
|
+
);
|
|
4245
|
+
const cacheStorage = CacheStorageMap.get(serverName);
|
|
4246
|
+
if (!cacheStorage) {
|
|
4247
|
+
throw new Error("CacheStorage not created");
|
|
4248
|
+
}
|
|
4249
|
+
const cacheData = await cacheStorage.get(methodKey);
|
|
4250
|
+
const { error } = cacheData;
|
|
4251
|
+
if (!error) {
|
|
4252
|
+
this.enableLog() && console.warn(`[${this.Name}]`, "Cache hit");
|
|
4253
|
+
}
|
|
4254
|
+
return cacheData;
|
|
4255
|
+
}
|
|
4256
|
+
async setCacheData(serverName, responseData) {
|
|
4257
|
+
const methodKey = await generateCacheKey(
|
|
4258
|
+
this.name,
|
|
4259
|
+
this.version,
|
|
4260
|
+
this.Request
|
|
4261
|
+
);
|
|
4262
|
+
this.enableLog() && console.log("setCacheData:", methodKey);
|
|
4263
|
+
const cacheStorage = CacheStorageMap.get(serverName);
|
|
4264
|
+
if (!cacheStorage) throw new Error("CacheStorage not created");
|
|
4265
|
+
const ttl = CacheTtlMap.get(serverName);
|
|
4266
|
+
const cacheData = {
|
|
4267
|
+
data: responseData.data,
|
|
4268
|
+
exp: Date.now() + ttl * 1e3
|
|
4269
|
+
};
|
|
4270
|
+
cacheStorage.set(methodKey, cacheData);
|
|
4271
|
+
}
|
|
4272
|
+
get response() {
|
|
4273
|
+
return this.Response;
|
|
4274
|
+
}
|
|
4275
|
+
get request() {
|
|
4276
|
+
return this.Request;
|
|
4277
|
+
}
|
|
4278
|
+
get version() {
|
|
4279
|
+
return this.Version;
|
|
4280
|
+
}
|
|
4281
|
+
get name() {
|
|
4282
|
+
return `${this.apiInstance.name}.${this.Name}`;
|
|
4283
|
+
}
|
|
4284
|
+
get error() {
|
|
4285
|
+
if (this.Error) return this.Error;
|
|
4286
|
+
return null;
|
|
4287
|
+
}
|
|
4288
|
+
registerStrategies(...strategys) {
|
|
4289
|
+
this.strategies.push(...strategys);
|
|
4290
|
+
}
|
|
4291
|
+
getStrategies() {
|
|
4292
|
+
const [serverName, apiName] = this.apiInstance.name.split(".");
|
|
4293
|
+
const serverStrategies = StrategyMap.get(serverName) ?? [];
|
|
4294
|
+
const apiStrategies = StrategyMap.get(apiName) ?? [];
|
|
4000
4295
|
const methodStrategies = Reflect.getMetadata(
|
|
4001
|
-
`${STRATEGY_KEY.toString()}_${propertyKey}`,
|
|
4002
|
-
target
|
|
4296
|
+
`${STRATEGY_KEY.toString()}_${this.propertyKey}`,
|
|
4297
|
+
this.target
|
|
4003
4298
|
) || [];
|
|
4004
4299
|
const allStrategies = [
|
|
4005
|
-
...this.strategies,
|
|
4006
4300
|
...serverStrategies,
|
|
4007
|
-
...
|
|
4301
|
+
...apiStrategies,
|
|
4008
4302
|
...methodStrategies
|
|
4009
4303
|
];
|
|
4010
4304
|
return allStrategies;
|
|
4011
4305
|
}
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4306
|
+
getMethodOptions() {
|
|
4307
|
+
const methodConfig = Reflect.getMetadata(
|
|
4308
|
+
METHOD_KEY,
|
|
4309
|
+
this.target,
|
|
4310
|
+
this.propertyKey
|
|
4311
|
+
);
|
|
4312
|
+
return methodConfig;
|
|
4313
|
+
}
|
|
4314
|
+
isNoCache() {
|
|
4315
|
+
const [serverName] = this.apiInstance.name.split(".");
|
|
4316
|
+
const cacheForMap = CacheForMap.get(serverName);
|
|
4317
|
+
let flag = false;
|
|
4318
|
+
if (typeof cacheForMap === "string" && (cacheForMap.toLowerCase() === "all" || cacheForMap === this.Method)) {
|
|
4319
|
+
flag = true;
|
|
4320
|
+
}
|
|
4321
|
+
if (Array.isArray(cacheForMap)) {
|
|
4322
|
+
flag = cacheForMap.includes(this.Method);
|
|
4323
|
+
}
|
|
4324
|
+
const isMethodNoCache = Reflect.getMetadata(
|
|
4325
|
+
NO_CACHE_KEY,
|
|
4326
|
+
this.target,
|
|
4327
|
+
this.propertyKey
|
|
4328
|
+
);
|
|
4329
|
+
const isApiNoCache = this.apiInstance.noCache;
|
|
4330
|
+
const noCacheFlag = isMethodNoCache ? true : isApiNoCache ? true : false;
|
|
4331
|
+
return flag ? noCacheFlag : true;
|
|
4332
|
+
}
|
|
4333
|
+
enableLog() {
|
|
4334
|
+
return this.apiInstance.enableLog;
|
|
4335
|
+
}
|
|
4336
|
+
async applyHitSource(serverName) {
|
|
4337
|
+
const sources = this.getExpireSources();
|
|
4338
|
+
const cacheStorage = CacheStorageMap.get(serverName);
|
|
4339
|
+
if (cacheStorage) {
|
|
4340
|
+
const cacheKeys = await cacheStorage.keys();
|
|
4341
|
+
sources.map((source) => {
|
|
4342
|
+
cacheKeys.map((key) => {
|
|
4343
|
+
if (key.startsWith(source)) {
|
|
4344
|
+
this.enableLog() && console.warn(`[${this.name}]请求成功,触发清除缓存:${source}`);
|
|
4345
|
+
cacheStorage.delete(key);
|
|
4346
|
+
}
|
|
4347
|
+
});
|
|
4348
|
+
});
|
|
4349
|
+
}
|
|
4350
|
+
}
|
|
4351
|
+
applyProgress(request) {
|
|
4352
|
+
const onUploadProgress = Reflect.getMetadata(
|
|
4353
|
+
UPLOAD_PROGRESS_KEY,
|
|
4354
|
+
this.target,
|
|
4355
|
+
this.propertyKey
|
|
4356
|
+
);
|
|
4357
|
+
const onDownloadProgress = Reflect.getMetadata(
|
|
4358
|
+
DOWNLOAD_PROGRESS_KEY,
|
|
4359
|
+
this.target,
|
|
4360
|
+
this.propertyKey
|
|
4361
|
+
);
|
|
4362
|
+
return {
|
|
4363
|
+
...request,
|
|
4364
|
+
onUploadProgress,
|
|
4365
|
+
onDownloadProgress
|
|
4366
|
+
};
|
|
4367
|
+
}
|
|
4368
|
+
}
|
|
4369
|
+
const CacheStorageMap = /* @__PURE__ */ new Map();
|
|
4370
|
+
const CacheTtlMap = /* @__PURE__ */ new Map();
|
|
4371
|
+
const CacheForMap = /* @__PURE__ */ new Map();
|
|
4372
|
+
const ExpireSourceMap = /* @__PURE__ */ new Map();
|
|
4373
|
+
const AxiosInstanceMap = /* @__PURE__ */ new Map();
|
|
4374
|
+
const StrategyMap = /* @__PURE__ */ new Map();
|
|
4375
|
+
const VersioningMap = /* @__PURE__ */ new Map();
|
|
4376
|
+
const defaultCacheManageOptions = {
|
|
4377
|
+
type: CacheType.Memory,
|
|
4378
|
+
ttl: 500
|
|
4379
|
+
};
|
|
4380
|
+
const defaultServerOptions = {
|
|
4381
|
+
baseURL: "",
|
|
4382
|
+
timeout: 5e3,
|
|
4383
|
+
cacheManage: defaultCacheManageOptions,
|
|
4384
|
+
cacheFor: "get",
|
|
4385
|
+
enableLog: false
|
|
4386
|
+
};
|
|
4387
|
+
class SnailServer {
|
|
4388
|
+
constructor() {
|
|
4389
|
+
__publicField(this, "Name");
|
|
4390
|
+
__publicField(this, "BaseURL");
|
|
4391
|
+
__publicField(this, "Version");
|
|
4392
|
+
__publicField(this, "EnableLog", false);
|
|
4393
|
+
const options = this.getServerOptions();
|
|
4394
|
+
this.Name = options.name ?? this.constructor.name;
|
|
4395
|
+
this.init();
|
|
4396
|
+
}
|
|
4397
|
+
init() {
|
|
4398
|
+
const options = this.getServerOptions();
|
|
4399
|
+
const { baseURL, timeout, cacheManage, enableLog, cacheFor } = options;
|
|
4400
|
+
this.BaseURL = baseURL ?? resolveUrl("");
|
|
4401
|
+
this.initAxios({ baseURL, timeout });
|
|
4402
|
+
this.initStrategy();
|
|
4403
|
+
this.initCacheManage(cacheManage, cacheFor);
|
|
4404
|
+
this.initVersioning();
|
|
4405
|
+
this.initExpireSource(this);
|
|
4406
|
+
this.EnableLog = enableLog ?? false;
|
|
4407
|
+
this.initLog(enableLog);
|
|
4408
|
+
}
|
|
4409
|
+
initLog(enableLog) {
|
|
4410
|
+
if (enableLog) {
|
|
4411
|
+
const axiosInstance = AxiosInstanceMap.get(this.Name);
|
|
4412
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
4413
|
+
console.log("Request:", config);
|
|
4414
|
+
return config;
|
|
4415
|
+
});
|
|
4416
|
+
axiosInstance.interceptors.response.use((response) => {
|
|
4417
|
+
console.log("Response:", response);
|
|
4418
|
+
return response;
|
|
4419
|
+
});
|
|
4420
|
+
}
|
|
4421
|
+
}
|
|
4422
|
+
initStrategy() {
|
|
4423
|
+
const serverStrategies = Reflect.getMetadata(STRATEGY_KEY, this.constructor) || [];
|
|
4424
|
+
StrategyMap.set(this.Name, serverStrategies);
|
|
4018
4425
|
}
|
|
4019
|
-
|
|
4426
|
+
initVersioning() {
|
|
4020
4427
|
const versioning = Reflect.getMetadata(
|
|
4021
4428
|
VERSIONING_KEY,
|
|
4022
4429
|
this.constructor
|
|
4023
4430
|
);
|
|
4024
|
-
if (
|
|
4025
|
-
|
|
4026
|
-
const currentVersion = version || this.version || versioning.defaultVersion;
|
|
4027
|
-
const { url, headers, params } = applyVersioning(
|
|
4028
|
-
currentVersion,
|
|
4029
|
-
versioning
|
|
4030
|
-
);
|
|
4031
|
-
const versionUrl = url ? `${url}/${request.url}` : request.url;
|
|
4032
|
-
return { ...request, url: versionUrl, headers, params };
|
|
4033
|
-
}
|
|
4034
|
-
async getCache(request, strategies) {
|
|
4035
|
-
var _a;
|
|
4036
|
-
if (!this.cacheStorage) return void 0;
|
|
4037
|
-
const cacheKey = await apiKey(request);
|
|
4038
|
-
const cached = await ((_a = this.cacheStorage) == null ? void 0 : _a.get(cacheKey));
|
|
4039
|
-
if (cached && cached.error === null) {
|
|
4040
|
-
let cacheResponse = {
|
|
4041
|
-
data: cached.data,
|
|
4042
|
-
status: 304,
|
|
4043
|
-
headers: {},
|
|
4044
|
-
statusText: "get response data from cache",
|
|
4045
|
-
config: { headers: new AxiosHeaders() }
|
|
4046
|
-
};
|
|
4047
|
-
return cacheResponse;
|
|
4431
|
+
if (versioning) {
|
|
4432
|
+
VersioningMap.set(this.Name, versioning);
|
|
4048
4433
|
}
|
|
4049
|
-
return void 0;
|
|
4050
4434
|
}
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4435
|
+
registerStrategies(...strategys) {
|
|
4436
|
+
const serverStrategies = StrategyMap.get(this.Name) ?? [];
|
|
4437
|
+
serverStrategies.push(...strategys);
|
|
4438
|
+
StrategyMap.set(this.Name, serverStrategies);
|
|
4055
4439
|
}
|
|
4056
|
-
|
|
4057
|
-
const
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
);
|
|
4061
|
-
if (methodHitSource !== void 0) return methodHitSource;
|
|
4062
|
-
const apiHitSource = Reflect.getMetadata(
|
|
4063
|
-
CACHE_OPTIONS_KEY,
|
|
4064
|
-
target.constructor
|
|
4440
|
+
createApi(constructor) {
|
|
4441
|
+
const serverVersioning = Reflect.getMetadata(
|
|
4442
|
+
VERSIONING_KEY,
|
|
4443
|
+
this.constructor
|
|
4065
4444
|
);
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
async setHitSource(request, hitSource) {
|
|
4069
|
-
const cacheKeys = this.sourceMap.get(hitSource);
|
|
4070
|
-
const currentCacheKey = await apiKey(request);
|
|
4071
|
-
if (cacheKeys) {
|
|
4072
|
-
cacheKeys.push(currentCacheKey);
|
|
4073
|
-
this.sourceMap.set(hitSource, cacheKeys);
|
|
4074
|
-
} else {
|
|
4075
|
-
this.sourceMap.set(hitSource, [currentCacheKey]);
|
|
4076
|
-
}
|
|
4077
|
-
}
|
|
4078
|
-
async setCache(request, response) {
|
|
4079
|
-
var _a;
|
|
4080
|
-
const cacheKey = await apiKey(request);
|
|
4081
|
-
await ((_a = this.cacheStorage) == null ? void 0 : _a.set(cacheKey, response.data));
|
|
4082
|
-
}
|
|
4083
|
-
async expireCache(hitSource) {
|
|
4084
|
-
const keys = this.sourceMap.get(hitSource);
|
|
4085
|
-
if (keys) {
|
|
4086
|
-
Promise.all(
|
|
4087
|
-
keys.map(async (key) => {
|
|
4088
|
-
var _a;
|
|
4089
|
-
await ((_a = this.cacheStorage) == null ? void 0 : _a.delete(key));
|
|
4090
|
-
})
|
|
4091
|
-
);
|
|
4445
|
+
if (serverVersioning) {
|
|
4446
|
+
this.Version = serverVersioning.defaultVersion || "";
|
|
4092
4447
|
}
|
|
4448
|
+
const apiInstanceOptions = {
|
|
4449
|
+
serverInstance: this,
|
|
4450
|
+
enableLog: this.EnableLog
|
|
4451
|
+
};
|
|
4452
|
+
const apiInstance = new constructor(apiInstanceOptions);
|
|
4453
|
+
this.initExpireSource(apiInstance);
|
|
4454
|
+
return new Proxy(apiInstance, {
|
|
4455
|
+
get: (target, propertyKey) => {
|
|
4456
|
+
if (typeof propertyKey !== "string")
|
|
4457
|
+
return target[propertyKey];
|
|
4458
|
+
const methodConfig = Reflect.getMetadata(
|
|
4459
|
+
METHOD_KEY,
|
|
4460
|
+
target,
|
|
4461
|
+
propertyKey
|
|
4462
|
+
);
|
|
4463
|
+
if (!methodConfig) return target[propertyKey];
|
|
4464
|
+
return (...args) => {
|
|
4465
|
+
const method = new SnailMethod(apiInstance, target, propertyKey, [
|
|
4466
|
+
...args
|
|
4467
|
+
]);
|
|
4468
|
+
this.initExpireSource(method, propertyKey);
|
|
4469
|
+
return method;
|
|
4470
|
+
};
|
|
4471
|
+
}
|
|
4472
|
+
});
|
|
4093
4473
|
}
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4474
|
+
initCacheManage(options, cacheFor) {
|
|
4475
|
+
const noCache = Reflect.getMetadata(
|
|
4476
|
+
NO_CACHE_KEY,
|
|
4097
4477
|
this.constructor
|
|
4098
4478
|
);
|
|
4479
|
+
if (noCache && options !== defaultCacheManageOptions) {
|
|
4480
|
+
console.warn("you configured cache manage, but you set noCache");
|
|
4481
|
+
return;
|
|
4482
|
+
}
|
|
4483
|
+
const cacheStorage = CacheStorageMap.get(this.Name);
|
|
4484
|
+
if (!cacheStorage && options !== void 0) {
|
|
4485
|
+
const storage = createCache(options);
|
|
4486
|
+
CacheStorageMap.set(this.Name, storage);
|
|
4487
|
+
CacheTtlMap.set(this.Name, options.ttl || 500);
|
|
4488
|
+
}
|
|
4489
|
+
if (cacheFor) {
|
|
4490
|
+
CacheForMap.set(this.Name, cacheFor);
|
|
4491
|
+
}
|
|
4099
4492
|
}
|
|
4100
|
-
|
|
4101
|
-
|
|
4493
|
+
initExpireSource(target, propertyKey) {
|
|
4494
|
+
if (propertyKey) {
|
|
4495
|
+
const expireSource = Reflect.getMetadata(
|
|
4496
|
+
CACHE_EXPIRE_SOURCE_KEY,
|
|
4497
|
+
target.target,
|
|
4498
|
+
propertyKey
|
|
4499
|
+
);
|
|
4500
|
+
if (expireSource && expireSource.length > 0) {
|
|
4501
|
+
expireSource.map((source) => {
|
|
4502
|
+
const invalidSources = ExpireSourceMap.get(source) ?? /* @__PURE__ */ new Set();
|
|
4503
|
+
invalidSources.add(target.name);
|
|
4504
|
+
ExpireSourceMap.set(source, invalidSources);
|
|
4505
|
+
});
|
|
4506
|
+
}
|
|
4507
|
+
return;
|
|
4508
|
+
}
|
|
4509
|
+
const expireSources = Reflect.getMetadata(
|
|
4510
|
+
CACHE_EXPIRE_SOURCE_KEY,
|
|
4511
|
+
target.constructor
|
|
4512
|
+
) ?? [];
|
|
4513
|
+
if (expireSources.length > 0) {
|
|
4514
|
+
expireSources.map((source) => {
|
|
4515
|
+
const invalidSources = ExpireSourceMap.get(source) ?? /* @__PURE__ */ new Set();
|
|
4516
|
+
invalidSources.add(target.name);
|
|
4517
|
+
ExpireSourceMap.set(source, invalidSources);
|
|
4518
|
+
});
|
|
4519
|
+
}
|
|
4102
4520
|
}
|
|
4103
4521
|
initAxios(config) {
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4522
|
+
const axiosInstance = AxiosInstanceMap.get(this.Name);
|
|
4523
|
+
if (!axiosInstance) {
|
|
4524
|
+
const axiosInstance2 = axios.create({
|
|
4525
|
+
baseURL: resolveUrl(config.baseURL),
|
|
4107
4526
|
timeout: config.timeout
|
|
4108
4527
|
});
|
|
4528
|
+
AxiosInstanceMap.set(this.Name, axiosInstance2);
|
|
4109
4529
|
}
|
|
4110
4530
|
}
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4531
|
+
getServerOptions() {
|
|
4532
|
+
const serverOptions = Reflect.getMetadata(
|
|
4533
|
+
SERVER_CONFIG_KEY,
|
|
4534
|
+
this.constructor
|
|
4535
|
+
);
|
|
4536
|
+
if (!serverOptions) {
|
|
4537
|
+
throw new Error(
|
|
4538
|
+
"Create SnailServer must be used for @Server() decoration"
|
|
4539
|
+
);
|
|
4117
4540
|
}
|
|
4118
|
-
|
|
4541
|
+
const options = Object.assign({}, defaultServerOptions, serverOptions);
|
|
4542
|
+
return options;
|
|
4119
4543
|
}
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
error: null,
|
|
4124
|
-
hitCache
|
|
4125
|
-
};
|
|
4544
|
+
createSse(constructor) {
|
|
4545
|
+
const sseInstance = new constructor(this);
|
|
4546
|
+
return sseInstance;
|
|
4126
4547
|
}
|
|
4127
|
-
|
|
4128
|
-
return
|
|
4129
|
-
data: null,
|
|
4130
|
-
error
|
|
4131
|
-
};
|
|
4548
|
+
get version() {
|
|
4549
|
+
return this.Version;
|
|
4132
4550
|
}
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
const serverConfig = this.getServerConfig();
|
|
4136
|
-
const { baseURL, enableLog } = serverConfig;
|
|
4137
|
-
const apiConfig = this.getApiConfig(constructor);
|
|
4138
|
-
enableLog && console.log("ApiConfig:", apiConfig);
|
|
4139
|
-
this.version = apiConfig.version;
|
|
4140
|
-
return new Proxy(instance, {
|
|
4141
|
-
get: (target, propertyKey) => {
|
|
4142
|
-
enableLog && console.log("proxy:", target, "|", propertyKey);
|
|
4143
|
-
if (typeof propertyKey !== "string") return;
|
|
4144
|
-
const sseOption = Reflect.getMetadata(
|
|
4145
|
-
EVENT_SOURCE_OPTION_KEY,
|
|
4146
|
-
target,
|
|
4147
|
-
propertyKey
|
|
4148
|
-
);
|
|
4149
|
-
if (sseOption) {
|
|
4150
|
-
const url = sseOption.path == "" ? apiConfig.url : apiConfig.url + `/${sseOption.path}`;
|
|
4151
|
-
const { url: versionUrl } = this.applyVersion(
|
|
4152
|
-
{ url },
|
|
4153
|
-
target,
|
|
4154
|
-
propertyKey
|
|
4155
|
-
);
|
|
4156
|
-
const sseUrl = this.generateSseUrl(baseURL, versionUrl || url);
|
|
4157
|
-
enableLog && console.log("sse-url:", sseUrl);
|
|
4158
|
-
return () => this.initSse(sseUrl, sseOption, target);
|
|
4159
|
-
}
|
|
4160
|
-
}
|
|
4161
|
-
});
|
|
4551
|
+
get name() {
|
|
4552
|
+
return this.Name;
|
|
4162
4553
|
}
|
|
4163
|
-
|
|
4164
|
-
return
|
|
4165
|
-
const eventSource = new EventSource(url, {
|
|
4166
|
-
withCredentials: options.withCredentials ? options.withCredentials : false
|
|
4167
|
-
});
|
|
4168
|
-
this.eventSource = eventSource;
|
|
4169
|
-
this.setSse(target);
|
|
4170
|
-
this.registerSseEvent(target);
|
|
4171
|
-
return {
|
|
4172
|
-
eventSource,
|
|
4173
|
-
close: eventSource.close
|
|
4174
|
-
};
|
|
4175
|
-
};
|
|
4554
|
+
get enableLog() {
|
|
4555
|
+
return this.EnableLog;
|
|
4176
4556
|
}
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4557
|
+
get baseUrl() {
|
|
4558
|
+
return this.BaseURL;
|
|
4559
|
+
}
|
|
4560
|
+
}
|
|
4561
|
+
class SnailApi {
|
|
4562
|
+
constructor(options) {
|
|
4563
|
+
__publicField(this, "Name");
|
|
4564
|
+
__publicField(this, "serverInstance");
|
|
4565
|
+
__publicField(this, "Version");
|
|
4566
|
+
__publicField(this, "Url");
|
|
4567
|
+
__publicField(this, "Timeout");
|
|
4568
|
+
__publicField(this, "EnableLog");
|
|
4569
|
+
const apiConfig = this.getApiConfig();
|
|
4570
|
+
if (!apiConfig) {
|
|
4571
|
+
throw new Error("Create SnailApi must be used for @Api() decoration");
|
|
4572
|
+
}
|
|
4573
|
+
const { serverInstance, enableLog } = options;
|
|
4574
|
+
this.serverInstance = serverInstance;
|
|
4575
|
+
this.EnableLog = enableLog ? true : false;
|
|
4576
|
+
this.init();
|
|
4577
|
+
}
|
|
4578
|
+
init() {
|
|
4579
|
+
const apiConfig = this.getApiConfig();
|
|
4580
|
+
this.initName();
|
|
4581
|
+
this.initStrategy();
|
|
4582
|
+
this.Version = apiConfig.version ?? this.serverInstance.version;
|
|
4583
|
+
this.Url = apiConfig.url;
|
|
4584
|
+
this.Timeout = apiConfig.timeout;
|
|
4585
|
+
}
|
|
4586
|
+
initStrategy() {
|
|
4587
|
+
const serverStrategies = Reflect.getMetadata(STRATEGY_KEY, this.constructor) || [];
|
|
4588
|
+
StrategyMap.set(this.Name, serverStrategies);
|
|
4589
|
+
}
|
|
4590
|
+
registerStrategies(...strategys) {
|
|
4591
|
+
const serverStrategies = StrategyMap.get(this.Name) ?? [];
|
|
4592
|
+
serverStrategies.push(...strategys);
|
|
4593
|
+
StrategyMap.set(this.Name, serverStrategies);
|
|
4594
|
+
}
|
|
4595
|
+
initName() {
|
|
4596
|
+
const { name } = this.getApiConfig();
|
|
4597
|
+
const apiName = name ?? this.constructor.name;
|
|
4598
|
+
this.Name = `${this.serverInstance.name}.${apiName}`;
|
|
4599
|
+
}
|
|
4600
|
+
getApiConfig() {
|
|
4601
|
+
return Reflect.getMetadata(
|
|
4602
|
+
API_CONFIG_KEY,
|
|
4603
|
+
this.constructor
|
|
4604
|
+
);
|
|
4605
|
+
}
|
|
4606
|
+
isNoCache() {
|
|
4607
|
+
const isServerNoCache = Reflect.getMetadata(
|
|
4608
|
+
NO_CACHE_KEY,
|
|
4609
|
+
this.serverInstance.constructor
|
|
4610
|
+
);
|
|
4611
|
+
const isApiNoCache = Reflect.getMetadata(
|
|
4612
|
+
NO_CACHE_KEY,
|
|
4613
|
+
this.constructor
|
|
4614
|
+
);
|
|
4615
|
+
return isServerNoCache || isApiNoCache;
|
|
4616
|
+
}
|
|
4617
|
+
get version() {
|
|
4618
|
+
return this.Version;
|
|
4619
|
+
}
|
|
4620
|
+
get url() {
|
|
4621
|
+
return this.Url;
|
|
4622
|
+
}
|
|
4623
|
+
get name() {
|
|
4624
|
+
return this.Name;
|
|
4625
|
+
}
|
|
4626
|
+
get noCache() {
|
|
4627
|
+
return this.isNoCache();
|
|
4628
|
+
}
|
|
4629
|
+
get enableLog() {
|
|
4630
|
+
return this.EnableLog;
|
|
4631
|
+
}
|
|
4632
|
+
get timeout() {
|
|
4633
|
+
return this.Timeout;
|
|
4634
|
+
}
|
|
4635
|
+
}
|
|
4636
|
+
const EVENT_SOURCE_OPTION_KEY = Symbol("SNAIL_EVENT_SOURCE_OPTION_KEY");
|
|
4637
|
+
const Sse = (path, options) => {
|
|
4638
|
+
return (target) => {
|
|
4639
|
+
Reflect.defineMetadata(
|
|
4640
|
+
EVENT_SOURCE_OPTION_KEY,
|
|
4641
|
+
{
|
|
4642
|
+
path,
|
|
4643
|
+
...options
|
|
4644
|
+
},
|
|
4645
|
+
target
|
|
4646
|
+
);
|
|
4647
|
+
};
|
|
4648
|
+
};
|
|
4649
|
+
const EVENT_SOURCE_EVENTS_KEY = Symbol("EVENT_SOURCE_EVENTS_KEY");
|
|
4650
|
+
const SseEvent = (eventName, options) => {
|
|
4651
|
+
return (target, propertyKey) => {
|
|
4652
|
+
const events = Reflect.getMetadata(EVENT_SOURCE_EVENTS_KEY, target) || [];
|
|
4653
|
+
events.push({
|
|
4654
|
+
eventName: eventName ? eventName : "message",
|
|
4655
|
+
emit: target[propertyKey],
|
|
4656
|
+
options
|
|
4657
|
+
});
|
|
4658
|
+
Reflect.defineMetadata(EVENT_SOURCE_EVENTS_KEY, events, target);
|
|
4659
|
+
};
|
|
4660
|
+
};
|
|
4661
|
+
const EVENT_SOURCE_OPEN_KEY = Symbol("EVENT_SOURCE_OPEN_KEY");
|
|
4662
|
+
const OnSseOpen = () => {
|
|
4663
|
+
return (target, propertyKey) => {
|
|
4664
|
+
Reflect.defineMetadata(EVENT_SOURCE_OPEN_KEY, target[propertyKey], target);
|
|
4665
|
+
};
|
|
4666
|
+
};
|
|
4667
|
+
const EVENT_SOURCE_ERROR_KEY = Symbol("EVENT_SOURCE_ERROR_KEY");
|
|
4668
|
+
const OnSseError = () => {
|
|
4669
|
+
return (target, propertyKey) => {
|
|
4670
|
+
Reflect.defineMetadata(EVENT_SOURCE_ERROR_KEY, target[propertyKey], target);
|
|
4671
|
+
};
|
|
4672
|
+
};
|
|
4673
|
+
const defaultSseOptions = {
|
|
4674
|
+
path: "",
|
|
4675
|
+
withCredentials: false
|
|
4676
|
+
};
|
|
4677
|
+
class SnailSse {
|
|
4678
|
+
constructor(server) {
|
|
4679
|
+
__publicField(this, "_serverInstance");
|
|
4680
|
+
__publicField(this, "_baseUrl");
|
|
4681
|
+
__publicField(this, "_url");
|
|
4682
|
+
__publicField(this, "_version");
|
|
4683
|
+
__publicField(this, "_withCredentials");
|
|
4684
|
+
__publicField(this, "_eventSource");
|
|
4685
|
+
this._serverInstance = server;
|
|
4686
|
+
this._baseUrl = server.baseUrl;
|
|
4687
|
+
const options = this.getSseOptions();
|
|
4688
|
+
const { path, withCredentials, version } = options;
|
|
4689
|
+
this._url = resolveUrl(path);
|
|
4690
|
+
this._version = version;
|
|
4691
|
+
this._withCredentials = withCredentials ? withCredentials : false;
|
|
4692
|
+
this.initVersion();
|
|
4693
|
+
}
|
|
4694
|
+
getSseOptions() {
|
|
4695
|
+
const sseOptions = Reflect.getMetadata(
|
|
4696
|
+
EVENT_SOURCE_OPTION_KEY,
|
|
4697
|
+
this.constructor
|
|
4698
|
+
);
|
|
4699
|
+
if (!sseOptions) {
|
|
4700
|
+
throw new Error(
|
|
4701
|
+
"you need to use @Sse() decorator to decorate your class"
|
|
4702
|
+
);
|
|
4181
4703
|
}
|
|
4182
|
-
const
|
|
4183
|
-
|
|
4184
|
-
|
|
4704
|
+
const options = Object.assign({}, defaultSseOptions, sseOptions);
|
|
4705
|
+
return options;
|
|
4706
|
+
}
|
|
4707
|
+
initVersion() {
|
|
4708
|
+
const versioningOptions = VersioningMap.get(this._serverInstance.name);
|
|
4709
|
+
if (!versioningOptions || !this._version) return;
|
|
4710
|
+
const versioningResult = applyVersioning(this._version, versioningOptions);
|
|
4711
|
+
if (!versioningResult) return;
|
|
4712
|
+
const { type, result } = versioningResult;
|
|
4713
|
+
if (type === VersioningType.Uri) {
|
|
4714
|
+
const url = `${resolveUrl(result)}${this._url}`;
|
|
4715
|
+
this._url = url;
|
|
4716
|
+
return;
|
|
4717
|
+
}
|
|
4718
|
+
if (type === VersioningType.Query) {
|
|
4719
|
+
const k = Object.keys(result)[0];
|
|
4720
|
+
const v = result[k];
|
|
4721
|
+
this._url = `resolveUrl(${this._url}?${k}=${v})`;
|
|
4722
|
+
return;
|
|
4185
4723
|
}
|
|
4186
4724
|
}
|
|
4187
|
-
|
|
4725
|
+
open() {
|
|
4726
|
+
const url = `${this._baseUrl}${this._url}`;
|
|
4727
|
+
const eventSource = new EventSource(url, {
|
|
4728
|
+
withCredentials: this._withCredentials
|
|
4729
|
+
});
|
|
4730
|
+
this._eventSource = eventSource;
|
|
4731
|
+
this.setEvent();
|
|
4732
|
+
this.registerSseEvent();
|
|
4733
|
+
}
|
|
4734
|
+
close() {
|
|
4188
4735
|
const eventSource = this.eventSource;
|
|
4189
4736
|
if (!eventSource) return;
|
|
4737
|
+
eventSource.close();
|
|
4738
|
+
}
|
|
4739
|
+
registerSseEvent() {
|
|
4740
|
+
const eventSource = this._eventSource;
|
|
4741
|
+
if (!eventSource) return;
|
|
4190
4742
|
const events = Reflect.getMetadata(
|
|
4191
4743
|
EVENT_SOURCE_EVENTS_KEY,
|
|
4192
|
-
|
|
4744
|
+
this.constructor
|
|
4193
4745
|
);
|
|
4194
4746
|
events.map((event) => {
|
|
4195
4747
|
eventSource.addEventListener(event.eventName, event.emit, event.options);
|
|
4196
4748
|
});
|
|
4197
4749
|
}
|
|
4750
|
+
setEvent() {
|
|
4751
|
+
const onOpenFunc = Reflect.getMetadata(
|
|
4752
|
+
EVENT_SOURCE_OPEN_KEY,
|
|
4753
|
+
this.constructor
|
|
4754
|
+
);
|
|
4755
|
+
if (typeof onOpenFunc == "function") {
|
|
4756
|
+
this._eventSource && (this._eventSource.onopen = onOpenFunc);
|
|
4757
|
+
}
|
|
4758
|
+
const onErrorFunc = Reflect.getMetadata(
|
|
4759
|
+
EVENT_SOURCE_ERROR_KEY,
|
|
4760
|
+
this.constructor
|
|
4761
|
+
);
|
|
4762
|
+
if (typeof onErrorFunc == "function") {
|
|
4763
|
+
this._eventSource && (this._eventSource.onerror = onErrorFunc);
|
|
4764
|
+
}
|
|
4765
|
+
}
|
|
4766
|
+
on(eventName, callback, options) {
|
|
4767
|
+
this._eventSource && this._eventSource.addEventListener(eventName, callback, options);
|
|
4768
|
+
}
|
|
4769
|
+
off(eventName, callback, options) {
|
|
4770
|
+
this._eventSource && this._eventSource.removeEventListener(eventName, callback, options);
|
|
4771
|
+
}
|
|
4772
|
+
get url() {
|
|
4773
|
+
return this._url;
|
|
4774
|
+
}
|
|
4775
|
+
get eventSource() {
|
|
4776
|
+
return this._eventSource;
|
|
4777
|
+
}
|
|
4198
4778
|
}
|
|
4199
4779
|
exports2.Api = Api;
|
|
4200
|
-
exports2.
|
|
4780
|
+
exports2.CacheStorageAdapter = CacheStorageAdapter;
|
|
4201
4781
|
exports2.CacheType = CacheType;
|
|
4202
4782
|
exports2.Data = Data;
|
|
4203
4783
|
exports2.Delete = Delete;
|
|
4784
|
+
exports2.EventType = EventType;
|
|
4204
4785
|
exports2.Get = Get;
|
|
4205
4786
|
exports2.Head = Head;
|
|
4787
|
+
exports2.HitSource = HitSource;
|
|
4788
|
+
exports2.NoCache = NoCache;
|
|
4206
4789
|
exports2.OnSseError = OnSseError;
|
|
4207
4790
|
exports2.OnSseOpen = OnSseOpen;
|
|
4208
4791
|
exports2.Options = Options;
|
|
@@ -4210,19 +4793,30 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4210
4793
|
exports2.Patch = Patch;
|
|
4211
4794
|
exports2.Post = Post;
|
|
4212
4795
|
exports2.Put = Put;
|
|
4213
|
-
exports2.
|
|
4214
|
-
exports2.
|
|
4796
|
+
exports2.Query = Query;
|
|
4797
|
+
exports2.RequestMethodEnum = RequestMethodEnum;
|
|
4215
4798
|
exports2.Server = Server;
|
|
4216
|
-
exports2.
|
|
4799
|
+
exports2.SnailApi = SnailApi;
|
|
4800
|
+
exports2.SnailMethod = SnailMethod;
|
|
4801
|
+
exports2.SnailPass = SnailPass;
|
|
4802
|
+
exports2.SnailServer = SnailServer;
|
|
4803
|
+
exports2.SnailSse = SnailSse;
|
|
4217
4804
|
exports2.Sse = Sse;
|
|
4218
4805
|
exports2.SseEvent = SseEvent;
|
|
4806
|
+
exports2.SseEventListener = SseEventListener;
|
|
4807
|
+
exports2.SseOptions = SseOptions;
|
|
4219
4808
|
exports2.Strategy = Strategy;
|
|
4220
4809
|
exports2.UseStrategy = UseStrategy;
|
|
4221
4810
|
exports2.Version = Version;
|
|
4222
4811
|
exports2.Versioning = Versioning;
|
|
4223
4812
|
exports2.VersioningType = VersioningType;
|
|
4224
|
-
exports2.
|
|
4813
|
+
exports2.applyStrategies = applyStrategies;
|
|
4814
|
+
exports2.buildRequestArgs = buildRequestArgs;
|
|
4815
|
+
exports2.generateCacheKey = generateCacheKey;
|
|
4225
4816
|
exports2.generateShortUniqueHash = generateShortUniqueHash;
|
|
4817
|
+
exports2.isSpecialResponse = isSpecialResponse;
|
|
4226
4818
|
exports2.recordToString = recordToString;
|
|
4819
|
+
exports2.replacePlaceholders = replacePlaceholders;
|
|
4820
|
+
exports2.resolveUrl = resolveUrl;
|
|
4227
4821
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
4228
4822
|
});
|