feat(core): Release 6.2.0: Improved async iterator support, enhanced error handling and refined API interfaces for better type safety and consistent behavior.

This commit is contained in:
2025-04-26 12:15:16 +00:00
parent 092a6ba55b
commit 1b34bee35d
8 changed files with 301 additions and 120 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@apiclient.xyz/cloudflare',
version: '6.1.0',
version: '6.3.0',
description: 'A TypeScript client for managing Cloudflare accounts, zones, DNS records, and workers with ease.'
}

View File

@ -32,12 +32,14 @@ export class CloudflareAccount {
* @param method HTTP method (GET, POST, PUT, DELETE)
* @param endpoint API endpoint path
* @param data Optional request body data
* @param customHeaders Optional custom headers to override defaults
* @returns API response
*/
public async request<T = any>(
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH',
endpoint: string,
data?: any
data?: any,
customHeaders?: Record<string, string>
): Promise<T> {
try {
const options: plugins.smartrequest.ISmartRequestOptions = {
@ -45,15 +47,48 @@ export class CloudflareAccount {
headers: {
'Authorization': `Bearer ${this.authToken}`,
'Content-Type': 'application/json',
...customHeaders,
},
};
if (data) {
options.requestBody = JSON.stringify(data);
if (customHeaders && customHeaders['Content-Type']?.includes('multipart/form-data')) {
// For multipart form data, use the data directly as the request body
options.requestBody = data;
} else {
// For JSON requests, stringify the data
options.requestBody = JSON.stringify(data);
}
}
logger.log('debug', `Making ${method} request to ${endpoint}`);
const response = await plugins.smartrequest.request(`https://api.cloudflare.com/client/v4${endpoint}`, options);
return JSON.parse(response.body);
// Check if response is already an object (might happen with newer smartrequest versions)
if (typeof response.body === 'object' && response.body !== null) {
return response.body;
}
// Otherwise try to parse as JSON
try {
if (typeof response.body === 'string' && response.body.trim()) {
return JSON.parse(response.body);
} else {
// If body is empty or not a string, return an empty result
logger.log('warn', `Empty or invalid response body: ${typeof response.body}`);
return { result: [] } as T;
}
} catch (parseError) {
logger.log('warn', `Failed to parse response as JSON: ${parseError.message}`);
// Create a fake response object to maintain expected structure
return {
result: [],
success: true,
errors: [],
messages: [`Failed to parse: ${typeof response.body === 'string' ? response.body?.substring(0, 50) : typeof response.body}...`]
} as T;
}
} catch (error) {
logger.log('error', `Cloudflare API request failed: ${error.message}`);
throw error;
@ -74,14 +109,24 @@ export class CloudflareAccount {
public convenience = {
/**
* listAccounts
* Lists all accounts accessible with the current API token
* @returns Array of Cloudflare account objects
*/
listAccounts: async () => {
const accounts: plugins.ICloudflareTypes['Account'][] = [];
for await (const account of this.apiAccount.accounts.list()) {
accounts.push(account as interfaces.ICloudflareApiAccountObject);
try {
const accounts: plugins.ICloudflareTypes['Account'][] = [];
// Collect all accounts using async iterator
for await (const account of this.apiAccount.accounts.list()) {
accounts.push(account as interfaces.ICloudflareApiAccountObject);
}
logger.log('info', `Found ${accounts.length} accounts`);
return accounts;
} catch (error) {
logger.log('error', `Failed to list accounts: ${error.message}`);
return [];
}
return accounts;
},
/**
* gets a zone id of a domain from cloudflare
@ -262,27 +307,19 @@ export class CloudflareAccount {
* @param domainNameArg - the domain name that you want to get the records from
*/
listRecords: async (domainNameArg: string) => {
const domain = new plugins.smartstring.Domain(domainNameArg);
const zoneId = await this.convenience.getZoneId(domain.zoneName);
const records: plugins.ICloudflareTypes['Record'][] = [];
try {
const result = await this.apiAccount.dns.records.list({
zone_id: zoneId,
});
const domain = new plugins.smartstring.Domain(domainNameArg);
const zoneId = await this.convenience.getZoneId(domain.zoneName);
const records: plugins.ICloudflareTypes['Record'][] = [];
// Check if the result has a 'result' property (API response format)
if (result && result.result && Array.isArray(result.result)) {
return result.result;
}
// Otherwise iterate through async iterator (new client format)
// Collect all records using async iterator
for await (const record of this.apiAccount.dns.records.list({
zone_id: zoneId,
})) {
records.push(record);
}
logger.log('info', `Found ${records.length} DNS records for ${domainNameArg}`);
return records;
} catch (error) {
logger.log('error', `Failed to list records for ${domainNameArg}: ${error.message}`);
@ -291,29 +328,23 @@ export class CloudflareAccount {
},
/**
* list all zones in the associated authenticated account
* @param domainName
* @param domainName optional filter by domain name
*/
listZones: async (domainName?: string) => {
const options: any = {};
if (domainName) {
options.name = domainName;
}
const zones: plugins.ICloudflareTypes['Zone'][] = [];
try {
const result = await this.apiAccount.zones.list(options);
// Check if the result has a 'result' property (API response format)
if (result && result.result && Array.isArray(result.result)) {
return result.result;
const options: any = {};
if (domainName) {
options.name = domainName;
}
// Otherwise iterate through async iterator (new client format)
const zones: plugins.ICloudflareTypes['Zone'][] = [];
// Collect all zones using async iterator
for await (const zone of this.apiAccount.zones.list(options)) {
zones.push(zone);
}
logger.log('info', `Found ${zones.length} zones${domainName ? ` matching ${domainName}` : ''}`);
return zones;
} catch (error) {
logger.log('error', `Failed to list zones: ${error.message}`);

View File

@ -44,27 +44,52 @@ export class CloudflareWorker {
* gets all routes for a worker
*/
public async getRoutes() {
const zones = await this.workerManager.cfAccount.convenience.listZones();
for (const zone of zones) {
try {
// The official client doesn't have a direct method to list worker routes
// We'll use the custom request method for this specific case
const response: {
result: interfaces.ICflareWorkerRoute[];
} = await this.workerManager.cfAccount.request('GET', `/zones/${zone.id}/workers/routes`);
for (const route of response.result) {
logger.log('debug', `Processing route: ${route.pattern}`);
logger.log('debug', `Comparing script: ${route.script} with worker ID: ${this.id}`);
if (route.script === this.id) {
this.routes.push({ ...route, zoneName: zone.name });
}
}
} catch (error) {
logger.log('error', `Failed to get worker routes for zone ${zone.name}: ${error.message}`);
try {
this.routes = []; // Reset routes before fetching
// Get all zones using the async iterator
const zones: plugins.ICloudflareTypes['Zone'][] = [];
for await (const zone of this.workerManager.cfAccount.apiAccount.zones.list()) {
zones.push(zone);
}
if (zones.length === 0) {
logger.log('warn', 'No zones found for the account');
return;
}
for (const zone of zones) {
try {
if (!zone || !zone.id) {
logger.log('warn', 'Zone is missing ID property');
continue;
}
// Get worker routes for this zone
const apiRoutes = [];
for await (const route of this.workerManager.cfAccount.apiAccount.workers.routes.list({
zone_id: zone.id
})) {
apiRoutes.push(route);
}
// Filter for routes that match this worker's ID
for (const route of apiRoutes) {
if (route.script === this.id) {
logger.log('debug', `Found route for worker ${this.id}: ${route.pattern}`);
this.routes.push({ ...route, zoneName: zone.name });
}
}
} catch (error) {
logger.log('error', `Failed to get worker routes for zone ${zone.name || zone.id}: ${error.message}`);
}
}
logger.log('info', `Found ${this.routes.length} routes for worker ${this.id}`);
} catch (error) {
logger.log('error', `Failed to get routes for worker ${this.id}: ${error.message}`);
// Initialize routes as empty array in case of error
this.routes = [];
}
}
@ -73,42 +98,49 @@ export class CloudflareWorker {
* @param routeArray Array of route definitions
*/
public async setRoutes(routeArray: IWorkerRouteDefinition[]) {
// First get all existing routes to determine what we need to create/update
await this.getRoutes();
for (const newRoute of routeArray) {
// Determine whether a route is new, needs an update, or is already up to date
let routeStatus: 'new' | 'needsUpdate' | 'alreadyUpToDate' = 'new';
let routeIdForUpdate: string;
let existingRouteId: string;
for (const existingRoute of this.routes) {
if (existingRoute.pattern === newRoute.pattern) {
routeStatus = 'needsUpdate';
routeIdForUpdate = existingRoute.id;
existingRouteId = existingRoute.id;
if (existingRoute.script === this.id) {
routeStatus = 'alreadyUpToDate';
logger.log('info', `Route already exists, no update needed`);
logger.log('info', `Route ${newRoute.pattern} already exists, no update needed`);
}
}
}
try {
const zoneId = await this.workerManager.cfAccount.convenience.getZoneId(newRoute.zoneName);
// Get the zone ID
const zone = await this.workerManager.cfAccount.zoneManager.getZoneByName(newRoute.zoneName);
// Handle route creation or update
if (!zone) {
logger.log('error', `Zone ${newRoute.zoneName} not found`);
continue;
}
// Handle route creation, update, or skip if already up to date
if (routeStatus === 'new') {
// The official client doesn't have a direct method to create worker routes
// We'll use the custom request method for this specific case
await this.workerManager.cfAccount.request('POST', `/zones/${zoneId}/workers/routes`, {
await this.workerManager.cfAccount.apiAccount.workers.routes.create({
zone_id: zone.id,
pattern: newRoute.pattern,
script: this.id,
script: this.id
});
logger.log('info', `Created new route ${newRoute.pattern} for worker ${this.id}`);
} else if (routeStatus === 'needsUpdate') {
// The official client doesn't have a direct method to update worker routes
// We'll use the custom request method for this specific case
await this.workerManager.cfAccount.request('PUT', `/zones/${zoneId}/workers/routes/${routeIdForUpdate}`, {
await this.workerManager.cfAccount.apiAccount.workers.routes.update(existingRouteId, {
zone_id: zone.id,
pattern: newRoute.pattern,
script: this.id,
script: this.id
});
logger.log('info', `Updated route ${newRoute.pattern} for worker ${this.id}`);
@ -117,6 +149,9 @@ export class CloudflareWorker {
logger.log('error', `Failed to set route ${newRoute.pattern}: ${error.message}`);
}
}
// Refresh routes after all changes
await this.getRoutes();
}
/**
@ -132,15 +167,20 @@ export class CloudflareWorker {
try {
logger.log('info', `Updating script for worker ${this.id}`);
// The official client requires the metadata property
// Use the official client to update the script
const updatedWorker = await this.workerManager.cfAccount.apiAccount.workers.scripts.content.update(this.id, {
account_id: this.workerManager.cfAccount.preselectedAccountId,
"CF-WORKER-BODY-PART": scriptContent,
metadata: {} // Required empty object
metadata: {}
});
// Update this instance with new data
Object.assign(this, updatedWorker);
if (updatedWorker && typeof updatedWorker === 'object') {
Object.assign(this, updatedWorker);
}
// Always ensure the script property is updated
this.script = scriptContent;
return this;
} catch (error) {
@ -161,6 +201,7 @@ export class CloudflareWorker {
try {
logger.log('info', `Deleting worker ${this.id}`);
// Use the official client to delete the worker
await this.workerManager.cfAccount.apiAccount.workers.scripts.delete(this.id, {
account_id: this.workerManager.cfAccount.preselectedAccountId
});

View File

@ -22,19 +22,25 @@ export class WorkerManager {
}
try {
// Create or update the worker script
// Use the official client to create/update the worker
await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, {
account_id: this.cfAccount.preselectedAccountId,
"CF-WORKER-BODY-PART": workerScript,
metadata: {} // Required empty object
metadata: {}
});
// Create a new worker instance directly
// Create a new worker instance
const worker = new CloudflareWorker(this);
worker.id = workerName;
worker.script = workerScript;
// Initialize the worker and get its routes
await worker.getRoutes();
try {
await worker.getRoutes();
} catch (routeError) {
logger.log('warn', `Failed to get routes for worker ${workerName}: ${routeError.message}`);
// Continue anyway since the worker was created
}
return worker;
} catch (error) {
@ -54,17 +60,27 @@ export class WorkerManager {
}
try {
// Check if the worker exists
await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
// Get the worker script using the official client
const workerScript = await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
account_id: this.cfAccount.preselectedAccountId
});
// Create a new worker instance directly
// Create a new worker instance
const worker = new CloudflareWorker(this);
worker.id = workerName;
// Save script content if available
if (workerScript && typeof workerScript === 'object') {
Object.assign(worker, workerScript);
}
// Initialize the worker and get its routes
await worker.getRoutes();
try {
await worker.getRoutes();
} catch (routeError) {
logger.log('warn', `Failed to get routes for worker ${workerName}: ${routeError.message}`);
// Continue anyway since we found the worker
}
return worker;
} catch (error) {
@ -83,23 +99,35 @@ export class WorkerManager {
}
try {
const result = await this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
});
// Check if the result has a 'result' property (API response format)
if (result && result.result && Array.isArray(result.result)) {
return result.result;
}
// Otherwise collect from async iterator (new client format)
// Collect all scripts using the new client's async iterator
const workerScripts: plugins.ICloudflareTypes['Script'][] = [];
for await (const scriptArg of this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
})) {
workerScripts.push(scriptArg);
try {
for await (const script of this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
})) {
workerScripts.push(script);
}
logger.log('info', `Found ${workerScripts.length} worker scripts`);
return workerScripts;
} catch (error) {
logger.log('warn', `Error while listing workers with async iterator: ${error.message}`);
// Try alternative approach if the async iterator fails
const result = await this.cfAccount.apiAccount.workers.scripts.list({
account_id: this.cfAccount.preselectedAccountId,
}) as any;
// Check if the result has a 'result' property (older API response format)
if (result && result.result && Array.isArray(result.result)) {
logger.log('info', `Found ${result.result.length} worker scripts using direct result`);
return result.result;
}
}
return workerScripts;
logger.log('warn', 'Could not retrieve worker scripts');
return [];
} catch (error) {
logger.log('error', `Failed to list worker scripts: ${error.message}`);
return [];