@eeymoo/hum 0.1.24 → 0.1.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/commands/auth.js +11 -5
- package/src/lib/api.js +4 -2
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eeymoo/hum",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
|
|
3
|
+
"version": "0.1.25",
|
|
4
|
+
"description": "Hum CLI - A command line tool for Hum API",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/Eeymoo/hum.git",
|
package/src/commands/auth.js
CHANGED
|
@@ -16,7 +16,7 @@ auth
|
|
|
16
16
|
console.log('Please visit:', deviceData.verificationUriComplete)
|
|
17
17
|
console.log('Code:', deviceData.userCode)
|
|
18
18
|
console.log('Waiting for authorization...')
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
const maxAttempts = 60
|
|
21
21
|
for (let i = 0; i < maxAttempts; i++) {
|
|
22
22
|
await new Promise(resolve => setTimeout(resolve, deviceData.interval * 1000))
|
|
@@ -28,10 +28,10 @@ auth
|
|
|
28
28
|
grantType: 'urn:ietf:params:oauth:grant-type:device_code'
|
|
29
29
|
})
|
|
30
30
|
})
|
|
31
|
-
|
|
32
|
-
if (tokenData.
|
|
33
|
-
config.set('accessToken', tokenData.
|
|
34
|
-
config.set('refreshToken', tokenData.
|
|
31
|
+
|
|
32
|
+
if (tokenData.access_token) {
|
|
33
|
+
config.set('accessToken', tokenData.access_token)
|
|
34
|
+
config.set('refreshToken', tokenData.refresh_token)
|
|
35
35
|
console.log('Successfully logged in!')
|
|
36
36
|
return
|
|
37
37
|
}
|
|
@@ -45,6 +45,12 @@ auth
|
|
|
45
45
|
const result = await request('/auth/verify', {
|
|
46
46
|
method: 'POST',
|
|
47
47
|
body: JSON.stringify({ apiKey })
|
|
48
|
+
}).catch(err => {
|
|
49
|
+
// verify 端点在 key 无效时返回 401,捕获后返回统一结构
|
|
50
|
+
if (err.message.includes('401')) {
|
|
51
|
+
return { valid: false }
|
|
52
|
+
}
|
|
53
|
+
throw err
|
|
48
54
|
})
|
|
49
55
|
|
|
50
56
|
if (result.valid) {
|
package/src/lib/api.js
CHANGED
|
@@ -9,6 +9,7 @@ function yellowWarn(message) {
|
|
|
9
9
|
export async function request(endpoint, options = {}) {
|
|
10
10
|
const apiUrl = config.get('apiUrl') || 'http://localhost:3000'
|
|
11
11
|
const apiKey = config.get('apiKey')
|
|
12
|
+
const accessToken = config.get('accessToken')
|
|
12
13
|
|
|
13
14
|
if (apiUrl === 'http://localhost:3000') {
|
|
14
15
|
yellowWarn('Warning: Using local API (http://localhost:3000). Ensure this is intended.')
|
|
@@ -23,8 +24,9 @@ export async function request(endpoint, options = {}) {
|
|
|
23
24
|
headers['Content-Type'] = 'application/json'
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
const bearerToken = apiKey || accessToken
|
|
28
|
+
if (bearerToken) {
|
|
29
|
+
headers['Authorization'] = `Bearer ${bearerToken}`
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
const response = await fetch(url, {
|