Skip to content

Commit

Permalink
More startup checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunoneill-r7 committed Jul 7, 2023
1 parent 72e9a52 commit 1b2e12a
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 98 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"type": "module",
"scripts": {
"start": "node ./src/index"
"start": "node ./src/index",
"dev": "node -r dotenv/config ./src/index"
},
"repository": {
"type": "git",
Expand All @@ -18,8 +19,9 @@
"license": "ISC",
"dependencies": {
"cron": "^2.3.1",
"cron-parser": "^4.8.1",
"dotenv": "^16.3.1",
"node-fetch": "^3.3.1"
},
"devDependencies": {
"dotenv": "^16.3.1"
}
}
122 changes: 108 additions & 14 deletions src/apiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
CREATE_ZONE_RECORD_URL,
} from "./constants.js";

const WEBHOOK_URL = process.env.WEBHOOK_URL;
const WEBHOOK_METHOD = process.env.WEBHOOK_METHOD;

const defaultHeaders = {
'Authorization': `Bearer ${process.env.CLOUDFLARE_API_KEY}`,
}
Expand All @@ -17,13 +20,25 @@ export const verifyCloudflareToken = async () => {
method: 'GET',
headers: defaultHeaders,
});
const json = await response.json();
if (json.result && json.result.status === 'active') {
return true;

if (response.ok) {
const json = await response.json();
if (json.result && json.result.status === 'active') {
return true;
}
return false;
}
return false;

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
return false;
}
};

Expand All @@ -32,8 +47,19 @@ export const getPublicIpAddress = async () => {
const response = await fetch('https://api.ipify.org?format=json', {
method: 'GET',
});
const json = await response.json();
return json;

if (response.ok) {
const json = await response.json();
return json;
}

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
}
Expand All @@ -45,8 +71,19 @@ export const getCloudflareZones = async () => {
method: 'GET',
headers: defaultHeaders,
});
const json = await response.json();
return json;

if (response.ok) {
const json = await response.json();
return json;
}

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
}
Expand All @@ -58,8 +95,18 @@ export const getCloudflareZoneRecords = async (zoneId) => {
method: 'GET',
headers: defaultHeaders,
});
const json = await response.json();
return json;
if (response.ok) {
const json = await response.json();
return json;
}

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
}
Expand All @@ -75,8 +122,19 @@ export const updateCloudflareZoneRecord = async (zoneId, recordId, data) => {
},
body: JSON.stringify(data),
});
const json = await response.json();
return json;

if (response.ok) {
const json = await response.json();
return json;
}

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
}
Expand All @@ -92,8 +150,44 @@ export const createCloudflareZoneRecord = async (zoneId, data) => {
},
body: JSON.stringify(data),
});
const json = await response.json();
return json;
if (response.ok) {
const json = await response.json();
return json;
}

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
}
};

export const sendWebhookRequest = async (data) => {
try {
const response = await fetch(WEBHOOK_URL, {
method: WEBHOOK_METHOD,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (response.ok) {
const json = await response.json();
return json;
}

// Check if the response code is 429 (too many requests)
if (response.status === 429) {
throw new Error('Too many requests');
}

// If the response code is not 429, throw an error
throw new Error(response.status);
} catch (error) {
console.error(error);
}
Expand Down
Loading

0 comments on commit 1b2e12a

Please sign in to comment.