# dec-http

> axios + kaop-ts =

Latest version **1.1.1** (published 2017-11-02) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install dec-http
pnpm add dec-http
yarn add dec-http
bun add dec-http
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2017-11-02 |
| First published | 2017-10-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | k1r0s |

## Links

- npm: https://www.npmjs.com/package/dec-http
- npm.io page: https://npm.io/package/dec-http

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.16.2
- [kaop-ts](https://npm.io/package/kaop-ts.md) ^1.5.0

## Recent versions

- 1.1.1 (latest) — 2017-11-02
- 1.1.0 — 2017-11-02
- 1.0.1 — 2017-10-01
- 1.0.0 — 2017-10-01

## README

### 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](https://www.npmjs.com/package/kaop-ts) API

[fork me on Github](https://github.com/k1r0s/kaop-ts/)

### Get Started

install: `npm install dec-http`

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

usage:

```javascript
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:

```javascript
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:

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

  }
}
```

### How it works -> 25 lines

```javascript
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();
  })
});
```

---
_Source: https://npm.io/package/dec-http · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
