-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.ts
81 lines (73 loc) · 1.98 KB
/
Api.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
79
80
81
import base64 from 'react-native-base64';
import {showToast} from './Utils';
const createAuthHeader = (registryUser: string, registryPass: string) => {
const auth = 'Basic ' + base64.encode(`${registryUser}:${registryPass}`);
return {
Authorization: auth,
};
};
const baseUrl = (hostname: string, endpoint: string) =>
`${hostname}/v2/${endpoint}`;
export const fetchRepositories = async (
hostname: string,
registryUser: string,
registryPass: string,
) => {
try {
const response = await fetch(baseUrl(hostname, '_catalog'), {
method: 'GET',
headers: createAuthHeader(registryUser, registryPass),
});
if (!response.ok) {
throw new Error('Error fetching repositories');
}
const responseData = await response.json();
return responseData.repositories;
} catch (error) {
if (error instanceof Error) {
showToast('Error: ' + error.message);
throw error;
} else {
showToast('An unexpected error occurred.');
throw new Error('An unexpected error occurred.');
}
}
};
export const fetchTags = async (
hostname: string,
registryUser: string,
registryPass: string,
tag: string,
) => {
return await fetch(baseUrl(hostname, `${tag}/tags/list`), {
method: 'GET',
headers: createAuthHeader(registryUser, registryPass),
});
};
export const fetchBlob = async (
hostname: string,
registryUser: string,
registryPass: string,
repo: string,
digest: string,
) => {
return await fetch(baseUrl(hostname, `${repo}/blobs/${digest}`), {
method: 'GET',
headers: createAuthHeader(registryUser, registryPass),
});
};
export const fetchManifest = async (
hostname: string,
registryUser: string,
registryPass: string,
repo: string,
tag: string,
) => {
return await fetch(baseUrl(hostname, `${repo}/manifests/${tag}`), {
method: 'GET',
headers: {
...createAuthHeader(registryUser, registryPass),
Accept: 'application/vnd.docker.distribution.manifest.v2+json',
},
});
};