Set preference

Creates a preference for the user or updates a preference's value by sending a plain text string.

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
 * Set preference
8
 * Creates a preference for the user or updates a preference's value by sending a plain text string.
9
 */
10
export async function main(auth: Jira, key: string | undefined, body: string) {
11
  const url = new URL(
12
    `https://${auth.domain}.atlassian.net/rest/api/2/mypreferences`
13
  );
14
  for (const [k, v] of [["key", key]]) {
15
    if (v !== undefined && v !== "") {
16
      url.searchParams.append(k, v);
17
    }
18
  }
19
  const response = await fetch(url, {
20
    method: "PUT",
21
    headers: {
22
      "Content-Type": "application/json",
23
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
24
    },
25
    body: JSON.stringify(body),
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.json();
32
}
33