
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
http-clienti
Advanced tools
A **declarative** RESTful lib for ajax http request, which supports [axios](https://github.com/axios/axios), [fetch](https://github.com/github/fetch) or [request](https://github.com/request/request).
A declarative RESTful lib for ajax http request, which supports axios, fetch or request.
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
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
Declare your requests as a service, e.g ArticleService.js.
import axios from 'axios';
import HttpClient, { Path, Query, Body, DELETE, POST, PATCH, GET, Headers, RequestOptions, Controller } from 'http-clienti';
@Controller('/articles')
export default class ArticleService extends HttpClient {
constructor() {
super(axios);
}
@POST()
@RequestOptions({
withCredentials: true
})
createArticle(@Body body) {
}
@DELETE('/:id')
@Headers({
'X-TOKEN': 'x-token'
})
deleteArticle(@Path('id') id) {
}
@PATCH('/:id')
updateArticle(@Path('id') id, @Body body) {
}
@GET()
fetchArticles(@Query('author') author) {
}
}
Then just call the methods, which will return the same result with the http you provided (axios here). e.g index.js.
import ArticleService from './ArticleService'
const articleService = new ArticleService();
articleService.fetchArticles('Warner')
.then(response => {
console.log(response);
});
This demonstrating a basic usage with HttpClient. Check out the /example
for more details.
Only axios / fetch / request are supported currently. We provided two approaches to inject your request engine.
1. Inject with constructor.
import axios from 'axios';
import HttpClient, { Controller } from 'http-clienti';
@Controller('/articles')
export default class ArticleService extends HttpClient {
constructor() {
// axios, fetch or request
super(axios);
}
...
}
2. Inject with defaults
.
import request from 'request';
import HttpClient, { Controller } from 'http-clienti';
// This may be declared in some entry or main file.
HttpClient.defaults = {
// axios, fetch or request
engine: request,
};
// Then declare your services
@Controller('/articles')
export default class ArticleService extends HttpClient {
/* This is no need any more
constructor() {
// axios, fetch or request
super(axios);
}
*/
...
}
1. Overwrite the getBaseURL
method. It's useful declaring a base class for baseURL along with engine injected.
BaseHttpClient.js
import HttpClient from 'http-clienti';
export default class BaseHttpClient extends HttpClient {
constructor() {
super(fetch/axios/request);
}
getBaseURL() {
return 'http://www.test.com/'
}
}
ArticleService.js
import { Body, GET, Path, POST, Query, Controller } from 'http-clienti';
import BaseHttpClient from './BaseHttpClient';
@Controller('/articles')
export default class ArticleService extends BaseHttpClient {
@GET('/:category')
fetchArticles(@Path('category') category, @Query('status') status) {
}
@POST()
createArticle(@Body body) {
}
}
2. Inject with defaults
, you may declare the engine at the mean time.
main.js
import request from 'request';
import HttpClient from 'http-clienti';
HttpClient.defaults = {
// axios, fetch or request
engine: request,
baseURL: 'http://www.test.com/'
};
ArticleService.js
import HttpClient, { Body, GET, Path, POST, Query, Controller } from 'http-clienti';
@Controller('/articles')
export default class ArticleService extends HttpClient {
constructor() {
super(axios);
}
@GET('/:category')
fetchArticles(@Path('category') category, @Query('status') status) {
}
@POST()
createArticle(@Body body) {
}
}
3. Axios specifically, you can also define the baseURL in axios directly.
localAxios.js
import axios from 'axios';
export default axios.create({
baseURL: 'https://www.test.com/',
// Other config
headers: {'X-Custom-Header': 'foobar'}
});
import axios from './localAxios'
import HttpClient, { Body, GET, Path, POST, Query, Controller } from 'http-clienti';
@Controller('/articles')
export default class ArticleService extends HttpClient {
constructor() {
super(axios);
}
@GET('/:category')
fetchArticles(@Path('category') category, @Query('status') status) {
}
@POST()
createArticle(@Body body) {
}
}
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.
FAQs
A **declarative** RESTful lib for ajax http request, which supports [axios](https://github.com/axios/axios), [fetch](https://github.com/github/fetch) or [request](https://github.com/request/request).
The npm package http-clienti receives a total of 0 weekly downloads. As such, http-clienti popularity was classified as not popular.
We found that http-clienti demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.