chore: Update checking kv namespace function to avoid failed to create namespace

This commit is contained in:
sunny
2025-03-05 13:28:45 +08:00
parent da979d2a51
commit b75d9ada43
2 changed files with 32 additions and 20 deletions
+12
View File
@@ -96,6 +96,18 @@ export const getKVNamespace = async (namespaceId: string) => {
} }
}; };
export const getKVNamespaceList = async () => {
try {
const kvNamespaces = await client.kv.namespaces.list({
account_id: CF_ACCOUNT_ID,
});
return kvNamespaces;
} catch (error) {
throw error;
}
}
export const createKVNamespace = async () => { export const createKVNamespace = async () => {
try { try {
console.log(`🆕 Creating new KV namespace: "${KV_NAMESPACE_NAME}"`); console.log(`🆕 Creating new KV namespace: "${KV_NAMESPACE_NAME}"`);
+20 -20
View File
@@ -9,6 +9,7 @@ import {
createPages, createPages,
getDatabase, getDatabase,
getKVNamespace, getKVNamespace,
getKVNamespaceList,
getPages, getPages,
} from "./cloudflare"; } from "./cloudflare";
@@ -212,31 +213,30 @@ const checkAndCreateKVNamespace = async () => {
console.log(`🔍 Checking if KV namespace "${KV_NAMESPACE_NAME}" exists...`); console.log(`🔍 Checking if KV namespace "${KV_NAMESPACE_NAME}" exists...`);
try { try {
if (!KV_NAMESPACE_ID) { let namespace;
console.log("⚠️ KV_NAMESPACE_ID is not set, creating a new KV namespace...");
const namespace = await createKVNamespace();
updateKVConfig(namespace.id);
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" created successfully (ID: ${namespace.id})`);
return;
}
const namespace = await getKVNamespace(KV_NAMESPACE_ID); if (KV_NAMESPACE_ID) {
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" already exists (ID: ${namespace.id})`); namespace = await getKVNamespace(KV_NAMESPACE_ID);
} catch (error) { console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" already exists (ID: ${namespace.id})`);
if (error instanceof NotFoundError || (error instanceof Error && error.message?.includes("required"))) { } else {
console.log(`⚠️ KV namespace not found or invalid, creating new KV namespace...`); console.log("⚠️ KV_NAMESPACE_ID is not set, checking by name...");
try {
const namespace = await createKVNamespace(); const namespaceList = await getKVNamespaceList();
namespace = namespaceList.result.find(ns => ns.title === KV_NAMESPACE_NAME);
if (namespace && namespace.id) {
updateKVConfig(namespace.id);
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" found by name (ID: ${namespace.id})`);
} else {
console.log("⚠️ KV namespace not found by name, creating new KV namespace...");
namespace = await createKVNamespace();
updateKVConfig(namespace.id); updateKVConfig(namespace.id);
console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" created successfully (ID: ${namespace.id})`); console.log(`✅ KV namespace "${KV_NAMESPACE_NAME}" created successfully (ID: ${namespace.id})`);
} catch (createError) {
console.error(`❌ Failed to create KV namespace:`, createError);
throw createError;
} }
} else {
console.error(`❌ An error occurred while checking the KV namespace:`, error);
throw error;
} }
} catch (error) {
console.error(`❌ An error occurred while checking the KV namespace:`, error);
throw error;
} }
}; };