gm-http
Version:
Http module for Greasyfork Script
206 lines (202 loc) • 7.86 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["gmHttp"] = factory();
else
root["gmHttp"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/**
* Created by axetroy on 17-6-23.
*/
/// <reference path="./index.d.ts" />
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
function isFunction(func) {
return typeof func === "function";
}
var Http = (function () {
function Http(config) {
if (config === void 0) { config = {}; }
this.config = config;
}
Http.prototype.setConfig = function (config) {
if (config === void 0) { config = {}; }
this.config = __assign({}, this.config, config);
};
Http.prototype.create = function (config) {
return new Http(config);
};
Http.prototype.request = function (config) {
var _this = this;
return new Promise(function (resolve, reject) {
var commonRequestConfig = {
method: config.method,
url: config.url,
data: config.body,
header: config.headers
};
var GM_xmlhttpRequestConfig = __assign({}, commonRequestConfig, config, _this.config);
var onreadystatechange = GM_xmlhttpRequestConfig.onreadystatechange, onerror = GM_xmlhttpRequestConfig.onerror, onabort = GM_xmlhttpRequestConfig.onabort, ontimeout = GM_xmlhttpRequestConfig.ontimeout;
GM_xmlhttpRequestConfig.synchronous = true; // async
GM_xmlhttpRequestConfig.onreadystatechange = function (response) {
try {
isFunction(onreadystatechange) &&
onreadystatechange.call(this, response);
}
catch (err) {
reject(err);
}
if (response.readyState !== 4)
return;
response.status >= 200 && response.status < 400
? resolve(response)
: reject(response);
};
GM_xmlhttpRequestConfig.onerror = function (response) {
try {
isFunction(onerror) && onerror.call(this, response);
reject(response);
}
catch (err) {
reject(err);
}
};
GM_xmlhttpRequestConfig.onabort = function (response) {
try {
isFunction(onabort) && onabort.call(this, response);
reject(response);
}
catch (err) {
reject(err);
}
};
GM_xmlhttpRequestConfig.ontimeout = function (response) {
try {
isFunction(ontimeout) && ontimeout.call(this, response);
reject(response);
}
catch (err) {
reject(err);
}
};
if (_this.config.debug) {
console.log("%c[" + commonRequestConfig.method.toUpperCase() + "]%c: " + commonRequestConfig.url, "color: green", "color: #000;text-style: under-line");
}
GM_xmlhttpRequest(__assign({}, GM_xmlhttpRequestConfig));
});
};
Http.prototype.get = function (url, data, headers, config) {
if (headers === void 0) { headers = {}; }
if (config === void 0) { config = {}; }
return this.request(__assign({ url: url, method: "GET", body: data, headers: headers }, config));
};
Http.prototype.post = function (url, data, headers, config) {
if (headers === void 0) { headers = {}; }
if (config === void 0) { config = {}; }
return this.request(__assign({ url: url, method: "POST", body: data, headers: headers }, config));
};
Http.prototype.put = function (url, data, headers, config) {
if (headers === void 0) { headers = {}; }
if (config === void 0) { config = {}; }
return this.request(__assign({ url: url, method: "POST", body: data, headers: headers }, config));
};
Http.prototype["delete"] = function (url, data, headers, config) {
if (headers === void 0) { headers = {}; }
if (config === void 0) { config = {}; }
return this.request(__assign({ url: url, method: "DELETE", body: data, headers: headers }, config));
};
Http.prototype.head = function (url, data, headers, config) {
if (headers === void 0) { headers = {}; }
if (config === void 0) { config = {}; }
return this.request(__assign({ url: url, method: "HEAD", body: data, headers: headers }, config));
};
return Http;
}());
exports.Http = Http;
var timeout = 5000;
exports.timeout = timeout;
var http = new Http({ timeout: timeout });
exports.http = http;
exports.default = http;
/***/ })
/******/ ]);
});