Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/k1r0s/http-decorator
HTTP ES7 Decorator to perform AJAX Requests. Powered by Axios
https://github.com/k1r0s/http-decorator
Last synced: 10 days ago
JSON representation
HTTP ES7 Decorator to perform AJAX Requests. Powered by Axios
- Host: GitHub
- URL: https://github.com/k1r0s/http-decorator
- Owner: k1r0s
- Created: 2017-10-01T09:54:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-22T12:28:15.000Z (over 6 years ago)
- Last Synced: 2024-10-13T02:13:54.692Z (24 days ago)
- Language: JavaScript
- Homepage: http://npm.im/http-decorator
- Size: 19.5 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### 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](https://www.npmjs.com/package/kaop-ts) API
[fork me on Github](https://github.com/k1r0s/kaop-ts/)
### Get Started
install: `npm install http-decorator`
import: `import { http } from 'http-decorator';`
usage:
```javascript
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:```javascript
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
```javascript
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();
})
})
};```