0
List snippets
One script reply has been approved by the moderators Verified

Returns all snippets.

Created by hugo697 277 days ago Viewed 8921 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 277 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List snippets
7
 * Returns all snippets.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  role: "owner" | "contributor" | "member" | undefined
12
) {
13
  const url = new URL(`https://api.bitbucket.org/2.0/snippets`);
14
  for (const [k, v] of [["role", role]]) {
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