@@ -30,13 +30,13 @@ type WorkerScriptParams struct {
30
30
Bindings map [string ]WorkerBinding
31
31
}
32
32
33
- // WorkerRoute aka filters are patterns used to enable or disable workers that match requests.
33
+ // WorkerRoute is used to map traffic matching a URL pattern to a workers
34
34
//
35
- // API reference: https://api.cloudflare.com/#worker-filters -properties
35
+ // API reference: https://api.cloudflare.com/#worker-routes -properties
36
36
type WorkerRoute struct {
37
37
ID string `json:"id,omitempty"`
38
38
Pattern string `json:"pattern"`
39
- Enabled bool `json:"enabled"`
39
+ Enabled bool `json:"enabled"` // this is deprecated: https://api.cloudflare.com/#worker-filters-deprecated--properties
40
40
Script string `json:"script,omitempty"`
41
41
}
42
42
@@ -566,14 +566,9 @@ func formatMultipartBody(params *WorkerScriptParams) (string, []byte, error) {
566
566
//
567
567
// API reference: https://api.cloudflare.com/#worker-filters-create-filter, https://api.cloudflare.com/#worker-routes-create-route
568
568
func (api * API ) CreateWorkerRoute (zoneID string , route WorkerRoute ) (WorkerRouteResponse , error ) {
569
- // Check whether a script name is defined in order to determine whether
570
- // to use the single-script or multi-script endpoint.
571
- pathComponent := "filters"
572
- if route .Script != "" {
573
- if api .AccountID == "" {
574
- return WorkerRouteResponse {}, errors .New ("account ID required for enterprise only request" )
575
- }
576
- pathComponent = "routes"
569
+ pathComponent , err := getRouteEndpoint (api , route )
570
+ if err != nil {
571
+ return WorkerRouteResponse {}, err
577
572
}
578
573
579
574
uri := "/zones/" + zoneID + "/workers/" + pathComponent
@@ -611,7 +606,16 @@ func (api *API) DeleteWorkerRoute(zoneID string, routeID string) (WorkerRouteRes
611
606
// API reference: https://api.cloudflare.com/#worker-filters-list-filters, https://api.cloudflare.com/#worker-routes-list-routes
612
607
func (api * API ) ListWorkerRoutes (zoneID string ) (WorkerRoutesResponse , error ) {
613
608
pathComponent := "filters"
614
- if api .AccountID != "" {
609
+ // Unfortunately we don't have a good signal of whether the user is wanting
610
+ // to use the deprecated filters endpoint (https://api.cloudflare.com/#worker-filters-list-filters)
611
+ // or the multi-script routes endpoint (https://api.cloudflare.com/#worker-script-list-workers)
612
+ //
613
+ // The filters endpoint does not support API tokens, so if an API token is specified we need to use
614
+ // the routes endpoint. Otherwise, since the multi-script API endpoints that operate on a script
615
+ // require an AccountID, we assume that anyone specifying an AccountID is using the routes endpoint.
616
+ // This is likely too presumptuous. In the next major version, we should just remove the deprecated
617
+ // filter endpoints entirely to avoid this ambiguity.
618
+ if api .AccountID != "" || api .APIToken != "" {
615
619
pathComponent = "routes"
616
620
}
617
621
uri := "/zones/" + zoneID + "/workers/" + pathComponent
@@ -640,14 +644,9 @@ func (api *API) ListWorkerRoutes(zoneID string) (WorkerRoutesResponse, error) {
640
644
//
641
645
// API reference: https://api.cloudflare.com/#worker-filters-update-filter, https://api.cloudflare.com/#worker-routes-update-route
642
646
func (api * API ) UpdateWorkerRoute (zoneID string , routeID string , route WorkerRoute ) (WorkerRouteResponse , error ) {
643
- // Check whether a script name is defined in order to determine whether
644
- // to use the single-script or multi-script endpoint.
645
- pathComponent := "filters"
646
- if route .Script != "" {
647
- if api .AccountID == "" {
648
- return WorkerRouteResponse {}, errors .New ("account ID required for enterprise only request" )
649
- }
650
- pathComponent = "routes"
647
+ pathComponent , err := getRouteEndpoint (api , route )
648
+ if err != nil {
649
+ return WorkerRouteResponse {}, err
651
650
}
652
651
uri := "/zones/" + zoneID + "/workers/" + pathComponent + "/" + routeID
653
652
res , err := api .makeRequest ("PUT" , uri , route )
@@ -661,3 +660,18 @@ func (api *API) UpdateWorkerRoute(zoneID string, routeID string, route WorkerRou
661
660
}
662
661
return r , nil
663
662
}
663
+
664
+ func getRouteEndpoint (api * API , route WorkerRoute ) (string , error ) {
665
+ if route .Script != "" && route .Enabled == true {
666
+ return "" , errors .New ("Only `Script` or `Enabled` may be specified for a WorkerRoute, not both" )
667
+ }
668
+
669
+ // For backwards-compatability, fallback to the deprecated filter
670
+ // endpoint if Enabled == true
671
+ // https://api.cloudflare.com/#worker-filters-deprecated--properties
672
+ if route .Enabled == true {
673
+ return "filters" , nil
674
+ }
675
+
676
+ return "routes" , nil
677
+ }
0 commit comments