-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathindex.ts
More file actions
66 lines (60 loc) · 1.75 KB
/
index.ts
File metadata and controls
66 lines (60 loc) · 1.75 KB
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
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { memCacheProvider } from '../../../util/http/cache/memory-http-cache-provider';
import { GithubHttp } from '../../../util/http/github';
import { fromBase64 } from '../../../util/string';
import type { Preset, PresetConfig } from '../types';
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util';
export const Endpoint = 'https://api.github.com/';
const http = new GithubHttp();
export async function fetchJSONFile(
repo: string,
fileName: string,
endpoint: string,
tag?: string,
): Promise<Preset> {
let ref = '';
if (is.nonEmptyString(tag)) {
ref = `?ref=${tag}`;
}
const url = `${endpoint}repos/${repo}/contents/${fileName}${ref}`;
logger.trace({ url }, `Preset URL`);
let res: { body: { content: string } };
try {
res = await http.getJsonUnchecked(url, {
cacheProvider: memCacheProvider,
});
} catch (err) {
if (err instanceof ExternalHostError) {
throw err;
}
logger.debug(`Preset file ${fileName} not found in ${repo}`);
throw new Error(PRESET_DEP_NOT_FOUND);
}
return parsePreset(fromBase64(res.body.content), fileName);
}
export function getPresetFromEndpoint(
repo: string,
filePreset: string,
presetPath?: string,
endpoint = Endpoint,
tag?: string,
): Promise<Preset | undefined> {
return fetchPreset({
repo,
filePreset,
presetPath,
endpoint,
tag,
fetch: fetchJSONFile,
});
}
export function getPreset({
repo,
presetName = 'default',
presetPath,
tag,
}: PresetConfig): Promise<Preset | undefined> {
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
}