Skip to content

Commit

Permalink
Merge pull request #355 from Wolfy64/addingNamecom
Browse files Browse the repository at this point in the history
Add Provider Name.com
  • Loading branch information
songz authored Jan 1, 2020
2 parents c8c3997 + 477932b commit 57463d7
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export const providerList = [
keys: ['GD_Key', 'GD_Secret'],
service: 'https://api.godaddy.com',
path: './goDaddy'
},
{
name: 'Name.com',
dns: 'dns_namecom',
keys: ['Namecom_Username', 'Namecom_Token'],
service: 'https://api.name.com',
path: './namecom'
}
] as ProviderInfo[]

Expand Down
105 changes: 105 additions & 0 deletions src/providers/namecom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import fetch from 'node-fetch'
import { sendRequest } from '../helpers/httpRequest'
import { getProviderKeys } from '../lib/data'
import { Provider, ServiceResponse } from '../types/general'
import { ServiceKey } from '../types/admin'
import { RequestForName } from '../types/general'
import { providerList } from './'

const provider = providerList.find(provider => provider.name === 'Name.com')
const { name, dns, keys, service } = provider

const getKeys = (): ServiceKey[] => {
const keysDefault: { key: string }[] = [{ key: keys[0] }, { key: keys[1] }]
const providerKeys = keysDefault.map(key => {
const serviceKeys = getProviderKeys()
return serviceKeys.find(k => k.service === dns && k.key === key.key) || key
})
return providerKeys as ServiceKey[]
}
const findKey = (key: string): string => {
return (getKeys().find(k => k.key === key) || { value: '' }).value
}

export const getDomains = async (): Promise<Provider> => {
const providerKeys = getKeys()
let domains = []
const options = {
headers: {
Authorization: `Basic ${Buffer.from(
`${findKey(keys[0])}:${findKey(keys[1])}`
).toString('base64')}`
}
}
const url = `${service}/v4/domains`
const request = await sendRequest<RequestForName>(url, options).catch(err => {
console.error(`getDomains Error: ${err}`)
return { domains: [] }
})

if (request.domains) domains = [...request.domains]

return {
id: dns,
service,
name,
keys: providerKeys,
domains: domains.map(el => ({ ...el, domain: el.domainName }))
}
}

export const setRecord = async (
domain: string,
ipaddress: string
): Promise<ServiceResponse> => {
const url = `${service}/v4/domains/${domain}/records`
const rootHost = {
host: '',
domainName: domain,
type: 'A',
answer: ipaddress,
ttl: 300
}
const subdomainHost = {
host: '*',
domainName: domain,
type: 'A',
answer: ipaddress,
ttl: 300
}

const options = {
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(
`${findKey(keys[0])}:${findKey(keys[1])}`
).toString('base64')}`
}
}

const rootHostOptions = {
...options,
body: JSON.stringify(rootHost)
}

const subdomainHostOptions = {
...options,
body: JSON.stringify(subdomainHost)
}

const response: ServiceResponse = {
success: true,
message: 'Successfully set CNAME records for wildcard domain'
}

await Promise.all([
fetch(url, rootHostOptions),
fetch(url, subdomainHostOptions)
]).catch(error => {
console.error('Error setting CNAME records', error)
response.success = false
response.message = 'Error setting CNAME records'
})

return response
}
14 changes: 14 additions & 0 deletions src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ type ProxyMapping = {
port?: string
}

type NamecomDomain = {
domainName: string
locked: boolean
autorenewEnabled: boolean
expireDate: string
createDate: string
}

type RequestForName = {
domains: NamecomDomain[] | []
}

type AccessToken = {
name: string
id: string
Expand All @@ -79,6 +91,8 @@ export {
ServiceConfig,
ProviderService,
ProxyMapping,
NamecomDomain,
RequestForName,
AccessToken,
AccessTokenById,
AuthenticatedRequest,
Expand Down

0 comments on commit 57463d7

Please sign in to comment.