GithubHelp home page GithubHelp logo

alexxnica / dec-http Goto Github PK

View Code? Open in Web Editor NEW

This project forked from k1r0s/http-decorator

0.0 2.0 0.0 6 KB

HTTP ES7 Decorator to perform AJAX Requests. Powered by Axios

JavaScript 24.39% TypeScript 75.61%

dec-http's Introduction

http decorator

axios + kaop-ts = <3

This library allows to easily implement AJAX calls without messing with Async stuff

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

fork me on Github

Get Started

install: npm install dec-http

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

usage:

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

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

You can use global configs to set a API root as follows:

import { config } from 'dec-http';

config.base = 'http://jsonplaceholder.typicode.com';

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

}

To use another HTTP verb just place it as a decorator argument:

class SomeClass {
  @http('put')
  public someMethod (url: string, params?: any, error?, result?): void {

  }
}

How it works -> 25 lines

import axios from 'axios';
import { beforeMethod, IAdviceSignature } from 'kaop-ts';

export interface HttpGlobals {
  base: string
}

export const config: HttpGlobals = { base: '' };

export const http = (method = 'get', headers?) =>
beforeMethod(function(meta){
  const [ url, params ] = meta.args;
  const opts = { method, headers }
  opts[method === 'get' ? 'params' : 'data'] = params;
  opts['url'] = config.base ? `${config.base}/${url}`: url;
  axios(opts)
  .then(({ data }) => {
    meta.args = [ url, params, null, data ];
    this.next();
  })
  .catch((error) => {
    meta.args = [ url, params, error, null];
    this.next();
  })
});

dec-http's People

Watchers

 avatar  avatar

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.