-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubApiGet.ts
233 lines (224 loc) · 8.5 KB
/
githubApiGet.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// githubApi.ts
import fetch from "node-fetch";
import jp from "jsonpath";
import { GITHUB_API_BASE_URL, GITHUB_PROJECT_TOPICS } from "./consts.js";
// const isProd = false; // import.meta.env.PROD; // false;
/**
* Returns the github project info - reduce with pathExpression constants.
*
* @remarks
* This method is part of the {@link githubApiGet | github-api-get}.
*
* @param {string} loginName - The github login name
* @param {string} projectName - The github project name
* @param {string} pathExpression - The path expression to reduce the project info
* @param {boolean} infoLog - true Logs group and info into the console
* @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure
* @returns {Promise<unknown>} The github topics of a project
*/
export async function githubApiGetProject(
loginName: string,
projectName: string,
pathExpression: string,
infoLog: boolean,
isProd: boolean,
): Promise<unknown> {
if (infoLog) {
console.group("githubApiGetProject");
}
if (loginName && projectName) {
if (infoLog) {
console.info("githubApi.ts githubApiGetProject(): loginName: " + loginName);
console.info("githubApi.ts githubApiGetProject(): projectName: " + projectName);
}
if (infoLog) {
console.groupEnd();
}
// https://www.npmjs.com/package/jsonpath MIT License
return Promise.resolve(jp.query(await returnData(loginName, projectName, infoLog, isProd), pathExpression));
} else {
if (infoLog) {
console.groupEnd();
}
return Promise.resolve({} as Array<string>);
}
}
/**
* Returns the github login info - reduce with pathExpression constants.
*
* @remarks
* This method is part of the {@link githubApiGet | github-api-get}.
*
* @param {string} loginName - The github login name
* @param {string} pathExpression - The path expression to reduce the login info
* @param {boolean} infoLog - true Logs group and info into the console
* @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure
* @returns {Promise<unknown>} The github login info
*/
export async function githubApiGetLogin(
loginName: string,
pathExpression: string,
infoLog: boolean,
isProd: boolean,
): Promise<unknown> {
if (infoLog) {
console.group("githubApiGetLogin");
}
if (loginName) {
if (infoLog) {
console.info("githubApi.ts githubApiGetLogin(): loginName: " + loginName);
}
if (infoLog) {
console.groupEnd();
}
// https://www.npmjs.com/package/jsonpath MIT License
return Promise.resolve(jp.query(await returnData(loginName, "", infoLog, isProd), pathExpression));
} else {
if (infoLog) {
console.groupEnd();
}
return Promise.resolve({});
}
}
/**
* Returns the github topics of a project.
*
* @deprecated
* use githubApiGetProject() with pathExpression constant GITHUB_PROJECT_TOPICS
*
* @remarks
* This method is part of the {@link githubApiGet | github-api-get}.
*
* @param {string} loginName - The github login name
* @param {string} projectName - The github project name
* @param {boolean} infoLog - true Logs group and info into the console
* @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure
* @returns {Promise<string[]>} The github topics of a project
*/
export async function getGithubTopics(
loginName: string,
projectName: string,
infoLog: boolean,
isProd: boolean,
): Promise<string[]> {
if (infoLog) {
console.group("getGithubTopics");
}
if (loginName && projectName) {
if (infoLog) {
console.info("githubApi.ts: loginName: " + loginName);
console.info("githubApi.ts: projectName: " + projectName);
}
if (infoLog) {
console.groupEnd();
}
let githubProjectTopics = await githubApiGetProject(loginName, projectName, GITHUB_PROJECT_TOPICS, infoLog, isProd);
if (isStringArray(githubProjectTopics)) {
return Promise.resolve(githubProjectTopics as string[]);
} else {
return Promise.resolve(["getGithubTopics_Error_in_Implementation_of_GithubApiGetProject"] as Array<string>);
}
} else {
if (infoLog) {
console.groupEnd();
}
return Promise.resolve([] as Array<string>);
}
}
/**
* is String Array.
*
* @param {string} value - A value
* @returns {boolean} true if the value is a String Array, else false
*
*/
function isStringArray(value: unknown): value is string[] {
return Array.isArray(value) && value.every((element) => typeof element === "string");
}
/**
* Returns the github project object / json of a project if with projectName, return the github login of a user object / json of a login of a user.
*
* @param {string} loginName - The github login name
* @param {string} projectName - The github project name - not empty for project info, empty for login of a user info
* @param {boolean} infoLog - true Logs group and info into the console
* @param {boolean} isProd - true Calls fetch to github api, false returns mock data with same structure
* @returns {Promise<any>} The github the project object / json of a project
*/
async function returnData(loginName: string, projectName: string, infoLog: boolean, isProd: boolean) {
if (isProd) {
if (projectName !== "") {
// get from github project api
const GITHUB_API_PROJECT_URL = GITHUB_API_BASE_URL + "repos/" + loginName + "/";
const response = await fetch(GITHUB_API_PROJECT_URL + projectName);
if (!response.ok) {
if (infoLog) {
console.info(
"API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
);
console.info("https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting");
}
// Github has a Rate Limit
// in this case Status is 403 Forbidden
// and return this Message Object / Json:
// {
// message: "API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
// documentation_url: 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting'
// }
}
return await response.json();
} else {
// get from github login of a user api
const GITHUB_API_LOGIN_URL = GITHUB_API_BASE_URL + "users/" + loginName;
const response = await fetch(GITHUB_API_LOGIN_URL);
if (!response.ok) {
if (infoLog) {
console.info(
"API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
);
console.info("https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting");
}
// Github has a Rate Limit
// in this case Status is 403 Forbidden
// and return this Message Object / Json:
// {
// message: "API rate limit exceeded for nnn.nnn.nnn.nnn. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
// documentation_url: 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting'
// }
}
return await response.json();
}
} else {
// return mock data
if (projectName !== "") {
// return mockdata of a project
// based on
// 01-01-vanilla-HTML5-starter-page.json
return Promise.resolve({
name: "01-01-vanilla-HTML5-starter-page",
owner: {
login: "roebi",
},
html_url: "https://github.com/roebi/01-01-vanilla-HTML5-starter-page",
description: "vanilla HTML 5 starter page - Have you ever heard of this HTML 5 tags ?",
url: "https://api.github.com/repos/roebi/01-01-vanilla-HTML5-starter-page",
homepage: "https://roebi.github.io/01-01-vanilla-HTML5-starter-page/",
license: {
key: "unlicense",
},
topics: [" mockdata!", "html5", "html5-template", "roebi", "starter"],
});
} else {
// return mockdata of a login of a user
// based on
// roebi.json
return Promise.resolve({
login: "roebi",
avatar_url: "https://avatars.githubusercontent.com/u/3611826?v=4",
url: "https://api.github.com/users/roebi",
html_url: "https://github.com/roebi",
location: "Switzerland",
bio: "...",
});
}
}
}