Updates an existing redirect

Updates an existing redirect

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Updates an existing redirect
7
 * Updates an existing redirect
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  redirect_id: string,
13
  body: {
14
    redirect?: {
15
      id?: number;
16
      path?: string;
17
      target?: string;
18
      [k: string]: unknown;
19
    };
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/redirects/${redirect_id}.json`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "PUT",
29
    headers: {
30
      "Content-Type": "application/json",
31
      "X-Shopify-Access-Token": auth.token,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41