0
Create a snippet for a workspace
One script reply has been approved by the moderators Verified

Identical to /snippets, except that the new snippet will be created under the workspace specified in the path parameter {workspace}.

Created by hugo697 198 days ago Viewed 5909 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 198 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a snippet for a workspace
7
 * Identical to `/snippets`, except that the new snippet will be
8
created under the workspace specified in the path parameter
9
`{workspace}`.
10
 */
11
export async function main(
12
  auth: Bitbucket,
13
  workspace: string,
14
  body: { type: string; [k: string]: unknown } & {
15
    id?: number;
16
    title?: string;
17
    scm?: "git";
18
    created_on?: string;
19
    updated_on?: string;
20
    owner?: { type: string; [k: string]: unknown } & {
21
      links?: {
22
        avatar?: { href?: string; name?: string };
23
        [k: string]: unknown;
24
      };
25
      created_on?: string;
26
      display_name?: string;
27
      username?: string;
28
      uuid?: string;
29
      [k: string]: unknown;
30
    };
31
    creator?: { type: string; [k: string]: unknown } & {
32
      links?: {
33
        avatar?: { href?: string; name?: string };
34
        [k: string]: unknown;
35
      };
36
      created_on?: string;
37
      display_name?: string;
38
      username?: string;
39
      uuid?: string;
40
      [k: string]: unknown;
41
    };
42
    is_private?: boolean;
43
    [k: string]: unknown;
44
  }
45
) {
46
  const url = new URL(`https://api.bitbucket.org/2.0/snippets/${workspace}`);
47

48
  const response = await fetch(url, {
49
    method: "POST",
50
    headers: {
51
      "Content-Type": "application/json",
52
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
53
    },
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62