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

Commit

Permalink
feat: supports all Inheritable keys fields (#10)
Browse files Browse the repository at this point in the history
* feat: supports all Inheritable keys fields

* chore: changeset
  • Loading branch information
sor4chi authored Apr 10, 2024
1 parent 9622e94 commit bff20f2
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 95 deletions.
7 changes: 7 additions & 0 deletions .changeset/quiet-taxis-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"twrangler": patch
---

Support all inheritable key fields

<https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys>
31 changes: 31 additions & 0 deletions examples/vanilla/wrangler.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,35 @@ export default defineConfig({
name: "my-project",
main: "src/index.ts",
compatibility_date: "2022-03-21",
route: "/api/*",
routes: [
{ pattern: "shop.example.com", custom_domain: true },
{ pattern: "admin.example.com", custom_domain: true },
],
tsconfig: "tsconfig.json",
triggers: {
crons: ["0 0 * * *"],
},
rules: [
{
type: "Text",
globs: ["**/*.md"],
fallthrough: true,
},
],
build: {
command: "npm run build",
cwd: "dist",
watch_dir: "src",
},
no_bundle: true,
minify: true,
node_compat: true,
preserve_file_names: true,
send_metrics: true,
keep_vars: true,
logpush: true,
limits: {
cpu_ms: 1000,
},
});
8 changes: 8 additions & 0 deletions packages/schema/src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#custom-builds
export const buildSchema = z.object({
command: z.string().optional(),
cwd: z.string().optional(),
watch_dir: z.union([z.string(), z.array(z.string())]).optional(),
});
92 changes: 92 additions & 0 deletions packages/schema/src/capability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { z } from "zod";

// ref: https://developers.cloudflare.com/workers/configuration/compatibility-dates
export const compatibilityDateSchema = z.enum([
"2024-04-29",
"2024-04-03",
"2024-04-01",
"2024-03-26",
"2024-03-18",
"2024-03-04",
"2023-12-01",
"2023-11-08",
"2023-08-15",
"2023-08-01",
"2023-03-14",
"2023-03-01",
"2022-11-30",
"2022-10-31",
"2022-08-04",
"2022-06-01",
"2022-04-05",
"2022-03-21",
"2022-03-08",
"2022-01-31",
"2021-11-10",
"2021-11-03",
]);

// ref: https://developers.cloudflare.com/workers/configuration/compatibility-dates/#compatibility-flags
export const compatibilityFlagsSchema = z.array(
z.enum([
"brotli_content_encoding",
"no_brotli_content_encoding",
"rpc",
"unwrap_custom_thenables",
"no_unwrap_custom_thenables",
"fetcher_no_get_put_delete",
"fetcher_has_get_put_delete",
"queues_json_messages",
"no_queues_json_messages",
"no_global_importscripts",
"global_importscripts",
"nodejs_als",
"no_nodejs_als",
"crypto_preserve_public_exponent",
"no_crypto_preserve_public_exponent",
"vectorize_query_metadata_optional",
"vectorize_query_original",
"web_socket_compression",
"no_web_socket_compression",
"strict_crypto_checks",
"no_strict_crypto_checks",
"strict_compression_checks",
"no_strict_compression_checks",
"response_redirect_url_standard",
"response_redirect_url_original",
"dynamic_dispatch_tunnel_exceptions",
"dynamic_dispatch_treat_exceptions_as_500",
"http_headers_getsetcookie",
"no_http_headers_getsetcookie",
"streams_enable_constructors",
"streams_disable_constructors",
"transformstream_enable_standard_constructor",
"transformstream_disable_standard_constructor",
"export_commonjs_default",
"export_commonjs_namespace",
"capture_async_api_throws",
"do_not_capture_async_api_throws",
"url_standard",
"url_original",
"r2_list_honor_include",
"dont_substitute_null_on_type_error",
"substitute_null_on_type_error",
"minimal_subrequests",
"no_minimal_subrequests",
"global_navigator",
"no_global_navigator",
"no_cots_on_external_fetch",
"cots_on_external_fetch",
"workers_api_getters_setters_on_prototype",
"workers_api_getters_setters_on_instance",
"durable_object_fetch_requires_full_url",
"durable_object_fetch_allows_relative_url",
"fetch_refuses_unknown_protocols",
"fetch_treats_unknown_protocols_as_http",
"streams_byob_reader_detaches_buffer",
"streams_byob_reader_does_not_detach_buffer",
"formdata_parser_supports_files",
"formdata_parser_converts_files_to_strings",
"html_rewriter_treats_esi_include_as_void_tag",
]),
);
118 changes: 23 additions & 95 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,35 @@
import { z } from "zod";

const compatibilityDateSchema = z.union([
z.literal("2024-04-29"),
z.literal("2024-04-03"),
z.literal("2024-04-01"),
z.literal("2024-03-26"),
z.literal("2024-03-18"),
z.literal("2024-03-04"),
z.literal("2023-12-01"),
z.literal("2023-11-08"),
z.literal("2023-08-15"),
z.literal("2023-08-01"),
z.literal("2023-03-14"),
z.literal("2023-03-01"),
z.literal("2022-11-30"),
z.literal("2022-10-31"),
z.literal("2022-08-04"),
z.literal("2022-06-01"),
z.literal("2022-04-05"),
z.literal("2022-03-21"),
z.literal("2022-03-08"),
z.literal("2022-01-31"),
z.literal("2021-11-10"),
z.literal("2021-11-03"),
]);

const compatibilityFlagsSchema = z.array(
z.union([
z.literal("brotli_content_encoding"),
z.literal("no_brotli_content_encoding"),
z.literal("rpc"),
z.literal("unwrap_custom_thenables"),
z.literal("no_unwrap_custom_thenables"),
z.literal("fetcher_no_get_put_delete"),
z.literal("fetcher_has_get_put_delete"),
z.literal("queues_json_messages"),
z.literal("no_queues_json_messages"),
z.literal("no_global_importscripts"),
z.literal("global_importscripts"),
z.literal("nodejs_als"),
z.literal("no_nodejs_als"),
z.literal("crypto_preserve_public_exponent"),
z.literal("no_crypto_preserve_public_exponent"),
z.literal("vectorize_query_metadata_optional"),
z.literal("vectorize_query_original"),
z.literal("web_socket_compression"),
z.literal("no_web_socket_compression"),
z.literal("strict_crypto_checks"),
z.literal("no_strict_crypto_checks"),
z.literal("strict_compression_checks"),
z.literal("no_strict_compression_checks"),
z.literal("response_redirect_url_standard"),
z.literal("response_redirect_url_original"),
z.literal("dynamic_dispatch_tunnel_exceptions"),
z.literal("dynamic_dispatch_treat_exceptions_as_500"),
z.literal("http_headers_getsetcookie"),
z.literal("no_http_headers_getsetcookie"),
z.literal("streams_enable_constructors"),
z.literal("streams_disable_constructors"),
z.literal("transformstream_enable_standard_constructor"),
z.literal("transformstream_disable_standard_constructor"),
z.literal("export_commonjs_default"),
z.literal("export_commonjs_namespace"),
z.literal("capture_async_api_throws"),
z.literal("do_not_capture_async_api_throws"),
z.literal("url_standard"),
z.literal("url_original"),
z.literal("r2_list_honor_include"),
z.literal("dont_substitute_null_on_type_error"),
z.literal("substitute_null_on_type_error"),
z.literal("minimal_subrequests"),
z.literal("no_minimal_subrequests"),
z.literal("global_navigator"),
z.literal("no_global_navigator"),
z.literal("no_cots_on_external_fetch"),
z.literal("cots_on_external_fetch"),
z.literal("workers_api_getters_setters_on_prototype"),
z.literal("workers_api_getters_setters_on_instance"),
z.literal("durable_object_fetch_requires_full_url"),
z.literal("durable_object_fetch_allows_relative_url"),
z.literal("fetch_refuses_unknown_protocols"),
z.literal("fetch_treats_unknown_protocols_as_http"),
z.literal("streams_byob_reader_detaches_buffer"),
z.literal("streams_byob_reader_does_not_detach_buffer"),
z.literal("formdata_parser_supports_files"),
z.literal("formdata_parser_converts_files_to_strings"),
z.literal("html_rewriter_treats_esi_include_as_void_tag"),
]),
);
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({
// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys
name: z.string(),
// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys
main: z.string(),
// ref: https://developers.cloudflare.com/workers/configuration/compatibility-dates
compatibility_date: compatibilityDateSchema,
// ref: // ref: https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys
account_id: z.string().optional(),
// ref: https://developers.cloudflare.com/workers/configuration/compatibility-dates/#compatibility-flags
compatibility_flags: compatibilityFlagsSchema.optional(),
// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys
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(),
});

export type Config = z.infer<typeof configSchema>;
Expand Down
6 changes: 6 additions & 0 deletions packages/schema/src/limits.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/#limits
export const limitsSchema = z.object({
cpu_ms: z.number().optional(),
});
38 changes: 38 additions & 0 deletions packages/schema/src/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { z } from "zod";

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#custom-domains
const customDomainsItemSchema = z.object({
pattern: z.string(),
custom_domain: z.boolean().optional(),
});

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#zone-id-route
const zoneIdRouteItemSchema = z.object({
pattern: z.string(),
zone_id: z.string(),
});

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#zone-name-route
const zoneNameRouteItemSchema = z.object({
pattern: z.string(),
zone_name: z.string(),
});

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#simple-route
const simpleRouteItemSchema = z.string();

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes
export const routeSchema = z.union([
customDomainsItemSchema,
zoneIdRouteItemSchema,
zoneNameRouteItemSchema,
simpleRouteItemSchema,
]);

// ref: https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routess
export const routesSchema = z.union([
z.array(customDomainsItemSchema),
z.array(zoneIdRouteItemSchema),
z.array(zoneNameRouteItemSchema),
z.array(simpleRouteItemSchema),
]);
10 changes: 10 additions & 0 deletions packages/schema/src/rule.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/#bundling
const ruleSchema = z.object({
type: z.enum(["ESModule", "CommonJS", "CompiledWasm", "Text", "Data"]),
globs: z.array(z.string()),
fallthrough: z.boolean().optional(),
});

export const rulesSchema = z.array(ruleSchema);
5 changes: 5 additions & 0 deletions packages/schema/src/trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from "zod";

export const triggersSchema = z.object({
crons: z.array(z.string()),
});

0 comments on commit bff20f2

Please sign in to comment.