-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
65 lines (59 loc) · 1.33 KB
/
client.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
54
55
56
57
58
59
60
61
62
63
64
65
const axios = require('axios')
let indate = 0
let Authorization = ''
// 添加请求拦截器
axios.interceptors.request.use(
async function (config) {
if (!/auth/.test(config.url) && indate < new Date().getTime()) {
token = await getTenantToken()
setToken(token)
}
return config
},
function (error) {
return error
}
)
const getTenantToken = async function (app_id, app_secret) {
const url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
const res = await (await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
app_id: app_id || this.app_id,
app_secret: app_secret || this.app_secret,
})
})).json()
indate = new Date().getTime() + res.expire * 1000
return res
}
const setToken = function (token) {
token = token.tenant_access_token
Authorization = `Bearer ${token}`
}
const client = {
get: async function (url) {
return fetch(url, {
method: 'GET',
headers: {
Authorization,
},
})
},
post: async function (url, data) {
return fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
Authorization,
},
})
},
}
module.exports = {
getTenantToken,
setToken,
http: client,
}