alchemy 0.3.0 → 0.4.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 (154) hide show
  1. package/README.md +8 -3
  2. package/lib/ai/astro-file.js +7 -24
  3. package/lib/ai/client.d.ts +7 -0
  4. package/lib/ai/client.js +45 -0
  5. package/lib/ai/css-file.js +7 -24
  6. package/lib/ai/data.d.ts +26 -21
  7. package/lib/ai/data.js +33 -22
  8. package/lib/ai/document.d.ts +76 -5
  9. package/lib/ai/document.js +112 -56
  10. package/lib/ai/html-file.js +7 -24
  11. package/lib/ai/json-file.js +11 -32
  12. package/lib/ai/typescript-file.d.ts +1 -2
  13. package/lib/ai/typescript-file.js +8 -39
  14. package/lib/ai/vue-file.js +7 -24
  15. package/lib/ai/yaml-file.js +12 -48
  16. package/lib/alchemy.d.ts +5 -0
  17. package/lib/alchemy.js +61 -3
  18. package/lib/apply.js +18 -11
  19. package/lib/aws/account-id.d.ts +4 -1
  20. package/lib/aws/account-id.js +1 -1
  21. package/lib/aws/credentials.d.ts +5 -0
  22. package/lib/aws/credentials.js +0 -0
  23. package/lib/aws/oidc/oidc-provider.js +2 -2
  24. package/lib/aws/role.js +35 -23
  25. package/lib/cloudflare/account-api-token.d.ts +168 -0
  26. package/lib/cloudflare/account-api-token.js +179 -0
  27. package/lib/cloudflare/account-id.d.ts +1 -0
  28. package/lib/cloudflare/account-id.js +0 -0
  29. package/lib/cloudflare/api.d.ts +38 -1
  30. package/lib/cloudflare/api.js +55 -4
  31. package/lib/cloudflare/bucket.d.ts +118 -3
  32. package/lib/cloudflare/bucket.js +269 -89
  33. package/lib/cloudflare/custom-domain.d.ts +75 -0
  34. package/lib/cloudflare/custom-domain.js +154 -0
  35. package/lib/cloudflare/{dns.js → dns-records.js} +14 -1
  36. package/lib/cloudflare/generate-asset-manifest.js +1 -1
  37. package/lib/cloudflare/index.d.ts +5 -1
  38. package/lib/cloudflare/index.js +5 -1
  39. package/lib/cloudflare/permission-groups.d.ts +78 -0
  40. package/lib/cloudflare/permission-groups.js +48 -0
  41. package/lib/cloudflare/r2-rest-state-store.d.ts +2 -1
  42. package/lib/cloudflare/r2-rest-state-store.js +3 -2
  43. package/lib/cloudflare/static-site-router.js +16 -7
  44. package/lib/cloudflare/static-site.d.ts +17 -18
  45. package/lib/cloudflare/static-site.js +19 -7
  46. package/lib/cloudflare/upload-asset-manifest.js +4 -2
  47. package/lib/{util/encrypt.d.ts → encrypt.d.ts} +1 -1
  48. package/lib/{util/encrypt.js → encrypt.js} +1 -1
  49. package/lib/fs/copy-file.d.ts +54 -0
  50. package/lib/fs/copy-file.js +63 -0
  51. package/lib/fs/file-system-state-store.js +1 -1
  52. package/lib/fs/file.d.ts +31 -0
  53. package/lib/fs/file.js +30 -5
  54. package/lib/fs/index.d.ts +6 -0
  55. package/lib/fs/index.js +6 -0
  56. package/lib/fs/static-astro-file.d.ts +34 -0
  57. package/lib/fs/static-astro-file.js +39 -0
  58. package/lib/fs/static-css-file.d.ts +24 -0
  59. package/lib/fs/static-css-file.js +29 -0
  60. package/lib/fs/static-html-file.d.ts +31 -0
  61. package/lib/fs/static-html-file.js +36 -0
  62. package/lib/fs/static-json-file.d.ts +2 -2
  63. package/lib/fs/static-json-file.js +3 -2
  64. package/lib/fs/static-text-file.d.ts +1 -1
  65. package/lib/fs/static-text-file.js +3 -2
  66. package/lib/fs/static-typescript-file.d.ts +2 -2
  67. package/lib/fs/static-typescript-file.js +3 -2
  68. package/lib/fs/static-vue-file.d.ts +28 -0
  69. package/lib/fs/static-vue-file.js +33 -0
  70. package/lib/fs/static-yaml-file.d.ts +11 -8
  71. package/lib/fs/static-yaml-file.js +3 -2
  72. package/lib/internal/{providers.d.ts → docs/providers.d.ts} +14 -4
  73. package/lib/internal/docs/providers.js +196 -0
  74. package/lib/secret.d.ts +3 -0
  75. package/lib/secret.js +13 -0
  76. package/lib/{util/serde.d.ts → serde.d.ts} +1 -1
  77. package/lib/{util/serde.js → serde.js} +12 -4
  78. package/lib/test/bun.js +3 -2
  79. package/lib/util/sha256.d.ts +1 -0
  80. package/lib/util/sha256.js +4 -0
  81. package/lib/web/vitepress/config.d.ts +11 -0
  82. package/lib/web/vitepress/config.js +32 -0
  83. package/lib/web/vitepress/home-page.d.ts +5 -3
  84. package/lib/web/vitepress/home-page.js +48 -35
  85. package/lib/web/vitepress/index.d.ts +2 -0
  86. package/lib/web/vitepress/index.js +2 -0
  87. package/lib/web/vitepress/process-front-matter-files.d.ts +18 -0
  88. package/lib/web/vitepress/process-front-matter-files.js +68 -0
  89. package/lib/web/vitepress/vitepress.d.ts +5 -4
  90. package/lib/web/vitepress/vitepress.js +20 -36
  91. package/package.json +8 -4
  92. package/src/ai/astro-file.ts +11 -30
  93. package/src/ai/client.ts +59 -0
  94. package/src/ai/css-file.ts +11 -30
  95. package/src/ai/data.ts +50 -25
  96. package/src/ai/document.ts +171 -66
  97. package/src/ai/html-file.ts +11 -30
  98. package/src/ai/json-file.ts +13 -37
  99. package/src/ai/typescript-file.ts +14 -48
  100. package/src/ai/vue-file.ts +11 -30
  101. package/src/ai/yaml-file.ts +13 -53
  102. package/src/alchemy.ts +74 -9
  103. package/src/apply.ts +27 -26
  104. package/src/aws/account-id.ts +6 -2
  105. package/src/aws/credentials.ts +6 -0
  106. package/src/aws/oidc/oidc-provider.ts +17 -17
  107. package/src/aws/role.ts +68 -54
  108. package/src/cloudflare/account-api-token.ts +365 -0
  109. package/src/cloudflare/account-id.ts +0 -0
  110. package/src/cloudflare/api.ts +88 -17
  111. package/src/cloudflare/bucket.ts +493 -133
  112. package/src/cloudflare/custom-domain.ts +318 -0
  113. package/src/cloudflare/{dns.ts → dns-records.ts} +36 -22
  114. package/src/cloudflare/generate-asset-manifest.ts +4 -4
  115. package/src/cloudflare/index.ts +5 -1
  116. package/src/cloudflare/permission-groups.ts +137 -0
  117. package/src/cloudflare/r2-rest-state-store.ts +14 -13
  118. package/src/cloudflare/static-site-router.ts +18 -8
  119. package/src/cloudflare/static-site.ts +27 -35
  120. package/src/cloudflare/upload-asset-manifest.ts +6 -4
  121. package/src/cloudflare/worker.ts +44 -43
  122. package/src/dns/import-dns.ts +4 -4
  123. package/src/{util/encrypt.ts → encrypt.ts} +7 -10
  124. package/src/fs/copy-file.ts +115 -0
  125. package/src/fs/file-system-state-store.ts +5 -5
  126. package/src/fs/file.ts +64 -9
  127. package/src/fs/index.ts +6 -0
  128. package/src/fs/static-astro-file.ts +45 -0
  129. package/src/fs/static-css-file.ts +35 -0
  130. package/src/fs/static-html-file.ts +42 -0
  131. package/src/fs/static-json-file.ts +4 -3
  132. package/src/fs/static-text-file.ts +5 -2
  133. package/src/fs/static-typescript-file.ts +4 -3
  134. package/src/fs/static-vue-file.ts +39 -0
  135. package/src/fs/static-yaml-file.ts +13 -9
  136. package/src/internal/docs/providers.ts +281 -0
  137. package/src/secret.ts +16 -0
  138. package/src/{util/serde.ts → serde.ts} +16 -10
  139. package/src/test/bun.ts +8 -7
  140. package/src/util/sha256.ts +5 -0
  141. package/src/web/vitepress/config.ts +50 -0
  142. package/src/web/vitepress/home-page.ts +54 -42
  143. package/src/web/vitepress/index.ts +2 -0
  144. package/src/web/vitepress/process-front-matter-files.ts +98 -0
  145. package/src/web/vitepress/vitepress.ts +34 -50
  146. package/lib/internal/getting-started.d.ts +0 -10
  147. package/lib/internal/getting-started.js +0 -94
  148. package/lib/internal/project.d.ts +0 -13
  149. package/lib/internal/project.js +0 -101
  150. package/lib/internal/providers.js +0 -148
  151. package/src/internal/getting-started.ts +0 -107
  152. package/src/internal/project.ts +0 -119
  153. package/src/internal/providers.ts +0 -209
  154. /package/lib/cloudflare/{dns.d.ts → dns-records.d.ts} +0 -0
@@ -1,3 +1,4 @@
1
+ import type { Secret } from "../secret";
1
2
  /**
2
3
  * Options for Cloudflare API requests
3
4
  */
@@ -5,7 +6,7 @@ export interface CloudflareApiOptions {
5
6
  /**
6
7
  * API Key to use (overrides CLOUDFLARE_API_KEY env var)
7
8
  */
8
- apiKey?: string;
9
+ apiKey?: Secret;
9
10
  /**
10
11
  * Account ID to use (overrides CLOUDFLARE_ACCOUNT_ID env var)
11
12
  * If not provided, will be automatically retrieved from the Cloudflare API
@@ -21,6 +22,38 @@ export interface CloudflareApiOptions {
21
22
  */
22
23
  email?: string;
23
24
  }
25
+ /**
26
+ * Custom error class for Cloudflare API errors
27
+ * Includes HTTP status information from the Response
28
+ */
29
+ export declare class CloudflareApiError extends Error {
30
+ /**
31
+ * HTTP status code
32
+ */
33
+ status: number;
34
+ /**
35
+ * HTTP status text
36
+ */
37
+ statusText: string;
38
+ /**
39
+ * Raw error data from the API
40
+ */
41
+ errorData?: any;
42
+ /**
43
+ * Create a new CloudflareApiError
44
+ */
45
+ constructor(message: string, response: Response, errorData?: any);
46
+ }
47
+ /**
48
+ * Helper function to handle API errors
49
+ *
50
+ * @param response The fetch Response object
51
+ * @param action The action being performed (e.g., "creating", "deleting")
52
+ * @param resourceType The type of resource being acted upon (e.g., "R2 bucket", "Worker")
53
+ * @param resourceName The name/identifier of the specific resource
54
+ * @returns Never returns - always throws an error
55
+ */
56
+ export declare function handleApiError(response: Response, action: string, resourceType: string, resourceName: string): Promise<never>;
24
57
  /**
25
58
  * Creates a CloudflareApi instance with automatic account ID discovery if not provided
26
59
  *
@@ -28,6 +61,10 @@ export interface CloudflareApiOptions {
28
61
  * @returns Promise resolving to a CloudflareApi instance
29
62
  */
30
63
  export declare function createCloudflareApi(options?: CloudflareApiOptions): Promise<CloudflareApi>;
64
+ export type CloudflareAccountId = string & {
65
+ readonly __brand: "CloudflareAccountId";
66
+ };
67
+ export declare function CloudflareAccountId(options?: CloudflareApiOptions): Promise<CloudflareAccountId>;
31
68
  /**
32
69
  * Cloudflare API client using raw fetch
33
70
  */
@@ -1,4 +1,50 @@
1
1
  import { getCloudflareHeaders } from "./auth";
2
+ /**
3
+ * Custom error class for Cloudflare API errors
4
+ * Includes HTTP status information from the Response
5
+ */
6
+ export class CloudflareApiError extends Error {
7
+ /**
8
+ * HTTP status code
9
+ */
10
+ status;
11
+ /**
12
+ * HTTP status text
13
+ */
14
+ statusText;
15
+ /**
16
+ * Raw error data from the API
17
+ */
18
+ errorData;
19
+ /**
20
+ * Create a new CloudflareApiError
21
+ */
22
+ constructor(message, response, errorData) {
23
+ super(message);
24
+ this.name = "CloudflareApiError";
25
+ this.status = response.status;
26
+ this.statusText = response.statusText;
27
+ this.errorData = errorData;
28
+ // Ensure instanceof works correctly
29
+ Object.setPrototypeOf(this, CloudflareApiError.prototype);
30
+ }
31
+ }
32
+ /**
33
+ * Helper function to handle API errors
34
+ *
35
+ * @param response The fetch Response object
36
+ * @param action The action being performed (e.g., "creating", "deleting")
37
+ * @param resourceType The type of resource being acted upon (e.g., "R2 bucket", "Worker")
38
+ * @param resourceName The name/identifier of the specific resource
39
+ * @returns Never returns - always throws an error
40
+ */
41
+ export async function handleApiError(response, action, resourceType, resourceName) {
42
+ const json = await response.json();
43
+ console.log(json);
44
+ const errorData = json.errors || [{ message: response.statusText }];
45
+ const errorMessage = `Error ${action} ${resourceType} '${resourceName}': ${errorData.errors?.[0]?.message || response.statusText}`;
46
+ throw new CloudflareApiError(errorMessage, response, errorData);
47
+ }
2
48
  /**
3
49
  * Creates a CloudflareApi instance with automatic account ID discovery if not provided
4
50
  *
@@ -9,9 +55,7 @@ export async function createCloudflareApi(options = {}) {
9
55
  try {
10
56
  return new CloudflareApi({
11
57
  ...options,
12
- accountId: options.accountId ||
13
- process.env.CLOUDFLARE_ACCOUNT_ID ||
14
- (await fetchAccountId()),
58
+ accountId: await CloudflareAccountId(options),
15
59
  });
16
60
  }
17
61
  catch (error) {
@@ -19,6 +63,11 @@ export async function createCloudflareApi(options = {}) {
19
63
  throw new Error("Failed to automatically discover Cloudflare account ID. Please provide an account ID explicitly or ensure your API token/key has sufficient permissions.");
20
64
  }
21
65
  }
66
+ export async function CloudflareAccountId(options = {}) {
67
+ return (options.accountId ||
68
+ process.env.CLOUDFLARE_ACCOUNT_ID ||
69
+ (await fetchAccountId()));
70
+ }
22
71
  /**
23
72
  * Cloudflare API client using raw fetch
24
73
  */
@@ -44,7 +93,9 @@ export class CloudflareApi {
44
93
  */
45
94
  constructor(options = {}) {
46
95
  this.baseUrl = "https://api.cloudflare.com/client/v4";
47
- const apiKey = options.apiKey || process.env.CLOUDFLARE_API_KEY || undefined;
96
+ const apiKey = options.apiKey?.unencrypted ||
97
+ process.env.CLOUDFLARE_API_KEY ||
98
+ undefined;
48
99
  if (!apiKey) {
49
100
  throw new Error("No API key provided. Use createCloudflareApi() instead for automatic account discovery.");
50
101
  }
@@ -1,9 +1,11 @@
1
+ import { AwsClient } from "aws4fetch";
1
2
  import type { Context } from "../context";
2
3
  import { Resource } from "../resource";
4
+ import { CloudflareApi, type CloudflareApiOptions } from "./api";
3
5
  /**
4
6
  * Properties for creating or updating an R2 Bucket
5
7
  */
6
- export interface BucketProps {
8
+ export interface BucketProps extends CloudflareApiOptions {
7
9
  /**
8
10
  * Name of the bucket
9
11
  * Names can only contain lowercase letters (a-z), numbers (0-9), and hyphens (-)
@@ -25,6 +27,12 @@ export interface BucketProps {
25
27
  * Only for development purposes - use custom domains for production
26
28
  */
27
29
  allowPublicAccess?: boolean;
30
+ /**
31
+ * Whether to delete the bucket and its contents during resource deletion
32
+ * If set to false, the bucket will remain but the resource will be removed from state
33
+ * Default: true
34
+ */
35
+ delete?: boolean;
28
36
  }
29
37
  /**
30
38
  * Output returned after R2 Bucket creation/update
@@ -39,9 +47,9 @@ export interface R2Bucket extends Resource<"cloudflare::R2Bucket">, BucketProps
39
47
  */
40
48
  location: string;
41
49
  /**
42
- * Time at which the bucket was created (Unix timestamp in seconds)
50
+ * Time at which the bucket was created
43
51
  */
44
- creationDate: number;
52
+ creationDate: Date;
45
53
  }
46
54
  /**
47
55
  * Creates and manages Cloudflare R2 Buckets for object storage.
@@ -80,3 +88,110 @@ export interface R2Bucket extends Resource<"cloudflare::R2Bucket">, BucketProps
80
88
  * @see https://developers.cloudflare.com/r2/buckets/
81
89
  */
82
90
  export declare const R2Bucket: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<R2Bucket>, id: string, props: BucketProps) => Promise<R2Bucket>);
91
+ /**
92
+ * Configuration for R2 client to connect to Cloudflare R2
93
+ */
94
+ export interface R2ClientConfig {
95
+ accountId?: string;
96
+ accessKeyId?: string;
97
+ secretAccessKey?: string;
98
+ jurisdiction?: string;
99
+ }
100
+ type R2Client = AwsClient & {
101
+ accountId: string;
102
+ };
103
+ /**
104
+ * Creates an aws4fetch client configured for Cloudflare R2
105
+ *
106
+ * @see https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js-v3/
107
+ */
108
+ export declare function createR2Client(config?: R2ClientConfig): Promise<R2Client>;
109
+ interface CloudflareBucketResponse {
110
+ result: {
111
+ name: string;
112
+ location?: string;
113
+ creation_date: string;
114
+ };
115
+ success: boolean;
116
+ errors: Array<{
117
+ code: number;
118
+ message: string;
119
+ }>;
120
+ messages: string[];
121
+ }
122
+ /**
123
+ * Adds jurisdiction header to the headers object if specified in props
124
+ *
125
+ * @param headers Headers object to modify
126
+ * @param props Props or jurisdiction string
127
+ * @returns Modified headers object
128
+ */
129
+ export declare function withJurisdiction(headers: Record<string, string> | undefined, props: BucketProps | {
130
+ jurisdiction?: string;
131
+ } | string | undefined): Record<string, string>;
132
+ /**
133
+ * Create a new bucket
134
+ */
135
+ export declare function createBucket(api: CloudflareApi, bucketName: string, props: BucketProps): Promise<CloudflareBucketResponse>;
136
+ /**
137
+ * Delete a bucket
138
+ */
139
+ export declare function deleteBucket(api: CloudflareApi, bucketName: string, props: BucketProps): Promise<void>;
140
+ /**
141
+ * List objects in an R2 bucket
142
+ *
143
+ * @param r2 R2Client instance
144
+ * @param bucketName Name of the bucket
145
+ * @param continuationToken Optional token for pagination
146
+ * @param jurisdiction Optional jurisdiction for the bucket
147
+ * @returns Object containing the list of objects and the next continuation token
148
+ */
149
+ export declare function listObjects(r2: R2Client, bucketName: string, continuationToken?: string, jurisdiction?: string): Promise<{
150
+ objects: {
151
+ Key: string;
152
+ }[];
153
+ continuationToken?: string;
154
+ }>;
155
+ /**
156
+ * Helper function to empty a bucket by deleting all objects
157
+ */
158
+ export declare function emptyBucket(r2: R2Client, bucketName: string, jurisdiction?: string): Promise<void>;
159
+ /**
160
+ * Update public access setting for a bucket
161
+ *
162
+ * This operation is not available through the S3 API for R2,
163
+ * so we still use the Cloudflare API directly.
164
+ */
165
+ export declare function updatePublicAccess(api: CloudflareApi, bucketName: string, allowPublicAccess: boolean, jurisdiction?: string): Promise<void>;
166
+ /**
167
+ * Set CORS configuration for a bucket using aws4fetch
168
+ */
169
+ export declare function setCorsConfiguration(r2: R2Client, bucketName: string, allowedOrigins?: string[], allowedMethods?: string[], allowedHeaders?: string[], maxAgeSeconds?: number, jurisdiction?: string): Promise<void>;
170
+ /**
171
+ * Information about an R2 bucket returned by list operations
172
+ */
173
+ export interface R2BucketInfo {
174
+ /**
175
+ * Name of the bucket
176
+ */
177
+ Name: string;
178
+ /**
179
+ * Creation date of the bucket
180
+ */
181
+ CreationDate: Date;
182
+ }
183
+ /**
184
+ * List all R2 buckets in an account
185
+ *
186
+ * @param api CloudflareApi instance
187
+ * @param options Optional listing options
188
+ * @returns Array of bucket information
189
+ */
190
+ export declare function listBuckets(api: CloudflareApi, options?: {
191
+ nameContains?: string;
192
+ perPage?: number;
193
+ cursor?: string;
194
+ direction?: "asc" | "desc";
195
+ jurisdiction?: string;
196
+ }): Promise<R2BucketInfo[]>;
197
+ export {};