0
Get a snippet's raw file
One script reply has been approved by the moderators Verified

Retrieves the raw contents of a specific file in the snippet. The Content-Disposition header will be "attachment" to avoid issues with malevolent executable files.

The file's mime type is derived from its filename and returned in the Content-Type header.

Note that for text files, no character encoding is included as part of the content type.

Created by hugo697 394 days ago Viewed 9002 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 394 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Get a snippet's raw file
7
 * Retrieves the raw contents of a specific file in the snippet. The
8
`Content-Disposition` header will be "attachment" to avoid issues with
9
malevolent executable files.
10

11
The file's mime type is derived from its filename and returned in the
12
`Content-Type` header.
13

14
Note that for text files, no character encoding is included as part of
15
the content type.
16
 */
17
export async function main(
18
  auth: Bitbucket,
19
  encoded_id: string,
20
  node_id: string,
21
  path: string,
22
  workspace: string
23
) {
24
  const url = new URL(
25
    `https://api.bitbucket.org/2.0/snippets/${workspace}/${encoded_id}/${node_id}/files/${path}`
26
  );
27

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