generated from 4Source/obsidian-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitHub.ts
120 lines (109 loc) · 3.11 KB
/
GitHub.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Credits: https://github.com/TfTHacker/obsidian42-brat
*/
import { PluginManifest, request } from 'obsidian';
export const repositoryRegEx = /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}\/[A-Za-z0-9_.-]+$/i;
export interface CommunityPlugin {
id: string;
name: string;
author: string;
description: string;
repo: string;
}
export type Release = {
url: string;
html_url: string;
assets_url: string;
upload_url: string;
tarball_url: string;
zipball_url: string;
id: number;
node_id: string,
tag_name: string,
target_commitish: string,
name: string,
body: string,
draft: boolean,
prerelease: boolean,
created_at: string,
published_at: string,
author: unknown,
assets: Asset[];
};
export type Asset = {
id: number;
name: string;
url: string;
browser_download_url: string;
};
export type File = {
name: string;
data: string;
};
/**
* Fetch all community plugin entrys.
* @returns A list of community plugins
*/
export async function fetchCommmunityPluginList(): Promise<CommunityPlugin[] | undefined> {
const URL = 'https://raw.githubusercontent.com/obsidianmd/obsidian-releases/HEAD/community-plugins.json';
try {
// Do a request to the url
const response = await request({ url: URL });
// Process the response
return (await JSON.parse(response)) as CommunityPlugin[];
}
catch (e) {
(e as Error).message = 'Failed to fetch community plugin list! ' + (e as Error).message;
console.error(e);
}
}
/**
* Fetch all releases for a plugin
* @param repository The <user>/<repo> of the plugin
* @returns A list of all releases
*/
export async function fetchReleases(repository: string): Promise<Partial<Release>[] | undefined> {
const URL = `https://api.github.com/repos/${repository}/releases`;
try {
if (!repositoryRegEx.test(repository)) {
throw Error('Repository string do not match the pattern!');
}
// Do a request to the url
const response = await request({ url: URL });
// Process the response
const data = await JSON.parse(response);
const releases = data.map((value: Release) => {
return {
tag_name: value.tag_name,
prerelease: value.prerelease,
};
});
return releases;
}
catch (e) {
(e as Error).message = 'Failed to fetch releases for plugin! ' + (e as Error).message;
console.error(e);
}
}
/**
* Fetch the manifest for a plugin
* @param repository The <user>/<repo> of the plugin
* @param tag_name The name of the tag associated with a release. Required if a specific manifest version is needed.
* @returns The plugin manifest object
*/
export async function fetchManifest(repository: string, tag_name?: string): Promise<PluginManifest | undefined> {
const URL = `https://raw.githubusercontent.com/${repository}/${tag_name ? tag_name : 'HEAD'}/manifest.json`;
try {
if (!repositoryRegEx.test(repository)) {
throw Error('Repository string do not match the pattern!');
}
// Do a request to the url
const response = await request({ url: URL });
// Process the response
return (await JSON.parse(response)) as PluginManifest;
}
catch (e) {
(e as Error).message = 'Failed to fetch the manifest for plugin! ' + (e as Error).message;
console.error(e);
}
}