Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
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).
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.