Get a previous revision of a snippet

Identical to `GET /snippets/encoded_id`, except that this endpoint can be used to retrieve the contents of the snippet as it was at an older revision, while `/snippets/encoded_id` always returns the snippet's current revision. Note that only the snippet's file contents are versioned, not its meta data properties like the title. Other than that, the two endpoints are identical in behavior.

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
 * Get a previous revision of a snippet
7
 * Identical to `GET /snippets/encoded_id`, except that this endpoint
8
can be used to retrieve the contents of the snippet as it was at an
9
older revision, while `/snippets/encoded_id` always returns the
10
snippet's current revision.
11

12
Note that only the snippet's file contents are versioned, not its
13
meta data properties like the title.
14

15
Other than that, the two endpoints are identical in behavior.
16
 */
17
export async function main(
18
  auth: Bitbucket,
19
  encoded_id: string,
20
  node_id: string,
21
  workspace: string
22
) {
23
  const url = new URL(
24
    `https://api.bitbucket.org/2.0/snippets/${workspace}/${encoded_id}/${node_id}`
25
  );
26

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