Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 6 additions & 38 deletions alchemy/src/cloudflare/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Context } from "../context.ts";
import { Resource } from "../resource.ts";
import { logger } from "../util/logger.ts";
import { handleApiError } from "./api-error.ts";
import { extractCloudflareResult } from "./api-response.ts";
import {
createCloudflareApi,
type CloudflareApi,
Expand Down Expand Up @@ -568,45 +569,12 @@ async function getZoneSettings(
export async function getZoneByDomain(
api: CloudflareApi,
domainName: string,
): Promise<ZoneData | null> {
const response = await api.get(
`/zones?name=${encodeURIComponent(domainName)}`,
): Promise<CloudflareZone | undefined> {
const [zone] = await extractCloudflareResult<CloudflareZone[]>(
`get zone for ${domainName}`,
api.get(`/zones?name=${encodeURIComponent(domainName)}`),
);

if (!response.ok) {
throw new Error(
`Error fetching zone for '${domainName}': ${response.statusText}`,
);
}

const zones = ((await response.json()) as { result: CloudflareZone[] })
.result;

if (zones.length === 0) {
return null;
}

const zoneData = zones[0];

// Get zone settings
const settings = await getZoneSettings(api, zoneData.id);

return {
id: zoneData.id,
name: zoneData.name,
type: zoneData.type,
status: zoneData.status,
paused: zoneData.paused,
accountId: zoneData.account.id,
nameservers: zoneData.name_servers,
originalNameservers: zoneData.original_name_servers,
createdAt: new Date(zoneData.created_on).getTime(),
modifiedAt: new Date(zoneData.modified_on).getTime(),
activatedAt: zoneData.activated_on
? new Date(zoneData.activated_on).getTime()
: null,
settings,
};
return zone;
}

/**
Expand Down
15 changes: 6 additions & 9 deletions alchemy/test/cloudflare/zone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,18 @@ describe.skipIf(!process.env.ALL_TESTS)("Zone Resource", () => {
const foundZone = await getZoneByDomain(api, lookupTestDomain);

// Verify the lookup returned the correct zone
expect(foundZone).toBeTruthy();
expect(foundZone!.id).toEqual(zone.id);
expect(foundZone!.name).toEqual(lookupTestDomain);
expect(foundZone!.type).toEqual("full");
expect(foundZone!.accountId).toEqual(zone.accountId);
expect(foundZone!.nameservers).toEqual(zone.nameservers);
expect(foundZone!.settings.ssl).toEqual("flexible");
expect(foundZone!.settings.alwaysUseHttps).toEqual("on");
expect(foundZone).toMatchObject({
id: zone.id,
name: lookupTestDomain,
type: "full",
});

// Test lookup of non-existent domain
const nonExistentZone = await getZoneByDomain(
api,
`${BRANCH_PREFIX}-non-existent.dev`,
);
expect(nonExistentZone).toBeNull();
expect(nonExistentZone).toBeUndefined();
} finally {
// Always clean up
await destroy(scope);
Expand Down
Loading