Skip to content

Commit 77a31a0

Browse files
matej21claude
andcommitted
fix: resolve KV state namespace lookup on accounts with many namespaces
CF API defaults to per_page=20 when listing KV namespaces, so the state namespace could be missed on accounts with >20 namespaces. Increase to per_page=1000 (CF max) and add --state-namespace-id option to skip the lookup entirely. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent eeab7ff commit 77a31a0

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

src/commands/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class CloudflareDeployExecutor implements ResourceApplier {
2020
apiToken: input.apiToken,
2121
})
2222

23-
const stateStore = new KVStateStorage(cfClient, input.stateNamespace)
23+
const stateStore = new KVStateStorage(cfClient, input.stateNamespace, input.stateNamespaceId)
2424
const stateKey = { env: input.env }
2525
const state = await stateStore.get(stateKey)
2626

src/commands/input.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const input = (() => {
77
options: {
88
env: { type: 'string' },
99
'state-namespace': { type: 'string' },
10+
'state-namespace-id': { type: 'string' },
1011
'account-id': { type: 'string' },
1112
'api-token': { type: 'string' },
1213
'dry-run': { type: 'boolean' },
@@ -27,6 +28,7 @@ export const input = (() => {
2728
main,
2829
env: values.env || process.env.CLOUDFLARE_ENV || 'local',
2930
stateNamespace: values['state-namespace'] || 'cf-state',
31+
stateNamespaceId: values['state-namespace-id'],
3032
accountId: values['account-id'] || process.env.CLOUDFLARE_ACCOUNT_ID || '',
3133
apiToken: values['api-token'] || process.env.CLOUDFLARE_API_TOKEN || '',
3234
dryRun: values['dry-run'] ?? false,

src/state.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ export class KVStateStorage implements StateStorage {
2020
constructor(
2121
private readonly cfClient: CloudflareClient,
2222
private readonly namespace: string,
23+
namespaceId?: string,
2324
) {
25+
this.id = namespaceId
2426
}
2527

2628
private async resolveKvId(): Promise<string> {
2729
if (this.id) {
2830
return this.id
2931
}
30-
const id = await this.cfClient.fetch<{ title: string; id: string }[]>({
31-
url: `/storage/kv/namespaces`,
32+
const namespaces = await this.cfClient.fetch<{ title: string; id: string }[]>({
33+
url: `/storage/kv/namespaces?per_page=1000`,
3234
method: 'GET',
3335
})
34-
const existing = id.find(i => i.title === this.namespace)
36+
const existing = namespaces.find(i => i.title === this.namespace)
3537
if (existing) {
3638
this.id = existing.id
3739
return existing.id

0 commit comments

Comments
 (0)