diff options
| author | Dax Raad <[email protected]> | 2025-11-08 20:21:02 -0500 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-11-08 20:21:02 -0500 |
| commit | 4d20e1c3c6207cf6731ad8db9f92caf4ef31cb99 (patch) | |
| tree | 1c0b14810b8919e0d9dba022384da876717f8e2e /packages/console/resource | |
| parent | 4bb7ea91271e85621a1394c70867ddc26be45631 (diff) | |
| parent | 969af4d54122b727d7251b2977f617424d08e4cb (diff) | |
| download | opencode-4d20e1c3c6207cf6731ad8db9f92caf4ef31cb99.tar.gz opencode-4d20e1c3c6207cf6731ad8db9f92caf4ef31cb99.zip | |
Merge remote-tracking branch 'origin/dev' into dev
Diffstat (limited to 'packages/console/resource')
| -rw-r--r-- | packages/console/resource/package.json | 5 | ||||
| -rw-r--r-- | packages/console/resource/resource.node.ts | 59 | ||||
| -rw-r--r-- | packages/console/resource/sst-env.d.ts | 9 |
3 files changed, 71 insertions, 2 deletions
diff --git a/packages/console/resource/package.json b/packages/console/resource/package.json index 6553feed1..f110f6c2a 100644 --- a/packages/console/resource/package.json +++ b/packages/console/resource/package.json @@ -13,6 +13,9 @@ } }, "devDependencies": { - "@tsconfig/node22": "22.0.2" + "@cloudflare/workers-types": "catalog:", + "@tsconfig/node22": "22.0.2", + "@types/node": "catalog:", + "cloudflare": "5.2.0" } } diff --git a/packages/console/resource/resource.node.ts b/packages/console/resource/resource.node.ts index d7dbb6c6d..f63d7bada 100644 --- a/packages/console/resource/resource.node.ts +++ b/packages/console/resource/resource.node.ts @@ -1 +1,58 @@ -export { Resource } from "sst" +import type { KVNamespaceListOptions, KVNamespaceListResult, KVNamespacePutOptions } from "@cloudflare/workers-types" +import { Resource as ResourceBase } from "sst" +import Cloudflare from "cloudflare" + +export const Resource = new Proxy( + {}, + { + get(_target, prop: keyof typeof ResourceBase) { + const value = ResourceBase[prop] + // @ts-ignore + if ("type" in value && value.type === "sst.cloudflare.Kv") { + const client = new Cloudflare({ + apiToken: ResourceBase.CLOUDFLARE_API_TOKEN.value, + }) + // @ts-ignore + const namespaceId = value.namespaceId + const accountId = ResourceBase.CLOUDFLARE_DEFAULT_ACCOUNT_ID.value + return { + get: (k: string | string[]) => { + const isMulti = Array.isArray(k) + return client.kv.namespaces + .bulkGet(namespaceId, { + keys: Array.isArray(k) ? k : [k], + account_id: accountId, + }) + .then((result) => (isMulti ? new Map(Object.entries(result?.values ?? {})) : result?.values?.[k])) + }, + put: (k: string, v: string, opts?: KVNamespacePutOptions) => + client.kv.namespaces.values.update(namespaceId, k, { + account_id: accountId, + value: v, + expiration: opts?.expiration, + expiration_ttl: opts?.expirationTtl, + metadata: opts?.metadata, + }), + delete: (k: string) => + client.kv.namespaces.values.delete(namespaceId, k, { + account_id: accountId, + }), + list: (opts?: KVNamespaceListOptions): Promise<KVNamespaceListResult<unknown, string>> => + client.kv.namespaces.keys + .list(namespaceId, { + account_id: accountId, + prefix: opts?.prefix ?? undefined, + }) + .then((result) => { + return { + keys: result.result, + list_complete: true, + cacheStatus: null, + } + }), + } + } + return value + }, + }, +) as Record<string, any> diff --git a/packages/console/resource/sst-env.d.ts b/packages/console/resource/sst-env.d.ts index ba4c5a623..bcd7c2650 100644 --- a/packages/console/resource/sst-env.d.ts +++ b/packages/console/resource/sst-env.d.ts @@ -22,6 +22,14 @@ declare module "sst" { type: "sst.sst.Secret" value: string } + CLOUDFLARE_API_TOKEN: { + type: "sst.sst.Secret" + value: string + } + CLOUDFLARE_DEFAULT_ACCOUNT_ID: { + type: "sst.sst.Secret" + value: string + } Console: { type: "sst.cloudflare.SolidStart" url: string @@ -96,6 +104,7 @@ declare module "sst" { AuthApi: cloudflare.Service AuthStorage: cloudflare.KVNamespace Bucket: cloudflare.R2Bucket + GatewayKv: cloudflare.KVNamespace LogProcessor: cloudflare.Service } } |
