This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (130 loc) · 4.08 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var request = require('request')
, inherits = require('util').inherits
var NODE_ENV = process.env.NODE_ENV || 'development'
exports.BASE_URL = ''
exports.client = function (clientInfo) {
return new Client(clientInfo)
}
function Client (clientInfo) {
this.clientInfo = clientInfo
}
Object.keys(request).forEach(function (key) {
Client.prototype[key] = 'function' === typeof request[key]
? request[key].bind(request)
: request[key]
})
!['get', 'post', 'put', 'patch', 'head', 'del'].forEach(function (key) {
var original = Client.prototype[key]
Client.prototype[key] = function (uri, options, callback) {
if (uri && 'string' === typeof uri && '/' === uri.slice(0, 1)) {
uri = exports.BASE_URL + uri
}
if ('function' === typeof options) {
callback = options
options = {}
}
options = options || {}
var self = this
if (self.expiresAt && self.expiresAt - new Date() < 60000) {
if (self.refreshToken && 'function' === typeof self._onRefreshed) {
self._refresh(self.refreshToken, function (err, refreshed) {
if (err) {
if ('function' === typeof self._onExpired) {
self._onExpired(self.accessToken)
}
return callback(err)
}
self._onRefreshed(refreshed.accessToken, refreshed.expiresAt, function (err) {
if (err) {
if ('function' === typeof self._onExpired) {
self._onExpired(self.accessToken)
}
return callback(err)
}
self.accessToken = refreshed.accessToken
self.expiresAt = refreshed.expiresAt
self._use(options)
return original(uri, options, callback)
})
})
}
else {
if ('function' === typeof self._onExpired) {
self._onExpired(self.accessToken)
}
return callback(new exports.TokenExpiredError(self.accessToken))
}
}
else {
if (self.accessToken) {
self._use(options)
}
return original(uri, options, callback)
}
}
})
Client.prototype.authorize = function (options) {
return this._authorize(options)
}
Client.prototype.credentical = function (code, callback) {
var self = this
self._credentical(code, function (err, credentical, userInfo) {
if (err) {
return callback(err)
}
self.accessToken = credentical.accessToken
if (credentical.expiresAt) {
self.expiresAt = new Date(credentical.expiresAt)
}
if (credentical.refreshToken) {
self.refreshToken = credentical.refreshToken
}
return callback(null, credentical, userInfo)
})
return self
}
Client.prototype.token = function (accessToken, expiresAt) {
this.accessToken = accessToken
if (expiresAt) {
this.expiresAt = new Date(expiresAt)
}
return this
}
Client.prototype.refresh = function (refreshToken, onRefreshed) {
if ('function' === typeof refreshToken && !onRefreshed) {
this._onRefreshed = refreshToken
}
else {
this.refreshToken = refreshToken
this._onRefreshed = onRefreshed
}
return this
}
Client.prototype.expired = function (onExpired) {
this._onExpired = onExpired
return this
}
Client.prototype._authorize = function (options) {
throw new Error('You can\'t call this method without override it.')
}
Client.prototype._credentical = function (code, callback) {
throw new Error('You can\'t call this method without override it.')
}
Client.prototype._refresh = function (refreshToken, callback) {
throw new Error('You can\'t call this method without override it.')
}
Client.prototype._use = function (options) {
throw new Error('You can\'t call this method without override it.')
}
exports.Client = Client
function TokenExpiredError (/* ..., */token) {
Error.apply(this, [].slice.call(arguments, 0, -1))
this.token = [].slice.call(arguments, -1)[0]
}
exports.TokenExpiredError = TokenExpiredError
inherits(TokenExpiredError, Error)
function extend (to, from) {
Object.keys(from).forEach(function (key) {
to[key] = form[key]
})
}