Change Polish setting

Automatically optimize image loading for website visitors on mobile devices. Refer to our [blog post](http://blog.cloudflare.com/polish-solving-mobile-speed) 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 Polish setting
8
 * Automatically optimize image loading for website visitors on mobile devices. Refer to our [blog post](http://blog.cloudflare.com/polish-solving-mobile-speed) for more information.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  body: {
14
    value: {
15
      editable?: true | false;
16
      id: string;
17
      modified_on?: string;
18
      value: { [k: string]: unknown };
19
      [k: string]: unknown;
20
    } & {
21
      id?: "polish";
22
      value?: "off" | "lossless" | "lossy";
23
      [k: string]: unknown;
24
    };
25
    [k: string]: unknown;
26
  }
27
) {
28
  const url = new URL(
29
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/settings/polish`
30
  );
31

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