Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-api

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-api - npm Package Compare versions

Comparing version 0.2.4 to 0.3.0

doc/axios.demo.js

87

doc/README.md
# better-api
## jquery
````html
<button id="jquery-pass" >pass</button>
<button id="jquery-fail" >fail</button>
<button id="jquery-fail-detailAction" >fail (defaultAction)</button>
<hr />
<button id="jquery-net-fail" >net.fail</button>
<button id="jquery-net-fail-custom" >net.fail(custom)</button>
<button id="jquery-net-fail-defaultAction" >net.fail (defaultAction)</button>
<hr />
<button id="jquery-abort" >abort</button>
<button id="jquery-loading" >loading</button>
````
````code
{
title: '基础使用',
title: 'jquery',
desc: '',
html: '<button id="basic-pass" >pass</button> <button id="basic-fail" >fail</button>',
js: './basic.demo.js',
source: './basic.demo.js',
open: true
html: '',
js: './jquery.demo.js',
source: './jquery.demo.js'
}
````
## axios
````html
<button id="axios-timeout">timeout</button>
<button id="axios-404">not found</button>
<button id="axios-abort">abort</button>
````
```js
var request = api.create({
settings: {
url: '/login',
type: 'post'
// dataType: 'json' // 在 defaultSettings 已经定义,无需配置
},
input: function (req) {
req.$testInputLogin = true
return req
},
output: function (res) {
res.$testOutputLogin = true
return res
}
})
request(
{
user: 'nimo',
passwrod: '1234'
},
{
// 请求发送前执行,适合修改 loading 状态
$before: function () {
},
$after: function () {
},
// responseType: success
success: function (res) {
},
/*
// responseType: error
error: function (res) {
},
*/
$net: {
done: function (res) {
},
fail: function () {
}
}
}
)
```
````code
{
title: 'axios',
desc: '',
html: '',
js: './axios.demo.js',
source: './axios.demo.js'
}
````

@@ -6,2 +6,8 @@ "use strict";

return {
defaultCallback: {
net: {
fail: function fail() {}
}
},
loading: function loading() {},
input: function input(req) {

@@ -17,2 +23,3 @@ return req;

return {
loading: function loading() {},
input: function input(req) {

@@ -29,5 +36,6 @@ return req;

return {
$before: function $before() {},
$after: function $after() {},
$net: {
loading: function loading() {},
before: function before() {},
after: function after() {},
net: {
done: function done() {}

@@ -34,0 +42,0 @@ }

@@ -25,14 +25,35 @@ "use strict";

createSettings = (0, _extend2.default)(true, {}, _defaultSettings2.default.create(), createSettings);
return function request(data, callback) {
return function request(data, responseType, callback) {
callback = (0, _extend2.default)(true, {}, _defaultSettings2.default.request.callback(), callback);
callback.$before();
callback.before();
var sendData = data;
var fetchSettins = (0, _extend2.default)(true, {}, self.settings.defaultSettings, createSettings.settings);
var userCallbackDone = callback.$net.done;
callback.$net.done = function proxyDone(res) {
var userCallbackDone = callback.net.done;
var userCallbackFail = callback.net.fail;
var failArg = [];
callback.net.fail = function proxyFail() {
for (var _len = arguments.length, arg = Array(_len), _key = 0; _key < _len; _key++) {
arg[_key] = arguments[_key];
}
var command = {
defaultAction: function defaultAction() {
self.settings.defaultCallback.net.fail.apply(null, arg);
}
};
failArg = arg;
if (typeof userCallbackFail === 'function') {
userCallbackFail.apply(command, arg);
} else {
command.defaultAction();
}
self.settings.loading(false, failArg);
createSettings.loading(false, failArg);
callback.loading(false, failArg);
};
callback.net.done = function proxyDone(res) {
res = self.settings.output(res, sendData);
res = createSettings.output(res, sendData);
userCallbackDone.apply(null, [res]);
var responseType = (0, _extend2.default)(true, {}, self.settings.defaultResponseType, callback);
Object.keys(responseType).forEach(function (key) {

@@ -44,13 +65,30 @@ if (key[0] === '$') {

var typeKey = self.settings.judgeResponseType.apply(null, [res]);
if (typeof responseType[typeKey] === 'function') {
responseType[typeKey].apply(null, [res]);
var defaultResponse = self.settings.defaultResponseType[typeKey];
var userResponse = responseType[typeKey];
var command = {
defaultAction: function defaultAction() {
defaultResponse(res);
}
};
if (typeof defaultResponse === 'undefined' && typeof userResponse === 'undefined') {
console.warn("node_modules/better-api: request(settings, callback); callback[\"" + typeKey + "\"] must be a function");
} else {
console.warn("node_modules/better-api: request(settings, callback); callback[\"" + typeKey + "\"] must be a function");
if (userResponse) {
userResponse.apply(command, [res]);
} else {
command.defaultAction();
}
}
self.settings.loading(false, failArg);
createSettings.loading(false, failArg);
callback.loading(false, failArg);
};
sendData = self.settings.input(sendData);
sendData = createSettings.input(sendData);
self.settings.fetch(sendData, fetchSettins, callback);
self.settings.loading(true, failArg);
createSettings.loading(true, failArg);
callback.loading(true, failArg);
return self.settings.fetch(sendData, fetchSettins, callback);
};
};
module.exports = Api;
{
"name": "better-api",
"main": "lib/index.js",
"version": "0.2.4",
"version": "0.3.0",
"description": "api encapsulation manage",

@@ -14,3 +14,3 @@ "keywords": "",

"doc": "npm run build:1 && npm run build:2",
"dp:gh": "node ./compile/deploy-gh",
"gh": "node ./compile/deploy-gh",
"test": "./node_modules/karma/bin/karma start ./compile/karma.conf.js --browsers Chrome",

@@ -22,2 +22,3 @@ "test:ci": "./node_modules/karma/bin/karma start ./compile/karma.ci.js"

"dependencies": {
"axios": "^0.18.0",
"extend": "^3.0.1"

@@ -45,2 +46,4 @@ },

"express": "^4.16.2",
"face-message": "^0.1.1",
"file-loader": "^1.1.11",
"fis-parser-less-2.x": "^0.1.4",

@@ -55,2 +58,3 @@ "fis3-hook-relative": "^2.0.1",

"jquery": "^3.3.1",
"json-loader": "^0.5.7",
"json5": "^0.5.1",

@@ -76,2 +80,3 @@ "karma": "^2.0.0",

"style-loader": "^0.13.1",
"url-loader": "^1.0.1",
"util.react": "^1.4.1",

@@ -78,0 +83,0 @@ "vue": "^2.5.13",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc