@livelybone/easy-request
v1.9.1
Published
A easy-to-use http request library to support all javascript runtime environments. 一个以支持所有JavaScript运行环境为目的、简单易用的http请求库。
Downloads
30
Maintainers
Readme
@livelybone/easy-request
pkg.module supported
, which means that you can apply tree-shaking in you project
A easy-to-use http request library to support all javascript runtime environments.
Feature list:
- Initiate request. customize header, responseType, withCredentials, timeout
- Cancel the request
- Receive responses
- Get the upload/receive progress
repository
https://github.com/livelybone/easy-request.git
Demo
https://github.com/livelybone/easy-request#readme
Run Example
Your can see the usage by run the example of the module, here is the step:
- Clone the library
git clone https://github.com/livelybone/easy-request.git
- Go to the directory
cd your-module-directory
- Install npm dependencies
npm i
(use taobao registry:npm i --registry=http://registry.npm.taobao.org
) - Open service
npm run dev
- See the example(usually is
http://127.0.0.1:3000/examples/test.html
) in your browser
Installation
npm i -S @livelybone/easy-request
Global name - The variable the module exported in umd
bundle
EasyRequest
Interface
See what method or params you can use in index.d.ts
Usage
import { Http, EngineName } from '@livelybone/easy-request'
const http = new Http(
EngineName.XHR,
{
baseURL: 'https://api.hostname.com',
responseType: 'json',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
timeout: 30000,
withCredentials: false,
},
)
// interceptors
http.interceptors.request.use(config => {
const token = localStorage.getItem('token')
if (token) config.headers.Authorization = token
return config
})
http.interceptors.response.use(response => {
return response.data
})
const data = { param: '1' }
http.get('/api1').then(res => {
// ... do something
})
http.get('/api2', data, { responseType: 'text' }).then(res => {
// ... do something
})
http.post('/api2', data, {
responseType: 'text',
headers: { 'Content-Type': 'multipart/form-data' },
}).then(res => {
// ... do something
})
// ... other methods: put, delete, downloadFile, uploadFile
Use in html, see what your can use in CDN: unpkg
<-- use what you want -->
<script src="https://unpkg.com/@livelybone/easy-request/lib/umd/<--module-->.js"></script>