0

Get preference

Published Nov 2, 2023

Returns the value of a preference of the current user.

Script jira
  • Submitted by hugo697 Typescript (fetch-only)
    Created 565 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Get preference
    8
     * Returns the value of a preference of the current user.
    9
     */
    10
    export async function main(auth: Jira, key: string | undefined) {
    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: "GET",
    21
        headers: {
    22
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    23
        },
    24
        body: undefined,
    25
      });
    26
      if (!response.ok) {
    27
        const text = await response.text();
    28
        throw new Error(`${response.status} ${text}`);
    29
      }
    30
      return await response.json();
    31
    }
    32