GithubHelp home page GithubHelp logo

http-decorator's Introduction

http decorator

axios + kaop-ts = <3

This library allows to easily implement AJAX calls without messing with Async stuff and with the same axios API.

You only have to include this decorator in one method, then the decorated method will be threated as then or catch for the call. Only receiving the params.

This tiny project is also a demo about writing custom decorators using kaop-ts API

fork me on Github

Get Started

install: npm install http-decorator

import: import { http } from 'http-decorator';

usage:

class SomeClass {
  @http({ url: 'localhost/resource'})
  public someMethod (params?: any, error?, result?): void {
    // error should be null if request was success
  }
}

someClassInstance.someMethod();
// $ curl localhost/resource
someClassInstance.someMethod({ id: 1 });
// $ curl localhost/resource?id=1

You should wrap this decorator with another function to set global axios options like headers or so:

import { http } from 'http-decorator';

// wrap a decorator to pass default arguments
export const get = (resourcePath) => http({ url: `http://jsonplaceholder.typicode.com${resourcePath}` })
export const post = (resourcePath) => http({ method: 'post', url: `http://jsonplaceholder.typicode.com${resourcePath}` })
export const put = (resourcePath) => http({ method: 'put', url: `http://jsonplaceholder.typicode.com${resourcePath}`, headers: { whatsoever } })

...

class SomeClass {

  // using the wrapper decorator
  @get('/users')
  public someMethod (params, error, result) {}
}

someClassInstance.someMethod();
// $ curl http://jsonplaceholder.typicode.com/users
}

How it works -> 17 lines

const axios = require('axios');
const { beforeMethod } = require('kaop-ts');

module.exports = {
  http: ({ method = 'get', ...options }) =>
  beforeMethod(function(meta){
    const [params] = meta.args;
    options[method === 'get' ? 'params' : 'data'] = params;
    axios({ method, ...options })
    .then(res => {
      meta.args = [params, null, res.data];
      this.next();
    })
    .catch(error => {
      meta.args = [params, error, null];
      this.next();
    })
  })
};

http-decorator's People

Contributors

k1r0s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

alexxnica

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.