Check if the current user is watching a snippet

Used to check if the current user is watching a specific snippet. Returns 204 (No Content) if the user is watching the snippet and 404 if not. Hitting this endpoint anonymously always returns a 404.

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Check if the current user is watching a snippet
7
 * Used to check if the current user is watching a specific snippet.
8

9
Returns 204 (No Content) if the user is watching the snippet and 404 if
10
not.
11

12
Hitting this endpoint anonymously always returns a 404.
13
 */
14
export async function main(
15
  auth: Bitbucket,
16
  encoded_id: string,
17
  workspace: string
18
) {
19
  const url = new URL(
20
    `https://api.bitbucket.org/2.0/snippets/${workspace}/${encoded_id}/watch`
21
  );
22

23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.text();
35
}
36