alchemy 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/README.md +3 -3
  2. package/lib/ai/astro-file.d.ts +137 -0
  3. package/lib/ai/astro-file.js +173 -0
  4. package/lib/ai/client.d.ts +2 -10
  5. package/lib/ai/client.js +8 -24
  6. package/lib/ai/css-file.d.ts +131 -0
  7. package/lib/ai/css-file.js +158 -0
  8. package/lib/ai/data.d.ts +45 -5
  9. package/lib/ai/data.js +45 -10
  10. package/lib/ai/document.d.ts +44 -20
  11. package/lib/ai/document.js +95 -28
  12. package/lib/ai/html-file.d.ts +128 -0
  13. package/lib/ai/html-file.js +155 -0
  14. package/lib/ai/index.d.ts +7 -0
  15. package/lib/ai/index.js +7 -0
  16. package/lib/ai/json-file.d.ts +160 -0
  17. package/lib/ai/json-file.js +208 -0
  18. package/lib/ai/typescript-file.d.ts +140 -0
  19. package/lib/ai/typescript-file.js +176 -0
  20. package/lib/ai/vue-file.d.ts +126 -0
  21. package/lib/ai/vue-file.js +153 -0
  22. package/lib/ai/yaml-file.d.ts +169 -0
  23. package/lib/ai/yaml-file.js +236 -0
  24. package/lib/cloudflare/dns.d.ts +88 -0
  25. package/lib/cloudflare/dns.js +192 -0
  26. package/lib/cloudflare/index.d.ts +2 -1
  27. package/lib/cloudflare/index.js +2 -1
  28. package/lib/cloudflare/r2-rest-state-store.d.ts +125 -0
  29. package/lib/cloudflare/r2-rest-state-store.js +254 -0
  30. package/lib/cloudflare/response.d.ts +12 -0
  31. package/lib/cloudflare/response.js +0 -0
  32. package/lib/cloudflare/zone.d.ts +18 -0
  33. package/lib/cloudflare/zone.js +17 -4
  34. package/lib/dns/godaddy.d.ts +7 -0
  35. package/lib/dns/godaddy.js +21 -0
  36. package/lib/dns/import-dns.d.ts +82 -0
  37. package/lib/dns/import-dns.js +114 -0
  38. package/lib/dns/index.d.ts +2 -0
  39. package/lib/dns/index.js +2 -0
  40. package/lib/dns/record.d.ts +88 -0
  41. package/lib/dns/record.js +13 -0
  42. package/lib/fs/file-system-state-store.d.ts +17 -0
  43. package/lib/fs/file-system-state-store.js +87 -0
  44. package/lib/fs/folder.d.ts +10 -0
  45. package/lib/fs/folder.js +2 -2
  46. package/lib/fs/index.d.ts +4 -4
  47. package/lib/fs/index.js +4 -4
  48. package/lib/fs/{json-file.d.ts → static-json-file.d.ts} +2 -2
  49. package/lib/fs/static-json-file.js +14 -0
  50. package/lib/fs/{text-file.d.ts → static-text-file.d.ts} +2 -2
  51. package/lib/fs/{text-file.js → static-text-file.js} +1 -1
  52. package/lib/fs/{typescript-file.d.ts → static-typescript-file.d.ts} +2 -2
  53. package/lib/fs/{typescript-file.js → static-typescript-file.js} +1 -1
  54. package/lib/fs/{yaml-file.d.ts → static-yaml-file.d.ts} +2 -2
  55. package/lib/fs/{yaml-file.js → static-yaml-file.js} +1 -1
  56. package/lib/internal/getting-started.d.ts +10 -0
  57. package/lib/internal/getting-started.js +94 -0
  58. package/lib/internal/project.d.ts +13 -0
  59. package/lib/internal/project.js +101 -0
  60. package/lib/internal/providers.d.ts +23 -0
  61. package/lib/internal/providers.js +148 -0
  62. package/lib/resource.js +8 -1
  63. package/lib/scope.d.ts +1 -1
  64. package/lib/scope.js +4 -2
  65. package/lib/state.d.ts +1 -16
  66. package/lib/state.js +0 -87
  67. package/lib/test/bun.d.ts +11 -0
  68. package/lib/test/bun.js +36 -8
  69. package/lib/web/astro.d.ts +147 -0
  70. package/lib/web/astro.js +414 -0
  71. package/lib/{shadcn/component.js → web/shadcn-component.js} +2 -9
  72. package/lib/web/shadcn.d.ts +97 -0
  73. package/lib/web/shadcn.js +99 -0
  74. package/lib/web/tailwind.d.ts +64 -0
  75. package/lib/web/tailwind.js +97 -0
  76. package/lib/{vite → web}/vite.d.ts +5 -0
  77. package/lib/{vite → web}/vite.js +44 -44
  78. package/lib/web/vitepress/custom-theme.d.ts +173 -0
  79. package/lib/web/vitepress/custom-theme.js +336 -0
  80. package/lib/{vitepress → web/vitepress}/dependencies.d.ts +2 -2
  81. package/lib/{vitepress → web/vitepress}/dependencies.js +1 -1
  82. package/lib/web/vitepress/home-page.d.ts +244 -0
  83. package/lib/web/vitepress/home-page.js +134 -0
  84. package/lib/{vitepress → web/vitepress}/index.d.ts +1 -0
  85. package/lib/{vitepress → web/vitepress}/index.js +1 -0
  86. package/lib/{vitepress → web/vitepress}/vitepress.d.ts +10 -8
  87. package/lib/{vitepress → web/vitepress}/vitepress.js +11 -12
  88. package/package.json +6 -2
  89. package/src/ai/astro-file.ts +288 -0
  90. package/src/ai/client.ts +8 -28
  91. package/src/ai/css-file.ts +266 -0
  92. package/src/ai/data.ts +53 -16
  93. package/src/ai/document.ts +130 -33
  94. package/src/ai/html-file.ts +263 -0
  95. package/src/ai/index.ts +7 -0
  96. package/src/ai/json-file.ts +349 -0
  97. package/src/ai/typescript-file.ts +293 -0
  98. package/src/ai/vue-file.ts +261 -0
  99. package/src/ai/yaml-file.ts +368 -0
  100. package/src/apply.ts +0 -1
  101. package/src/cloudflare/dns.ts +344 -0
  102. package/src/cloudflare/index.ts +2 -1
  103. package/src/cloudflare/r2-rest-state-store.ts +353 -0
  104. package/src/cloudflare/response.ts +9 -0
  105. package/src/cloudflare/zone.ts +39 -23
  106. package/src/dns/godaddy.ts +29 -0
  107. package/src/dns/import-dns.ts +213 -0
  108. package/src/dns/index.ts +2 -0
  109. package/src/dns/record.ts +121 -0
  110. package/src/fs/file-system-state-store.ts +109 -0
  111. package/src/fs/folder.ts +14 -2
  112. package/src/fs/index.ts +4 -4
  113. package/src/fs/{json-file.ts → static-json-file.ts} +13 -3
  114. package/src/fs/{text-file.ts → static-text-file.ts} +5 -2
  115. package/src/fs/{typescript-file.ts → static-typescript-file.ts} +3 -3
  116. package/src/fs/{yaml-file.ts → static-yaml-file.ts} +5 -2
  117. package/src/internal/getting-started.ts +107 -0
  118. package/src/internal/project.ts +119 -0
  119. package/src/internal/providers.ts +209 -0
  120. package/src/resource.ts +10 -1
  121. package/src/scope.ts +5 -6
  122. package/src/state.ts +1 -111
  123. package/src/test/bun.ts +60 -10
  124. package/src/web/astro.ts +644 -0
  125. package/src/{shadcn/component.ts → web/shadcn-component.ts} +5 -13
  126. package/src/web/shadcn.ts +219 -0
  127. package/src/web/tailwind.ts +167 -0
  128. package/src/{vite → web}/vite.ts +54 -56
  129. package/src/web/vitepress/custom-theme.ts +514 -0
  130. package/src/{vitepress → web/vitepress}/dependencies.ts +2 -2
  131. package/src/web/vitepress/home-page.ts +363 -0
  132. package/src/{vitepress → web/vitepress}/index.ts +1 -0
  133. package/src/{vitepress → web/vitepress}/vitepress.ts +34 -27
  134. package/lib/cloudflare/state.d.ts +0 -82
  135. package/lib/cloudflare/state.js +0 -108
  136. package/lib/fs/json-file.js +0 -7
  137. package/lib/internal/docs.d.ts +0 -5
  138. package/lib/internal/docs.js +0 -287
  139. package/lib/shadcn/index.d.ts +0 -1
  140. package/lib/shadcn/index.js +0 -1
  141. package/lib/vite/index.d.ts +0 -1
  142. package/lib/vite/index.js +0 -1
  143. package/lib/vitepress/home-page.d.ts +0 -133
  144. package/lib/vitepress/home-page.js +0 -11
  145. package/src/cloudflare/state.ts +0 -138
  146. package/src/internal/docs.ts +0 -338
  147. package/src/shadcn/index.ts +0 -1
  148. package/src/vite/index.ts +0 -1
  149. package/src/vitepress/home-page.ts +0 -166
  150. /package/lib/{shadcn/component.d.ts → web/shadcn-component.d.ts} +0 -0
  151. /package/src/{vitepress → web/vitepress}/index.md +0 -0
@@ -1,287 +0,0 @@
1
- import { type } from "arktype";
2
- import fs from "fs/promises";
3
- import path from "path";
4
- import { Data } from "../ai/data";
5
- import { Document } from "../ai/document";
6
- import { alchemy } from "../alchemy";
7
- import { Folder } from "../fs/folder";
8
- import { VitePressProject } from "../vitepress/vitepress";
9
- export async function AlchemyDocs({ docs: isDocsEnabled }) {
10
- const root = (await Folder("alchemy-web")).path;
11
- const docs = (await Folder(path.join(root, "docs"))).path;
12
- const providersDir = (await Folder(path.join(docs, "providers"))).path;
13
- const exclude = ["util", "test", "vitepress", "vite", "shadcn", "internal"];
14
- // Get all folders in the alchemy/src directory
15
- let providers = (await fs.readdir(path.resolve("alchemy", "src"), {
16
- withFileTypes: true,
17
- }))
18
- .filter((dirent) => dirent.isDirectory() && !exclude.includes(dirent.name))
19
- .map((dirent) => path.join(dirent.parentPath, dirent.name));
20
- // For each provider, list all files
21
- if (isDocsEnabled === false) {
22
- return;
23
- }
24
- else if (typeof isDocsEnabled === "number") {
25
- providers = providers.slice(0, isDocsEnabled);
26
- }
27
- const providerDocs = await Promise.all(providers.map(async (provider) => {
28
- const providerName = path.basename(provider);
29
- const files = (await fs.readdir(path.resolve(provider), {
30
- withFileTypes: true,
31
- }))
32
- .filter((dirent) => dirent.isFile())
33
- .map((dirent) => path.relative(process.cwd(), path.resolve(provider, dirent.name)))
34
- .filter((file) => file.endsWith(".ts") && !file.endsWith("index.ts"));
35
- const { object: { groups }, } = await Data(`docs/${providerName}`, {
36
- schema: type({
37
- groups: type({
38
- title: type("string").describe("The title of the group, should be the Resource Name exactly without spaces, e.g. Bucket or Static Site."),
39
- filename: type("string").describe("The filename of the Resource's Document, e.g. bucket.md or static-site.md"),
40
- category: type("'Resource'|'Client'|'Utility'|'Types'").describe("The classification of the Resource's Document, one of: Resource, Client, Utility, or Types."),
41
- }).array(),
42
- }),
43
- system: await alchemy `
44
- You are a technical writer tasked with identifying the distinct documents that need to be written for a document group (folder) in a documentation site.
45
- You will be provided with a list of documents and instructions on how to classify them.
46
- Each document has a title, file name, and category.
47
- `,
48
- prompt: await alchemy `
49
- Identify and classify the documents that need to be written for the '${provider}' Service's Alchemy Resources.
50
- For background knowledge on Alchemy, see ${alchemy.file("./README.md")}.
51
- For background knowledge on the structure of an Alchemy Resource, see ${alchemy.file("./.cursorrules")}.
52
-
53
- The ${provider} Service has the following resources:
54
- ${alchemy.files(files)}
55
-
56
- A file is considered a "Resource" if it contains a const <ResourceName> = Resource(...) call or if it is a function that calls a Resource function, e.g. const TypeScriptFile = () => File(...).
57
- A file is considered a "Client" if it exposes a wrapper around creating a SDK client or fetch.
58
- A file is considered a "Utility" if it contains utility functions that are not resources or clients.
59
- A file is considered a "Types" if it contains just type definitions and maybe helpers around working with those types.
60
-
61
- The title should be simply the name of the resource, e.g. "Bucket" or "Function", except with spaces, e.g. "Static Site" instead of "StaticSite". Maintain all other casing.
62
- `,
63
- });
64
- const providerDocsDir = (await Folder(path.join(providersDir, providerName))).path;
65
- const documents = await Promise.all(groups
66
- .filter((g) => g.category === "Resource")
67
- .map(async (g) => Document(`docs/${providerName}/${g.title}`, {
68
- title: g.title,
69
- path: path.join(providerDocsDir, `${g.filename.replace(".ts", "").replace(".md", "")}.md`),
70
- prompt: await alchemy `
71
- You are a technical writer writing API documentation for an Alchemy IaC Resource.
72
- See ${alchemy.file("./README.md")} to understand the overview of Alchemy.
73
- See ${alchemy.file("./.cursorrules")} to better understand the structure and convention of an Alchemy Resource.
74
-
75
- Relevant files for the ${providerName} Service:
76
- ${alchemy.files(files)}
77
-
78
- Write concise documentation for the "${g.title}" Resource.
79
-
80
- > [!CAUTION]
81
- > Avoid the temptation to over explain or over describe. Focus on concise, simple, high value snippets. One heading and 0-1 descriptions per snippet.
82
-
83
- > [!TIP]
84
- > Make sure the examples follow a natural progression from the minimal example to logical next steps of how the Resource might be used.
85
-
86
- Each document must follow the following format:
87
-
88
- # ${g.title}
89
-
90
- (simple description with an external link to the provider's website)
91
- e.g.
92
- The Efs component lets you add [Amazon Elastic File System (EFS)](https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html) to your app.
93
-
94
- # Minimal Example
95
-
96
- \`\`\`ts
97
- import { ${g.title.replaceAll(" ", "")} } from "alchemy/${providerName}";
98
-
99
- (example)
100
- \`\`\`
101
-
102
-
103
- # Create the ${g.title}
104
-
105
- \`\`\`ts
106
- import { ${g.title.replaceAll(" ", "")} } from "alchemy/${providerName}";
107
-
108
- (example)
109
- \`\`\`
110
-
111
- ${providerName === "cloudflare"
112
- ? await alchemy `# Bind to a Worker
113
- (if it is a Cloudflare Resource)
114
-
115
- \`\`\`ts
116
- import { Worker, ${g.title.replaceAll(" ", "")} } from "alchemy/${providerName}";
117
-
118
- const myResource = await ${g.title.replaceAll(" ", "")}("my-resource", {
119
- // ...
120
- });
121
-
122
- await Worker("my-worker", {
123
- name: "my-worker",
124
- script: "console.log('Hello, world!')",
125
- bindings: {
126
- myResource,
127
- },
128
- });
129
- \`\`\``
130
- : ""}
131
- `,
132
- })));
133
- return {
134
- dir: providerDocsDir,
135
- provider: providerName,
136
- documents,
137
- };
138
- }));
139
- await VitePressProject("docs", {
140
- name: "alchemy-web",
141
- title: "Alchemy",
142
- description: "Alchemy is an TypeScript-native, embeddable IaC library",
143
- overwrite: true,
144
- delete: false,
145
- tsconfig: {
146
- extends: "../tsconfig.base.json",
147
- references: ["../alchemy/tsconfig.json"],
148
- },
149
- devDependencies: {
150
- alchemy: "workspace:*",
151
- },
152
- theme: {
153
- light: "light-plus",
154
- dark: "dark-plus",
155
- },
156
- home: {
157
- layout: "home",
158
- hero: {
159
- name: "Alchemy",
160
- text: "Materialize all the things! 🪄",
161
- tagline: "TypeScript-native Infrastructure-as-Code (IaC)",
162
- image: {
163
- src: "./public/alchemist.png",
164
- alt: "The Alchemist",
165
- },
166
- actions: [
167
- {
168
- text: "Get Started",
169
- link: "/docs",
170
- theme: "brand",
171
- },
172
- ],
173
- },
174
- features: [
175
- {
176
- title: "JS-native",
177
- details: "No second language, toolchains, dependencies, processes, services, etc. to lug around.",
178
- },
179
- {
180
- title: "Async",
181
- details: "Resources are just async functions - no complex abstraction to learn.",
182
- },
183
- {
184
- title: "ESM",
185
- details: "Built exclusively on ESM, with a slight preference for modern JS runtimes like Bun.",
186
- },
187
- {
188
- title: "Embeddable",
189
- details: "Runs in any JavaScript/TypeScript environment, including the browser!",
190
- },
191
- {
192
- title: "Extensible",
193
- details: "Implement your own resources with a simple function.",
194
- },
195
- {
196
- title: "AI-first",
197
- details: "Create, copy, fork, and modify resources using LLMs to fit your needs.",
198
- },
199
- {
200
- title: "No dependencies",
201
- details: "The alchemy core package has 0 required dependencies.",
202
- },
203
- {
204
- title: "No service",
205
- details: "State files are stored locally in your project for easy inspection and version control.",
206
- },
207
- {
208
- title: "No strong opinions",
209
- details: "Structure your codebase however you want, store state anywhere - we don't care!",
210
- },
211
- ],
212
- },
213
- themeConfig: {
214
- sidebar: {
215
- "/blog/": [{ text: "Blog", items: [{ text: "Blog", link: "/blog/" }] }],
216
- "/docs/": [
217
- {
218
- text: "Getting Started",
219
- items: [{ text: "Install", link: "/docs/getting-started/install" }],
220
- },
221
- {
222
- text: "Guides",
223
- items: [
224
- {
225
- text: "Custom Resource",
226
- link: "/docs/guides/custom-resource",
227
- },
228
- {
229
- text: "Automating with LLMs",
230
- link: "/docs/guides/llms",
231
- },
232
- ],
233
- },
234
- {
235
- text: "Core",
236
- collapsed: true,
237
- items: [
238
- { text: "App", link: "/docs/core/app" },
239
- { text: "Resource", link: "/docs/core/resource" },
240
- { text: "Scope", link: "/docs/core/scope" },
241
- { text: "Phase", link: "/docs/core/phase" },
242
- { text: "Finalize", link: "/docs/core/finalize" },
243
- { text: "State", link: "/docs/core/state" },
244
- { text: "Secret", link: "/docs/core/secret" },
245
- { text: "Context", link: "/docs/core/context" },
246
- ],
247
- },
248
- {
249
- text: "Providers",
250
- items: providerDocs
251
- .map((provider) => ({
252
- text: provider.provider,
253
- link: `/docs/providers/${provider.provider}`,
254
- collapsed: true,
255
- items: provider.documents.map((document) => ({
256
- text: document.title,
257
- link: `/docs/providers/${provider.provider}/${path.basename(document.path, ".md")}`,
258
- })),
259
- }))
260
- .sort((a, b) => a.text.localeCompare(b.text)),
261
- },
262
- ],
263
- "/examples/": [
264
- {
265
- text: "Examples",
266
- items: [{ text: "Foo", link: "/examples/foo" }],
267
- },
268
- ],
269
- "/": [
270
- {
271
- text: "Home",
272
- items: [
273
- { text: "Markdown Examples", link: "/markdown-examples" },
274
- { text: "Runtime API Examples", link: "/api-examples" },
275
- ],
276
- },
277
- ],
278
- },
279
- socialLinks: [
280
- {
281
- icon: "github",
282
- link: "https://github.com/sam-goodwin/alchemy",
283
- },
284
- ],
285
- },
286
- });
287
- }
@@ -1 +0,0 @@
1
- export * from "./component";
@@ -1 +0,0 @@
1
- export * from "./component";
@@ -1 +0,0 @@
1
- export * from "./vite";
package/lib/vite/index.js DELETED
@@ -1 +0,0 @@
1
- export * from "./vite";
@@ -1,133 +0,0 @@
1
- /**
2
- * Image that can be themed for light/dark mode
3
- */
4
- export interface ThemeableImage {
5
- /**
6
- * Image source URL
7
- */
8
- src?: string;
9
- /**
10
- * Image alt text
11
- */
12
- alt?: string;
13
- /**
14
- * Light theme image URL
15
- */
16
- light?: string;
17
- /**
18
- * Dark theme image URL
19
- */
20
- dark?: string;
21
- }
22
- /**
23
- * Hero action button configuration
24
- */
25
- export interface HeroAction {
26
- /**
27
- * Color theme of the button
28
- * @default "brand"
29
- */
30
- theme?: "brand" | "alt";
31
- /**
32
- * Label of the button
33
- */
34
- text: string;
35
- /**
36
- * Destination link of the button
37
- */
38
- link: string;
39
- /**
40
- * Link target attribute
41
- */
42
- target?: string;
43
- /**
44
- * Link rel attribute
45
- */
46
- rel?: string;
47
- }
48
- /**
49
- * Hero section configuration
50
- */
51
- export interface Hero {
52
- /**
53
- * The string shown top of text. Comes with brand color
54
- * and expected to be short, such as product name.
55
- */
56
- name?: string;
57
- /**
58
- * The main text for the hero section. This will be defined
59
- * as h1 tag.
60
- */
61
- text: string;
62
- /**
63
- * Tagline displayed below text
64
- */
65
- tagline?: string;
66
- /**
67
- * The image displayed next to the text and tagline area
68
- */
69
- image?: string | ThemeableImage;
70
- /**
71
- * Action buttons to display in home hero section
72
- */
73
- actions?: HeroAction[];
74
- }
75
- /**
76
- * Feature item configuration
77
- */
78
- export interface Feature {
79
- /**
80
- * Show icon on each feature box
81
- */
82
- icon?: string | ThemeableImage;
83
- /**
84
- * Title of the feature
85
- */
86
- title: string;
87
- /**
88
- * Details of the feature
89
- */
90
- details: string;
91
- /**
92
- * Link when clicked on feature component
93
- */
94
- link?: string;
95
- /**
96
- * Link text to be shown inside feature component
97
- */
98
- linkText?: string;
99
- /**
100
- * Link rel attribute
101
- */
102
- rel?: string;
103
- /**
104
- * Link target attribute
105
- */
106
- target?: string;
107
- }
108
- /**
109
- * VitePress home page configuration
110
- */
111
- export interface HomePage {
112
- /**
113
- * Use home page layout
114
- */
115
- layout: "home";
116
- /**
117
- * Hero section configuration
118
- */
119
- hero?: Hero;
120
- /**
121
- * Features section configuration
122
- */
123
- features?: Feature[];
124
- /**
125
- * Whether to apply default markdown styles
126
- * @default true
127
- */
128
- markdownStyles?: boolean;
129
- }
130
- /**
131
- * Parse YAML frontmatter to home page config
132
- */
133
- export declare function parseHomePage(content: string): HomePage;
@@ -1,11 +0,0 @@
1
- import yaml from "yaml";
2
- /**
3
- * Parse YAML frontmatter to home page config
4
- */
5
- export function parseHomePage(content) {
6
- const match = content.match(/^---\n([\s\S]*?)\n---/);
7
- if (!match) {
8
- throw new Error("Invalid frontmatter format");
9
- }
10
- return yaml.parse(match[1]);
11
- }
@@ -1,138 +0,0 @@
1
- import type { KVNamespace } from "@cloudflare/workers-types";
2
- import type { State, StateStore } from "../state";
3
-
4
- /**
5
- * Options for CloudflareStateStore
6
- */
7
- export interface CloudflareStateStoreOptions {
8
- /**
9
- * The prefix to use for keys in the KV namespace
10
- * This allows multiple state stores to use the same KV namespace
11
- */
12
- prefix?: string;
13
- }
14
-
15
- /**
16
- * State store implementation using Cloudflare KV Namespace
17
- */
18
- export class CloudflareStateStore implements StateStore {
19
- private namespace: KVNamespace;
20
- private prefix: string;
21
-
22
- /**
23
- * Create a new CloudflareStateStore
24
- *
25
- * @param namespace The KV namespace to use for storing state
26
- * @param options Options for the state store
27
- */
28
- constructor(
29
- namespace: KVNamespace,
30
- options: CloudflareStateStoreOptions = {},
31
- ) {
32
- this.namespace = namespace;
33
- this.prefix = options.prefix || "alchemy-state:";
34
- }
35
-
36
- /**
37
- * No initialization needed for KV namespaces as they are pre-provisioned
38
- */
39
- async init(): Promise<void> {
40
- // KV namespaces are pre-provisioned in Cloudflare, so no initialization is needed
41
- }
42
-
43
- /**
44
- * KV namespaces cannot be deleted programmatically
45
- */
46
- async deinit(): Promise<void> {
47
- // KV namespaces can only be deleted through the Cloudflare dashboard or API
48
- // We cannot delete the namespace itself here
49
- }
50
-
51
- /**
52
- * List all resources in the state store
53
- */
54
- async list(): Promise<string[]> {
55
- const list = await this.namespace.list({ prefix: this.prefix });
56
- return list.keys.map((key) => key.name.slice(this.prefix.length));
57
- }
58
-
59
- /**
60
- * Count the number of items in the state store
61
- */
62
- async count(): Promise<number> {
63
- const list = await this.namespace.list({ prefix: this.prefix });
64
- return list.keys.length;
65
- }
66
-
67
- /**
68
- * Get a state by key
69
- *
70
- * @param key The key to look up
71
- * @returns The state or undefined if not found
72
- */
73
- async get(key: string): Promise<State | undefined> {
74
- const data = await this.namespace.get(this.getNamespacedKey(key), "json");
75
- return data as State | undefined;
76
- }
77
-
78
- /**
79
- * Get multiple states by their keys
80
- *
81
- * @param ids Array of keys to fetch
82
- * @returns Record mapping keys to their states
83
- */
84
- async getBatch(ids: string[]): Promise<Record<string, State>> {
85
- const result: Record<string, State> = {};
86
-
87
- // KV doesn't have a native batch get operation, so we need to make multiple requests
88
- // We could optimize this with Promise.all for parallel fetching
89
- const promises = ids.map(async (id) => {
90
- const state = await this.get(id);
91
- if (state) {
92
- result[id] = state;
93
- }
94
- });
95
-
96
- await Promise.all(promises);
97
- return result;
98
- }
99
-
100
- /**
101
- * Get all states in the store
102
- *
103
- * @returns Record mapping all keys to their states
104
- */
105
- async all(): Promise<Record<string, State>> {
106
- const keys = await this.list();
107
- return this.getBatch(keys);
108
- }
109
-
110
- /**
111
- * Set a state for a key
112
- *
113
- * @param key The key to set
114
- * @param value The state to store
115
- */
116
- async set(key: string, value: State): Promise<void> {
117
- await this.namespace.put(this.getNamespacedKey(key), JSON.stringify(value));
118
- }
119
-
120
- /**
121
- * Delete a state by key
122
- *
123
- * @param key The key to delete
124
- */
125
- async delete(key: string): Promise<void> {
126
- await this.namespace.delete(this.getNamespacedKey(key));
127
- }
128
-
129
- /**
130
- * Get the namespaced key for storage
131
- *
132
- * @param key The original key
133
- * @returns The key with prefix for use in the KV namespace
134
- */
135
- private getNamespacedKey(key: string): string {
136
- return `${this.prefix}${key}`;
137
- }
138
- }