Retrieves a list of URL redirects

Retrieves a list of URL redirects. Note: As of version 2019-10, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.

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
 * Retrieves a list of URL redirects
7
 * Retrieves a list of URL redirects. Note: As of version 2019-10, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  limit: string | undefined,
13
  since_id: string | undefined,
14
  path: string | undefined,
15
  target: string | undefined,
16
  fields: string | undefined
17
) {
18
  const url = new URL(
19
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/redirects.json`
20
  );
21
  for (const [k, v] of [
22
    ["limit", limit],
23
    ["since_id", since_id],
24
    ["path", path],
25
    ["target", target],
26
    ["fields", fields],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      "X-Shopify-Access-Token": auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45