fix(core): Improve logging consistency, record update functionality, and API error handling in Cloudflare modules
This commit is contained in:
@@ -1,9 +1,177 @@
|
||||
import * as plugins from './cloudflare.plugins.js';
|
||||
import { logger } from './cloudflare.logger.js';
|
||||
import * as interfaces from './interfaces/index.js';
|
||||
|
||||
export class CloudflareZone {
|
||||
public static createFromApiObject(apiObject: plugins.ICloudflareTypes['Zone']) {
|
||||
// Zone properties
|
||||
public id: string;
|
||||
public name: string;
|
||||
public status: interfaces.ICflareZone['status'];
|
||||
public paused: boolean;
|
||||
public type: interfaces.ICflareZone['type'];
|
||||
public development_mode: number;
|
||||
public name_servers: string[];
|
||||
public original_name_servers: string[];
|
||||
public original_registrar: string | null;
|
||||
public original_dnshost: string | null;
|
||||
public modified_on: string;
|
||||
public created_on: string;
|
||||
public activated_on: string;
|
||||
public meta: interfaces.ICflareZone['meta'];
|
||||
public owner: interfaces.ICflareZone['owner'];
|
||||
public account: interfaces.ICflareZone['account'];
|
||||
public permissions: string[];
|
||||
public plan: interfaces.ICflareZone['plan'];
|
||||
|
||||
private cfAccount: any; // Will be set when created through a manager
|
||||
|
||||
/**
|
||||
* Create a CloudflareZone instance from an API object
|
||||
* @param apiObject Cloudflare Zone API object
|
||||
* @param cfAccount Optional Cloudflare account instance
|
||||
* @returns CloudflareZone instance
|
||||
*/
|
||||
public static createFromApiObject(
|
||||
apiObject: plugins.ICloudflareTypes['Zone'],
|
||||
cfAccount?: any
|
||||
): CloudflareZone {
|
||||
const cloudflareZone = new CloudflareZone();
|
||||
Object.assign(cloudflareZone, apiObject);
|
||||
|
||||
if (cfAccount) {
|
||||
cloudflareZone.cfAccount = cfAccount;
|
||||
}
|
||||
|
||||
return cloudflareZone;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if development mode is currently active
|
||||
* @returns True if development mode is active
|
||||
*/
|
||||
public isDevelopmentModeActive(): boolean {
|
||||
return this.development_mode > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable development mode for the zone
|
||||
* @param cfAccount Cloudflare account to use if not already set
|
||||
* @param duration Duration in seconds (default: 3 hours)
|
||||
* @returns Updated zone
|
||||
*/
|
||||
public async enableDevelopmentMode(
|
||||
cfAccount?: any,
|
||||
duration: number = 10800
|
||||
): Promise<CloudflareZone> {
|
||||
const account = cfAccount || this.cfAccount;
|
||||
if (!account) {
|
||||
throw new Error('CloudflareAccount is required to enable development mode');
|
||||
}
|
||||
|
||||
logger.log('info', `Enabling development mode for zone ${this.name}`);
|
||||
|
||||
const response = await account.request('PATCH', `/zones/${this.id}/settings/development_mode`, {
|
||||
value: 'on',
|
||||
time: duration
|
||||
});
|
||||
|
||||
this.development_mode = duration;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable development mode for the zone
|
||||
* @param cfAccount Cloudflare account to use if not already set
|
||||
* @returns Updated zone
|
||||
*/
|
||||
public async disableDevelopmentMode(cfAccount?: any): Promise<CloudflareZone> {
|
||||
const account = cfAccount || this.cfAccount;
|
||||
if (!account) {
|
||||
throw new Error('CloudflareAccount is required to disable development mode');
|
||||
}
|
||||
|
||||
logger.log('info', `Disabling development mode for zone ${this.name}`);
|
||||
|
||||
const response = await account.request('PATCH', `/zones/${this.id}/settings/development_mode`, {
|
||||
value: 'off'
|
||||
});
|
||||
|
||||
this.development_mode = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge all cached content for this zone
|
||||
* @param cfAccount Cloudflare account to use if not already set
|
||||
* @returns True if successful
|
||||
*/
|
||||
public async purgeCache(cfAccount?: any): Promise<boolean> {
|
||||
const account = cfAccount || this.cfAccount;
|
||||
if (!account) {
|
||||
throw new Error('CloudflareAccount is required to purge cache');
|
||||
}
|
||||
|
||||
logger.log('info', `Purging all cache for zone ${this.name}`);
|
||||
|
||||
try {
|
||||
await account.request('POST', `/zones/${this.id}/purge_cache`, {
|
||||
purge_everything: true
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.log('error', `Failed to purge cache: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge specific URLs from the cache
|
||||
* @param urls Array of URLs to purge
|
||||
* @param cfAccount Cloudflare account to use if not already set
|
||||
* @returns True if successful
|
||||
*/
|
||||
public async purgeUrls(urls: string[], cfAccount?: any): Promise<boolean> {
|
||||
const account = cfAccount || this.cfAccount;
|
||||
if (!account) {
|
||||
throw new Error('CloudflareAccount is required to purge URLs');
|
||||
}
|
||||
|
||||
if (!urls.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
logger.log('info', `Purging ${urls.length} URLs from cache for zone ${this.name}`);
|
||||
|
||||
try {
|
||||
await account.request('POST', `/zones/${this.id}/purge_cache`, {
|
||||
files: urls
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.log('error', `Failed to purge URLs: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the zone is active
|
||||
* @returns True if the zone is active
|
||||
*/
|
||||
public isActive(): boolean {
|
||||
return this.status === 'active' && !this.paused;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the zone is using Cloudflare nameservers
|
||||
* @returns True if using Cloudflare nameservers
|
||||
*/
|
||||
public isUsingCloudflareNameservers(): boolean {
|
||||
// Check if original nameservers match current nameservers
|
||||
if (!this.original_name_servers || !this.name_servers) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If they're different, and current nameservers are Cloudflare's
|
||||
return this.name_servers.some(ns => ns.includes('cloudflare'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user