Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat: supports non inheritable keys (#13)
Browse files Browse the repository at this point in the history
* refactor: separate folder by whether they are inheritable or not

* feat: add non inheritable properties

* chore: changeset
  • Loading branch information
sor4chi authored Apr 11, 2024
1 parent fe7afd6 commit a1574c7
Show file tree
Hide file tree
Showing 25 changed files with 250 additions and 31 deletions.
8 changes: 8 additions & 0 deletions .changeset/three-jobs-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"twrangler": patch
---

Supports non inheritable keys

- Multi Environments Configuration
- Bindings
41 changes: 10 additions & 31 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<typeof configSchema>;

Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions packages/schema/src/inheritable-keys/index.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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()),
});
6 changes: 6 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/ai.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
Original file line number Diff line number Diff line change
@@ -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(),
}),
);
6 changes: 6 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/browser.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
11 changes: 11 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/d1-database.ts
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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(),
});
Original file line number Diff line number Diff line change
@@ -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(),
});
10 changes: 10 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/email.ts
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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(),
});
33 changes: 33 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/index.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
10 changes: 10 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/kv-namespaces.ts
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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(),
}),
);
27 changes: 27 additions & 0 deletions packages/schema/src/non-inheritable-keys/bindings/queues.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
Original file line number Diff line number Diff line change
@@ -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(),
}),
);
Original file line number Diff line number Diff line change
@@ -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(),
}),
);
Original file line number Diff line number Diff line change
@@ -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(),
}),
);
7 changes: 7 additions & 0 deletions packages/schema/src/non-inheritable-keys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from "zod";

import { bindingsKeysSchema } from "./bindings";

export const nonInheritableKeysSchema = bindingsKeysSchema.extend({
define: z.record(z.string()).optional(),
});

0 comments on commit a1574c7

Please sign in to comment.