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

Identical to /snippets, except that the result is further filtered by the snippet owner and only those that are owned by {workspace} are returned.

Created by hugo697 425 days ago Viewed 9156 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 425 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List snippets in a workspace
7
 * Identical to `/snippets`, except that the result is further filtered
8
by the snippet owner and only those that are owned by `{workspace}` are
9
returned.
10
 */
11
export async function main(
12
  auth: Bitbucket,
13
  workspace: string,
14
  role: "owner" | "contributor" | "member" | undefined
15
) {
16
  const url = new URL(`https://api.bitbucket.org/2.0/snippets/${workspace}`);
17
  for (const [k, v] of [["role", role]]) {
18
    if (v !== undefined && v !== "") {
19
      url.searchParams.append(k, v);
20
    }
21
  }
22
  const response = await fetch(url, {
23
    method: "GET",
24
    headers: {
25
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
26
    },
27
    body: undefined,
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35