0.1.19 • Published 7 years ago
zoyi-simple-fetch v0.1.19
simple-fetch
Simple, easy to use fetch wrapping module
Installation
npm install zoyi-simple-fetch
Testing
npm test
Building
npm run build
Usage
// Require
var sf = require('zoyi-simple-fetch')
// Create instance
var client = new sf.Client()
Features
Set base url depending on NODE_ENV
// Set base url
client.setBaseUrl('http://base.url')
// Set base url depending on NODE_ENV
client.setBaseUrl({
staging: 'http://dev.base.url', // if NODE_ENV is 'staging'
production: 'http://base.url' // if NODE_ENV is 'production'
})
Set default header which is applied to every request
client.setDefaultHeader({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
Set header interceptor (filter) for dynamic header
client.setHeaderInterceptor((header) => {
// header is defaultHeader
header['X-Auth-Token'] = 'ABCD'
return header
})
Set credential options
// default value is '' (means no use)
client.credentials = ''
// Use cookies for same origin
client.credentials = 'same-origin'
// For CORS
client.credentials = 'include'
Encode query string on GET method
const query = { key: 'value' }
client.get('url', query)
Encode request body to json on POST, PUT method
const body = { key: 'value' }
client.post('url', body)
client.put('url', body)
Encode request body to url encoded form on POST, PUT method
const body = { key: 'value' }
client.postByUrlEncoding('url', body)
client.putByUrlEncoding('url', body)
No encode on DELETE method
client.delete('url')
Decode response body to json
client.get('url').then((body: JSON) => {
console.log(body)
})
Reject if status code is not 2XX
client.get('url').then(() => {}).catch((err: Error) => {
console.error(err)
})
// Error type is below.
interface Error {
status: string; // status code
statusText: string; // status text
body: JSON; // json decoded response body
}
Throw status code 0 if internet is disconnected (Actually, not reachable to server)
// Tested in Safari, Chrome, IE10, IE11, Edge
{
status: 0,
statusText: 'TypeError ~~'
}
To Do
- Abstract file upload and Support uploading progress
0.1.19
7 years ago
0.1.18
7 years ago
0.1.17
7 years ago
0.1.16
7 years ago
0.1.15
7 years ago
0.1.14
7 years ago
0.1.13
7 years ago
0.1.12
7 years ago
0.1.11
7 years ago
0.1.10
7 years ago
0.1.9
7 years ago
0.1.8
7 years ago
0.1.7
7 years ago
0.1.6
7 years ago
0.1.5
7 years ago
0.1.4
7 years ago
0.1.3
7 years ago
0.1.2
7 years ago
0.1.1
7 years ago
0.1.0
7 years ago