Gotch – A simple, elegant and lightweight HTTP client for browser
gotch

  


  A simple, elegant and lightweight HTTP client for browser







Features

Promise based
Configurable and chainable API
Automatically transforms for JSON data
Supports request body transforms
Monitor download and upload progress
Cancel requests

Installation
Using npm:
$ npm install gotch --save
Using yarn:

Using CDN:
<script src="https://unpkg.com/gotch/dist/gotch.min.js"></script>
Basic Usage
Import
import gotch from 'gotch';
Send a GET request
gotch  .get('/users/1')  .then((res) => {    console.log(res);  })  .catch((e) => {    console.log(e);  });try {  const res = await gotch.get('/users/1');  console.log(res);} catch (e) {  console.log(e);}
Send a POST request
gotch  .post('/users', {    firstName: 'Jermey',    lastName: 'Lin',  })  .then((res) => {    console.log(res);  })  .catch((e) => {    console.log(e);  });try {  const res = await gotch    .post('/users', {      firstName: 'Jermey',      lastName: 'Lin',    });  console.log(res);} catch (e) {  console.log(e);}
Create an instance
const exampleAPI = gotch.create({  baseURL: 'https://example.com/api/v1',  timeout: 2000,});
Instance configuration
Available configuration for creating an instance by using .create([config])
{  baseURL: '',  requestType: 'json',  responseType: 'json',  tag: '',  timeout: 0,  credentials: 'same-origin',  authorization: undefined,  onUploadProgress: undefined,  onDownloadProgress: undefined,}
Request options
Available options for sending a request by using .request(url[, options])
{  method: 'GET',  headers: {},  body: undefined,}
API

NOTE: .with* methods are chainable and can be used to overwrite the instance configuration, but the overwritten configuration will just works once for the next request, so if you want to reuse the same configuration multiple times, please create an instance by using .create([config])

.create([config])
Create a new instance of Gotch
gotch.create({  baseURL: 'https://example.com/api/v1',});
.withBaseURL(baseURL)
Overwrite baseURL for next request
gotch.withBaseURL('https://example.com/api/v1');
.withRequestType(requestType)
Overwrite requestType for next request
gotch.withRequestType('json');
.withResponseType(responseType)
Overwrite responseType for next request
gotch.withResponseType('json');
.withTag(tag)
Overwrite tag for next request
gotch.withTag('get-user-req');
.withTimeout(timeout)
Overwrite timeout for next request

.withCredentials(credentials)
Overwrite credentials for next request
gotch.withCredentials('include');
.withAuthorization(authorization)
Overwrite authorization for next request
gotch.withAuthorization('Bearer ***********');gotch.withAuthorization(() => {  const token = window.localStorage.getItem('token');  return `Bearer ${token}`;});
.withOnUploadProgress(handleEvent)
Overwrite onUploadProgress for next request
gotch.withOnUploadProgress((e) => {  console.log(e.loaded / e.total);});
.withOnDownloadProgress(handleEvent)
Overwrite onDownloadProgress for next request
gotch.withOnDownloadProgress((e) => {  console.log(e.loaded / e.total);});
.request(url[, options])
Send a customized request
gotch.request('/users', {  method: 'POST',  headers: {    Authorization: 'Bearer xxxxx.yyyyy.zzzzz',    'Content-Type': 'application/json;charset=UTF-8',  },  body: '{"firstName":"Jeremy","lastName":"Lin"}'});
.get(url)
Send a GET request

.post(url[, params])
Send a POST request
gotch.post('/users', {  firstName: 'Jeremy',  lastName: 'Lin',});
.put(url[, params])
Send a PUT request
gotch.put('/users/1', {  firstName: 'Jeremy',  lastName: 'Lin',});
.patch(url[, params])
Send a PATCH request
gotch.patch('/users/1', {  age: 99,});
.delete(url)
Send a DELETE request
gotch.delete('/users/1');
.cancel(tag)
Cancel request(s) grouping by the same tag
gotch.cancel('get-user-req');
License
MIT
