Permalink
Browse files

feat: add options.init

  • Loading branch information...
pi0 committed Sep 6, 2017
1 parent e9e225f commit 8e0c0e8c4c2586d74f43d5a336679e124fcd6db3
Showing with 16 additions and 2 deletions.
  1. +5 −0 README.md
  2. +11 −2 lib/plugin.template.js
@@ -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)

@@ -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)

0 comments on commit 8e0c0e8

Please sign in to comment.