replace with custom superagent based on fetch API

This commit is contained in:
Girish Ramakrishnan
2025-02-14 17:26:54 +01:00
parent 68a08b1f62
commit 8e58349bfa
66 changed files with 1086 additions and 1031 deletions
+17 -16
View File
@@ -17,7 +17,7 @@ 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 BUNNY_API = 'https://api.bunny.net';
@@ -32,7 +32,7 @@ function recordTypeToInt(value) {
}
function formatError(response) {
return `Bunny DNS error ${response.statusCode} ${JSON.stringify(response.body)}`;
return `Bunny DNS error ${response.status} ${response.text}`;
}
function removePrivateFields(domainObject) {
@@ -54,10 +54,10 @@ async function getZoneId(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.Items)) throw new BoxError(BoxError.EXTERNAL_ERROR, `Invalid records in response: ${JSON.stringify(response.body)}`);
if (!Array.isArray(response.body.Items)) throw new BoxError(BoxError.EXTERNAL_ERROR, `Invalid records in response: ${response.text}`);
const item = response.body.Items.filter(item => item.Domain === zoneName);
if (item.length === 0) throw new BoxError(BoxError.NOT_FOUND, 'Domain not found');
@@ -79,9 +79,9 @@ 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 (!Array.isArray(response.body.Records)) throw new BoxError(BoxError.EXTERNAL_ERROR, `Invalid records in response: ${JSON.stringify(response.body)}`);
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.Records)) throw new BoxError(BoxError.EXTERNAL_ERROR, `Invalid records in response: ${response.text}`);
return response.body.Records.filter(r => recordTypeToString(r.Type) === type && r.Name === name);
}
@@ -102,7 +102,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;
@@ -130,8 +131,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));
} else {
const [error, response] = await safe(superagent.post(`${BUNNY_API}/dnszone/${zoneId}/records/${records[i].Id}`)
.set('AccessKey', domainConfig.accessKey)
@@ -142,8 +143,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 !== 204) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
if (response.status === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
if (response.status !== 204) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
recordIds.push(safe.query(records.body, 'domain_record.id'));
}
@@ -199,9 +200,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 === 400) 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 === 400) continue;
if (response.status !== 204) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
}
}