catta
1.0.0
catta is a simple request client for browser Support Fetch, AJAX, JSONP and even custom your own adapter. 中文文档-请点我
catta-min.js
catta-min-comp.js
{data: HTMLFormElement}
need IE 10+ to workRecommend to use the pure one, if your project already have those polyfill or no need IE Support.
# local install
npm install catta --save
// With ES6 - *Recommend*
import catta from 'catta';
catta('http://some/url').then(function (res) {
console.log(res);
});
// With CommonJS
const catta = require('catta');
// or catta.ajax or catta.jsonp or catta.fetch
catta.default('http://some/url').then(function (res) {
console.log(res);
});
<!-- And also with <script> in HTML - *Not Recommend* -->
<script src="./node_modules/catta/dist/catta-min.js"></script>
<script>
// or catta.ajax or catta.jsonp or catta.fetch
catta.default('http://some/url').then(function (res) {
console.log(res);
});
</script>
import {ajax, fetch, jsonp, getScript} from 'catta';
/**
* make fetch/ajax/jsonp/getScript request
* @param {string} url - request url
* @param {Object} options - request options
*
*/
ajax(url, options);
// fetch request
fetch(url, options);
// jsonp request
jsonp(url, options);
// getScript
getScript(url);
Description | Type | Default | Fetch | AJAX | JSONP | |
---|---|---|---|---|---|---|
url | request url | string | null | v | v | v |
method | request method | string { get , post, put, delete, head } | 'get' | v | v | x |
data | the data send to server | string/Object/Form Element [3] | {} | v | v | v |
Description | Type | Default | Fetch | AJAX | JSONP | |
---|---|---|---|---|---|---|
type | restrict request type | string { fetch, ajax, jsonp, script } | 'auto' | — | — | — |
timeout | throw timeout error after seconds | number | 3 | ! [1] | v | ! [1] |
resultType | the type of result | { text, json, response } | text | v | v | ! [2] |
v Supported ! Partial Supported × Not Supported
Fetch and JSONP request can't be abort, current timeout is just throw timeout error
resultType option can't work with jsonp, because the result must be executable javascript code
Only support form element with FormData feature
Property | Description | Type | |
---|---|---|---|
jsonp | callbackName | set custom callback name | string |
fetch | cross | indicate whether request can cross-origin | boolean |
ajax | - | - | - |
import catta from 'catta';
catta('http://some/url').then(function (res) {
console.log(res);
});
import catta from 'catta';
catta('http://some/url', {
type: 'jsonp',
data: {
page: 5,
count: 20
},
timeout: 2,
credential: false,
cross: false,
// sp. options
jsonp: {
callbackName: 'myCustomJSONP1'
}
})
.then(res => console.log(res))
.catch(err => console.log(err));
import {fetch} from 'catta';
// only use fetch
fetch('http://some/url', {
data: {a:1}
}).then(function (res) {
console.log(res);
});
import catta from 'catta';
catta('http://some/url', {
headers: {
'Content-Type': 'appliction/json'
}
})
.then(function (res) {
console.log(res);
});
import {globalConfig} from 'catta';
// set global config, it will work for each request
globalConfig({
timeout: 10
});
A custom adapter is just an object, that has detector
and processor
function.This feature is use to make a wrapper to your special request, and let catta
to handle it.More detail see mtop adapter example
import {customAdapter} from 'catta';
import mtopAdapter from 'catta/lib/custom/mtop';
// set mtop adapter
customAdapter('mtop', mtopAdapter);
options.type
MIT License