Change Image Resizing setting

Image Resizing provides on-demand resizing, conversion and optimisation for images served through Cloudflare's network. Refer to the [Image Resizing documentation](https://developers.cloudflare.com/images/) for more information.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Change Image Resizing setting
8
 * Image Resizing provides on-demand resizing, conversion and optimisation
9
for images served through Cloudflare's network. Refer to the
10
[Image Resizing documentation](https://developers.cloudflare.com/images/)
11
for more information.
12

13
 */
14
export async function main(
15
  auth: Cloudflare,
16
  zone_identifier: string,
17
  body: {
18
    value: {
19
      editable?: true | false;
20
      id: string;
21
      modified_on?: string;
22
      value: { [k: string]: unknown };
23
      [k: string]: unknown;
24
    } & {
25
      id?: "image_resizing";
26
      value?: "on" | "off" | "open";
27
      [k: string]: unknown;
28
    };
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/settings/image_resizing`
34
  );
35

36
  const response = await fetch(url, {
37
    method: "PATCH",
38
    headers: {
39
      "X-AUTH-EMAIL": auth.email,
40
      "X-AUTH-KEY": auth.key,
41
      "Content-Type": "application/json",
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: JSON.stringify(body),
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52