From a1574c75a2b6863e004c637d38645fcfeb2b97de Mon Sep 17 00:00:00 2001 From: Sor4chi <80559385+sor4chi@users.noreply.github.com> Date: Fri, 12 Apr 2024 05:42:05 +0900 Subject: [PATCH] feat: supports non inheritable keys (#13) * refactor: separate folder by whether they are inheritable or not * feat: add non inheritable properties * chore: changeset --- .changeset/three-jobs-confess.md | 8 ++++ packages/schema/src/index.ts | 41 +++++-------------- .../src/{ => inheritable-keys}/build.ts | 0 .../src/{ => inheritable-keys}/capability.ts | 0 packages/schema/src/inheritable-keys/index.ts | 34 +++++++++++++++ .../src/{ => inheritable-keys}/limits.ts | 0 .../src/{ => inheritable-keys}/route.ts | 0 .../schema/src/{ => inheritable-keys}/rule.ts | 0 .../src/{ => inheritable-keys}/trigger.ts | 1 + .../src/non-inheritable-keys/bindings/ai.ts | 6 +++ .../bindings/analytics-engine-datasets.ts | 9 ++++ .../non-inheritable-keys/bindings/browser.ts | 6 +++ .../bindings/d1-database.ts | 11 +++++ .../bindings/dispatch-namespaces.ts | 7 ++++ .../bindings/durable-object.ts | 27 ++++++++++++ .../non-inheritable-keys/bindings/email.ts | 10 +++++ .../bindings/hyperdrive.ts | 7 ++++ .../non-inheritable-keys/bindings/index.ts | 33 +++++++++++++++ .../bindings/kv-namespaces.ts | 10 +++++ .../bindings/mtls-certificates.ts | 9 ++++ .../non-inheritable-keys/bindings/queues.ts | 27 ++++++++++++ .../bindings/r2-buckets.ts | 9 ++++ .../bindings/service-bindings.ts | 10 +++++ .../bindings/vectorize-indexes.ts | 9 ++++ .../schema/src/non-inheritable-keys/index.ts | 7 ++++ 25 files changed, 250 insertions(+), 31 deletions(-) create mode 100644 .changeset/three-jobs-confess.md rename packages/schema/src/{ => inheritable-keys}/build.ts (100%) rename packages/schema/src/{ => inheritable-keys}/capability.ts (100%) create mode 100644 packages/schema/src/inheritable-keys/index.ts rename packages/schema/src/{ => inheritable-keys}/limits.ts (100%) rename packages/schema/src/{ => inheritable-keys}/route.ts (100%) rename packages/schema/src/{ => inheritable-keys}/rule.ts (100%) rename packages/schema/src/{ => inheritable-keys}/trigger.ts (54%) create mode 100644 packages/schema/src/non-inheritable-keys/bindings/ai.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/analytics-engine-datasets.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/browser.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/d1-database.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/dispatch-namespaces.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/durable-object.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/email.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/hyperdrive.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/index.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/kv-namespaces.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/mtls-certificates.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/queues.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/r2-buckets.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/service-bindings.ts create mode 100644 packages/schema/src/non-inheritable-keys/bindings/vectorize-indexes.ts create mode 100644 packages/schema/src/non-inheritable-keys/index.ts diff --git a/.changeset/three-jobs-confess.md b/.changeset/three-jobs-confess.md new file mode 100644 index 0000000..a067c35 --- /dev/null +++ b/.changeset/three-jobs-confess.md @@ -0,0 +1,8 @@ +--- +"twrangler": patch +--- + +Supports non inheritable keys + +- Multi Environments Configuration +- Bindings diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index 0af6a01..d222f82 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -1,36 +1,15 @@ import { z } from "zod"; -import { buildSchema } from "./build"; -import { - compatibilityDateSchema, - compatibilityFlagsSchema, -} from "./capability"; -import { limitsSchema } from "./limits"; -import { routeSchema, routesSchema } from "./route"; -import { rulesSchema } from "./rule"; -import { triggersSchema } from "./trigger"; -export const configSchema = z.object({ - name: z.string(), - main: z.string(), - compatibility_date: compatibilityDateSchema, - account_id: z.string().optional(), - compatibility_flags: compatibilityFlagsSchema.optional(), - workers_dev: z.boolean().optional(), - route: routeSchema.optional(), - routes: routesSchema.optional(), - tsconfig: z.string().optional(), - triggers: triggersSchema.optional(), - rules: rulesSchema.optional(), - build: buildSchema.optional(), - no_bundle: z.boolean().optional(), - minify: z.boolean().optional(), - node_compat: z.boolean().optional(), - preserve_file_names: z.boolean().optional(), - send_metrics: z.boolean().optional(), - keep_vars: z.boolean().optional(), - logpush: z.boolean().optional(), - limits: limitsSchema.optional(), -}); +import { inheritableKeysSchema } from "./inheritable-keys"; +import { nonInheritableKeysSchema } from "./non-inheritable-keys"; + +export const configSchema = nonInheritableKeysSchema + .merge(inheritableKeysSchema) + .extend({ + env: z + .record(nonInheritableKeysSchema.merge(inheritableKeysSchema.partial())) + .optional(), + }); export type Config = z.infer; diff --git a/packages/schema/src/build.ts b/packages/schema/src/inheritable-keys/build.ts similarity index 100% rename from packages/schema/src/build.ts rename to packages/schema/src/inheritable-keys/build.ts diff --git a/packages/schema/src/capability.ts b/packages/schema/src/inheritable-keys/capability.ts similarity index 100% rename from packages/schema/src/capability.ts rename to packages/schema/src/inheritable-keys/capability.ts diff --git a/packages/schema/src/inheritable-keys/index.ts b/packages/schema/src/inheritable-keys/index.ts new file mode 100644 index 0000000..789f421 --- /dev/null +++ b/packages/schema/src/inheritable-keys/index.ts @@ -0,0 +1,34 @@ +import { z } from "zod"; +import { buildSchema } from "./build"; +import { + compatibilityDateSchema, + compatibilityFlagsSchema, +} from "./capability"; +import { limitsSchema } from "./limits"; +import { routeSchema, routesSchema } from "./route"; +import { rulesSchema } from "./rule"; +import { triggersSchema } from "./trigger"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys +export const inheritableKeysSchema = z.object({ + name: z.string(), + main: z.string(), + compatibility_date: compatibilityDateSchema, + account_id: z.string().optional(), + compatibility_flags: compatibilityFlagsSchema.optional(), + workers_dev: z.boolean().optional(), + route: routeSchema.optional(), + routes: routesSchema.optional(), + tsconfig: z.string().optional(), + triggers: triggersSchema.optional(), + rules: rulesSchema.optional(), + build: buildSchema.optional(), + no_bundle: z.boolean().optional(), + minify: z.boolean().optional(), + node_compat: z.boolean().optional(), + preserve_file_names: z.boolean().optional(), + send_metrics: z.boolean().optional(), + keep_vars: z.boolean().optional(), + logpush: z.boolean().optional(), + limits: limitsSchema.optional(), +}); diff --git a/packages/schema/src/limits.ts b/packages/schema/src/inheritable-keys/limits.ts similarity index 100% rename from packages/schema/src/limits.ts rename to packages/schema/src/inheritable-keys/limits.ts diff --git a/packages/schema/src/route.ts b/packages/schema/src/inheritable-keys/route.ts similarity index 100% rename from packages/schema/src/route.ts rename to packages/schema/src/inheritable-keys/route.ts diff --git a/packages/schema/src/rule.ts b/packages/schema/src/inheritable-keys/rule.ts similarity index 100% rename from packages/schema/src/rule.ts rename to packages/schema/src/inheritable-keys/rule.ts diff --git a/packages/schema/src/trigger.ts b/packages/schema/src/inheritable-keys/trigger.ts similarity index 54% rename from packages/schema/src/trigger.ts rename to packages/schema/src/inheritable-keys/trigger.ts index ba5ce42..e8b04bb 100644 --- a/packages/schema/src/trigger.ts +++ b/packages/schema/src/inheritable-keys/trigger.ts @@ -1,5 +1,6 @@ import { z } from "zod"; +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#triggers export const triggersSchema = z.object({ crons: z.array(z.string()), }); diff --git a/packages/schema/src/non-inheritable-keys/bindings/ai.ts b/packages/schema/src/non-inheritable-keys/bindings/ai.ts new file mode 100644 index 0000000..fac7aa8 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/ai.ts @@ -0,0 +1,6 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai +export const aiSchema = z.object({ + binding: z.string(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/analytics-engine-datasets.ts b/packages/schema/src/non-inheritable-keys/bindings/analytics-engine-datasets.ts new file mode 100644 index 0000000..ff17caf --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/analytics-engine-datasets.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets +export const analyticsEngineDatasetsSchema = z.array( + z.object({ + binding: z.string(), + dataset: z.string().optional(), + }), +); diff --git a/packages/schema/src/non-inheritable-keys/bindings/browser.ts b/packages/schema/src/non-inheritable-keys/bindings/browser.ts new file mode 100644 index 0000000..141bb77 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/browser.ts @@ -0,0 +1,6 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering +export const browserSchema = z.object({ + binding: z.string(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/d1-database.ts b/packages/schema/src/non-inheritable-keys/bindings/d1-database.ts new file mode 100644 index 0000000..9757aae --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/d1-database.ts @@ -0,0 +1,11 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases +const d1DatabaseSchema = z.object({ + binding: z.string(), + database_name: z.string(), + database_id: z.string(), + preview_database_id: z.string().optional(), +}); + +export const d1DatabasesSchema = z.array(d1DatabaseSchema); diff --git a/packages/schema/src/non-inheritable-keys/bindings/dispatch-namespaces.ts b/packages/schema/src/non-inheritable-keys/bindings/dispatch-namespaces.ts new file mode 100644 index 0000000..8f73764 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/dispatch-namespaces.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms +export const dispatchNamespacesSchema = z.object({ + binding: z.string(), + namespace: z.string(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/durable-object.ts b/packages/schema/src/non-inheritable-keys/bindings/durable-object.ts new file mode 100644 index 0000000..32e8eba --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/durable-object.ts @@ -0,0 +1,27 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects +const durableObjectsBindingsSchema = z.object({ + name: z.string(), + class_name: z.string(), + script_name: z.string().optional(), + environment: z.string().optional(), +}); + +export const durableObjectsSchema = z.object({ + bindings: z.record(durableObjectsBindingsSchema), +}); + +export const migrationsSchema = z.object({ + tag: z.string(), + new_classes: z.array(z.string()).optional(), + renamed_classes: z + .array( + z.object({ + from: z.string(), + to: z.string(), + }), + ) + .optional(), + deleted_classes: z.array(z.string()).optional(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/email.ts b/packages/schema/src/non-inheritable-keys/bindings/email.ts new file mode 100644 index 0000000..dffe979 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/email.ts @@ -0,0 +1,10 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#email-bindings +const emailSchema = z.object({ + name: z.string(), + destination_address: z.string().optional(), + allowed_destination_addresses: z.array(z.string()).optional(), +}); + +export const sendEmailSchema = z.array(emailSchema); diff --git a/packages/schema/src/non-inheritable-keys/bindings/hyperdrive.ts b/packages/schema/src/non-inheritable-keys/bindings/hyperdrive.ts new file mode 100644 index 0000000..346e076 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/hyperdrive.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#email-bindings +export const hyperdriveSchema = z.object({ + binding: z.string(), + id: z.string(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/index.ts b/packages/schema/src/non-inheritable-keys/bindings/index.ts new file mode 100644 index 0000000..aeafd89 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/index.ts @@ -0,0 +1,33 @@ +import { z } from "zod"; +import { aiSchema } from "./ai"; +import { analyticsEngineDatasetsSchema } from "./analytics-engine-datasets"; +import { browserSchema } from "./browser"; +import { d1DatabasesSchema } from "./d1-database"; +import { dispatchNamespacesSchema } from "./dispatch-namespaces"; +import { durableObjectsSchema, migrationsSchema } from "./durable-object"; +import { sendEmailSchema } from "./email"; +import { hyperdriveSchema } from "./hyperdrive"; +import { kvNamespacesSchema } from "./kv-namespaces"; +import { mtlsCertificatesSchema } from "./mtls-certificates"; +import { queuesSchema } from "./queues"; +import { r2BucketsSchema } from "./r2-buckets"; +import { vectorizeSchema } from "./vectorize-indexes"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#non-inheritable-keys +export const bindingsKeysSchema = z.object({ + vars: z.record(z.any()).optional(), + browser: browserSchema.optional(), + d1_databases: d1DatabasesSchema.optional(), + durable_objects: durableObjectsSchema.optional(), + dispatch_namespaces: dispatchNamespacesSchema.optional(), + migrations: migrationsSchema.optional(), + send_email: sendEmailSchema.optional(), + hyperdrive: hyperdriveSchema.optional(), + kv_namespaces: kvNamespacesSchema.optional(), + queues: queuesSchema.optional(), + r2_buckets: r2BucketsSchema.optional(), + vectorize: vectorizeSchema.optional(), + analytics_engine_datasets: analyticsEngineDatasetsSchema.optional(), + mtls_certificates: mtlsCertificatesSchema.optional(), + ai: aiSchema.optional(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/kv-namespaces.ts b/packages/schema/src/non-inheritable-keys/bindings/kv-namespaces.ts new file mode 100644 index 0000000..0d2c0f9 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/kv-namespaces.ts @@ -0,0 +1,10 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces +const kvNamespaceSchema = z.object({ + binding: z.string(), + id: z.string(), + preview_id: z.string().optional(), +}); + +export const kvNamespacesSchema = z.array(kvNamespaceSchema); diff --git a/packages/schema/src/non-inheritable-keys/bindings/mtls-certificates.ts b/packages/schema/src/non-inheritable-keys/bindings/mtls-certificates.ts new file mode 100644 index 0000000..0ca9d5e --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/mtls-certificates.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates +export const mtlsCertificatesSchema = z.array( + z.object({ + binding: z.string(), + certificate_id: z.string(), + }), +); diff --git a/packages/schema/src/non-inheritable-keys/bindings/queues.ts b/packages/schema/src/non-inheritable-keys/bindings/queues.ts new file mode 100644 index 0000000..d7db65a --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/queues.ts @@ -0,0 +1,27 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#queues +const producersSchema = z.array( + z.object({ + binding: z.string(), + queue: z.string(), + delivery_delay: z.number().optional(), + }), +); + +const consumersSchema = z.array( + z.object({ + queue: z.string(), + max_batch_size: z.number().optional(), + max_batch_timeout: z.number().optional(), + max_retries: z.number().optional(), + dead_letter_queue: z.string().optional(), + max_concurrency: z.number().optional(), + retry_delay: z.number().optional(), + }), +); + +export const queuesSchema = z.object({ + producers: z.array(producersSchema).optional(), + consumers: z.array(consumersSchema).optional(), +}); diff --git a/packages/schema/src/non-inheritable-keys/bindings/r2-buckets.ts b/packages/schema/src/non-inheritable-keys/bindings/r2-buckets.ts new file mode 100644 index 0000000..d3e08c8 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/r2-buckets.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets +export const r2BucketsSchema = z.array( + z.object({ + binding: z.string(), + bucket_name: z.string(), + }), +); diff --git a/packages/schema/src/non-inheritable-keys/bindings/service-bindings.ts b/packages/schema/src/non-inheritable-keys/bindings/service-bindings.ts new file mode 100644 index 0000000..f336eba --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/service-bindings.ts @@ -0,0 +1,10 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings +export const serviceBindingsSchema = z.array( + z.object({ + binding: z.string(), + service: z.string(), + entrypoint: z.string().optional(), + }), +); diff --git a/packages/schema/src/non-inheritable-keys/bindings/vectorize-indexes.ts b/packages/schema/src/non-inheritable-keys/bindings/vectorize-indexes.ts new file mode 100644 index 0000000..cc15d84 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/bindings/vectorize-indexes.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; + +// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes +export const vectorizeSchema = z.array( + z.object({ + binding: z.string(), + index_name: z.string(), + }), +); diff --git a/packages/schema/src/non-inheritable-keys/index.ts b/packages/schema/src/non-inheritable-keys/index.ts new file mode 100644 index 0000000..720acb7 --- /dev/null +++ b/packages/schema/src/non-inheritable-keys/index.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +import { bindingsKeysSchema } from "./bindings"; + +export const nonInheritableKeysSchema = bindingsKeysSchema.extend({ + define: z.record(z.string()).optional(), +});