replace with custom superagent based on fetch API
This commit is contained in:
@@ -18,7 +18,7 @@ const assert = require('assert'),
|
||||
dns = require('../dns.js'),
|
||||
ipaddr = require('ipaddr.js'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent'),
|
||||
superagent = require('../superagent.js'),
|
||||
waitForDns = require('./waitfordns.js'),
|
||||
_ = require('../underscore.js');
|
||||
|
||||
@@ -34,23 +34,23 @@ function injectPrivateFields(newConfig, currentConfig) {
|
||||
if (newConfig.token === constants.SECRET_PLACEHOLDER) newConfig.token = currentConfig.token;
|
||||
}
|
||||
|
||||
function translateRequestError(result) {
|
||||
assert.strictEqual(typeof result, 'object');
|
||||
function translateResponseError(response) {
|
||||
assert.strictEqual(typeof response, 'object');
|
||||
|
||||
if (result.statusCode === 404) return new BoxError(BoxError.NOT_FOUND, `[${result.statusCode}] ${JSON.stringify(result.body)}`);
|
||||
if (result.statusCode === 422) return new BoxError(BoxError.BAD_FIELD, result.body.message);
|
||||
if (result.statusCode === 400 || result.statusCode === 401 || result.statusCode === 403) {
|
||||
if (response.status === 404) return new BoxError(BoxError.NOT_FOUND, `[${response.status}] ${response.text}`);
|
||||
if (response.status === 422) return new BoxError(BoxError.BAD_FIELD, response.body.message);
|
||||
if (response.status === 400 || response.status === 401 || response.status === 403) {
|
||||
let message = 'Unknown error';
|
||||
if (typeof result.body.error === 'string') {
|
||||
message = `[${result.statusCode}] ${result.body.error}`;
|
||||
} else if (Array.isArray(result.body.errors) && result.body.errors.length > 0) {
|
||||
const error = result.body.errors[0];
|
||||
message = `[${result.statusCode}] ${error.message} code:${error.code}`;
|
||||
if (typeof response.body.error === 'string') {
|
||||
message = `[${response.status}] ${response.body.error}`;
|
||||
} else if (Array.isArray(response.body.errors) && response.body.errors.length > 0) {
|
||||
const error = response.body.errors[0];
|
||||
message = `[${response.status}] ${error.message} code:${error.code}`;
|
||||
}
|
||||
return new BoxError(BoxError.ACCESS_DENIED, message);
|
||||
}
|
||||
|
||||
return new BoxError(BoxError.EXTERNAL_ERROR, `${result.statusCode} ${JSON.stringify(result.body)}`);
|
||||
return new BoxError(BoxError.EXTERNAL_ERROR, `${response.status} ${response.text}`);
|
||||
}
|
||||
|
||||
function createRequest(method, url, domainConfig) {
|
||||
@@ -58,7 +58,7 @@ function createRequest(method, url, domainConfig) {
|
||||
assert.strictEqual(typeof url, 'string');
|
||||
assert.strictEqual(typeof domainConfig, 'object');
|
||||
|
||||
const request = superagent(method, url).timeout(30 * 1000).ok(() => true);
|
||||
const request = superagent.request(method, url).timeout(30 * 1000).ok(() => true);
|
||||
|
||||
if (domainConfig.tokenType === 'GlobalApiKey') {
|
||||
request.set('X-Auth-Key', domainConfig.token).set('X-Auth-Email', domainConfig.email);
|
||||
@@ -75,15 +75,15 @@ async function getZoneByName(domainConfig, zoneName) {
|
||||
|
||||
const [error, response] = await safe(createRequest('GET', `${CLOUDFLARE_ENDPOINT}/zones?name=${zoneName}&status=active`, domainConfig));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode !== 200 || response.body.success !== true) throw translateRequestError(response);
|
||||
if (!response.body.result || !response.body.result.length) throw new BoxError(BoxError.NOT_FOUND, `${response.statusCode} ${response.text}`);
|
||||
if (response.status !== 200 || response.body.success !== true) throw translateResponseError(response);
|
||||
if (!response.body.result || !response.body.result.length) throw new BoxError(BoxError.NOT_FOUND, `${response.status} ${response.text}`);
|
||||
|
||||
// check 'id' and 'name_servers' exist in the response
|
||||
const zone = response.body.result[0];
|
||||
const zoneId = safe.query(zone, 'id');
|
||||
if (typeof zoneId !== 'string') throw new BoxError(BoxError.EXTERNAL_ERROR, `No zone id in response: ${response.statusCode} ${response.text}`);
|
||||
if (typeof zoneId !== 'string') throw new BoxError(BoxError.EXTERNAL_ERROR, `No zone id in response: ${response.status} ${response.text}`);
|
||||
const name_servers = safe.query(zone, 'name_servers');
|
||||
if (!Array.isArray(name_servers)) throw new BoxError(BoxError.EXTERNAL_ERROR, `name_servers is not an array: ${response.statusCode} ${response.text}`);
|
||||
if (!Array.isArray(name_servers)) throw new BoxError(BoxError.EXTERNAL_ERROR, `name_servers is not an array: ${response.status} ${response.text}`);
|
||||
|
||||
return zone;
|
||||
}
|
||||
@@ -99,11 +99,11 @@ async function getDnsRecords(domainConfig, zoneId, fqdn, type) {
|
||||
.query({ type: type, name: fqdn }));
|
||||
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode !== 200 || response.body.success !== true) throw translateRequestError(response);
|
||||
if (response.status !== 200 || response.body.success !== true) throw translateResponseError(response);
|
||||
|
||||
const result = response.body.result;
|
||||
if (result === null) return []; // sometime about now, cloudflare API has started returning null instead of empty array
|
||||
if (!Array.isArray(result)) throw new BoxError(BoxError.EXTERNAL_ERROR, `result is not an array when getting records: ${response.statusCode} ${response.text}`);
|
||||
if (!Array.isArray(result)) throw new BoxError(BoxError.EXTERNAL_ERROR, `result is not an array when getting records: ${response.status} ${response.text}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ async function upsert(domainObject, location, type, values) {
|
||||
const [error, response] = await safe(createRequest('POST', `${CLOUDFLARE_ENDPOINT}/zones/${zoneId}/dns_records`, domainConfig)
|
||||
.send(data));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode !== 200 || response.body.success !== true) throw translateRequestError(response);
|
||||
if (response.status !== 200 || response.body.success !== true) throw translateResponseError(response);
|
||||
} else { // replace existing record
|
||||
data.proxied = records[i].proxied; // preserve proxied parameter
|
||||
|
||||
@@ -164,7 +164,7 @@ async function upsert(domainObject, location, type, values) {
|
||||
const [error, response] = await safe(createRequest('PUT', `${CLOUDFLARE_ENDPOINT}/zones/${zoneId}/dns_records/${records[i].id}`, domainConfig)
|
||||
.send(data));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode !== 200 || response.body.success !== true) throw translateRequestError(response);
|
||||
if (response.status !== 200 || response.body.success !== true) throw translateResponseError(response);
|
||||
++i; // increment, as we have consumed the record
|
||||
}
|
||||
}
|
||||
@@ -214,7 +214,7 @@ async function del(domainObject, location, type, values) {
|
||||
for (const r of tmp) {
|
||||
const [error, response] = await safe(createRequest('DELETE', `${CLOUDFLARE_ENDPOINT}/zones/${zone.id}/dns_records/${r.id}`, domainConfig));
|
||||
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
|
||||
if (response.statusCode !== 200 || response.body.success !== true) throw translateRequestError(response);
|
||||
if (response.status !== 200 || response.body.success !== true) throw translateResponseError(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user