@jzwd/vnd-design 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +259 -2
- package/dist/file-center/api/endpoints.d.ts +42 -0
- package/dist/file-center/api/endpoints.js +45 -0
- package/dist/file-center/api/error.d.ts +6 -0
- package/dist/file-center/api/error.js +34 -0
- package/dist/file-center/api/index.d.ts +64 -0
- package/dist/file-center/api/index.js +238 -0
- package/dist/file-center/api/request.d.ts +28 -0
- package/dist/file-center/api/request.js +170 -0
- package/dist/file-center/api/token.d.ts +10 -0
- package/dist/file-center/api/token.js +122 -0
- package/dist/file-center/components/FcFileList/index.d.ts +39 -0
- package/dist/file-center/components/FcFileList/index.js +804 -0
- package/dist/file-center/components/FcFileList/style.css +106 -0
- package/dist/file-center/components/FcImage/index.d.ts +30 -0
- package/dist/file-center/components/FcImage/index.js +144 -0
- package/dist/file-center/components/FcImage/style.css +14 -0
- package/dist/file-center/components/FcUpload/index.d.ts +5 -0
- package/dist/file-center/components/FcUpload/index.js +642 -0
- package/dist/file-center/components/FcUpload/interface.d.ts +65 -0
- package/dist/file-center/components/FcUpload/interface.js +1 -0
- package/dist/file-center/components/FcUpload/style.css +279 -0
- package/dist/file-center/context.d.ts +22 -0
- package/dist/file-center/context.js +22 -0
- package/dist/file-center/index.d.ts +26 -0
- package/dist/file-center/index.js +21 -0
- package/dist/file-center/types.d.ts +144 -0
- package/dist/file-center/types.js +1 -0
- package/dist/file-center/upload.d.ts +35 -0
- package/dist/file-center/upload.js +295 -0
- package/dist/file-center/utils/common.d.ts +10 -0
- package/dist/file-center/utils/common.js +165 -0
- package/dist/file-center/utils/compress.d.ts +18 -0
- package/dist/file-center/utils/compress.js +108 -0
- package/dist/file-center/utils/md5.d.ts +5 -0
- package/dist/file-center/utils/md5.js +41 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +15 -0
- package/dist/mobile/FcImage/index.d.ts +26 -0
- package/dist/mobile/FcImage/index.js +114 -0
- package/dist/mobile/FcImage/style.css +11 -0
- package/dist/mobile/FcUpload/index.d.ts +74 -0
- package/dist/mobile/FcUpload/index.js +539 -0
- package/dist/mobile/FcUpload/style.css +233 -0
- package/dist/mobile/index.d.ts +13 -0
- package/dist/mobile/index.js +9 -0
- package/package.json +70 -4
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
4
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
5
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
7
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
8
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
9
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
10
|
+
import { FC_ENDPOINTS } from "./endpoints";
|
|
11
|
+
import { FcError } from "./error";
|
|
12
|
+
import { fcRequest } from "./request";
|
|
13
|
+
import { ensureToken, invalidateToken } from "./token";
|
|
14
|
+
export { FC_ENDPOINTS, DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_THRESHOLD } from "./endpoints";
|
|
15
|
+
export { FcError } from "./error";
|
|
16
|
+
export { fcRequest } from "./request";
|
|
17
|
+
export { ensureToken, invalidateToken } from "./token";
|
|
18
|
+
|
|
19
|
+
/** 带表单体的请求(上传类接口共用):自动携带令牌,401 刷新重试一次 */
|
|
20
|
+
export function fcFormRequest(_x, _x2, _x3) {
|
|
21
|
+
return _fcFormRequest.apply(this, arguments);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* ---------------- OPEN-4 秒传校验 ---------------- */
|
|
25
|
+
function _fcFormRequest() {
|
|
26
|
+
_fcFormRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ctx, path, form) {
|
|
27
|
+
var options,
|
|
28
|
+
signal,
|
|
29
|
+
onProgress,
|
|
30
|
+
doSend,
|
|
31
|
+
token,
|
|
32
|
+
freshToken,
|
|
33
|
+
_args = arguments;
|
|
34
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
35
|
+
while (1) switch (_context.prev = _context.next) {
|
|
36
|
+
case 0:
|
|
37
|
+
options = _args.length > 3 && _args[3] !== undefined ? _args[3] : {};
|
|
38
|
+
signal = options.signal, onProgress = options.onProgress;
|
|
39
|
+
doSend = function doSend(token) {
|
|
40
|
+
return new Promise(function (resolve, reject) {
|
|
41
|
+
var xhr = new XMLHttpRequest();
|
|
42
|
+
xhr.open('POST', "".concat(ctx.baseUrl).concat(path));
|
|
43
|
+
xhr.setRequestHeader('Authorization', "Bearer ".concat(token));
|
|
44
|
+
xhr.responseType = 'text';
|
|
45
|
+
if (signal) {
|
|
46
|
+
if (signal.aborted) {
|
|
47
|
+
xhr.abort();
|
|
48
|
+
reject(new FcError('上传已取消', 'ABORT'));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
signal.addEventListener('abort', function () {
|
|
52
|
+
return xhr.abort();
|
|
53
|
+
}, {
|
|
54
|
+
once: true
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (onProgress) {
|
|
58
|
+
xhr.upload.addEventListener('progress', function (event) {
|
|
59
|
+
if (event.lengthComputable) {
|
|
60
|
+
onProgress(Math.round(event.loaded / event.total * 100));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
xhr.addEventListener('load', function () {
|
|
65
|
+
var parsed;
|
|
66
|
+
try {
|
|
67
|
+
parsed = JSON.parse(xhr.responseText);
|
|
68
|
+
} catch (_unused) {
|
|
69
|
+
reject(new FcError("\u6587\u4EF6\u4E2D\u5FC3\u54CD\u5E94\u89E3\u6790\u5931\u8D25\uFF08HTTP ".concat(xhr.status, "\uFF09"), xhr.status));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (xhr.status >= 200 && xhr.status < 300 && parsed.code === 0) {
|
|
73
|
+
resolve({
|
|
74
|
+
status: xhr.status,
|
|
75
|
+
data: parsed.data
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
reject({
|
|
80
|
+
__httpStatus: xhr.status,
|
|
81
|
+
__parsed: parsed
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
xhr.addEventListener('error', function () {
|
|
85
|
+
return reject(new FcError('上传失败:网络错误', 'NETWORK_ERROR'));
|
|
86
|
+
});
|
|
87
|
+
xhr.addEventListener('abort', function () {
|
|
88
|
+
return reject(new FcError('上传已取消', 'ABORT'));
|
|
89
|
+
});
|
|
90
|
+
xhr.send(form);
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
_context.next = 5;
|
|
94
|
+
return ensureToken(ctx.baseUrl, ctx.appKey, ctx.getToken);
|
|
95
|
+
case 5:
|
|
96
|
+
token = _context.sent;
|
|
97
|
+
_context.prev = 6;
|
|
98
|
+
_context.next = 9;
|
|
99
|
+
return doSend(token);
|
|
100
|
+
case 9:
|
|
101
|
+
return _context.abrupt("return", _context.sent.data);
|
|
102
|
+
case 12:
|
|
103
|
+
_context.prev = 12;
|
|
104
|
+
_context.t0 = _context["catch"](6);
|
|
105
|
+
if (!(_context.t0 && _context.t0.__httpStatus === 401)) {
|
|
106
|
+
_context.next = 30;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
// 令牌失效:刷新后重试一次
|
|
110
|
+
invalidateToken(ctx.baseUrl, ctx.appKey);
|
|
111
|
+
_context.next = 18;
|
|
112
|
+
return ensureToken(ctx.baseUrl, ctx.appKey, ctx.getToken);
|
|
113
|
+
case 18:
|
|
114
|
+
freshToken = _context.sent;
|
|
115
|
+
_context.prev = 19;
|
|
116
|
+
_context.next = 22;
|
|
117
|
+
return doSend(freshToken);
|
|
118
|
+
case 22:
|
|
119
|
+
return _context.abrupt("return", _context.sent.data);
|
|
120
|
+
case 25:
|
|
121
|
+
_context.prev = 25;
|
|
122
|
+
_context.t1 = _context["catch"](19);
|
|
123
|
+
if (!(_context.t1 && _context.t1.__parsed)) {
|
|
124
|
+
_context.next = 29;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
throw new FcError(_context.t1.__parsed.message || '上传失败', _context.t1.__parsed.code);
|
|
128
|
+
case 29:
|
|
129
|
+
throw _context.t1;
|
|
130
|
+
case 30:
|
|
131
|
+
if (!(_context.t0 && _context.t0.__parsed)) {
|
|
132
|
+
_context.next = 32;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
throw new FcError(_context.t0.__parsed.message || '上传失败', _context.t0.__parsed.code);
|
|
136
|
+
case 32:
|
|
137
|
+
throw _context.t0;
|
|
138
|
+
case 33:
|
|
139
|
+
case "end":
|
|
140
|
+
return _context.stop();
|
|
141
|
+
}
|
|
142
|
+
}, _callee, null, [[6, 12], [19, 25]]);
|
|
143
|
+
}));
|
|
144
|
+
return _fcFormRequest.apply(this, arguments);
|
|
145
|
+
}
|
|
146
|
+
export function checkInstantUpload(ctx, params) {
|
|
147
|
+
return fcRequest(ctx, FC_ENDPOINTS.instant, {
|
|
148
|
+
method: 'POST',
|
|
149
|
+
body: params
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/* ---------------- OPEN-5 分片上传 ---------------- */
|
|
154
|
+
|
|
155
|
+
export function initChunkUpload(ctx, params) {
|
|
156
|
+
return fcRequest(ctx, FC_ENDPOINTS.chunkInit, {
|
|
157
|
+
method: 'POST',
|
|
158
|
+
body: params
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
export function uploadChunk(ctx, params, options) {
|
|
162
|
+
var form = new FormData();
|
|
163
|
+
form.append('uploadId', params.uploadId);
|
|
164
|
+
form.append('partNumber', String(params.partNumber));
|
|
165
|
+
form.append('chunk', params.blob);
|
|
166
|
+
return fcFormRequest(ctx, FC_ENDPOINTS.chunkUpload, form, options);
|
|
167
|
+
}
|
|
168
|
+
export function mergeChunkUpload(ctx, params) {
|
|
169
|
+
return fcRequest(ctx, FC_ENDPOINTS.chunkMerge, {
|
|
170
|
+
method: 'POST',
|
|
171
|
+
body: params
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/* ---------------- OPEN-7 ~ OPEN-11 文件查询与管理 ---------------- */
|
|
176
|
+
|
|
177
|
+
export function fetchFileList(ctx) {
|
|
178
|
+
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
179
|
+
return fcRequest(ctx, FC_ENDPOINTS.list, {
|
|
180
|
+
query: _objectSpread({}, query)
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
export function fetchFileDetail(ctx, fileId) {
|
|
184
|
+
return fcRequest(ctx, "".concat(FC_ENDPOINTS.detail, "/").concat(encodeURIComponent(fileId)));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** OPEN-9 批量取可访问 URL(私有文件返回带签名的临时 URL),回显图片核心接口 */
|
|
188
|
+
export function fetchFileUrls(ctx, fileIds) {
|
|
189
|
+
return fcRequest(ctx, FC_ENDPOINTS.urls, {
|
|
190
|
+
method: 'POST',
|
|
191
|
+
body: {
|
|
192
|
+
fileIds: fileIds
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** OPEN-10 删除文件(逻辑删除);存在业务引用时返回引用列表要求二次确认 */
|
|
198
|
+
export function deleteFile(ctx, fileId) {
|
|
199
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
200
|
+
return fcRequest(ctx, "".concat(FC_ENDPOINTS.remove, "/").concat(encodeURIComponent(fileId)), {
|
|
201
|
+
method: 'DELETE',
|
|
202
|
+
body: {
|
|
203
|
+
force: options.force
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** OPEN-11 刷新私有文件签名 URL */
|
|
209
|
+
export function refreshFileSign(ctx, fileId) {
|
|
210
|
+
return fcRequest(ctx, "".concat(FC_ENDPOINTS.refreshSign, "/").concat(encodeURIComponent(fileId), "/refresh-sign"), {
|
|
211
|
+
method: 'POST'
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* ---------------- OPEN-12 ~ OPEN-14 引用登记与溯源 ---------------- */
|
|
216
|
+
|
|
217
|
+
/** OPEN-12 登记引用(支持批量) */
|
|
218
|
+
export function addReferences(ctx, refs) {
|
|
219
|
+
return fcRequest(ctx, FC_ENDPOINTS.refAdd, {
|
|
220
|
+
method: 'POST',
|
|
221
|
+
body: {
|
|
222
|
+
references: refs
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** OPEN-13 解除引用 */
|
|
228
|
+
export function removeReference(ctx, params) {
|
|
229
|
+
return fcRequest(ctx, FC_ENDPOINTS.refRemove, {
|
|
230
|
+
method: 'DELETE',
|
|
231
|
+
body: params
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** OPEN-14 查询文件的全部引用记录 */
|
|
236
|
+
export function fetchReferences(ctx, fileId) {
|
|
237
|
+
return fcRequest(ctx, "".concat(FC_ENDPOINTS.refList, "/").concat(encodeURIComponent(fileId)));
|
|
238
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { FcConfig, FcGetToken } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 请求上下文:由各组件的文件中心 props 解析而来,
|
|
4
|
+
* 所有接口函数第一个参数均为该上下文。
|
|
5
|
+
*/
|
|
6
|
+
export interface FcRequestContext {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
appKey: string;
|
|
9
|
+
getToken?: FcGetToken;
|
|
10
|
+
/** 大文件分片阈值(字节),仅上传链路消费 */
|
|
11
|
+
chunkThreshold?: FcConfig['chunkThreshold'];
|
|
12
|
+
/** 分片大小(字节),仅上传链路消费 */
|
|
13
|
+
chunkSize?: FcConfig['chunkSize'];
|
|
14
|
+
}
|
|
15
|
+
interface JsonRequestOptions {
|
|
16
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
17
|
+
/** JSON 请求体 */
|
|
18
|
+
body?: unknown;
|
|
19
|
+
/** URL 查询参数(值为 undefined 的键会被忽略) */
|
|
20
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
21
|
+
/** 透传 AbortSignal,用于取消 */
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 发送 JSON 请求(自动携带令牌;401 时刷新令牌重试一次)
|
|
26
|
+
*/
|
|
27
|
+
export declare function fcRequest<T>(ctx: FcRequestContext, path: string, options?: JsonRequestOptions): Promise<T>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
7
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
9
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
10
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
11
|
+
import { FcError } from "./error";
|
|
12
|
+
import { ensureToken, invalidateToken } from "./token";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 请求上下文:由各组件的文件中心 props 解析而来,
|
|
16
|
+
* 所有接口函数第一个参数均为该上下文。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
function buildUrl(baseUrl, path, query) {
|
|
20
|
+
var url = "".concat(baseUrl).concat(path);
|
|
21
|
+
if (query) {
|
|
22
|
+
var search = new URLSearchParams();
|
|
23
|
+
Object.entries(query).forEach(function (_ref) {
|
|
24
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
25
|
+
key = _ref2[0],
|
|
26
|
+
value = _ref2[1];
|
|
27
|
+
if (value !== undefined && value !== '') {
|
|
28
|
+
search.append(key, String(value));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
var qs = search.toString();
|
|
32
|
+
if (qs) url += "?".concat(qs);
|
|
33
|
+
}
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
function parseResponse(_x) {
|
|
37
|
+
return _parseResponse.apply(this, arguments);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 发送 JSON 请求(自动携带令牌;401 时刷新令牌重试一次)
|
|
41
|
+
*/
|
|
42
|
+
function _parseResponse() {
|
|
43
|
+
_parseResponse = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(response) {
|
|
44
|
+
var body, _body$code;
|
|
45
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
46
|
+
while (1) switch (_context.prev = _context.next) {
|
|
47
|
+
case 0:
|
|
48
|
+
_context.prev = 0;
|
|
49
|
+
_context.next = 3;
|
|
50
|
+
return response.json();
|
|
51
|
+
case 3:
|
|
52
|
+
body = _context.sent;
|
|
53
|
+
_context.next = 9;
|
|
54
|
+
break;
|
|
55
|
+
case 6:
|
|
56
|
+
_context.prev = 6;
|
|
57
|
+
_context.t0 = _context["catch"](0);
|
|
58
|
+
throw new FcError("\u6587\u4EF6\u4E2D\u5FC3\u54CD\u5E94\u89E3\u6790\u5931\u8D25\uFF08HTTP ".concat(response.status, "\uFF09"), response.status);
|
|
59
|
+
case 9:
|
|
60
|
+
if (!(!response.ok || body.code !== 0)) {
|
|
61
|
+
_context.next = 11;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
throw new FcError(body.message || "\u8BF7\u6C42\u5931\u8D25\uFF08HTTP ".concat(response.status, "\uFF09"), (_body$code = body.code) !== null && _body$code !== void 0 ? _body$code : response.status);
|
|
65
|
+
case 11:
|
|
66
|
+
return _context.abrupt("return", body.data);
|
|
67
|
+
case 12:
|
|
68
|
+
case "end":
|
|
69
|
+
return _context.stop();
|
|
70
|
+
}
|
|
71
|
+
}, _callee, null, [[0, 6]]);
|
|
72
|
+
}));
|
|
73
|
+
return _parseResponse.apply(this, arguments);
|
|
74
|
+
}
|
|
75
|
+
export function fcRequest(_x2, _x3) {
|
|
76
|
+
return _fcRequest.apply(this, arguments);
|
|
77
|
+
}
|
|
78
|
+
function _fcRequest() {
|
|
79
|
+
_fcRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(ctx, path) {
|
|
80
|
+
var options,
|
|
81
|
+
_options$method,
|
|
82
|
+
method,
|
|
83
|
+
body,
|
|
84
|
+
query,
|
|
85
|
+
signal,
|
|
86
|
+
doFetch,
|
|
87
|
+
token,
|
|
88
|
+
response,
|
|
89
|
+
freshToken,
|
|
90
|
+
_args3 = arguments;
|
|
91
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
92
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
93
|
+
case 0:
|
|
94
|
+
options = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : {};
|
|
95
|
+
_options$method = options.method, method = _options$method === void 0 ? 'GET' : _options$method, body = options.body, query = options.query, signal = options.signal;
|
|
96
|
+
doFetch = /*#__PURE__*/function () {
|
|
97
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(token) {
|
|
98
|
+
var res;
|
|
99
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
100
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
101
|
+
case 0:
|
|
102
|
+
_context2.prev = 0;
|
|
103
|
+
_context2.next = 3;
|
|
104
|
+
return fetch(buildUrl(ctx.baseUrl, path, query), {
|
|
105
|
+
method: method,
|
|
106
|
+
signal: signal,
|
|
107
|
+
headers: {
|
|
108
|
+
'Content-Type': 'application/json',
|
|
109
|
+
Authorization: "Bearer ".concat(token)
|
|
110
|
+
},
|
|
111
|
+
body: body === undefined ? undefined : JSON.stringify(body)
|
|
112
|
+
});
|
|
113
|
+
case 3:
|
|
114
|
+
res = _context2.sent;
|
|
115
|
+
_context2.next = 11;
|
|
116
|
+
break;
|
|
117
|
+
case 6:
|
|
118
|
+
_context2.prev = 6;
|
|
119
|
+
_context2.t0 = _context2["catch"](0);
|
|
120
|
+
if (!(signal !== null && signal !== void 0 && signal.aborted)) {
|
|
121
|
+
_context2.next = 10;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
throw new FcError('请求已取消', 'ABORT');
|
|
125
|
+
case 10:
|
|
126
|
+
throw new FcError('无法连接文件中心服务,请检查网络或 baseUrl 配置', 'NETWORK_ERROR');
|
|
127
|
+
case 11:
|
|
128
|
+
return _context2.abrupt("return", res);
|
|
129
|
+
case 12:
|
|
130
|
+
case "end":
|
|
131
|
+
return _context2.stop();
|
|
132
|
+
}
|
|
133
|
+
}, _callee2, null, [[0, 6]]);
|
|
134
|
+
}));
|
|
135
|
+
return function doFetch(_x4) {
|
|
136
|
+
return _ref3.apply(this, arguments);
|
|
137
|
+
};
|
|
138
|
+
}();
|
|
139
|
+
_context3.next = 5;
|
|
140
|
+
return ensureToken(ctx.baseUrl, ctx.appKey, ctx.getToken);
|
|
141
|
+
case 5:
|
|
142
|
+
token = _context3.sent;
|
|
143
|
+
_context3.next = 8;
|
|
144
|
+
return doFetch(token);
|
|
145
|
+
case 8:
|
|
146
|
+
response = _context3.sent;
|
|
147
|
+
if (!(response.status === 401)) {
|
|
148
|
+
_context3.next = 17;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
// 令牌失效:清缓存重新换取后重试一次
|
|
152
|
+
invalidateToken(ctx.baseUrl, ctx.appKey);
|
|
153
|
+
_context3.next = 13;
|
|
154
|
+
return ensureToken(ctx.baseUrl, ctx.appKey, ctx.getToken);
|
|
155
|
+
case 13:
|
|
156
|
+
freshToken = _context3.sent;
|
|
157
|
+
_context3.next = 16;
|
|
158
|
+
return doFetch(freshToken);
|
|
159
|
+
case 16:
|
|
160
|
+
response = _context3.sent;
|
|
161
|
+
case 17:
|
|
162
|
+
return _context3.abrupt("return", parseResponse(response));
|
|
163
|
+
case 18:
|
|
164
|
+
case "end":
|
|
165
|
+
return _context3.stop();
|
|
166
|
+
}
|
|
167
|
+
}, _callee3);
|
|
168
|
+
}));
|
|
169
|
+
return _fcRequest.apply(this, arguments);
|
|
170
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FcGetToken } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 获取令牌(带缓存与并发去重):
|
|
4
|
+
* - 有效期内直接复用缓存;
|
|
5
|
+
* - 并发请求只发一次认证请求;
|
|
6
|
+
* - 传入自定义 getToken 时同样走缓存(按 appKey 维度)。
|
|
7
|
+
*/
|
|
8
|
+
export declare function ensureToken(baseUrl: string, appKey: string, customGetToken?: FcGetToken): Promise<string>;
|
|
9
|
+
/** 强制失效某项目的令牌缓存(收到 401 时调用,下次请求会重新换令牌) */
|
|
10
|
+
export declare function invalidateToken(baseUrl: string, appKey: string): void;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
import { FC_ENDPOINTS, TOKEN_EXPIRE_AHEAD } from "./endpoints";
|
|
6
|
+
import { FcError } from "./error";
|
|
7
|
+
|
|
8
|
+
/** 单个项目的令牌缓存 */
|
|
9
|
+
|
|
10
|
+
/** 令牌缓存表:key 为 `${baseUrl}::${appKey}` */
|
|
11
|
+
var tokenCache = new Map();
|
|
12
|
+
|
|
13
|
+
/** 裸请求(不带令牌),仅用于认证接口本身 */
|
|
14
|
+
function rawRequest(_x, _x2, _x3) {
|
|
15
|
+
return _rawRequest.apply(this, arguments);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 获取令牌(带缓存与并发去重):
|
|
19
|
+
* - 有效期内直接复用缓存;
|
|
20
|
+
* - 并发请求只发一次认证请求;
|
|
21
|
+
* - 传入自定义 getToken 时同样走缓存(按 appKey 维度)。
|
|
22
|
+
*/
|
|
23
|
+
function _rawRequest() {
|
|
24
|
+
_rawRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(baseUrl, path, init) {
|
|
25
|
+
var response, body, _body$code;
|
|
26
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
27
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
28
|
+
case 0:
|
|
29
|
+
_context2.prev = 0;
|
|
30
|
+
_context2.next = 3;
|
|
31
|
+
return fetch("".concat(baseUrl).concat(path), init);
|
|
32
|
+
case 3:
|
|
33
|
+
response = _context2.sent;
|
|
34
|
+
_context2.next = 9;
|
|
35
|
+
break;
|
|
36
|
+
case 6:
|
|
37
|
+
_context2.prev = 6;
|
|
38
|
+
_context2.t0 = _context2["catch"](0);
|
|
39
|
+
throw new FcError('无法连接文件中心服务,请检查网络或 baseUrl 配置', 'NETWORK_ERROR');
|
|
40
|
+
case 9:
|
|
41
|
+
_context2.prev = 9;
|
|
42
|
+
_context2.next = 12;
|
|
43
|
+
return response.json();
|
|
44
|
+
case 12:
|
|
45
|
+
body = _context2.sent;
|
|
46
|
+
_context2.next = 18;
|
|
47
|
+
break;
|
|
48
|
+
case 15:
|
|
49
|
+
_context2.prev = 15;
|
|
50
|
+
_context2.t1 = _context2["catch"](9);
|
|
51
|
+
throw new FcError("\u6587\u4EF6\u4E2D\u5FC3\u54CD\u5E94\u89E3\u6790\u5931\u8D25\uFF08HTTP ".concat(response.status, "\uFF09"), response.status);
|
|
52
|
+
case 18:
|
|
53
|
+
if (!(!response.ok || body.code !== 0)) {
|
|
54
|
+
_context2.next = 20;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
throw new FcError(body.message || "\u8BF7\u6C42\u5931\u8D25\uFF08HTTP ".concat(response.status, "\uFF09"), (_body$code = body.code) !== null && _body$code !== void 0 ? _body$code : response.status);
|
|
58
|
+
case 20:
|
|
59
|
+
return _context2.abrupt("return", body.data);
|
|
60
|
+
case 21:
|
|
61
|
+
case "end":
|
|
62
|
+
return _context2.stop();
|
|
63
|
+
}
|
|
64
|
+
}, _callee2, null, [[0, 6], [9, 15]]);
|
|
65
|
+
}));
|
|
66
|
+
return _rawRequest.apply(this, arguments);
|
|
67
|
+
}
|
|
68
|
+
export function ensureToken(baseUrl, appKey, customGetToken) {
|
|
69
|
+
var cacheKey = "".concat(baseUrl, "::").concat(appKey);
|
|
70
|
+
var cached = tokenCache.get(cacheKey);
|
|
71
|
+
if (cached && cached.expireAt > Date.now()) {
|
|
72
|
+
return cached.promise;
|
|
73
|
+
}
|
|
74
|
+
var fetcher = customGetToken !== null && customGetToken !== void 0 ? customGetToken : ( /*#__PURE__*/function () {
|
|
75
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(key) {
|
|
76
|
+
var data;
|
|
77
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
78
|
+
while (1) switch (_context.prev = _context.next) {
|
|
79
|
+
case 0:
|
|
80
|
+
_context.next = 2;
|
|
81
|
+
return rawRequest(baseUrl, FC_ENDPOINTS.auth, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: {
|
|
84
|
+
'Content-Type': 'application/json'
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify({
|
|
87
|
+
appKey: key
|
|
88
|
+
})
|
|
89
|
+
});
|
|
90
|
+
case 2:
|
|
91
|
+
data = _context.sent;
|
|
92
|
+
return _context.abrupt("return", data.accessToken);
|
|
93
|
+
case 4:
|
|
94
|
+
case "end":
|
|
95
|
+
return _context.stop();
|
|
96
|
+
}
|
|
97
|
+
}, _callee);
|
|
98
|
+
}));
|
|
99
|
+
return function (_x4) {
|
|
100
|
+
return _ref.apply(this, arguments);
|
|
101
|
+
};
|
|
102
|
+
}());
|
|
103
|
+
var promise = fetcher(appKey);
|
|
104
|
+
// 失败时清掉缓存,避免把 rejected promise 留在表里
|
|
105
|
+
promise.catch(function () {
|
|
106
|
+
var _tokenCache$get;
|
|
107
|
+
if (((_tokenCache$get = tokenCache.get(cacheKey)) === null || _tokenCache$get === void 0 ? void 0 : _tokenCache$get.promise) === promise) {
|
|
108
|
+
tokenCache.delete(cacheKey);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
// 默认按 OPEN-1 的 2 小时有效期缓存;自定义 getToken 无法得知有效期,同样按 2 小时处理
|
|
112
|
+
tokenCache.set(cacheKey, {
|
|
113
|
+
promise: promise,
|
|
114
|
+
expireAt: Date.now() + 2 * 60 * 60 * 1000 - TOKEN_EXPIRE_AHEAD
|
|
115
|
+
});
|
|
116
|
+
return promise;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 强制失效某项目的令牌缓存(收到 401 时调用,下次请求会重新换令牌) */
|
|
120
|
+
export function invalidateToken(baseUrl, appKey) {
|
|
121
|
+
tokenCache.delete("".concat(baseUrl, "::").concat(appKey));
|
|
122
|
+
}
|