HttpClient
A declarative RESTful lib for ajax http request, which supports axios, fetch or request.
Installation
This package is based on the ES6 decorator, a few babel plugins required for decorators transform.
npm install --save-dev @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties babel-plugin-parameter-decorator
npm install --save http-clienti
Note:
This HttpClient lib doesn't provide any actual request tool, so you still need to install them explicitly. For example, if you'd like to build it on axios
, you still need this:
npm install --save axios
Usage
Declare your requests as a service, e.g ArticleService.js.
;; @ { superaxios; } @ @ { } @ @ @ @
Then just call the methods, which will return the same result with the http you provided (axios here). e.g index.js.
const articleService = ;articleService ;
This demonstrating a basic usage with HttpClient. Check out the /example
for more details.
Document
Inject your request engine
Only axios / fetch / request are supported currently. We provided two approaches to inject your request engine.
1. Inject with constructor.
;; @ { // axios, fetch or request superaxios; } ...
2. Inject with defaults
.
;; // This may be declared in some entry or main file.HttpClientdefaults = // axios, fetch or request engine: request; // Then declare your services@ /* This is no need any more constructor() { // axios, fetch or request super(axios); } */ ...
Declare the baseURL
1. Overwrite the getBaseURL
method. It's useful declaring a base class for baseURL along with engine injected.
BaseHttpClient.js
; { superfetch/axios/request; } { return 'http://www.test.com/' }
ArticleService.js
;; @ @ @ { }
2. Inject with defaults
, you may declare the engine at the mean time.
main.js
;; HttpClientdefaults = // axios, fetch or request engine: request baseURL: 'http://www.test.com/';
ArticleService.js
; @ { superaxios; } @ @ { }
3. Axios specifically, you can also define the baseURL in axios directly.
localAxios.js
; ;
; @ { superaxios; } @ @ { }
:)
Hope you enjoy this declarative request lib. Feel free to open an issue if you get any problem. I will keep my eyes on it.