npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

http-clienti

v1.0.0

Published

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).

Downloads

4

Readme

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.

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.

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.

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);
  }
  */
  
  ...
}

Declare the baseURL

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.