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

axios-decorators

v1.0.4

Published

It is an HTTP client library that uses Axios and JavaScript decorators. It is inspired by the popular Android library Retrofit.

Downloads

9

Readme

Axios Decorators

JavaScript Style Guide Quality Gate Status npm

It is an HTTP client library that uses Axios and JavaScript decorators. It is inspired by the popular Android library Retrofit.

Dependencies

The main dependency is Babel, it uses decorators and classes to make JavaScript developers' life easier. Hence there are two dependencies as plugin-proposal-decorators and plugin-proposal-class-properties.

Axios and Regenerator are other dependencies, you can find dependency installation in the following section.

Installation

Babel Core

First of all, Babel has to be installed, but if you have already Babel on your project, you can skip this part. Babel installation can differ by application. Please check the Babel's documentation for detailed information.

Babel Plugins

npm install --save-dev @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators

After plugin installation, following configuration have to be added to .babelrc file.

{
  "presets": [...],
  "plugins": [
    [
      "@babel/plugin-proposal-decorators",
      {"legacy": true}
    ],
    ["@babel/plugin-proposal-class-properties"]
  ]
}

Runtime Regenerator

If you already use @babel/polyfill, you can skip this part. However, @babel/polyfill has been deprecated, upgrading the project to core-js and regenerator is suggested.

npm install --save regenerator

Flow

Flow is suggested to get auto-suggestion support on the IDE.

npm install --save-dev @babel/core @babel/cli @babel/preset-flow

After installation, Flow's preset has to be added to .babelrc file.

{
  "presets": ["@babel/preset-flow"]
}

Axios & Axios Decorators

npm install --save axios axios-decorators

Usage Examples

Basic

You can access the basic example's code from this link.

// @flow
import { AxiosPromise } from 'axios'
import { DELETE, GET, PATCH, POST, PUT, Client } from 'axios-decorators'

type PostType = {
  userId: number;
  id: number;
  title: string;
  body: string;
}

@Client({ baseURL: 'https://jsonplaceholder.typicode.com' })
class PostClient {
  @GET('/posts')
  static getPostList (): AxiosPromise<PostType> {}

  @GET('/posts/:id')
  static getPost (args: { path: { id: string } }): AxiosPromise<PostType> {}

  @POST('/posts')
  static createPost (args: { data: PostType }): AxiosPromise<PostType> {}

  @PUT('/posts/:id')
  static putPost (args: { path: { id: string }, data: PostType }): AxiosPromise<PostType> {}

  @PATCH('/posts/:id')
  static patchPost (args: { path: { id: string }, data: PostType }): AxiosPromise<PostType> {}

  @DELETE('/posts/:id')
  static deletePost (args: { path: { id: string } }): AxiosPromise<PostType> {}
}


(async () => {
  // Get post list
  const postList = await PostClient.getPostList()

  // Get post by id
  const postById = await PostClient.getPost({ path: { id: '1' } })

  // Create a post
  const createPost = await PostClient.createPost({
    data: {
      userId: 1,
      title: 'Post title',
      body: 'Post body'
    }
  })

  // Put data to the post
  const putPost = await PostClient.putPost({
    path: {
      id: '1'
    },
    data: {
      userId: 2,
      title: 'Modified post title',
      body: 'Modified post body'
    }
  })

  // Patch the post
  const patchPost = await PostClient.patchPost({
    path: {
      id: '1'
    },
    data: {
      body: 'Modified post body for patch'
    }
  })

  // Delete the post
  const deletePost = await PostClient.deletePost({
    path: {
      id: '1'
    }
  })
})()

Micro Services (like Feign)

// flow
import { AxiosPromise } from 'axios'
import { GET, Client, ClientConfigurator } from 'axios-decorators'

// Assume that this map is generated using consul service info
const serviceMap = {
    PostService: 'http://172.0.0.1',
    CommentService: 'http://172.0.0.2'
}

ClientConfigurator.setBaseURL((serviceName) => {
    // Service name is used to get service URL
    return serviceMap[serviceName]
})

// Client definitions
@Client({ name: 'PostService', basePath: '/post' })
class PostClient {
  @GET('/posts')
  static getPostList (): AxiosPromise<PostType> {}
}

@Client({ name: 'CommentService', basePath: '/comment' })
class CommentClient {
  @GET('/comments')
  static getCommentList (): AxiosPromise<CommentType> {}
}

Reference

ClientConfigurator

It stores client configurations.

ClientConfigurator.setBaseURL

// flow
ClientConfigurator.setBaseURL(baseURL: string | ((serviceName: string) => void))

|Property|Description|Type| |---|---|---| |baseURL|It is global Base URL.|string | ((serviceName: string) => void)|

ClientConfigurator.setHeaders

// flow
ClientConfigurator.setBaseURL(headers: object | ((serviceName: string) => void))

|Property|Description|Type| |---|---|---| |headers|It is global headers value.|object | ((serviceName: string) => void)|

Decorators

@Client

It is used to define client class.

// flow
Client(name?: string);

|Property|Description|Type| |---|---|---| |options.name|It is distinctive feature of the client class. It could be used to find service's IP address.|string|

// flow
Client(options?: { name?: string; baseURL?: string; basePath?: string; });

|Property|Description|Type| |---|---|---| |options.name|It is distinctive feature of the client class. It could be used to find service's IP address.|string| |options.baseURL|Base URL of the API can be changed directly using this parameter. It overrides baseURL coming from ClientConfigurator.|string| |options.basePath|It is common path of the service.|string|

Methods (@GET, @POST, @PUT, @PATCH, @DELETE)

// flow
type MethodOptions = {
    headers?: object | (() => object);
};

GET(endpoint: string, methodOptions?: MethodOptions);
POST(endpoint: string, methodOptions?: MethodOptions);
PUT(endpoint: string, methodOptions?: MethodOptions);
PATCH(endpoint: string, methodOptions?: MethodOptions);
DELETE(endpoint: string, methodOptions?: MethodOptions);

|Property|Description|Type| |---|---|---| |endpoint|This is specific path of your controller.|string| |methodOptions.headers|Headers of the request.|object | function|