Skip to content

Commit

Permalink
refactor: 移除 axios
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Jan 12, 2025
1 parent 9b75ef8 commit e6671b5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"dependencies": {
"@mongodb-js/zstd": "^2.0.0",
"@tsconfig/node20": "^20.1.4",
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/node": "^22.5.5",
"acme-client": "^5.4.0",
Expand All @@ -24,7 +23,6 @@
"crc-32": "^1.2.2",
"cron": "^3.1.7",
"dotenv": "^16.4.5",
"env-var": "^7.5.0",
"express": "^4.21.2",
"express-async-errors": "^3.1.1",
"got": "^14.4.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export class Server {
}
try {
const result = await fn(...data);
// 如果不为 undefined
// 如果为 undefined
if (result === undefined) {
callback([null, false]);
return;
Expand Down
27 changes: 12 additions & 15 deletions src/certificate-manager/CloudFlare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from 'axios';
import * as crypto from 'crypto';
import { DnsManager } from './DnsManager.js';
import got from 'got';

export class CloudFlare implements DnsManager {
private apiToken: string; // Cloudflare API Token
Expand All @@ -20,21 +19,19 @@ export class CloudFlare implements DnsManager {
console.log(`Cloudflare request: ${method} ${url}`);

try {
const response = await axios({
method,
const response = await got(
url,
headers: {
'Authorization': `Bearer ${this.apiToken}`,
'Content-Type': 'application/json',
},
data,
});
{
method,
headers: {
'Authorization': `Bearer ${this.apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}
).json<{ error: string, result: string }>();

if (!response.data.success) {
throw new Error(response.data.errors.map((err: any) => err.message).join(', '));
}

return response.data.result;
return response.result;
} catch (error: any) {
throw new Error(`Cloudflare request failed: ${error.message}`);
}
Expand Down
23 changes: 16 additions & 7 deletions src/certificate-manager/DNSPod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import axios from 'axios';
import * as crypto from 'crypto';
import { DnsManager } from './DnsManager.js';
import got from 'got';

interface ApiResponse {
Response: {
Error: {
Message: string;
};
RecordList: any[]
};
}

export class DNSPod implements DnsManager {
private secretId: string;
Expand Down Expand Up @@ -30,17 +39,17 @@ export class DNSPod implements DnsManager {
const signature = crypto.createHmac('sha1', this.secretKey).update(signStr).digest('base64');

try {
const response = await axios.get('https://dnspod.tencentcloudapi.com/', {
params: {
const response = await got('https://dnspod.tencentcloudapi.com/', {
searchParams: {
...params,
Signature: signature,
},
});
}).json<ApiResponse>();

if (response.data.Response && response.data.Response.Error) {
throw new Error(response.data.Response.Error.Message);
if (response.Response && response.Response.Error) {
throw new Error(response.Response.Error.Message);
}
return response.data.Response;
return response.Response;
} catch (error: any) {
throw new Error(`DNSPod request failed: ${error}`);
}
Expand Down

0 comments on commit e6671b5

Please sign in to comment.