@@ -3,9 +3,6 @@ import * as dns from '@alicloud/alidns20150109';
33
44type DnsSdkClient = DnsClient ;
55
6- /**
7- * DNS record configuration
8- */
96export type DnsRecordConfig = {
107 domainName : string ;
118 rr : string ;
@@ -14,9 +11,6 @@ export type DnsRecordConfig = {
1411 ttl ?: number ;
1512} ;
1613
17- /**
18- * DNS record info
19- */
2014export type DnsRecordInfo = {
2115 recordId ?: string ;
2216 domainName ?: string ;
@@ -27,76 +21,78 @@ export type DnsRecordInfo = {
2721 status ?: string ;
2822} ;
2923
30- /**
31- * Create DNS operations
32- */
33- export function createDnsOperations ( dnsClient : DnsSdkClient ) {
34- const operations = {
35- /**
36- * Add a DNS record
37- */
38- addDomainRecord : async ( config : DnsRecordConfig ) : Promise < string > => {
39- const request = new dns . AddDomainRecordRequest ( {
40- domainName : config . domainName ,
41- RR : config . rr ,
42- type : config . type ,
43- value : config . value ,
44- TTL : config . ttl || 600 ,
45- } ) ;
24+ export type DnsOperations = {
25+ addDomainRecord : ( config : DnsRecordConfig ) => Promise < string > ;
26+ deleteDomainRecord : ( recordId : string ) => Promise < void > ;
27+ describeDomainRecords : ( domainName : string , rrKeyWord ?: string ) => Promise < Array < DnsRecordInfo > > ;
28+ checkDomainRecordExists : ( domainName : string , rr : string ) => Promise < boolean > ;
29+ } ;
30+
31+ const addDomainRecord = ( dnsClient : DnsSdkClient ) => async (
32+ config : DnsRecordConfig ,
33+ ) : Promise < string > => {
34+ const request = new dns . AddDomainRecordRequest ( {
35+ domainName : config . domainName ,
36+ RR : config . rr ,
37+ type : config . type ,
38+ value : config . value ,
39+ TTL : config . ttl || 600 ,
40+ } ) ;
41+
42+ const response = await dnsClient . addDomainRecord ( request ) ;
43+ return response . body ?. recordId || '' ;
44+ } ;
4645
47- const response = await dnsClient . addDomainRecord ( request ) ;
48- return response . body ?. recordId || '' ;
49- } ,
46+ const deleteDomainRecord = ( dnsClient : DnsSdkClient ) => async ( recordId : string ) : Promise < void > => {
47+ const request = new dns . DeleteDomainRecordRequest ( {
48+ recordId,
49+ } ) ;
5050
51- /**
52- * Delete a DNS record
53- */
54- deleteDomainRecord : async ( recordId : string ) : Promise < void > => {
55- const request = new dns . DeleteDomainRecordRequest ( {
56- recordId,
57- } ) ;
51+ await dnsClient . deleteDomainRecord ( request ) ;
52+ } ;
5853
59- await dnsClient . deleteDomainRecord ( request ) ;
60- } ,
54+ const describeDomainRecords = ( dnsClient : DnsSdkClient ) => async (
55+ domainName : string ,
56+ rrKeyWord ?: string ,
57+ ) : Promise < Array < DnsRecordInfo > > => {
58+ const request = new dns . DescribeDomainRecordsRequest ( {
59+ domainName,
60+ RRKeyWord : rrKeyWord ,
61+ } ) ;
6162
62- /**
63- * Describe domain records
64- */
65- describeDomainRecords : async (
66- domainName : string ,
67- rrKeyWord ?: string ,
68- ) : Promise < Array < DnsRecordInfo > > => {
69- const request = new dns . DescribeDomainRecordsRequest ( {
70- domainName,
71- RRKeyWord : rrKeyWord ,
72- } ) ;
63+ const response = await dnsClient . describeDomainRecords ( request ) ;
64+ const records = response . body ?. domainRecords ?. record || [ ] ;
7365
74- const response = await dnsClient . describeDomainRecords ( request ) ;
75- const records = response . body ?. domainRecords ?. record || [ ] ;
66+ return records . map ( ( record : dns . DescribeDomainRecordsResponseBodyDomainRecordsRecord ) => ( {
67+ recordId : record . recordId ,
68+ domainName : record . domainName ,
69+ rr : record . RR ,
70+ type : record . type ,
71+ value : record . value ,
72+ ttl : record . TTL ,
73+ status : record . status ,
74+ } ) ) ;
75+ } ;
7676
77- return records . map ( ( record : dns . DescribeDomainRecordsResponseBodyDomainRecordsRecord ) => ( {
78- recordId : record . recordId ,
79- domainName : record . domainName ,
80- rr : record . RR ,
81- type : record . type ,
82- value : record . value ,
83- ttl : record . TTL ,
84- status : record . status ,
85- } ) ) ;
86- } ,
77+ const checkDomainRecordExists = (
78+ describeFn : ( domainName : string , rrKeyWord ?: string ) => Promise < Array < DnsRecordInfo > > ,
79+ ) => async ( domainName : string , rr : string ) : Promise < boolean > => {
80+ try {
81+ const records = await describeFn ( domainName , rr ) ;
82+ return records . some ( ( record ) => record . rr === rr ) ;
83+ } catch {
84+ return false ;
85+ }
86+ } ;
8787
88- /**
89- * Check if a DNS record exists
90- */
91- checkDomainRecordExists : async ( domainName : string , rr : string ) : Promise < boolean > => {
92- try {
93- const records = await operations . describeDomainRecords ( domainName , rr ) ;
94- return records . some ( ( record ) => record . rr === rr ) ;
95- } catch {
96- return false ;
97- }
98- } ,
88+ export const createDnsOperations = ( dnsClient : DnsSdkClient ) : DnsOperations => {
89+ const describe = describeDomainRecords ( dnsClient ) ;
90+
91+ return {
92+ addDomainRecord : addDomainRecord ( dnsClient ) ,
93+ deleteDomainRecord : deleteDomainRecord ( dnsClient ) ,
94+ describeDomainRecords : describe ,
95+ checkDomainRecordExists : checkDomainRecordExists ( describe ) ,
9996 } ;
97+ } ;
10098
101- return operations ;
102- }
0 commit comments