type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Update a zone ruleset
* Updates a zone ruleset, creating a new version.
*/
export async function main(
auth: Cloudflare,
ruleset_id: string,
zone_id: string,
body: {
description?: string;
id: string & { [k: string]: unknown };
kind?: "managed" | "custom" | "root" | "zone";
last_updated: string;
name?: string;
phase?:
| "ddos_l4"
| "ddos_l7"
| "http_config_settings"
| "http_custom_errors"
| "http_log_custom_fields"
| "http_ratelimit"
| "http_request_cache_settings"
| "http_request_dynamic_redirect"
| "http_request_firewall_custom"
| "http_request_firewall_managed"
| "http_request_late_transform"
| "http_request_origin"
| "http_request_redirect"
| "http_request_sanitize"
| "http_request_sbfm"
| "http_request_select_configuration"
| "http_request_transform"
| "http_response_compression"
| "http_response_firewall_managed"
| "http_response_headers_transform"
| "magic_transit"
| "magic_transit_ids_managed"
| "magic_transit_managed";
version: string;
[k: string]: unknown;
} & {
rules: (
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: string[];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "block";
action_parameters?: {
response?: {
content: string;
content_type: string;
status_code: number;
[k: string]: unknown;
};
[k: string]: unknown;
};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: string[];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "execute";
action_parameters?: {
id: string & { [k: string]: unknown };
matched_data?: { public_key: string; [k: string]: unknown };
overrides?: {
action?: string & { [k: string]: unknown };
categories?: {
action?: string & { [k: string]: unknown };
category: string & { [k: string]: unknown };
enabled?: boolean & { [k: string]: unknown };
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
}[];
enabled?: boolean & { [k: string]: unknown };
rules?: {
action?: string & { [k: string]: unknown };
enabled?: boolean & { [k: string]: unknown };
id: string & { [k: string]: unknown };
score_threshold?: number;
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
}[];
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
};
[k: string]: unknown;
};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: string[];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "log";
action_parameters?: {};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: string[];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "skip";
action_parameters?: {
phases?: (
| "ddos_l4"
| "ddos_l7"
| "http_config_settings"
| "http_custom_errors"
| "http_log_custom_fields"
| "http_ratelimit"
| "http_request_cache_settings"
| "http_request_dynamic_redirect"
| "http_request_firewall_custom"
| "http_request_firewall_managed"
| "http_request_late_transform"
| "http_request_origin"
| "http_request_redirect"
| "http_request_sanitize"
| "http_request_sbfm"
| "http_request_select_configuration"
| "http_request_transform"
| "http_response_compression"
| "http_response_firewall_managed"
| "http_response_headers_transform"
| "magic_transit"
| "magic_transit_ids_managed"
| ("magic_transit_managed" & { [k: string]: unknown })
)[];
products?:
| "bic"
| "hot"
| "rateLimit"
| "securityLevel"
| "uaBlock"
| "waf"
| "zoneLockdown"[];
rules?: { [k: string]: (string & { [k: string]: unknown })[] };
ruleset?: "current";
rulesets?: (string & { [k: string]: unknown })[];
[k: string]: unknown;
};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
)[];
[k: string]: unknown;
}
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/zones/${zone_id}/rulesets/${ruleset_id}`
);
const response = await fetch(url, {
method: "PUT",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
"Content-Type": "application/json",
Authorization: "Bearer " + auth.token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 291 days ago
type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* Update a zone ruleset
* Updates a zone ruleset, creating a new version.
*/
export async function main(
auth: Cloudflare,
ruleset_id: string,
zone_id: string,
body: {
description?: string;
id: string & { [k: string]: unknown };
kind?: "managed" | "custom" | "root" | "zone";
last_updated: string;
name?: string;
phase?:
| "ddos_l4"
| "ddos_l7"
| "http_config_settings"
| "http_custom_errors"
| "http_log_custom_fields"
| "http_ratelimit"
| "http_request_cache_settings"
| "http_request_dynamic_redirect"
| "http_request_firewall_custom"
| "http_request_firewall_managed"
| "http_request_late_transform"
| "http_request_origin"
| "http_request_redirect"
| "http_request_sanitize"
| "http_request_sbfm"
| "http_request_select_configuration"
| "http_request_transform"
| "http_response_compression"
| "http_response_firewall_managed"
| "http_response_headers_transform"
| "magic_transit"
| "magic_transit_ids_managed"
| "magic_transit_managed";
version: string;
[k: string]: unknown;
} & {
rules: (
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: [string, ...string[]];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "block";
action_parameters?: {
response?: {
content: string;
content_type: string;
status_code: number;
[k: string]: unknown;
};
[k: string]: unknown;
};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: [string, ...string[]];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "execute";
action_parameters?: {
id: string & { [k: string]: unknown };
matched_data?: { public_key: string; [k: string]: unknown };
overrides?: {
action?: string & { [k: string]: unknown };
categories?: [
{
action?: string & { [k: string]: unknown };
category: string & { [k: string]: unknown };
enabled?: boolean & { [k: string]: unknown };
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
},
...{
action?: string & { [k: string]: unknown };
category: string & { [k: string]: unknown };
enabled?: boolean & { [k: string]: unknown };
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
}[]
];
enabled?: boolean & { [k: string]: unknown };
rules?: [
{
action?: string & { [k: string]: unknown };
enabled?: boolean & { [k: string]: unknown };
id: string & { [k: string]: unknown };
score_threshold?: number;
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
},
...{
action?: string & { [k: string]: unknown };
enabled?: boolean & { [k: string]: unknown };
id: string & { [k: string]: unknown };
score_threshold?: number;
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
}[]
];
sensitivity_level?:
| "default"
| "medium"
| "low"
| ("eoff" & { [k: string]: unknown });
[k: string]: unknown;
};
[k: string]: unknown;
};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: [string, ...string[]];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "log";
action_parameters?: {};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
| ({
action?: string;
action_parameters?: { [k: string]: unknown };
categories?: [string, ...string[]];
description?: string;
enabled?: boolean & boolean;
expression?: string;
id?: string;
last_updated: string;
logging?: { enabled: boolean; [k: string]: unknown };
ref?: string;
version: string;
[k: string]: unknown;
} & {
action?: "skip";
action_parameters?: {
phases?: [
(
| "ddos_l4"
| "ddos_l7"
| "http_config_settings"
| "http_custom_errors"
| "http_log_custom_fields"
| "http_ratelimit"
| "http_request_cache_settings"
| "http_request_dynamic_redirect"
| "http_request_firewall_custom"
| "http_request_firewall_managed"
| "http_request_late_transform"
| "http_request_origin"
| "http_request_redirect"
| "http_request_sanitize"
| "http_request_sbfm"
| "http_request_select_configuration"
| "http_request_transform"
| "http_response_compression"
| "http_response_firewall_managed"
| "http_response_headers_transform"
| "magic_transit"
| "magic_transit_ids_managed"
| ("magic_transit_managed" & { [k: string]: unknown })
),
...(
| "ddos_l4"
| "ddos_l7"
| "http_config_settings"
| "http_custom_errors"
| "http_log_custom_fields"
| "http_ratelimit"
| "http_request_cache_settings"
| "http_request_dynamic_redirect"
| "http_request_firewall_custom"
| "http_request_firewall_managed"
| "http_request_late_transform"
| "http_request_origin"
| "http_request_redirect"
| "http_request_sanitize"
| "http_request_sbfm"
| "http_request_select_configuration"
| "http_request_transform"
| "http_response_compression"
| "http_response_firewall_managed"
| "http_response_headers_transform"
| "magic_transit"
| "magic_transit_ids_managed"
| ("magic_transit_managed" & { [k: string]: unknown })
)[]
];
products?: [
(
| "bic"
| "hot"
| "rateLimit"
| "securityLevel"
| "uaBlock"
| "waf"
| "zoneLockdown"
),
...(
| "bic"
| "hot"
| "rateLimit"
| "securityLevel"
| "uaBlock"
| "waf"
| "zoneLockdown"
)[]
];
rules?: {
[k: string]: [
string & { [k: string]: unknown },
...(string & { [k: string]: unknown })[]
];
};
ruleset?: "current";
rulesets?: [
string & { [k: string]: unknown },
...(string & { [k: string]: unknown })[]
];
[k: string]: unknown;
};
description?: { [k: string]: unknown };
[k: string]: unknown;
})
)[];
[k: string]: unknown;
}
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/zones/${zone_id}/rulesets/${ruleset_id}`
);
const response = await fetch(url, {
method: "PUT",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
"Content-Type": "application/json",
Authorization: "Bearer " + auth.token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 402 days ago