Get fields paginated

Returns a [paginated](#pagination) list of fields for Classic Jira projects.

Script jira Verified

by hugo697 ยท 11/2/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Get fields paginated
8
 * Returns a [paginated](#pagination) list of fields for Classic Jira projects.
9
 */
10
export async function main(
11
  auth: Jira,
12
  startAt: string | undefined,
13
  maxResults: string | undefined,
14
  type: string | undefined,
15
  id: string | undefined,
16
  query: string | undefined,
17
  orderBy:
18
    | "contextsCount"
19
    | "-contextsCount"
20
    | "+contextsCount"
21
    | "lastUsed"
22
    | "-lastUsed"
23
    | "+lastUsed"
24
    | "name"
25
    | "-name"
26
    | "+name"
27
    | "screensCount"
28
    | "-screensCount"
29
    | "+screensCount"
30
    | "projectsCount"
31
    | "-projectsCount"
32
    | "+projectsCount"
33
    | undefined,
34
  expand: string | undefined
35
) {
36
  const url = new URL(
37
    `https://${auth.domain}.atlassian.net/rest/api/2/field/search`
38
  );
39
  for (const [k, v] of [
40
    ["startAt", startAt],
41
    ["maxResults", maxResults],
42
    ["type", type],
43
    ["id", id],
44
    ["query", query],
45
    ["orderBy", orderBy],
46
    ["expand", expand],
47
  ]) {
48
    if (v !== undefined && v !== "") {
49
      url.searchParams.append(k, v);
50
    }
51
  }
52
  const response = await fetch(url, {
53
    method: "GET",
54
    headers: {
55
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
56
    },
57
    body: undefined,
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65