replace with custom superagent based on fetch API
This commit is contained in:
+17
-16
@@ -17,13 +17,13 @@ const assert = require('assert'),
|
||||
dig = require('../dig.js'),
|
||||
dns = require('../dns.js'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent'),
|
||||
superagent = require('../superagent.js'),
|
||||
waitForDns = require('./waitfordns.js');
|
||||
|
||||
const DNSIMPLE_API = 'https://api.dnsimple.com/v2';
|
||||
|
||||
function formatError(response) {
|
||||
return `dnsimple DNS error ${response.statusCode} ${JSON.stringify(response.body)}`;
|
||||
return `dnsimple DNS error ${response.status} ${response.text}`;
|
||||
}
|
||||
|
||||
function removePrivateFields(domainObject) {
|
||||
@@ -44,8 +44,8 @@ async function getAccountId(domainConfig) {
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.statusCode !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
|
||||
const accountId = safe.query(response.body, 'data[0].id', null);
|
||||
if (!accountId || typeof accountId !== 'number') throw new BoxError(BoxError.EXTERNAL_ERROR, `Could not determine account id: ${JSON.stringify(response.body)}`);
|
||||
@@ -63,8 +63,8 @@ async function getZone(domainConfig, zoneName) {
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.statusCode !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
|
||||
if (!Array.isArray(response.body.data)) throw new BoxError(BoxError.EXTERNAL_ERROR, `Invalid data in response: ${JSON.stringify(response.body)}`);
|
||||
|
||||
@@ -88,8 +88,8 @@ async function getDnsRecords(domainConfig, zoneName, name, type) {
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.statusCode !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (!Array.isArray(response.body.data)) throw new BoxError(BoxError.EXTERNAL_ERROR, `Invalid data in response: ${JSON.stringify(response.body)}`);
|
||||
|
||||
return response.body.data;
|
||||
@@ -111,7 +111,8 @@ async function upsert(domainObject, location, type, values) {
|
||||
const records = await getDnsRecords(domainConfig, zoneName, name, type);
|
||||
|
||||
// used to track available records to update instead of create
|
||||
let i = 0, recordIds = [];
|
||||
let i = 0;
|
||||
const recordIds = [];
|
||||
|
||||
for (let value of values) {
|
||||
let priority = 0;
|
||||
@@ -139,8 +140,8 @@ async function upsert(domainObject, location, type, values) {
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.statusCode !== 201) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.status !== 201) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
recordIds.push(safe.query(response.body, 'data.id'));
|
||||
} else {
|
||||
const [error, response] = await safe(superagent.patch(`${DNSIMPLE_API}/${accountId}/zones/${zoneId}/records/${records[i].id}`)
|
||||
@@ -152,8 +153,8 @@ async function upsert(domainObject, location, type, values) {
|
||||
++i;
|
||||
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.statusCode !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
recordIds.push(safe.query(response.body, 'data.id'));
|
||||
}
|
||||
}
|
||||
@@ -208,9 +209,9 @@ async function del(domainObject, location, type, values) {
|
||||
.ok(() => true));
|
||||
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.statusCode === 404) continue;
|
||||
if (response.statusCode !== 204) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
|
||||
if (response.status === 404) continue;
|
||||
if (response.status !== 204) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user