axios-endpoint
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "axios-endpoint", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Scalable and easy to use axios arch", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -13,35 +13,89 @@ ## Axios endpoint | ||
#### Import it | ||
#### Import it | ||
```javascript | ||
let route = require('axios-endpoint') | ||
let route = require("axios-endpoint"); | ||
// es6 | ||
import route from "axios-endpoint"; | ||
``` | ||
##### or ES6 | ||
```javascript | ||
import route from 'axios-endpoint' | ||
``` | ||
#### Configure axios as you like | ||
#### (Optional) Configure axios as you like | ||
```javascript | ||
const axios = require('axios') | ||
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com'; | ||
const axios = require("axios"); | ||
axios.defaults.baseURL = "https://jsonplaceholder.typicode.com"; | ||
``` | ||
#### Use it | ||
```javascript | ||
let endpoint = { | ||
getTodos: route('GET','/todos/').generate(), | ||
} | ||
getTodos: route("GET", "/todos/1").bundle(), | ||
endpoint.getTodos().then(response => { | ||
console.log(response) | ||
}).catch(error => { | ||
console.error(error) | ||
}) | ||
getTodosBefore: route("GET", "/todos/1") | ||
.before(() => { | ||
console.log("this is gonna run before the request!"); | ||
}) | ||
.bundle(), | ||
getTodosAfter: route("GET", "/todos/1") | ||
.after(() => { | ||
console.log("this is gonna run after the request!"); | ||
}) | ||
.bundle(), | ||
getTodosBeforeAfter: route("GET", "/todos/1") | ||
.before(() => { | ||
console.log("this is gonna run before the request!"); | ||
}) | ||
.after(() => { | ||
console.log("this is gonna run after the request!"); | ||
}) | ||
.bundle() | ||
}; | ||
endpoint | ||
.getTodos() | ||
.then(response => { | ||
console.log(response); | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
}); | ||
endpoint | ||
.getTodosBefore() | ||
.then(response => { | ||
console.log(response); | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
}); | ||
endpoint | ||
.getTodosAfter() | ||
.then(response => { | ||
console.log(response); | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
}); | ||
endpoint | ||
.getTodosBeforeAfter() | ||
.then(response => { | ||
console.log(response); | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
}); | ||
``` | ||
### Callback hooks | ||
`before` runs exactly before making the request | ||
`after` runs exactly after making the request | ||
### Run tests | ||
```bash | ||
npm run test | ||
``` | ||
``` |
@@ -5,21 +5,45 @@ "use strict"; | ||
module.exports = function route(method,address) { | ||
let route = { | ||
method, | ||
address | ||
class Bundle { | ||
route = {}; | ||
constructor(route) { | ||
this.route = route; | ||
} | ||
return { | ||
route, | ||
before: () => { | ||
}, | ||
after: () => { | ||
before(callback){ | ||
this.beforeCallback = callback; | ||
return this | ||
} | ||
}, | ||
generate: () => (data) => axios({ | ||
method: route.method, | ||
url: route.address, | ||
data | ||
}) | ||
after(callback){ | ||
this.afterCallback = callback; | ||
return this | ||
} | ||
bundle() { | ||
const beforeCallback = this.beforeCallback || (() => {}); | ||
const afterCallback = this.afterCallback || (() => {}); | ||
return (data) => { | ||
beforeCallback() | ||
return axios({ | ||
method: this.route.method, | ||
url: this.route.address, | ||
data | ||
}).then(data => { | ||
afterCallback() | ||
return data | ||
}).catch(error => { | ||
afterCallback() | ||
throw(error) | ||
}) | ||
} | ||
} | ||
} | ||
module.exports = function route(method,address) { | ||
return new Bundle({ | ||
method, | ||
address | ||
}); | ||
} |
@@ -9,13 +9,66 @@ var assert = require('assert'); | ||
let endpoint = { | ||
getTodos: route('GET','/todos/1').generate() | ||
getTodos: route('GET','/todos/1').bundle(), | ||
getTodosBefore: route('GET','/todos/1').before(() => { | ||
console.log('this is gonna run before the request!') | ||
}).bundle(), | ||
getTodosAfter: route('GET','/todos/1').after(() => { | ||
console.log('this is gonna run after the request!') | ||
}).bundle(), | ||
getTodosBeforeAfter: route('GET','/todos/1').before(() => { | ||
console.log('this is gonna run before the request!') | ||
}).after(() => { | ||
console.log('this is gonna run after the request!') | ||
}).bundle() | ||
} | ||
describe('Route', () => { | ||
describe('Simple Request', () => { | ||
it('Should get todos with status code 200', () => { | ||
describe('Simple request', () => { | ||
it('Should get todos', () => { | ||
endpoint.getTodos().then(data => { | ||
console.log('ok') | ||
assert.equal(data.status, 200) | ||
}).catch(error => { | ||
console.log('failed') | ||
}) | ||
}) | ||
}) | ||
describe('Simple request with before hook', () => { | ||
it('Should get todos with before request hook', () => { | ||
endpoint.getTodosBefore().then(data => { | ||
console.log('ok') | ||
assert.equal(data.status, 200) | ||
}).catch(error => { | ||
console.log('failed') | ||
}) | ||
}) | ||
}) | ||
describe('Simple request with after hook', () => { | ||
it('Should get todos with after request hook', () => { | ||
endpoint.getTodosAfter().then(data => { | ||
console.log('ok') | ||
assert.equal(data.status, 200) | ||
}).catch(error => { | ||
console.log('failed') | ||
}) | ||
}) | ||
}) | ||
describe('Simple request with before and after hook', () => { | ||
it('Should get todos with before and after request hook', () => { | ||
endpoint.getTodosBeforeAfter().then(data => { | ||
console.log('ok') | ||
assert.equal(data.status, 200) | ||
}).catch(error => { | ||
console.log('failed') | ||
}) | ||
}) | ||
}) | ||
}) |
5513
98
101