chore: refactor any with unknown and add jsdocs
This commit is contained in:
@@ -1,18 +1,15 @@
|
|||||||
import type { CachedData } from '@shared/types/CachedData';
|
import type { CachedData } from '@shared/types/CachedData';
|
||||||
import { createLocalStore, debugStore } from 'chrome-extension-toolkit';
|
import { createLocalStore, debugStore } from 'chrome-extension-toolkit';
|
||||||
|
|
||||||
import { generateRandomId } from '../util/random';
|
|
||||||
|
|
||||||
interface ICacheStore {
|
interface ICacheStore {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
github: Record<string, CachedData<unknown>>;
|
||||||
github: Record<string, CachedData<any>>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A store that is used for storing cached data such as GitHub contributors
|
* A store that is used for storing cached data such as GitHub contributors
|
||||||
*/
|
*/
|
||||||
export const CacheStore = createLocalStore<ICacheStore>({
|
export const CacheStore = createLocalStore<ICacheStore>({
|
||||||
github: {}
|
github: {},
|
||||||
});
|
});
|
||||||
|
|
||||||
debugStore({ cacheStore: CacheStore });
|
debugStore({ cacheStore: CacheStore });
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* Represents cached data with its fetch timestamp
|
||||||
|
* @template T The type of the cached data
|
||||||
|
*/
|
||||||
export type CachedData<T> = {
|
export type CachedData<T> = {
|
||||||
data: T;
|
data: T;
|
||||||
dataFetched: number;
|
dataFetched: number;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Octokit } from '@octokit/rest';
|
import { Octokit } from '@octokit/rest';
|
||||||
import { CacheStore } from '@shared/storage/CacheStore';
|
import { CacheStore } from '@shared/storage/CacheStore';
|
||||||
import type { CachedData } from '@shared/types/CachedData';
|
import type { CachedData } from '@shared/types/CachedData';
|
||||||
import { serialize } from 'chrome-extension-toolkit';
|
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
type TeamMember = {
|
type TeamMember = {
|
||||||
@@ -66,7 +65,7 @@ export type LD_ADMIN_GITHUB_USERNAMES = (typeof LONGHORN_DEVELOPERS_ADMINS)[numb
|
|||||||
*/
|
*/
|
||||||
export class GitHubStatsService {
|
export class GitHubStatsService {
|
||||||
private octokit: Octokit;
|
private octokit: Octokit;
|
||||||
private cache: Record<string, CachedData<any>>;
|
private cache: Record<string, CachedData<unknown>>;
|
||||||
|
|
||||||
constructor(githubToken?: string) {
|
constructor(githubToken?: string) {
|
||||||
this.octokit = githubToken ? new Octokit({ auth: githubToken }) : new Octokit();
|
this.octokit = githubToken ? new Octokit({ auth: githubToken }) : new Octokit();
|
||||||
@@ -75,10 +74,14 @@ export class GitHubStatsService {
|
|||||||
|
|
||||||
private async getCachedData<T>(key: string): Promise<CachedData<T> | null> {
|
private async getCachedData<T>(key: string): Promise<CachedData<T> | null> {
|
||||||
if (Object.keys(this.cache).length === 0) {
|
if (Object.keys(this.cache).length === 0) {
|
||||||
this.cache = await CacheStore.get('github') as Record<string, CachedData<any>>;
|
const githubCache = await CacheStore.get('github');
|
||||||
|
if (githubCache && typeof githubCache === 'object') {
|
||||||
|
this.cache = githubCache as Record<string, CachedData<unknown>>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const cachedItem = this.cache[key];
|
|
||||||
if (cachedItem && Date.now() - new Date(cachedItem.dataFetched).getTime() < CACHE_TTL) {
|
const cachedItem = this.cache[key] as CachedData<T> | undefined;
|
||||||
|
if (cachedItem && Date.now() - cachedItem.dataFetched < CACHE_TTL) {
|
||||||
return cachedItem;
|
return cachedItem;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -86,9 +89,13 @@ export class GitHubStatsService {
|
|||||||
|
|
||||||
private async setCachedData<T>(key: string, data: T): Promise<void> {
|
private async setCachedData<T>(key: string, data: T): Promise<void> {
|
||||||
if (Object.keys(this.cache).length === 0) {
|
if (Object.keys(this.cache).length === 0) {
|
||||||
this.cache = await CacheStore.get('github') as Record<string, CachedData<any>>;
|
const githubCache = await CacheStore.get('github');
|
||||||
|
if (githubCache && typeof githubCache === 'object') {
|
||||||
|
this.cache = githubCache as Record<string, CachedData<unknown>>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.cache[key] = { data, dataFetched: (new Date()).getTime() };
|
|
||||||
|
this.cache[key] = { data, dataFetched: Date.now() };
|
||||||
await CacheStore.set('github', this.cache);
|
await CacheStore.set('github', this.cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +149,7 @@ export class GitHubStatsService {
|
|||||||
private async fetchContributorNames(contributors: string[]): Promise<Record<string, string>> {
|
private async fetchContributorNames(contributors: string[]): Promise<Record<string, string>> {
|
||||||
const names: Record<string, string> = {};
|
const names: Record<string, string> = {};
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
contributors.map(async (contributor) => {
|
contributors.map(async contributor => {
|
||||||
const cacheKey = `contributor_name_${contributor}`;
|
const cacheKey = `contributor_name_${contributor}`;
|
||||||
const cachedName = await this.getCachedData<string>(cacheKey);
|
const cachedName = await this.getCachedData<string>(cacheKey);
|
||||||
let name = `@${contributor}`;
|
let name = `@${contributor}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user