go async/await
This commit is contained in:
@ -1,161 +1,161 @@
|
||||
import 'typings-global';
|
||||
import 'typings-global'
|
||||
import plugins = require('./cflare.plugins')
|
||||
import helpers = require('./cflare.classes.helpers')
|
||||
import * as interfaces from './cflare.interfaces'
|
||||
|
||||
export class CflareAccount {
|
||||
private authEmail: string
|
||||
private authKey: string
|
||||
constructor() {
|
||||
private authEmail: string
|
||||
private authKey: string
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
auth (optionsArg: { email: string, key: string }) {
|
||||
this.authEmail = optionsArg.email
|
||||
this.authKey = optionsArg.key
|
||||
}
|
||||
|
||||
async getZoneId (domainName: string) {
|
||||
let zoneArray = await this.listZones(domainName)
|
||||
let filteredResponse = zoneArray.filter((zoneArg) => {
|
||||
return zoneArg.name === domainName
|
||||
})
|
||||
if (filteredResponse.length >= 1) {
|
||||
return filteredResponse[ 0 ].id
|
||||
} else {
|
||||
plugins.beautylog.error(`the domain ${domainName} does not appear to be in this account!`)
|
||||
throw new Error(`the domain ${domainName} does not appear to be in this account!`)
|
||||
}
|
||||
|
||||
auth(optionsArg: { email: string, key: string }) {
|
||||
this.authEmail = optionsArg.email
|
||||
this.authKey = optionsArg.key
|
||||
}
|
||||
}
|
||||
getRecord (domainNameArg: string, typeArg: string): Promise<interfaces.ICflareRecord> {
|
||||
let done = plugins.q.defer()
|
||||
let result: interfaces.ICflareRecord
|
||||
|
||||
getZoneId(domainName: string) {
|
||||
let done = plugins.q.defer()
|
||||
this.listZones(domainName)
|
||||
.then(zoneArrayArg => {
|
||||
let filteredResponse = zoneArrayArg.filter((zoneArg) => {
|
||||
return zoneArg.name === domainName
|
||||
})
|
||||
if (filteredResponse.length >= 1) {
|
||||
done.resolve(filteredResponse[ 0 ].id)
|
||||
} else {
|
||||
plugins.beautylog.error(`the domain ${domainName} does not appear to be in this account!`)
|
||||
done.reject(undefined)
|
||||
}
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
getRecord(domainNameArg: string, typeArg: string): Promise<interfaces.ICflareRecord> {
|
||||
let done = plugins.q.defer()
|
||||
let result: interfaces.ICflareRecord
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
this.listRecords(domain.zoneName)
|
||||
.then((recordArrayArg) => {
|
||||
let filteredResponse = recordArrayArg.filter((recordArg) => {
|
||||
return (recordArg.type === typeArg && recordArg.name === domainNameArg)
|
||||
})
|
||||
done.resolve(filteredResponse[ 0 ])
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
this.listRecords(domain.zoneName)
|
||||
.then((recordArrayArg) => {
|
||||
let filteredResponse = recordArrayArg.filter((recordArg) => {
|
||||
return (recordArg.type === typeArg && recordArg.name === domainNameArg)
|
||||
})
|
||||
done.resolve(filteredResponse[ 0 ])
|
||||
})
|
||||
return done.promise
|
||||
};
|
||||
createRecord(domainNameArg: string, typeArg: string, contentArg: string) {
|
||||
let done = plugins.q.defer()
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
this.getZoneId(domain.zoneName)
|
||||
.then((domainIdArg) => {
|
||||
let dataObject = {
|
||||
name: domain.fullName,
|
||||
type: typeArg,
|
||||
content: contentArg
|
||||
}
|
||||
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject)
|
||||
.then(function (responseArg) {
|
||||
done.resolve(responseArg)
|
||||
})
|
||||
})
|
||||
return done.promise
|
||||
};
|
||||
removeRecord(domainNameArg: string, typeArg: string) {
|
||||
let done = plugins.q.defer()
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
this.getRecord(domain.fullName, typeArg)
|
||||
async createRecord (domainNameArg: string, typeArg: string, contentArg: string) {
|
||||
let done = plugins.q.defer()
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
let domainIdArg = await this.getZoneId(domain.zoneName)
|
||||
let dataObject = {
|
||||
name: domain.fullName,
|
||||
type: typeArg,
|
||||
content: contentArg
|
||||
}
|
||||
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject)
|
||||
.then(function (responseArg) {
|
||||
done.resolve(responseArg)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
removeRecord (domainNameArg: string, typeArg: string) {
|
||||
let done = plugins.q.defer()
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
this.getRecord(domain.fullName, typeArg)
|
||||
.then((responseArg) => {
|
||||
if (responseArg) {
|
||||
let requestRoute: string = '/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id
|
||||
this.request('DELETE', requestRoute)
|
||||
.then((responseArg) => {
|
||||
if (responseArg) {
|
||||
let requestRoute: string = '/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id
|
||||
this.request('DELETE', requestRoute)
|
||||
.then((responseArg) => {
|
||||
done.resolve(responseArg)
|
||||
})
|
||||
} else {
|
||||
done.reject()
|
||||
}
|
||||
done.resolve(responseArg)
|
||||
})
|
||||
return done.promise
|
||||
};
|
||||
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
||||
let done = plugins.q.defer()
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
return done.promise
|
||||
};
|
||||
listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
||||
let done = plugins.q.defer<interfaces.ICflareRecord[]>()
|
||||
let result: interfaces.ICflareRecord[] = []
|
||||
} else {
|
||||
done.reject()
|
||||
}
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
this.getZoneId(domain.zoneName)
|
||||
.then((domainIdArg) => {
|
||||
this.request('GET', '/zones/' + domainIdArg + '/dns_records?per_page=100')
|
||||
.then(function (responseArg: any) {
|
||||
result = responseArg.result
|
||||
done.resolve(result)
|
||||
})
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
listZones(domainName?: string): Promise<interfaces.ICflareZone[]> { // TODO: handle pagination
|
||||
let done = plugins.q.defer<interfaces.ICflareZone[]>()
|
||||
let requestRoute = '/zones?per_page=50'
|
||||
if (domainName) requestRoute = requestRoute + '&name=' + domainName
|
||||
let result = []
|
||||
this.request('GET', requestRoute)
|
||||
.then((responseArg: any) => {
|
||||
result = responseArg.result
|
||||
done.resolve(result)
|
||||
})
|
||||
return done.promise
|
||||
};
|
||||
request(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
let done = plugins.q.defer()
|
||||
let jsonArg: string = JSON.stringify(dataArg)
|
||||
let options: plugins.smartrequest.ISmartRequestOptions = {
|
||||
method: methodArg,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Auth-Email': this.authEmail,
|
||||
'X-Auth-Key': this.authKey
|
||||
},
|
||||
requestBody: jsonArg
|
||||
}
|
||||
// console.log(options);
|
||||
let retryCount = 0
|
||||
updateRecord (domainNameArg: string, typeArg: string, valueArg) {
|
||||
let done = plugins.q.defer()
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
return done.promise
|
||||
}
|
||||
|
||||
let makeRequest = async () => {
|
||||
let response: any = await plugins.smartrequest.request(
|
||||
`https://api.cloudflare.com/client/v4${routeArg}`,
|
||||
options
|
||||
)
|
||||
if (response.statusCode === 200) {
|
||||
done.resolve(response.body)
|
||||
} else if (response.statusCode === 429) {
|
||||
console.log('rate limited! Waiting for retry!')
|
||||
retryRequest()
|
||||
} else if(response.statusCode === 400) {
|
||||
console.log('bad request! Going to retry!')
|
||||
} else {
|
||||
console.log(response.statusCode)
|
||||
done.reject(new Error('request failed'))
|
||||
}
|
||||
}
|
||||
let retryRequest = async (delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)) => {
|
||||
console.log(`retry started and waiting for ${delayTimeArg} ms`)
|
||||
await plugins.smartdelay.delayFor(delayTimeArg)
|
||||
if (retryCount < 10) {
|
||||
retryCount++
|
||||
return await makeRequest()
|
||||
}
|
||||
}
|
||||
makeRequest()
|
||||
return done.promise
|
||||
/**
|
||||
* list all records of a specified domain name
|
||||
* @param domainNameArg - the domain name that you want to get the records from
|
||||
*/
|
||||
async listRecords (domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg)
|
||||
let domainId = await this.getZoneId(domain.zoneName)
|
||||
let responseArg: any = await this.request('GET', '/zones/' + domainId + '/dns_records?per_page=100')
|
||||
let result: interfaces.ICflareRecord[] = responseArg.result
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* list all zones in the associated authenticated account
|
||||
* @param domainName
|
||||
*/
|
||||
async listZones (domainName?: string): Promise<interfaces.ICflareZone[]> { // TODO: handle pagination
|
||||
let requestRoute = '/zones?per_page=50'
|
||||
|
||||
// may be optionally filtered by domain name
|
||||
if (domainName) {
|
||||
requestRoute = requestRoute + '&name=' + domainName
|
||||
}
|
||||
|
||||
private authCheck() {
|
||||
return (this.authEmail && this.authKey) // check if auth is available
|
||||
let response: any = await this.request('GET', requestRoute)
|
||||
let result = response.result
|
||||
return result
|
||||
}
|
||||
|
||||
request (methodArg: string, routeArg: string, dataArg = {}) {
|
||||
let done = plugins.q.defer()
|
||||
let jsonArg: string = JSON.stringify(dataArg)
|
||||
let options: plugins.smartrequest.ISmartRequestOptions = {
|
||||
method: methodArg,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Auth-Email': this.authEmail,
|
||||
'X-Auth-Key': this.authKey
|
||||
},
|
||||
requestBody: jsonArg
|
||||
}
|
||||
// console.log(options);
|
||||
let retryCount = 0
|
||||
|
||||
let makeRequest = async () => {
|
||||
let response: any = await plugins.smartrequest.request(
|
||||
`https://api.cloudflare.com/client/v4${routeArg}`,
|
||||
options
|
||||
)
|
||||
if (response.statusCode === 200) {
|
||||
done.resolve(response.body)
|
||||
} else if (response.statusCode === 429) {
|
||||
console.log('rate limited! Waiting for retry!')
|
||||
retryRequest()
|
||||
} else if (response.statusCode === 400) {
|
||||
console.log('bad request! Going to retry!')
|
||||
} else {
|
||||
console.log(response.statusCode)
|
||||
done.reject(new Error('request failed'))
|
||||
}
|
||||
}
|
||||
let retryRequest = async (delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)) => {
|
||||
console.log(`retry started and waiting for ${delayTimeArg} ms`)
|
||||
await plugins.smartdelay.delayFor(delayTimeArg)
|
||||
if (retryCount < 10) {
|
||||
retryCount++
|
||||
return await makeRequest()
|
||||
}
|
||||
}
|
||||
makeRequest()
|
||||
return done.promise
|
||||
}
|
||||
|
||||
private authCheck () {
|
||||
return (this.authEmail && this.authKey) // check if auth is available
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +0,0 @@
|
||||
import "typings-global";
|
||||
import plugins = require("./cflare.plugins");
|
@ -1,59 +1,61 @@
|
||||
import * as plugins from './cflare.plugins'
|
||||
|
||||
export type TRecord = ''
|
||||
|
||||
export interface ICflareZone {
|
||||
"id": string
|
||||
"name": string
|
||||
"development_mode": number
|
||||
"original_name_servers": string[]
|
||||
"original_registrar": string
|
||||
"original_dnshost": string
|
||||
"created_on": string
|
||||
"modified_on": string
|
||||
"name_servers": string[]
|
||||
"owner": {
|
||||
"id": string
|
||||
"email": string
|
||||
"owner_type": string
|
||||
},
|
||||
"permissions": string []
|
||||
"plan": {
|
||||
"id": string
|
||||
"name": string
|
||||
"price": number
|
||||
"currency": string
|
||||
"frequency": string
|
||||
"legacy_id": string
|
||||
"is_subscribed": boolean
|
||||
"can_subscribe": boolean
|
||||
},
|
||||
"plan_pending": {
|
||||
"id": string
|
||||
"name": string
|
||||
"price": number
|
||||
"currency": string
|
||||
"frequency": string
|
||||
"legacy_id": string
|
||||
"is_subscribed": string
|
||||
"can_subscribe": string
|
||||
},
|
||||
"status": string
|
||||
"paused": boolean
|
||||
"type": string
|
||||
"checked_on": string
|
||||
'id': string
|
||||
'name': string
|
||||
'development_mode': number
|
||||
'original_name_servers': string[]
|
||||
'original_registrar': string
|
||||
'original_dnshost': string
|
||||
'created_on': string
|
||||
'modified_on': string
|
||||
'name_servers': string[]
|
||||
'owner': {
|
||||
'id': string
|
||||
'email': string
|
||||
'owner_type': string
|
||||
},
|
||||
'permissions': string[]
|
||||
'plan': {
|
||||
'id': string
|
||||
'name': string
|
||||
'price': number
|
||||
'currency': string
|
||||
'frequency': string
|
||||
'legacy_id': string
|
||||
'is_subscribed': boolean
|
||||
'can_subscribe': boolean
|
||||
},
|
||||
'plan_pending': {
|
||||
'id': string
|
||||
'name': string
|
||||
'price': number
|
||||
'currency': string
|
||||
'frequency': string
|
||||
'legacy_id': string
|
||||
'is_subscribed': string
|
||||
'can_subscribe': string
|
||||
},
|
||||
'status': string
|
||||
'paused': boolean
|
||||
'type': string
|
||||
'checked_on': string
|
||||
}
|
||||
|
||||
export interface ICflareRecord {
|
||||
"id": string
|
||||
"type": string
|
||||
"name": string
|
||||
"content": string
|
||||
"proxiable": boolean
|
||||
"proxied": boolean
|
||||
"ttl": number
|
||||
"locked": boolean
|
||||
"zone_id": string
|
||||
"zone_name": string
|
||||
"created_on": string
|
||||
"modified_on": string
|
||||
"data": any
|
||||
}
|
||||
'id': string
|
||||
'type': string
|
||||
'name': string
|
||||
'content': string
|
||||
'proxiable': boolean
|
||||
'proxied': boolean
|
||||
'ttl': number
|
||||
'locked': boolean
|
||||
'zone_id': string
|
||||
'zone_name': string
|
||||
'created_on': string
|
||||
'modified_on': string
|
||||
'data': any
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import "typings-global";
|
||||
export let beautylog = require("beautylog");
|
||||
export import q = require("smartq");
|
||||
export import smartrequest = require("smartrequest");
|
||||
export import smartstring = require("smartstring");
|
||||
export import smartdelay = require('smartdelay');
|
||||
import 'typings-global'
|
||||
export let beautylog = require('beautylog')
|
||||
export import q = require('smartq')
|
||||
export import smartrequest = require('smartrequest')
|
||||
export import smartstring = require('smartstring')
|
||||
export import smartdelay = require('smartdelay')
|
||||
|
Reference in New Issue
Block a user