-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathgenericRegistry.ts
81 lines (69 loc) · 1.75 KB
/
genericRegistry.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 {makeFetchCache} from './fetchCache';
import {BASE_REGISTRY_URL} from './shared';
type FileData = {
checksums: {
[name: string]: string;
};
};
type VersionData = {
canonical: string;
main_docs_url: string;
name: string;
repo_url: string;
version: string;
api_docs_url?: string;
files?: {
[name: string]: FileData;
};
package_url?: string;
};
interface Options {
/**
* The name of the registry
*/
name: string;
/**
* The URL path of the registry
*/
path: string;
}
// XXX(epurkhiser): Right now the SDK and Apps registry have the same
// structure, so we can reuse the same helpers
/**
* Constructs helper functions for a "generic" registry.
*/
export function makeRegistry({name, path}: Options) {
const ensureData = makeFetchCache<Record<string, VersionData>>({
name,
dataUrl: `${BASE_REGISTRY_URL}/${path}`,
});
async function resolveRegistry() {
const data = await ensureData();
/**
* Note that this will return `null` unless `ensureData` has been called.
*/
function getItem(key: string) {
return data?.[key] ?? null;
}
function version(key: string, defaultValue: string = '') {
const item = getItem(key);
return item?.version || defaultValue;
}
function checksum(key: string, fileName: string, checksumKey: string) {
const item = getItem(key);
return item?.files?.[fileName]?.checksums?.[checksumKey] || '';
}
/**
* Provides a synchronous interface for accessing data from the registry.
* `ensureData` must be called first otherwise these will return empty values
*/
const accessors = {
data,
getItem,
version,
checksum,
};
return accessors;
}
return resolveRegistry;
}