@mastra/deployer-cloudflare 0.10.12-alpha.0 → 0.10.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/deployer-cloudflare",
3
- "version": "0.10.12-alpha.0",
3
+ "version": "0.10.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [
@@ -38,9 +38,9 @@
38
38
  "@babel/core": "^7.27.7",
39
39
  "@rollup/plugin-virtual": "^3.0.2",
40
40
  "cloudflare": "^4.4.1",
41
- "rollup": "^4.44.2",
41
+ "rollup": "~4.44.2",
42
42
  "zod": "^3.25.67",
43
- "@mastra/deployer": "^0.10.12-alpha.0"
43
+ "@mastra/deployer": "^0.10.13"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@microsoft/api-extractor": "^7.52.8",
@@ -49,8 +49,8 @@
49
49
  "tsup": "^8.5.0",
50
50
  "typescript": "^5.8.3",
51
51
  "vitest": "^3.2.4",
52
- "@internal/lint": "0.0.18",
53
- "@mastra/core": "0.10.12-alpha.0"
52
+ "@internal/lint": "0.0.19",
53
+ "@mastra/core": "0.10.13"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "@mastra/core": "^0.10.1-alpha.0"
@@ -1,85 +0,0 @@
1
- import type { analyzeBundle } from '@mastra/deployer/analyze';
2
- import { Deployer } from '@mastra/deployer';
3
- import { InputOptions } from 'rollup';
4
-
5
- declare interface CFRoute {
6
- pattern: string;
7
- zone_name: string;
8
- custom_domain?: boolean;
9
- }
10
-
11
- export declare class CloudflareDeployer extends Deployer {
12
- private cloudflare;
13
- routes?: CFRoute[];
14
- workerNamespace?: string;
15
- scope: string;
16
- env?: Record<string, any>;
17
- projectName?: string;
18
- d1Databases?: D1DatabaseBinding[];
19
- kvNamespaces?: KVNamespaceBinding[];
20
- constructor({ scope, env, projectName, routes, workerNamespace, auth, d1Databases, kvNamespaces, }: {
21
- env?: Record<string, any>;
22
- scope: string;
23
- projectName?: string;
24
- routes?: CFRoute[];
25
- workerNamespace?: string;
26
- auth: {
27
- apiToken: string;
28
- apiEmail?: string;
29
- };
30
- d1Databases?: D1DatabaseBinding[];
31
- kvNamespaces?: KVNamespaceBinding[];
32
- });
33
- writeFiles(outputDirectory: string): Promise<void>;
34
- private getEntry;
35
- prepare(outputDirectory: string): Promise<void>;
36
- getBundlerOptions(serverFile: string, mastraEntryFile: string, analyzedBundleInfo: Awaited<ReturnType<typeof analyzeBundle>>, toolsPaths: string[]): Promise<InputOptions>;
37
- bundle(entryFile: string, outputDirectory: string, toolsPaths: string[]): Promise<void>;
38
- deploy(): Promise<void>;
39
- tagWorker({ workerName, namespace, tags, scope, }: {
40
- scope: string;
41
- workerName: string;
42
- namespace: string;
43
- tags: string[];
44
- }): Promise<void>;
45
- lint(entryFile: string, outputDirectory: string, toolsPaths: string[]): Promise<void>;
46
- }
47
-
48
- export declare class CloudflareSecretsManager {
49
- accountId: string;
50
- apiToken: string;
51
- baseUrl: string;
52
- constructor({ accountId, apiToken }: {
53
- accountId: string;
54
- apiToken: string;
55
- });
56
- createSecret({ workerId, secretName, secretValue, }: {
57
- workerId: string;
58
- secretName: string;
59
- secretValue: string;
60
- }): Promise<any>;
61
- createProjectSecrets({ workerId, customerId, envVars, }: {
62
- workerId: string;
63
- customerId: string;
64
- envVars: Record<string, string>;
65
- }): Promise<any>;
66
- deleteSecret({ workerId, secretName }: {
67
- workerId: string;
68
- secretName: string;
69
- }): Promise<any>;
70
- listSecrets(workerId: string): Promise<any>;
71
- }
72
-
73
- declare interface D1DatabaseBinding {
74
- binding: string;
75
- database_name: string;
76
- database_id: string;
77
- preview_database_id?: string;
78
- }
79
-
80
- declare interface KVNamespaceBinding {
81
- binding: string;
82
- id: string;
83
- }
84
-
85
- export { }
@@ -1,85 +0,0 @@
1
- import type { analyzeBundle } from '@mastra/deployer/analyze';
2
- import { Deployer } from '@mastra/deployer';
3
- import { InputOptions } from 'rollup';
4
-
5
- declare interface CFRoute {
6
- pattern: string;
7
- zone_name: string;
8
- custom_domain?: boolean;
9
- }
10
-
11
- export declare class CloudflareDeployer extends Deployer {
12
- private cloudflare;
13
- routes?: CFRoute[];
14
- workerNamespace?: string;
15
- scope: string;
16
- env?: Record<string, any>;
17
- projectName?: string;
18
- d1Databases?: D1DatabaseBinding[];
19
- kvNamespaces?: KVNamespaceBinding[];
20
- constructor({ scope, env, projectName, routes, workerNamespace, auth, d1Databases, kvNamespaces, }: {
21
- env?: Record<string, any>;
22
- scope: string;
23
- projectName?: string;
24
- routes?: CFRoute[];
25
- workerNamespace?: string;
26
- auth: {
27
- apiToken: string;
28
- apiEmail?: string;
29
- };
30
- d1Databases?: D1DatabaseBinding[];
31
- kvNamespaces?: KVNamespaceBinding[];
32
- });
33
- writeFiles(outputDirectory: string): Promise<void>;
34
- private getEntry;
35
- prepare(outputDirectory: string): Promise<void>;
36
- getBundlerOptions(serverFile: string, mastraEntryFile: string, analyzedBundleInfo: Awaited<ReturnType<typeof analyzeBundle>>, toolsPaths: string[]): Promise<InputOptions>;
37
- bundle(entryFile: string, outputDirectory: string, toolsPaths: string[]): Promise<void>;
38
- deploy(): Promise<void>;
39
- tagWorker({ workerName, namespace, tags, scope, }: {
40
- scope: string;
41
- workerName: string;
42
- namespace: string;
43
- tags: string[];
44
- }): Promise<void>;
45
- lint(entryFile: string, outputDirectory: string, toolsPaths: string[]): Promise<void>;
46
- }
47
-
48
- export declare class CloudflareSecretsManager {
49
- accountId: string;
50
- apiToken: string;
51
- baseUrl: string;
52
- constructor({ accountId, apiToken }: {
53
- accountId: string;
54
- apiToken: string;
55
- });
56
- createSecret({ workerId, secretName, secretValue, }: {
57
- workerId: string;
58
- secretName: string;
59
- secretValue: string;
60
- }): Promise<any>;
61
- createProjectSecrets({ workerId, customerId, envVars, }: {
62
- workerId: string;
63
- customerId: string;
64
- envVars: Record<string, string>;
65
- }): Promise<any>;
66
- deleteSecret({ workerId, secretName }: {
67
- workerId: string;
68
- secretName: string;
69
- }): Promise<any>;
70
- listSecrets(workerId: string): Promise<any>;
71
- }
72
-
73
- declare interface D1DatabaseBinding {
74
- binding: string;
75
- database_name: string;
76
- database_id: string;
77
- preview_database_id?: string;
78
- }
79
-
80
- declare interface KVNamespaceBinding {
81
- binding: string;
82
- id: string;
83
- }
84
-
85
- export { }
package/dist/index.cjs DELETED
@@ -1,179 +0,0 @@
1
- 'use strict';
2
-
3
- var promises = require('fs/promises');
4
- var path = require('path');
5
- var deployer = require('@mastra/deployer');
6
- var virtual = require('@rollup/plugin-virtual');
7
- var cloudflare = require('cloudflare');
8
-
9
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
-
11
- var virtual__default = /*#__PURE__*/_interopDefault(virtual);
12
-
13
- // src/index.ts
14
- var CloudflareDeployer = class extends deployer.Deployer {
15
- cloudflare;
16
- routes = [];
17
- workerNamespace;
18
- scope;
19
- env;
20
- projectName;
21
- d1Databases;
22
- kvNamespaces;
23
- constructor({
24
- scope,
25
- env,
26
- projectName = "mastra",
27
- routes,
28
- workerNamespace,
29
- auth,
30
- d1Databases,
31
- kvNamespaces
32
- }) {
33
- super({ name: "CLOUDFLARE" });
34
- this.scope = scope;
35
- this.projectName = projectName;
36
- this.routes = routes;
37
- this.workerNamespace = workerNamespace;
38
- if (env) {
39
- this.env = env;
40
- }
41
- if (d1Databases) this.d1Databases = d1Databases;
42
- if (kvNamespaces) this.kvNamespaces = kvNamespaces;
43
- this.cloudflare = new cloudflare.Cloudflare(auth);
44
- }
45
- async writeFiles(outputDirectory) {
46
- const env = await this.loadEnvVars();
47
- const envsAsObject = Object.assign({}, Object.fromEntries(env.entries()), this.env);
48
- const cfWorkerName = this.projectName;
49
- const wranglerConfig = {
50
- name: cfWorkerName,
51
- main: "./index.mjs",
52
- compatibility_date: "2025-04-01",
53
- compatibility_flags: ["nodejs_compat", "nodejs_compat_populate_process_env"],
54
- observability: {
55
- logs: {
56
- enabled: true
57
- }
58
- },
59
- vars: envsAsObject
60
- };
61
- if (!this.workerNamespace && this.routes) {
62
- wranglerConfig.routes = this.routes;
63
- }
64
- if (this.d1Databases?.length) {
65
- wranglerConfig.d1_databases = this.d1Databases;
66
- }
67
- if (this.kvNamespaces?.length) {
68
- wranglerConfig.kv_namespaces = this.kvNamespaces;
69
- }
70
- await promises.writeFile(path.join(outputDirectory, this.outputDir, "wrangler.json"), JSON.stringify(wranglerConfig));
71
- }
72
- getEntry() {
73
- return `
74
- import '#polyfills';
75
- import { mastra } from '#mastra';
76
- import { createHonoServer } from '#server';
77
- import { evaluate } from '@mastra/core/eval';
78
- import { AvailableHooks, registerHook } from '@mastra/core/hooks';
79
- import { TABLE_EVALS } from '@mastra/core/storage';
80
- import { checkEvalStorageFields } from '@mastra/core/utils';
81
-
82
- registerHook(AvailableHooks.ON_GENERATION, ({ input, output, metric, runId, agentName, instructions }) => {
83
- evaluate({
84
- agentName,
85
- input,
86
- metric,
87
- output,
88
- runId,
89
- globalRunId: runId,
90
- instructions,
91
- });
92
- });
93
-
94
- registerHook(AvailableHooks.ON_EVALUATION, async traceObject => {
95
- const storage = mastra.getStorage();
96
- if (storage) {
97
- // Check for required fields
98
- const logger = mastra?.getLogger();
99
- const areFieldsValid = checkEvalStorageFields(traceObject, logger);
100
- if (!areFieldsValid) return;
101
-
102
- await storage.insert({
103
- tableName: TABLE_EVALS,
104
- record: {
105
- input: traceObject.input,
106
- output: traceObject.output,
107
- result: JSON.stringify(traceObject.result || {}),
108
- agent_name: traceObject.agentName,
109
- metric_name: traceObject.metricName,
110
- instructions: traceObject.instructions,
111
- test_info: null,
112
- global_run_id: traceObject.globalRunId,
113
- run_id: traceObject.runId,
114
- created_at: new Date().toISOString(),
115
- },
116
- });
117
- }
118
- });
119
-
120
- export default {
121
- fetch: async (request, env, context) => {
122
- const app = await createHonoServer(mastra)
123
- return app.fetch(request, env, context);
124
- }
125
- }
126
- `;
127
- }
128
- async prepare(outputDirectory) {
129
- await super.prepare(outputDirectory);
130
- await this.writeFiles(outputDirectory);
131
- }
132
- async getBundlerOptions(serverFile, mastraEntryFile, analyzedBundleInfo, toolsPaths) {
133
- const inputOptions = await super.getBundlerOptions(serverFile, mastraEntryFile, analyzedBundleInfo, toolsPaths);
134
- if (Array.isArray(inputOptions.plugins)) {
135
- inputOptions.plugins = [
136
- virtual__default.default({
137
- "#polyfills": `
138
- process.versions = process.versions || {};
139
- process.versions.node = '${process.versions.node}';
140
- `
141
- }),
142
- ...inputOptions.plugins
143
- ];
144
- }
145
- return inputOptions;
146
- }
147
- async bundle(entryFile, outputDirectory, toolsPaths) {
148
- return this._bundle(this.getEntry(), entryFile, outputDirectory, toolsPaths);
149
- }
150
- async deploy() {
151
- this.logger?.info("Deploying to Cloudflare failed. Please use the Cloudflare dashboard to deploy.");
152
- }
153
- async tagWorker({
154
- workerName,
155
- namespace,
156
- tags,
157
- scope
158
- }) {
159
- if (!this.cloudflare) {
160
- throw new Error("Cloudflare Deployer not initialized");
161
- }
162
- await this.cloudflare.workersForPlatforms.dispatch.namespaces.scripts.tags.update(namespace, workerName, {
163
- account_id: scope,
164
- body: tags
165
- });
166
- }
167
- async lint(entryFile, outputDirectory, toolsPaths) {
168
- await super.lint(entryFile, outputDirectory, toolsPaths);
169
- const hasLibsql = await this.deps.checkDependencies(["@mastra/libsql"]) === `ok`;
170
- if (hasLibsql) {
171
- this.logger.error(
172
- "Cloudflare Deployer does not support @libsql/client(which may have been installed by @mastra/libsql) as a dependency. Please use Cloudflare D1 instead @mastra/cloudflare-d1"
173
- );
174
- process.exit(1);
175
- }
176
- }
177
- };
178
-
179
- exports.CloudflareDeployer = CloudflareDeployer;
package/dist/index.d.cts DELETED
@@ -1 +0,0 @@
1
- export { CloudflareDeployer } from './_tsup-dts-rollup.cjs';
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { CloudflareDeployer } from './_tsup-dts-rollup.js';
package/dist/index.js DELETED
@@ -1,173 +0,0 @@
1
- import { writeFile } from 'fs/promises';
2
- import { join } from 'path';
3
- import { Deployer } from '@mastra/deployer';
4
- import virtual from '@rollup/plugin-virtual';
5
- import { Cloudflare } from 'cloudflare';
6
-
7
- // src/index.ts
8
- var CloudflareDeployer = class extends Deployer {
9
- cloudflare;
10
- routes = [];
11
- workerNamespace;
12
- scope;
13
- env;
14
- projectName;
15
- d1Databases;
16
- kvNamespaces;
17
- constructor({
18
- scope,
19
- env,
20
- projectName = "mastra",
21
- routes,
22
- workerNamespace,
23
- auth,
24
- d1Databases,
25
- kvNamespaces
26
- }) {
27
- super({ name: "CLOUDFLARE" });
28
- this.scope = scope;
29
- this.projectName = projectName;
30
- this.routes = routes;
31
- this.workerNamespace = workerNamespace;
32
- if (env) {
33
- this.env = env;
34
- }
35
- if (d1Databases) this.d1Databases = d1Databases;
36
- if (kvNamespaces) this.kvNamespaces = kvNamespaces;
37
- this.cloudflare = new Cloudflare(auth);
38
- }
39
- async writeFiles(outputDirectory) {
40
- const env = await this.loadEnvVars();
41
- const envsAsObject = Object.assign({}, Object.fromEntries(env.entries()), this.env);
42
- const cfWorkerName = this.projectName;
43
- const wranglerConfig = {
44
- name: cfWorkerName,
45
- main: "./index.mjs",
46
- compatibility_date: "2025-04-01",
47
- compatibility_flags: ["nodejs_compat", "nodejs_compat_populate_process_env"],
48
- observability: {
49
- logs: {
50
- enabled: true
51
- }
52
- },
53
- vars: envsAsObject
54
- };
55
- if (!this.workerNamespace && this.routes) {
56
- wranglerConfig.routes = this.routes;
57
- }
58
- if (this.d1Databases?.length) {
59
- wranglerConfig.d1_databases = this.d1Databases;
60
- }
61
- if (this.kvNamespaces?.length) {
62
- wranglerConfig.kv_namespaces = this.kvNamespaces;
63
- }
64
- await writeFile(join(outputDirectory, this.outputDir, "wrangler.json"), JSON.stringify(wranglerConfig));
65
- }
66
- getEntry() {
67
- return `
68
- import '#polyfills';
69
- import { mastra } from '#mastra';
70
- import { createHonoServer } from '#server';
71
- import { evaluate } from '@mastra/core/eval';
72
- import { AvailableHooks, registerHook } from '@mastra/core/hooks';
73
- import { TABLE_EVALS } from '@mastra/core/storage';
74
- import { checkEvalStorageFields } from '@mastra/core/utils';
75
-
76
- registerHook(AvailableHooks.ON_GENERATION, ({ input, output, metric, runId, agentName, instructions }) => {
77
- evaluate({
78
- agentName,
79
- input,
80
- metric,
81
- output,
82
- runId,
83
- globalRunId: runId,
84
- instructions,
85
- });
86
- });
87
-
88
- registerHook(AvailableHooks.ON_EVALUATION, async traceObject => {
89
- const storage = mastra.getStorage();
90
- if (storage) {
91
- // Check for required fields
92
- const logger = mastra?.getLogger();
93
- const areFieldsValid = checkEvalStorageFields(traceObject, logger);
94
- if (!areFieldsValid) return;
95
-
96
- await storage.insert({
97
- tableName: TABLE_EVALS,
98
- record: {
99
- input: traceObject.input,
100
- output: traceObject.output,
101
- result: JSON.stringify(traceObject.result || {}),
102
- agent_name: traceObject.agentName,
103
- metric_name: traceObject.metricName,
104
- instructions: traceObject.instructions,
105
- test_info: null,
106
- global_run_id: traceObject.globalRunId,
107
- run_id: traceObject.runId,
108
- created_at: new Date().toISOString(),
109
- },
110
- });
111
- }
112
- });
113
-
114
- export default {
115
- fetch: async (request, env, context) => {
116
- const app = await createHonoServer(mastra)
117
- return app.fetch(request, env, context);
118
- }
119
- }
120
- `;
121
- }
122
- async prepare(outputDirectory) {
123
- await super.prepare(outputDirectory);
124
- await this.writeFiles(outputDirectory);
125
- }
126
- async getBundlerOptions(serverFile, mastraEntryFile, analyzedBundleInfo, toolsPaths) {
127
- const inputOptions = await super.getBundlerOptions(serverFile, mastraEntryFile, analyzedBundleInfo, toolsPaths);
128
- if (Array.isArray(inputOptions.plugins)) {
129
- inputOptions.plugins = [
130
- virtual({
131
- "#polyfills": `
132
- process.versions = process.versions || {};
133
- process.versions.node = '${process.versions.node}';
134
- `
135
- }),
136
- ...inputOptions.plugins
137
- ];
138
- }
139
- return inputOptions;
140
- }
141
- async bundle(entryFile, outputDirectory, toolsPaths) {
142
- return this._bundle(this.getEntry(), entryFile, outputDirectory, toolsPaths);
143
- }
144
- async deploy() {
145
- this.logger?.info("Deploying to Cloudflare failed. Please use the Cloudflare dashboard to deploy.");
146
- }
147
- async tagWorker({
148
- workerName,
149
- namespace,
150
- tags,
151
- scope
152
- }) {
153
- if (!this.cloudflare) {
154
- throw new Error("Cloudflare Deployer not initialized");
155
- }
156
- await this.cloudflare.workersForPlatforms.dispatch.namespaces.scripts.tags.update(namespace, workerName, {
157
- account_id: scope,
158
- body: tags
159
- });
160
- }
161
- async lint(entryFile, outputDirectory, toolsPaths) {
162
- await super.lint(entryFile, outputDirectory, toolsPaths);
163
- const hasLibsql = await this.deps.checkDependencies(["@mastra/libsql"]) === `ok`;
164
- if (hasLibsql) {
165
- this.logger.error(
166
- "Cloudflare Deployer does not support @libsql/client(which may have been installed by @mastra/libsql) as a dependency. Please use Cloudflare D1 instead @mastra/cloudflare-d1"
167
- );
168
- process.exit(1);
169
- }
170
- }
171
- };
172
-
173
- export { CloudflareDeployer };
@@ -1,89 +0,0 @@
1
- 'use strict';
2
-
3
- // src/secrets-manager/index.ts
4
- var CloudflareSecretsManager = class {
5
- accountId;
6
- apiToken;
7
- baseUrl;
8
- constructor({ accountId, apiToken }) {
9
- this.accountId = accountId;
10
- this.apiToken = apiToken;
11
- this.baseUrl = "https://api.cloudflare.com/client/v4";
12
- }
13
- async createSecret({
14
- workerId,
15
- secretName,
16
- secretValue
17
- }) {
18
- const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets`;
19
- try {
20
- const response = await fetch(url, {
21
- method: "PUT",
22
- headers: {
23
- Authorization: `Bearer ${this.apiToken}`,
24
- "Content-Type": "application/json"
25
- },
26
- body: JSON.stringify({
27
- name: secretName,
28
- text: secretValue
29
- })
30
- });
31
- const data = await response.json();
32
- if (!data.success) {
33
- throw new Error(data.errors[0].message);
34
- }
35
- return data.result;
36
- } catch (error) {
37
- console.error("Failed to create secret:", error);
38
- throw error;
39
- }
40
- }
41
- async createProjectSecrets({
42
- workerId,
43
- customerId,
44
- envVars
45
- }) {
46
- const secretName = `PROJECT_${customerId.toUpperCase()}`;
47
- const secretValue = JSON.stringify(envVars);
48
- return this.createSecret({ workerId, secretName, secretValue });
49
- }
50
- async deleteSecret({ workerId, secretName }) {
51
- const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets/${secretName}`;
52
- try {
53
- const response = await fetch(url, {
54
- method: "DELETE",
55
- headers: {
56
- Authorization: `Bearer ${this.apiToken}`
57
- }
58
- });
59
- const data = await response.json();
60
- if (!data.success) {
61
- throw new Error(data.errors[0].message);
62
- }
63
- return data.result;
64
- } catch (error) {
65
- console.error("Failed to delete secret:", error);
66
- throw error;
67
- }
68
- }
69
- async listSecrets(workerId) {
70
- const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets`;
71
- try {
72
- const response = await fetch(url, {
73
- headers: {
74
- Authorization: `Bearer ${this.apiToken}`
75
- }
76
- });
77
- const data = await response.json();
78
- if (!data.success) {
79
- throw new Error(data.errors[0].message);
80
- }
81
- return data.result;
82
- } catch (error) {
83
- console.error("Failed to list secrets:", error);
84
- throw error;
85
- }
86
- }
87
- };
88
-
89
- exports.CloudflareSecretsManager = CloudflareSecretsManager;
@@ -1 +0,0 @@
1
- export { CloudflareSecretsManager } from '../_tsup-dts-rollup.cjs';
@@ -1 +0,0 @@
1
- export { CloudflareSecretsManager } from '../_tsup-dts-rollup.js';
@@ -1,87 +0,0 @@
1
- // src/secrets-manager/index.ts
2
- var CloudflareSecretsManager = class {
3
- accountId;
4
- apiToken;
5
- baseUrl;
6
- constructor({ accountId, apiToken }) {
7
- this.accountId = accountId;
8
- this.apiToken = apiToken;
9
- this.baseUrl = "https://api.cloudflare.com/client/v4";
10
- }
11
- async createSecret({
12
- workerId,
13
- secretName,
14
- secretValue
15
- }) {
16
- const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets`;
17
- try {
18
- const response = await fetch(url, {
19
- method: "PUT",
20
- headers: {
21
- Authorization: `Bearer ${this.apiToken}`,
22
- "Content-Type": "application/json"
23
- },
24
- body: JSON.stringify({
25
- name: secretName,
26
- text: secretValue
27
- })
28
- });
29
- const data = await response.json();
30
- if (!data.success) {
31
- throw new Error(data.errors[0].message);
32
- }
33
- return data.result;
34
- } catch (error) {
35
- console.error("Failed to create secret:", error);
36
- throw error;
37
- }
38
- }
39
- async createProjectSecrets({
40
- workerId,
41
- customerId,
42
- envVars
43
- }) {
44
- const secretName = `PROJECT_${customerId.toUpperCase()}`;
45
- const secretValue = JSON.stringify(envVars);
46
- return this.createSecret({ workerId, secretName, secretValue });
47
- }
48
- async deleteSecret({ workerId, secretName }) {
49
- const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets/${secretName}`;
50
- try {
51
- const response = await fetch(url, {
52
- method: "DELETE",
53
- headers: {
54
- Authorization: `Bearer ${this.apiToken}`
55
- }
56
- });
57
- const data = await response.json();
58
- if (!data.success) {
59
- throw new Error(data.errors[0].message);
60
- }
61
- return data.result;
62
- } catch (error) {
63
- console.error("Failed to delete secret:", error);
64
- throw error;
65
- }
66
- }
67
- async listSecrets(workerId) {
68
- const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets`;
69
- try {
70
- const response = await fetch(url, {
71
- headers: {
72
- Authorization: `Bearer ${this.apiToken}`
73
- }
74
- });
75
- const data = await response.json();
76
- if (!data.success) {
77
- throw new Error(data.errors[0].message);
78
- }
79
- return data.result;
80
- } catch (error) {
81
- console.error("Failed to list secrets:", error);
82
- throw error;
83
- }
84
- }
85
- };
86
-
87
- export { CloudflareSecretsManager };