modelproxy-engine-fetch
Advanced tools
Comparing version 3.0.30 to 3.0.31
149
out/index.js
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -10,13 +28,44 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const modelproxy_1 = require("modelproxy"); | ||
var modelproxy_1 = require("modelproxy"); | ||
if (!modelproxy_1.globalObj.fetch) { | ||
modelproxy_1.globalObj.fetch = require("isomorphic-fetch"); | ||
} | ||
const { URLSearchParams } = modelproxy_1.globalObj; | ||
const defaultHeaders = { | ||
var URLSearchParams = modelproxy_1.globalObj.URLSearchParams; | ||
var defaultHeaders = { | ||
Accept: "application/json", | ||
"Content-Type": "application/json" | ||
}; | ||
class FetchEngine extends modelproxy_1.DefaultEngine { | ||
var FetchEngine = /** @class */ (function (_super) { | ||
__extends(FetchEngine, _super); | ||
function FetchEngine() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
/** | ||
@@ -28,44 +77,58 @@ * 发起请求 | ||
*/ | ||
fetch(ctx, next) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let formData = new URLSearchParams(), bodyParams = new URLSearchParams(), { executeInfo = {}, instance = {} } = ctx, body, headers = {}, { timeout = 5000, headers: originHeaders = {}, type = "", fetch: fetchOptions = {} } = executeInfo.settings || {}, url = this.getFullPath(instance, executeInfo); | ||
if (typeof FormData !== "undefined") { | ||
formData = new FormData(); | ||
} | ||
// 根据type来设置不同的headers | ||
switch (type) { | ||
case "params": | ||
headers = Object.assign({}, defaultHeaders, headers); | ||
body = bodyParams; | ||
break; | ||
case "formData": | ||
body = formData; | ||
break; | ||
default: | ||
headers = Object.assign({}, defaultHeaders, headers); | ||
body = JSON.stringify(executeInfo.data || {}); | ||
break; | ||
} | ||
// 合并headers | ||
headers = Object.assign({}, headers || {}, originHeaders); | ||
// 处理数据 | ||
for (const key in executeInfo.data) { | ||
if (executeInfo.data.hasOwnProperty(key)) { | ||
const data = executeInfo.data[key]; | ||
formData.append(key, data); | ||
bodyParams.append(key, data); | ||
FetchEngine.prototype.fetch = function (ctx, next) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var formData, bodyParams, _a, executeInfo, _b, instance, body, headers, _c, _d, timeout, _e, originHeaders, _f, type, _g, fetchOptions, url, key, data, _h; | ||
return __generator(this, function (_j) { | ||
switch (_j.label) { | ||
case 0: | ||
formData = new URLSearchParams(), bodyParams = new URLSearchParams(), _a = ctx.executeInfo, executeInfo = _a === void 0 ? {} : _a, _b = ctx.instance, instance = _b === void 0 ? {} : _b, headers = {}, _c = executeInfo.settings || {}, _d = _c.timeout, timeout = _d === void 0 ? 5000 : _d, _e = _c.headers, originHeaders = _e === void 0 ? {} : _e, _f = _c.type, type = _f === void 0 ? "" : _f, _g = _c.fetch, fetchOptions = _g === void 0 ? {} : _g, url = this.getFullPath(instance, executeInfo); | ||
if (typeof FormData !== "undefined") { | ||
formData = new FormData(); | ||
} | ||
// 根据type来设置不同的headers | ||
switch (type) { | ||
case "params": | ||
headers = Object.assign({}, defaultHeaders, headers); | ||
body = bodyParams; | ||
break; | ||
case "formData": | ||
body = formData; | ||
break; | ||
default: | ||
headers = Object.assign({}, defaultHeaders, headers); | ||
body = JSON.stringify(executeInfo.data || {}); | ||
break; | ||
} | ||
// 合并headers | ||
headers = Object.assign({}, headers || {}, originHeaders); | ||
// 处理数据 | ||
for (key in executeInfo.data) { | ||
if (executeInfo.data.hasOwnProperty(key)) { | ||
data = executeInfo.data[key]; | ||
formData.append(key, data); | ||
bodyParams.append(key, data); | ||
} | ||
} | ||
// 发送请求 | ||
_h = ctx; | ||
return [4 /*yield*/, Promise.race([ | ||
this.delay(timeout || 5000).then(function () { | ||
throw new modelproxy_1.MPError("\u63A5\u53E3\u8BF7\u6C42\u8D85\u65F6\uFF01(" + timeout + ")", "9999", ctx); | ||
}), | ||
fetch(url, __assign({ body: ["GET", "OPTIONS", "HEAD"].indexOf(instance.method.toUpperCase()) === -1 ? body : null, credentials: "same-origin", headers: headers, method: instance.method }, fetchOptions)) | ||
])]; | ||
case 1: | ||
// 发送请求 | ||
_h.result = _j.sent(); | ||
return [4 /*yield*/, next()]; | ||
case 2: | ||
_j.sent(); | ||
return [2 /*return*/]; | ||
} | ||
} | ||
// 发送请求 | ||
ctx.result = yield Promise.race([ | ||
this.delay(timeout || 5000).then(() => { | ||
throw new modelproxy_1.MPError(`接口请求超时!(${timeout})`, "9999", ctx); | ||
}), | ||
fetch(url, Object.assign({ body: ["GET", "OPTIONS", "HEAD"].indexOf(instance.method.toUpperCase()) === -1 ? body : null, credentials: "same-origin", headers: headers, method: instance.method }, fetchOptions)) | ||
]); | ||
yield next(); | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
return FetchEngine; | ||
}(modelproxy_1.DefaultEngine)); | ||
exports.FetchEngine = FetchEngine; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "modelproxy-engine-fetch", | ||
"version": "3.0.30", | ||
"version": "3.0.31", | ||
"description": "modelproxy中fetch的engine", | ||
@@ -29,3 +29,3 @@ "main": "out/index.js", | ||
"mochawesome": "~1.2.1", | ||
"modelproxy": "^3.0.30", | ||
"modelproxy": "^3.0.31", | ||
"ts-loader": "^4.4.1", | ||
@@ -32,0 +32,0 @@ "tsconfig-paths-webpack-plugin": "^3.2.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33695
306