-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from Wolfy64/addingNamecom
Add Provider Name.com
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters