Skip to content

Commit 385a0dc

Browse files
dingzhenznen丁振振
and
丁振振
authored
add login to index.ts (#145)
* add login to index.ts * add remarks * add remote server * add remote server * add remote server * add response check * fix init function * fix init function Co-authored-by: 丁振振 <[email protected]>
1 parent a369f8d commit 385a0dc

18 files changed

+220
-572
lines changed

packages/cli/src/actions/appAction.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ export async function appListCommand() {
99

1010
//init table
1111
const table = new Table({
12-
head: ['APPId', 'name','status'],
12+
head: ['appid', 'name','status'],
1313
});
1414

15-
// create app
15+
// user create app
1616
if(response.data.created){
1717
response.data.created.forEach(app => {
1818
table.push([app.appid,app.name,app.status])
1919
});
2020
}
2121

22-
// join app
22+
// user join app
2323
if(response.data.joined){
2424
response.data.joined.forEach(app => {
2525
table.push([app.appid,app.name,app.status])

packages/cli/src/actions/userAction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fs from 'node:fs'
33
import {AUTH_FILE} from '../utils/constants'
44
import { checkCredentialsDir } from '../utils/util'
55

6-
export async function loginCommand(remote:string,username:string,password:string) {
6+
export async function handleLoginCommand(remote:string,username:string,password:string) {
77

88
// check auth dir
99
checkCredentialsDir()

packages/cli/src/api/functions.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { requestData } from "./request"
2+
3+
4+
export async function pullFunction(appid: string,functionName:string) {
5+
6+
let url = ''
7+
8+
if(functionName){
9+
url = `/sys-api/apps/${appid}/function?status=1&page=1&limit=100&keyword=${functionName}`
10+
}else{
11+
url = `/sys-api/apps/${appid}/function?status=1&page=1&limit=100`
12+
13+
}
14+
const obj = {
15+
method: "GET",
16+
url
17+
}
18+
19+
const result = await requestData(obj)
20+
return result.data
21+
}

packages/cli/src/api/login.ts

-7
This file was deleted.

packages/cli/src/api/request.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import { getAccessToken } from '../utils/tokens'
66

77

88
export const request = axios.create({
9-
// 联调
9+
// set baseURL
1010
baseURL: getRemoteServe()
1111
})
1212

13-
13+
// http request
1414
request.interceptors.request.use(
1515
async (config) => {
16-
config.headers.VERSION = ''
16+
1717
const token = await getAccessToken()
1818
if (token) {
1919
config.headers.Authorization = `Bearer ${token}`
2020
} else {
21-
config.headers.Authorization = 'Basic aGVhbHRoOmhlYWx0aA=='
21+
console.error("please login first")
22+
process.exit(1)
2223
}
2324
return config
2425
},
@@ -30,9 +31,21 @@ request.interceptors.request.use(
3031
},
3132
)
3233

34+
// http response
35+
request.interceptors.response.use(function (response) {
36+
// 对响应数据做点什么
37+
return response;
38+
}, function (error) {
39+
//return Promise.reject(err);
40+
console.error(error.response.status)
41+
console.error(error.response.statusText)
42+
process.exit(1)
43+
});
44+
45+
3346

3447
/**
35-
* 描述 axios post 请求
48+
* 描述 axios request 请求
3649
* @param {Object} obj
3750
*/
3851
export function requestData(obj: object) {

packages/cli/src/api/user.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import axios from 'axios'
22

3-
4-
53
export async function loginApi(server:string,obj:Object) {
6-
const url = `${server}sys-extension-api/func/password-login`;
7-
const result = await axios.post(url,obj)
4+
// remote server login url
5+
const url = `${server}/sys-extension-api/func/password-login`;
86

9-
const response = result.data
7+
try{
108

11-
if(response.code!=0){
12-
return false
13-
}
9+
const result = await axios.post(url,obj,{timeout:5000})
1410

15-
console.log(response.data)
11+
const response = result.data
1612

17-
return {access_token:response.data.access_token,expire_time:response.data.expire}
13+
if(response.code!=0){
14+
return false
15+
}
1816

19-
17+
return {access_token:response.data.access_token,expire_time:response.data.expire}
18+
19+
}catch(err){
20+
21+
console.error(err.message)
22+
process.exit(1)
23+
}
2024
}

packages/cli/src/apps.js

-118
This file was deleted.

packages/cli/src/apps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { appListCommand } from './actions/appAction'
66
program
77
.command('list')
88
.action(async () => {
9-
9+
1010
await appListCommand()
1111

1212
})

0 commit comments

Comments
 (0)