This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbranch.ts
78 lines (67 loc) · 1.89 KB
/
branch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import camelcaseKeys from 'camelcase-keys';
import { StatusCodes } from 'http-status-codes';
import { instance, headers } from './setting';
import { _fetch } from './_base';
import { Branch, HttpNotFoundError } from '../models';
const mapDataToBranch = (data: any): Branch => {
const branch = camelcaseKeys(data, { deep: true });
return branch;
};
export const listBranches = async (
namespace: string,
name: string,
page = 1,
perPage = 30
): Promise<Branch[]> => {
const branches: Branch[] = await _fetch(
`${instance}/api/v1/repos/${namespace}/${name}/branches?page=${page}&per_page=${perPage}`,
{
headers,
credentials: 'same-origin',
}
)
.then((response) => response.json())
.then((branches) => branches.map((b: any) => mapDataToBranch(b)));
return branches;
};
export const getBranch = async (
namespace: string,
name: string,
branch: string
): Promise<Branch> => {
const response = await _fetch(
`${instance}/api/v1/repos/${namespace}/${name}/branches/${branch}`,
{
headers,
credentials: 'same-origin',
}
);
if (response.status === StatusCodes.NOT_FOUND) {
const message = await response.json().then((data) => data.message);
throw new HttpNotFoundError(message);
}
const ret: Branch = await response
.json()
.then((b: any) => mapDataToBranch(b));
return ret;
};
export const getDefaultBranch = async (
namespace: string,
name: string
): Promise<Branch> => {
const response = await _fetch(
`${instance}/api/v1/repos/${namespace}/${name}/default-branch`,
{
headers,
credentials: 'same-origin',
}
);
if (response.status === StatusCodes.NOT_FOUND) {
const message = await response.json().then((data) => data.message);
throw new HttpNotFoundError(message);
}
const ret: Branch = await response
.json()
.then((b: any) => mapDataToBranch(b));
return ret;
};