Skip to content

Commit 8f5105e

Browse files
committed
feat: optionally send zone_name with routes
A followup to #778, this lets you send an optional `zone_name` with routes. This is particularly useful when using ssl for saas (https://developers.cloudflare.com/ssl/ssl-for-saas/). Fixes #793
1 parent 34552d9 commit 8f5105e

File tree

9 files changed

+145
-23
lines changed

9 files changed

+145
-23
lines changed

.changeset/fast-news-dress.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat: optionally send `zone_name` with routes
6+
7+
A followup to https://github.com/cloudflare/wrangler2/pull/778, this lets you send an optional `zone_name` with routes. This is particularly useful when using ssl for saas (https://developers.cloudflare.com/ssl/ssl-for-saas/).
8+
9+
Fixes https://github.com/cloudflare/wrangler2/issues/793

packages/wrangler/src/__tests__/configuration.test.ts

+47-4
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,17 @@ describe("normalizeAndValidateConfig()", () => {
643643
"example.com/*",
644644
{ pattern: 123, zone_id: "zone_id_1" },
645645
{ pattern: "route_2", zone_id: 123 },
646+
{ pattern: "route_2", zone_name: 123 },
646647
{ pattern: "route_3" },
647648
{ zone_id: "zone_id_4" },
649+
{ zone_name: "zone_name_4" },
648650
{ pattern: undefined },
649651
{ pattern: "route_5", zone_id: "zone_id_5", some_other_key: 123 },
652+
{ pattern: "route_5", zone_name: "zone_name_5", some_other_key: 123 },
650653
// this one's valid too
651654
{ pattern: "route_6", zone_id: "zone_id_6" },
655+
// as well as this one
656+
{ pattern: "route_6", zone_name: "zone_name_6" },
652657
],
653658
route: 888,
654659
jsx_factory: 999,
@@ -674,8 +679,43 @@ describe("normalizeAndValidateConfig()", () => {
674679
expect(diagnostics.hasWarnings()).toBe(false);
675680
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
676681
"Processing wrangler configuration:
677-
- Expected \\"route\\" to be either a string or an object with shape {pattern: string, zone_id: string}, but got 888.
678-
- Expected \\"routes\\" to be an array of either strings or objects with the shape {pattern: string, zone_id: string}, but these weren't valid: [666,777,{\\"pattern\\":123,\\"zone_id\\":\\"zone_id_1\\"},{\\"pattern\\":\\"route_2\\",\\"zone_id\\":123},{\\"pattern\\":\\"route_3\\"},{\\"zone_id\\":\\"zone_id_4\\"},{},{\\"pattern\\":\\"route_5\\",\\"zone_id\\":\\"zone_id_5\\",\\"some_other_key\\":123}].
682+
- Expected \\"route\\" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got 888.
683+
- Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: [
684+
666,
685+
777,
686+
{
687+
\\"pattern\\": 123,
688+
\\"zone_id\\": \\"zone_id_1\\"
689+
},
690+
{
691+
\\"pattern\\": \\"route_2\\",
692+
\\"zone_id\\": 123
693+
},
694+
{
695+
\\"pattern\\": \\"route_2\\",
696+
\\"zone_name\\": 123
697+
},
698+
{
699+
\\"pattern\\": \\"route_3\\"
700+
},
701+
{
702+
\\"zone_id\\": \\"zone_id_4\\"
703+
},
704+
{
705+
\\"zone_name\\": \\"zone_name_4\\"
706+
},
707+
{},
708+
{
709+
\\"pattern\\": \\"route_5\\",
710+
\\"zone_id\\": \\"zone_id_5\\",
711+
\\"some_other_key\\": 123
712+
},
713+
{
714+
\\"pattern\\": \\"route_5\\",
715+
\\"zone_name\\": \\"zone_name_5\\",
716+
\\"some_other_key\\": 123
717+
}
718+
].
679719
- Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
680720
- Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
681721
- Expected \\"build.command\\" to be of type string but got 1444.
@@ -1816,8 +1856,11 @@ describe("normalizeAndValidateConfig()", () => {
18161856
"Processing wrangler configuration:
18171857
18181858
- \\"env.ENV1\\" environment configuration
1819-
- Expected \\"route\\" to be either a string or an object with shape {pattern: string, zone_id: string}, but got 888.
1820-
- Expected \\"routes\\" to be an array of either strings or objects with the shape {pattern: string, zone_id: string}, but these weren't valid: [666,777].
1859+
- Expected \\"route\\" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got 888.
1860+
- Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: [
1861+
666,
1862+
777
1863+
].
18211864
- Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
18221865
- Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
18231866
- Expected \\"build.command\\" to be of type string but got 1444.

packages/wrangler/src/__tests__/dev.test.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ describe("wrangler dev", () => {
233233
});
234234
});
235235

236+
it("should, when provided, use a zone_name to get a zone_id", async () => {
237+
writeWranglerToml({
238+
main: "index.js",
239+
routes: [
240+
{ pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
241+
],
242+
});
243+
fs.writeFileSync("index.js", `export default {};`);
244+
mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
245+
await runWrangler("dev");
246+
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
247+
// note that it uses the provided zone_name as a host too
248+
host: "some-zone.com",
249+
id: "a-zone-id",
250+
});
251+
});
252+
236253
it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
237254
writeWranglerToml({
238255
main: "index.js",

packages/wrangler/src/__tests__/publish.test.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,15 @@ describe("publish", () => {
316316
await runWrangler("publish ./index");
317317
});
318318

319-
it("should publish to a route with a pattern/zone_id combo", async () => {
319+
it("should publish to a route with a pattern/{zone_id|zone_name} combo", async () => {
320320
writeWranglerToml({
321321
routes: [
322322
"some-example.com/some-route/*",
323323
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
324+
{
325+
pattern: "*another-boring-website.com",
326+
zone_name: "some-zone.com",
327+
},
324328
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
325329
"more-examples.com/*",
326330
],
@@ -332,6 +336,10 @@ describe("publish", () => {
332336
routes: [
333337
"some-example.com/some-route/*",
334338
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
339+
{
340+
pattern: "*another-boring-website.com",
341+
zone_name: "some-zone.com",
342+
},
335343
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
336344
"more-examples.com/*",
337345
],
@@ -343,21 +351,26 @@ describe("publish", () => {
343351
"out": "Uploaded test-name (TIMINGS)
344352
Published test-name (TIMINGS)
345353
some-example.com/some-route/*
346-
*a-boring-website.com (zone: 54sdf7fsda)
347-
example.com/some-route/* (zone: JGHFHG654gjcj)
354+
*a-boring-website.com (zone id: 54sdf7fsda)
355+
*another-boring-website.com (zone name: some-zone.com)
356+
example.com/some-route/* (zone id: JGHFHG654gjcj)
348357
more-examples.com/*",
349358
"warn": "",
350359
}
351360
`);
352361
});
353362

354-
it("should publish to a route with a pattern/zone_id combo (service environments)", async () => {
363+
it("should publish to a route with a pattern/{zone_id|zone_name} combo (service environments)", async () => {
355364
writeWranglerToml({
356365
env: {
357366
staging: {
358367
routes: [
359368
"some-example.com/some-route/*",
360369
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
370+
{
371+
pattern: "*another-boring-website.com",
372+
zone_name: "some-zone.com",
373+
},
361374
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
362375
"more-examples.com/*",
363376
],
@@ -380,6 +393,10 @@ describe("publish", () => {
380393
routes: [
381394
"some-example.com/some-route/*",
382395
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
396+
{
397+
pattern: "*another-boring-website.com",
398+
zone_name: "some-zone.com",
399+
},
383400
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
384401
"more-examples.com/*",
385402
],
@@ -393,8 +410,9 @@ describe("publish", () => {
393410
"out": "Uploaded test-name (staging) (TIMINGS)
394411
Published test-name (staging) (TIMINGS)
395412
some-example.com/some-route/*
396-
*a-boring-website.com (zone: 54sdf7fsda)
397-
example.com/some-route/* (zone: JGHFHG654gjcj)
413+
*a-boring-website.com (zone id: 54sdf7fsda)
414+
*another-boring-website.com (zone name: some-zone.com)
415+
example.com/some-route/* (zone id: JGHFHG654gjcj)
398416
more-examples.com/*",
399417
"warn": "",
400418
}
@@ -3919,7 +3937,11 @@ function mockPublishRoutesRequest({
39193937
env = undefined,
39203938
legacyEnv = false,
39213939
}: {
3922-
routes: (string | { pattern: string; zone_id: string })[];
3940+
routes: (
3941+
| string
3942+
| { pattern: string; zone_id: string }
3943+
| { pattern: string; zone_name: string }
3944+
)[];
39233945
env?: string | undefined;
39243946
legacyEnv?: boolean | undefined;
39253947
}) {

packages/wrangler/src/config/config.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,13 @@ export interface DevConfig {
185185
/**
186186
* Host to forward requests to, defaults to the host of the first route of project
187187
*/
188-
host: (string | { pattern: string; zone_id: string }) | undefined;
188+
host:
189+
| (
190+
| string
191+
| { pattern: string; zone_id: string }
192+
| { pattern: string; zone_name: string }
193+
)
194+
| undefined;
189195
}
190196

191197
export type RawDevConfig = Partial<DevConfig>;

packages/wrangler/src/config/environment.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ interface EnvironmentInheritable {
7474
*
7575
* @inheritable
7676
*/
77-
routes: (string | { pattern: string; zone_id: string })[] | undefined;
77+
routes:
78+
| (
79+
| string
80+
| { pattern: string; zone_id: string }
81+
| { pattern: string; zone_name: string }
82+
)[]
83+
| undefined;
7884

7985
/**
8086
* A route that your worker should be published to. Literally
@@ -85,7 +91,13 @@ interface EnvironmentInheritable {
8591
*
8692
* @inheritable
8793
*/
88-
route: (string | { pattern: string; zone_id: string }) | undefined;
94+
route:
95+
| (
96+
| string
97+
| { pattern: string; zone_id: string }
98+
| { pattern: string; zone_name: string }
99+
)
100+
| undefined;
89101

90102
/**
91103
* Path to a custom tsconfig

packages/wrangler/src/config/validation.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,11 @@ function isValidRouteValue(item: unknown): boolean {
472472
(typeof item === "object" &&
473473
hasProperty(item, "pattern") &&
474474
typeof item.pattern === "string" &&
475-
hasProperty(item, "zone_id") &&
476-
typeof item.zone_id === "string" &&
475+
// it could have a zone_name
476+
((hasProperty(item, "zone_name") &&
477+
typeof item.zone_name === "string") ||
478+
// or a zone_id
479+
(hasProperty(item, "zone_id") && typeof item.zone_id === "string")) &&
477480
Object.keys(item).length === 2))
478481
);
479482
}
@@ -484,7 +487,7 @@ function isValidRouteValue(item: unknown): boolean {
484487
const isRoute: ValidatorFn = (diagnostics, field, value) => {
485488
if (value !== undefined && !isValidRouteValue(value)) {
486489
diagnostics.errors.push(
487-
`Expected "${field}" to be either a string or an object with shape {pattern: string, zone_id: string}, but got ${JSON.stringify(
490+
`Expected "${field}" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got ${JSON.stringify(
488491
value
489492
)}.`
490493
);
@@ -514,8 +517,10 @@ const isRouteArray: ValidatorFn = (diagnostics, field, value) => {
514517
}
515518
if (invalidRoutes.length > 0) {
516519
diagnostics.errors.push(
517-
`Expected "${field}" to be an array of either strings or objects with the shape {pattern: string, zone_id: string}, but these weren't valid: ${JSON.stringify(
518-
invalidRoutes
520+
`Expected "${field}" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: ${JSON.stringify(
521+
invalidRoutes,
522+
null,
523+
2
519524
)}.`
520525
);
521526
}

packages/wrangler/src/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,17 @@ export async function main(argv: string[]): Promise<void> {
813813
(config.routes && config.routes[0]);
814814

815815
let zoneId: string | undefined =
816-
typeof hostLike === "object" ? hostLike.zone_id : undefined;
816+
typeof hostLike === "object" && "zone_id" in hostLike
817+
? hostLike.zone_id
818+
: undefined;
817819

818820
const host =
819821
typeof hostLike === "string"
820822
? getHost(hostLike)
821823
: typeof hostLike === "object"
822-
? getHost(hostLike.pattern)
824+
? "zone_name" in hostLike
825+
? getHost(hostLike.zone_name)
826+
: getHost(hostLike.pattern)
823827
: undefined;
824828

825829
const hostPieces =

packages/wrangler/src/publish.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,18 @@ export default async function publish(props: Props): Promise<void> {
319319
.map((route) =>
320320
typeof route === "string"
321321
? route
322-
: `${route.pattern} (zone: ${route.zone_id})`
322+
: "zone_id" in route
323+
? `${route.pattern} (zone id: ${route.zone_id})`
324+
: `${route.pattern} (zone name: ${route.zone_name})`
323325
)
324326
.concat([`...and ${routes.length - 10} more routes`]);
325327
}
326328
return routes.map((route) =>
327329
typeof route === "string"
328330
? route
329-
: `${route.pattern} (zone: ${route.zone_id})`
331+
: "zone_id" in route
332+
? `${route.pattern} (zone id: ${route.zone_id})`
333+
: `${route.pattern} (zone name: ${route.zone_name})`
330334
);
331335
})
332336
);

0 commit comments

Comments
 (0)