@stats-organization/github-readme-stats-core 2.1.4 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/build/api/gist.d.ts +2 -7
  2. package/build/api/gist.d.ts.map +1 -1
  3. package/build/api/gist.js +28 -26
  4. package/build/api/index.d.ts +3 -8
  5. package/build/api/index.d.ts.map +1 -1
  6. package/build/api/index.js +32 -36
  7. package/build/api/pin.d.ts +2 -7
  8. package/build/api/pin.d.ts.map +1 -1
  9. package/build/api/pin.js +18 -33
  10. package/build/api/top-langs.d.ts +2 -7
  11. package/build/api/top-langs.d.ts.map +1 -1
  12. package/build/api/top-langs.js +32 -44
  13. package/build/api/wakatime.d.ts +2 -7
  14. package/build/api/wakatime.d.ts.map +1 -1
  15. package/build/api/wakatime.js +28 -26
  16. package/build/calculateRank.d.ts +12 -11
  17. package/build/calculateRank.d.ts.map +1 -1
  18. package/build/calculateRank.js +18 -18
  19. package/build/cards/common-options.d.ts +13 -0
  20. package/build/cards/common-options.d.ts.map +1 -0
  21. package/build/cards/common-options.js +1 -0
  22. package/build/cards/gist.d.ts +11 -16
  23. package/build/cards/gist.d.ts.map +1 -1
  24. package/build/cards/gist.js +26 -31
  25. package/build/cards/repo.d.ts +19 -16
  26. package/build/cards/repo.d.ts.map +1 -1
  27. package/build/cards/repo.js +79 -72
  28. package/build/cards/stats.d.ts +29 -44
  29. package/build/cards/stats.d.ts.map +1 -1
  30. package/build/cards/stats.js +118 -173
  31. package/build/cards/top-languages.d.ts +81 -77
  32. package/build/cards/top-languages.d.ts.map +1 -1
  33. package/build/cards/top-languages.js +186 -184
  34. package/build/cards/wakatime.d.ts +21 -11
  35. package/build/cards/wakatime.d.ts.map +1 -1
  36. package/build/cards/wakatime.js +92 -109
  37. package/build/common/Card.d.ts +41 -14
  38. package/build/common/Card.d.ts.map +1 -1
  39. package/build/common/Card.js +92 -31
  40. package/build/common/I18n.d.ts +4 -2
  41. package/build/common/I18n.d.ts.map +1 -1
  42. package/build/common/color.d.ts +91 -15
  43. package/build/common/color.d.ts.map +1 -1
  44. package/build/common/color.js +163 -17
  45. package/build/common/config.d.ts +2 -1
  46. package/build/common/config.d.ts.map +1 -1
  47. package/build/common/config.js +16 -26
  48. package/build/common/constants.d.ts +2 -1
  49. package/build/common/constants.d.ts.map +1 -1
  50. package/build/common/date.d.ts +37 -0
  51. package/build/common/date.d.ts.map +1 -0
  52. package/build/common/date.js +31 -0
  53. package/build/common/html.js +1 -1
  54. package/build/common/http.d.ts +37 -5
  55. package/build/common/http.d.ts.map +1 -1
  56. package/build/common/http.js +29 -6
  57. package/build/common/icons.d.ts +1 -0
  58. package/build/common/icons.d.ts.map +1 -1
  59. package/build/common/icons.js +1 -0
  60. package/build/common/languageColors.d.ts +11 -0
  61. package/build/common/languageColors.d.ts.map +1 -0
  62. package/build/common/languageColors.js +15 -0
  63. package/build/common/languageColors.json +10 -1
  64. package/build/common/ops.d.ts +27 -33
  65. package/build/common/ops.d.ts.map +1 -1
  66. package/build/common/ops.js +31 -48
  67. package/build/common/render.d.ts +154 -113
  68. package/build/common/render.d.ts.map +1 -1
  69. package/build/common/render.js +209 -92
  70. package/build/common/retryer.d.ts +15 -6
  71. package/build/common/retryer.d.ts.map +1 -1
  72. package/build/common/retryer.js +2 -0
  73. package/build/fetchers/gist.d.ts +6 -21
  74. package/build/fetchers/gist.d.ts.map +1 -1
  75. package/build/fetchers/gist.js +28 -64
  76. package/build/fetchers/repo.d.ts +12 -11
  77. package/build/fetchers/repo.d.ts.map +1 -1
  78. package/build/fetchers/repo.js +27 -68
  79. package/build/fetchers/stats.d.ts +24 -17
  80. package/build/fetchers/stats.d.ts.map +1 -1
  81. package/build/fetchers/stats.js +302 -184
  82. package/build/fetchers/top-languages.d.ts +10 -15
  83. package/build/fetchers/top-languages.d.ts.map +1 -1
  84. package/build/fetchers/top-languages.js +52 -103
  85. package/build/fetchers/types.d.ts +130 -0
  86. package/build/fetchers/types.d.ts.map +1 -0
  87. package/build/fetchers/types.js +1 -0
  88. package/build/fetchers/wakatime.d.ts +9 -5
  89. package/build/fetchers/wakatime.d.ts.map +1 -1
  90. package/build/fetchers/wakatime.js +8 -4
  91. package/build/graphql/contributionsDocument.d.ts +18 -0
  92. package/build/graphql/contributionsDocument.d.ts.map +1 -0
  93. package/build/graphql/contributionsDocument.js +30 -0
  94. package/build/graphql/generated/common.d.ts +42 -0
  95. package/build/graphql/generated/common.d.ts.map +1 -0
  96. package/build/graphql/generated/common.js +2 -0
  97. package/build/graphql/generated/gist.d.ts +44 -0
  98. package/build/graphql/generated/gist.d.ts.map +1 -0
  99. package/build/graphql/generated/gist.js +27 -0
  100. package/build/graphql/generated/repo.d.ts +67 -0
  101. package/build/graphql/generated/repo.d.ts.map +1 -0
  102. package/build/graphql/generated/repo.js +30 -0
  103. package/build/graphql/generated/stats.d.ts +150 -0
  104. package/build/graphql/generated/stats.d.ts.map +1 -0
  105. package/build/graphql/generated/stats.js +95 -0
  106. package/build/graphql/generated/top-languages.d.ts +54 -0
  107. package/build/graphql/generated/top-languages.d.ts.map +1 -0
  108. package/build/graphql/generated/top-languages.js +27 -0
  109. package/build/graphql/graphqlDocument.d.ts +22 -0
  110. package/build/graphql/graphqlDocument.d.ts.map +1 -0
  111. package/build/graphql/graphqlDocument.js +8 -0
  112. package/build/graphql/reposContributedToDocument.d.ts +27 -0
  113. package/build/graphql/reposContributedToDocument.d.ts.map +1 -0
  114. package/build/graphql/reposContributedToDocument.js +59 -0
  115. package/build/index.d.ts +3 -1
  116. package/build/index.d.ts.map +1 -1
  117. package/build/index.js +2 -1
  118. package/build/themes/index.d.ts +32 -0
  119. package/build/themes/index.d.ts.map +1 -1
  120. package/build/themes/index.js +28 -0
  121. package/build/translations.d.ts +17 -0
  122. package/build/translations.d.ts.map +1 -1
  123. package/build/translations.js +17 -0
  124. package/package.json +14 -8
  125. package/src/_emoji-name-map.d.ts +10 -0
  126. package/src/_github-username-regex.d.ts +4 -0
  127. package/src/api/gist.js +31 -31
  128. package/src/api/index.js +40 -42
  129. package/src/api/pin.js +20 -38
  130. package/src/api/top-langs.js +36 -49
  131. package/src/api/wakatime.js +31 -31
  132. package/src/{calculateRank.js → calculateRank.ts} +29 -19
  133. package/src/cards/common-options.ts +13 -0
  134. package/src/cards/{gist.js → gist.ts} +37 -37
  135. package/src/cards/{repo.js → repo.ts} +107 -79
  136. package/src/cards/{stats.js → stats.ts} +173 -210
  137. package/src/cards/{top-languages.js → top-languages.ts} +325 -243
  138. package/src/cards/{wakatime.js → wakatime.ts} +158 -127
  139. package/src/common/Card.ts +123 -43
  140. package/src/common/I18n.ts +6 -2
  141. package/src/common/color.ts +240 -28
  142. package/src/common/config.ts +22 -29
  143. package/src/common/constants.ts +3 -1
  144. package/src/common/date.ts +45 -0
  145. package/src/common/html.ts +1 -1
  146. package/src/common/http.ts +71 -0
  147. package/src/common/icons.ts +1 -0
  148. package/src/common/languageColors.json +10 -1
  149. package/src/common/languageColors.ts +19 -0
  150. package/src/common/ops.ts +158 -0
  151. package/src/common/render.ts +652 -0
  152. package/src/common/retryer.ts +21 -12
  153. package/src/fetchers/gist.ts +79 -0
  154. package/src/fetchers/repo.ts +114 -0
  155. package/src/fetchers/stats.ts +743 -0
  156. package/src/fetchers/top-languages.ts +135 -0
  157. package/src/fetchers/{types.d.ts → types.ts} +28 -20
  158. package/src/fetchers/{wakatime.js → wakatime.ts} +19 -5
  159. package/src/graphql/contributionsDocument.ts +44 -0
  160. package/src/graphql/generated/common.ts +26 -0
  161. package/src/graphql/generated/gist.ts +59 -0
  162. package/src/graphql/generated/repo.ts +90 -0
  163. package/src/graphql/generated/stats.ts +194 -0
  164. package/src/graphql/generated/top-languages.ts +75 -0
  165. package/src/graphql/graphqlDocument.ts +25 -0
  166. package/src/graphql/queries/gist.graphql +25 -0
  167. package/src/graphql/queries/repo.graphql +28 -0
  168. package/src/graphql/queries/stats.graphql +120 -0
  169. package/src/graphql/queries/top-languages.graphql +34 -0
  170. package/src/graphql/reposContributedToDocument.ts +86 -0
  171. package/src/index.ts +3 -1
  172. package/src/themes/index.ts +33 -0
  173. package/src/translations.ts +17 -0
  174. package/src/cards/types.d.ts +0 -78
  175. package/src/common/http.js +0 -19
  176. package/src/common/ops.js +0 -170
  177. package/src/common/render.js +0 -440
  178. package/src/fetchers/gist.js +0 -112
  179. package/src/fetchers/repo.js +0 -161
  180. package/src/fetchers/stats.js +0 -474
  181. package/src/fetchers/top-languages.js +0 -175
  182. package/src/themes/README.md +0 -229
@@ -5,10 +5,11 @@ import { CustomError } from "./error.js";
5
5
  import { logger } from "./log.js";
6
6
 
7
7
  /**
8
- * Subset of the response payload the retryer inspects to detect
9
- * rate-limiting and credential failures.
8
+ * Error-detection fields the retryer inspects to detect rate-limiting and credential failures.
9
+ * Every fetcher's payload is intersected with
10
+ * this, so the retryer can read `errors`/`message` regardless of the payload's own shape.
10
11
  */
11
- interface ResponseData {
12
+ interface ResponseErrors {
12
13
  errors?: Array<{ type?: string; message?: string }>;
13
14
  message?: string;
14
15
  }
@@ -27,27 +28,35 @@ function getRandomInt(max: number): number {
27
28
  return Math.floor(Math.random() * max);
28
29
  }
29
30
 
30
- type FetcherResponse = AxiosResponse<ResponseData>;
31
+ /**
32
+ * A fetcher's Axios response. `TData` is the shape of `response.data`,
33
+ * which is intersected with {@link ResponseErrors} so the retryer can inspect
34
+ * `errors`/`message`.
35
+ * Defaults to `unknown` (error fields only) for callers that don't care about the payload.
36
+ */
37
+ type FetcherResponse<TData = unknown> = AxiosResponse<TData & ResponseErrors>;
31
38
 
32
- type FetcherFunction = (
33
- variables: Record<string, unknown>,
39
+ type FetcherFunction<TData = unknown, TVariables = Record<string, unknown>> = (
40
+ variables: TVariables,
34
41
  token: string,
35
42
  retriesForTests?: number,
36
- ) => Promise<FetcherResponse>;
43
+ ) => Promise<FetcherResponse<TData>>;
37
44
 
38
45
  /**
39
46
  * Try to execute the fetcher function until it succeeds or the max number of retries is reached.
40
47
  *
48
+ * @template TData Shape of `response.data` returned by the fetcher.
49
+ * @template TVariables Variables the fetcher accepts.
41
50
  * @param fetcher The fetcher function.
42
51
  * @param variables Object with arguments to pass to the fetcher function.
43
52
  * @param pat Optional PAT override.
44
53
  * @returns The response from the fetcher function.
45
54
  */
46
- const retryer = async (
47
- fetcher: FetcherFunction,
48
- variables: Record<string, unknown>,
55
+ const retryer = async <TData = unknown, TVariables = Record<string, unknown>>(
56
+ fetcher: FetcherFunction<TData, TVariables>,
57
+ variables: TVariables,
49
58
  pat: string | null = null,
50
- ): Promise<FetcherResponse> => {
59
+ ): Promise<FetcherResponse<TData>> => {
51
60
  const PATs = pat
52
61
  ? [{ name: "user PAT from database", value: pat }]
53
62
  : getConfig().pats;
@@ -86,7 +95,7 @@ const retryer = async (
86
95
  return response;
87
96
  }
88
97
  } catch (err) {
89
- const e = err as { response?: FetcherResponse };
98
+ const e = err as { response?: FetcherResponse<TData> };
90
99
 
91
100
  // network/unexpected error → let caller treat as failure
92
101
  if (!e.response) {
@@ -0,0 +1,79 @@
1
+ import { MissingParamError } from "../common/error.js";
2
+ import { createGraphQLFetcher } from "../common/http.js";
3
+ import { retryer } from "../common/retryer.js";
4
+ import { GistInfoDocument } from "../graphql/generated/gist.js";
5
+ import type { GistFileInfoFragment } from "../graphql/generated/gist.js";
6
+
7
+ import type { GistData } from "./types.js";
8
+
9
+ const fetcher = createGraphQLFetcher(GistInfoDocument, "token");
10
+
11
+ /**
12
+ * This function calculates the primary language of a gist by files size.
13
+ *
14
+ * @param files Files.
15
+ * @returns Primary language, or `null` when no file has a language.
16
+ */
17
+ const calculatePrimaryLanguage = (
18
+ files: Array<GistFileInfoFragment>,
19
+ ): string | null => {
20
+ const languages: Record<string, number> = {};
21
+
22
+ for (const file of files) {
23
+ if (file.language) {
24
+ languages[file.language.name] =
25
+ (languages[file.language.name] ?? 0) + (file.size ?? 0);
26
+ }
27
+ }
28
+
29
+ let primaryLanguage: string | null = null;
30
+ let maxSize = -1;
31
+ for (const [language, size] of Object.entries(languages)) {
32
+ if (size > maxSize) {
33
+ maxSize = size;
34
+ primaryLanguage = language;
35
+ }
36
+ }
37
+
38
+ return primaryLanguage;
39
+ };
40
+
41
+ /**
42
+ * Fetch GitHub gist information by given username and ID.
43
+ *
44
+ * @param id GitHub gist ID.
45
+ * @param pat Optional PAT override.
46
+ * @returns Gist data.
47
+ */
48
+ const fetchGist = async (
49
+ id: string,
50
+ pat: string | null = null,
51
+ ): Promise<GistData> => {
52
+ if (!id) {
53
+ throw new MissingParamError(["id"], "/api/gist?id=GIST_ID");
54
+ }
55
+ const res = await retryer(fetcher, { gistName: id }, pat);
56
+ if (res.data.errors) {
57
+ throw new Error(res.data.errors[0]?.message);
58
+ }
59
+ const gist = res.data.data.viewer.gist;
60
+ if (!gist) {
61
+ throw new Error("Gist not found");
62
+ }
63
+ const firstFile = gist.files?.[0];
64
+ if (!firstFile?.name) {
65
+ throw new Error("Gist has no files");
66
+ }
67
+ return {
68
+ name: firstFile.name,
69
+ nameWithOwner: `${gist.owner?.login ?? ""}/${firstFile.name}`,
70
+ description: gist.description,
71
+ language: calculatePrimaryLanguage(
72
+ gist.files?.filter((file) => !!file) ?? [],
73
+ ),
74
+ starsCount: gist.stargazerCount,
75
+ forksCount: gist.forks.totalCount,
76
+ };
77
+ };
78
+
79
+ export { fetchGist };
@@ -0,0 +1,114 @@
1
+ import { MissingParamError } from "../common/error.js";
2
+ import { createGraphQLFetcher } from "../common/http.js";
3
+ import { retryer } from "../common/retryer.js";
4
+ import { GetRepoDocument } from "../graphql/generated/repo.js";
5
+
6
+ import { fetchRepoUserStats } from "./stats.js";
7
+ import type { RepositoryData } from "./types.js";
8
+
9
+ const fetcher = createGraphQLFetcher(GetRepoDocument, "token");
10
+
11
+ const urlExample = "/api/pin?username=USERNAME&repo=REPO_NAME";
12
+
13
+ /**
14
+ * Fetch repository data.
15
+ *
16
+ * @param username GitHub username.
17
+ * @param reponame GitHub repository name.
18
+ * @param include_prs_authored Include count of PRs authored.
19
+ * @param include_prs_commented Include count of PRs commented.
20
+ * @param include_prs_reviewed Include count of PRs reviewed.
21
+ * @param include_issues_authored Include count of issues authored.
22
+ * @param include_issues_commented Include count of issues commented.
23
+ * @param pat Optional PAT override.
24
+ * @returns Repository data.
25
+ */
26
+ const fetchRepo = async (
27
+ username: string,
28
+ reponame: string,
29
+ include_prs_authored = false,
30
+ include_prs_commented = false,
31
+ include_prs_reviewed = false,
32
+ include_issues_authored = false,
33
+ include_issues_commented = false,
34
+ pat: string | null = null,
35
+ ): Promise<RepositoryData> => {
36
+ let owner = username;
37
+ if (reponame && reponame.includes("/")) {
38
+ const [parsedOwner, parsedRepo] = reponame.split("/");
39
+ owner = parsedOwner ?? "";
40
+ reponame = parsedRepo ?? "";
41
+ }
42
+
43
+ if (owner && !username) {
44
+ username = owner;
45
+ }
46
+ if (username && !owner) {
47
+ owner = username;
48
+ }
49
+ if (!username && !reponame) {
50
+ throw new MissingParamError(["username", "repo"], urlExample);
51
+ }
52
+ if (!username) {
53
+ throw new MissingParamError(["username"], urlExample);
54
+ }
55
+ if (!reponame) {
56
+ throw new MissingParamError(["repo"], urlExample);
57
+ }
58
+
59
+ const res = await retryer(fetcher, { login: owner, repo: reponame }, pat);
60
+
61
+ const data = res.data.data;
62
+
63
+ if (!data.user && !data.organization) {
64
+ throw new Error("Not found");
65
+ }
66
+
67
+ if (data.organization === null && data.user) {
68
+ const repository = data.user.repository;
69
+ if (!repository || repository.isPrivate) {
70
+ throw new Error("User Repository Not found");
71
+ }
72
+ const repoUserStats = await fetchRepoUserStats(
73
+ username,
74
+ [owner + "/" + reponame],
75
+ [],
76
+ include_prs_authored,
77
+ include_prs_commented,
78
+ include_prs_reviewed,
79
+ include_issues_authored,
80
+ include_issues_commented,
81
+ pat,
82
+ );
83
+ return {
84
+ ...repoUserStats,
85
+ ...repository,
86
+ };
87
+ }
88
+
89
+ if (data.user === null && data.organization) {
90
+ const repository = data.organization.repository;
91
+ if (!repository || repository.isPrivate) {
92
+ throw new Error("Organization Repository Not found");
93
+ }
94
+ const repoUserStats = await fetchRepoUserStats(
95
+ username,
96
+ [owner + "/" + reponame],
97
+ [],
98
+ include_prs_authored,
99
+ include_prs_commented,
100
+ include_prs_reviewed,
101
+ include_issues_authored,
102
+ include_issues_commented,
103
+ pat,
104
+ );
105
+ return {
106
+ ...repoUserStats,
107
+ ...repository,
108
+ };
109
+ }
110
+
111
+ throw new Error("Unexpected behavior");
112
+ };
113
+
114
+ export { fetchRepo };