refactor: change the return value of uploadPublicKey as promise<string>
This commit is contained in:
@@ -21,40 +21,44 @@ export default class DevstarAPIHandler {
|
||||
}
|
||||
|
||||
// 上传公钥
|
||||
public async uploadUserPublicKey() {
|
||||
// 获取机器名称
|
||||
const machineName = os.hostname();
|
||||
// 组成公钥名称
|
||||
const timestamp = Date.now();
|
||||
const keyTitle = `${this.user.getUsernameFromLocal()}-${machineName}-${timestamp}`
|
||||
const postData = {
|
||||
"key": this.user.getUserPublicKey(),
|
||||
"title": keyTitle
|
||||
}
|
||||
public async uploadUserPublicKey(): Promise<string> {
|
||||
return new Promise(async (resolve) => {
|
||||
// 获取机器名称
|
||||
const machineName = os.hostname();
|
||||
// 组成公钥名称
|
||||
const timestamp = Date.now();
|
||||
const keyTitle = `${this.user.getUsernameFromLocal()}-${machineName}-${timestamp}`
|
||||
const postData = {
|
||||
"key": this.user.getUserPublicKey(),
|
||||
"title": keyTitle
|
||||
}
|
||||
|
||||
const uploadUrl = this.devstarDomain + `/api/v1/user/keys`
|
||||
const uploadUrl = this.devstarDomain + `/api/v1/user/keys`
|
||||
|
||||
// 上传公钥
|
||||
fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'token ' + this.user.getUserTokenFromLocal()
|
||||
},
|
||||
body: JSON.stringify(postData)
|
||||
})
|
||||
.then(response => {
|
||||
response.json().then(data => {
|
||||
if (response.ok) {
|
||||
console.log("Successfully upload new created public key.\n", data)
|
||||
resolve("ok")
|
||||
} else {
|
||||
throw new Error(`Failed to upload new created public key!\nError: ${data.message}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
// 上传公钥
|
||||
fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'token ' + this.user.getUserTokenFromLocal()
|
||||
},
|
||||
body: JSON.stringify(postData)
|
||||
})
|
||||
.then(response => {
|
||||
response.json().then(data => {
|
||||
if (response.ok) {
|
||||
console.log("Successfully upload new created public key.\n", data)
|
||||
} else {
|
||||
throw new Error(`Failed to upload new created public key!\nError: ${data.message}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user