feat: add options.init

#21
master
Pooya Parsa 2017-09-06 22:30:58 +04:30
parent e9e225f142
commit 8e0c0e8c4c
2 changed files with 16 additions and 2 deletions

View File

@ -151,6 +151,11 @@ requestInterceptor: (config, { store }) => {
Function for manipulating axios responses.
### `init`
- Default: `null`
Function `init(axios, ctx)` to do additional things with axios.
### `errorHandler`
- Default: (Return promise rejection with error)

View File

@ -126,7 +126,12 @@ export default (ctx, inject) => {
headers
})
<% if(options.credentials) { %>
// Custom init hook
<% if (options.init) { %>
const initHook = <%= serialize(options.init).replace('init(', 'function(').replace('function function', 'function') %>
<% } %>
<% if (options.credentials) { %>
// Send credentials only to relative and API Backend requests
axios.interceptors.request.use(config => {
if (config.withCredentials === undefined) {
@ -167,7 +172,7 @@ export default (ctx, inject) => {
const resInter = <%= serialize(options.responseInterceptor).replace('responseInterceptor(', 'function(').replace('function function', 'function') %>
axios.interceptors.response.use(config => resInter(config, ctx))
<% } %>
// Error handler
axios.interceptors.response.use(undefined, errorHandler.bind(ctx));
@ -176,6 +181,10 @@ export default (ctx, inject) => {
axios.interceptors.response.use(undefined, customErrorHandler.bind(ctx))
<% } %>
<% if (options.init) { %>
await initHook(axios, ctx)
<% } %>
// Inject axios to the context as $axios
inject('axios', axios)