-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
53 lines (41 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict'
const fp = require('fastify-plugin')
const axios = require('axios')
async function fastifyAxios (fastify, opts) {
const { clients = {}, ...defaultArgs } = opts
let interceptors = null
if (defaultArgs.interceptors) {
interceptors = defaultArgs.interceptors
delete defaultArgs.interceptors
}
const instance = axios.create(defaultArgs)
if (interceptors) {
if (interceptors.request && interceptors.errorRequest) {
instance.interceptors.request.use(interceptors.request, interceptors.errorRequest)
}
if (interceptors.response && interceptors.errorResponse) {
instance.interceptors.response.use(interceptors.response, interceptors.errorResponse)
}
}
if (Object.keys(clients).length !== 0) {
for (const name of Object.keys(clients)) {
const axiosInstance = axios.create(clients[name])
if (clients[name].interceptors) {
if (clients[name].interceptors.request && clients[name].interceptors.errorRequest) {
axiosInstance.interceptors.request.use(clients[name].interceptors.request, clients[name].interceptors.errorRequest)
}
if (clients[name].interceptors.response && clients[name].interceptors.errorResponse) {
axiosInstance.interceptors.response.use(clients[name].interceptors.response, clients[name].interceptors.errorResponse)
}
}
Object.assign(instance, {
[name]: axiosInstance
})
}
}
fastify.decorate('axios', instance)
}
module.exports = fp(fastifyAxios, {
fastify: '>= 2.0.0',
name: 'fastify-axios'
})