mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Merge branch 'fix/reduce-config-api-calls' of https://github.com/all-hands-ai/openhands into fix/reduce-config-api-calls
This commit is contained in:
commit
48de69153c
41
frontend/src/types/github.ts
Normal file
41
frontend/src/types/github.ts
Normal file
@ -0,0 +1,41 @@
|
||||
export interface GitHubUser {
|
||||
id: number;
|
||||
login: string;
|
||||
avatar_url: string;
|
||||
}
|
||||
|
||||
export interface GitHubErrorReponse {
|
||||
message: string;
|
||||
documentation_url: string;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export interface GitHubRepository {
|
||||
id: number;
|
||||
name: string;
|
||||
full_name: string;
|
||||
private: boolean;
|
||||
html_url: string;
|
||||
description: string | null;
|
||||
fork: boolean;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
pushed_at: string;
|
||||
git_url: string;
|
||||
ssh_url: string;
|
||||
clone_url: string;
|
||||
default_branch: string;
|
||||
}
|
||||
|
||||
export interface GitHubCommit {
|
||||
sha: string;
|
||||
commit: {
|
||||
author: {
|
||||
name: string;
|
||||
email: string;
|
||||
date: string;
|
||||
};
|
||||
message: string;
|
||||
};
|
||||
html_url: string;
|
||||
}
|
||||
@ -1,15 +1,19 @@
|
||||
import type { GitHubUser, GitHubErrorReponse } from "#/types/github";
|
||||
|
||||
interface CacheEntry<T> {
|
||||
value: T;
|
||||
timestamp: number;
|
||||
token: string;
|
||||
}
|
||||
|
||||
type GitHubUserResponse = GitHubUser | GitHubErrorReponse;
|
||||
|
||||
class AuthCache {
|
||||
private static instance: AuthCache;
|
||||
|
||||
private cache: {
|
||||
isAuthed?: CacheEntry<boolean>;
|
||||
githubUser?: CacheEntry<any>;
|
||||
githubUser?: CacheEntry<GitHubUserResponse>;
|
||||
} = {};
|
||||
|
||||
private constructor() {}
|
||||
@ -21,11 +25,11 @@ class AuthCache {
|
||||
return AuthCache.instance;
|
||||
}
|
||||
|
||||
private isExpired(entry: CacheEntry<any>, maxAge: number): boolean {
|
||||
private isExpired<T>(entry: CacheEntry<T>, maxAge: number): boolean {
|
||||
return Date.now() - entry.timestamp > maxAge;
|
||||
}
|
||||
|
||||
private tokenChanged(entry: CacheEntry<any>, currentToken: string): boolean {
|
||||
private tokenChanged<T>(entry: CacheEntry<T>, currentToken: string): boolean {
|
||||
return entry.token !== currentToken;
|
||||
}
|
||||
|
||||
@ -49,7 +53,7 @@ class AuthCache {
|
||||
};
|
||||
}
|
||||
|
||||
getGithubUser(token: string): any | undefined {
|
||||
getGithubUser(token: string): GitHubUserResponse | undefined {
|
||||
const entry = this.cache.githubUser;
|
||||
if (
|
||||
!entry ||
|
||||
@ -61,7 +65,7 @@ class AuthCache {
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
setGithubUser(token: string, value: any): void {
|
||||
setGithubUser(token: string, value: GitHubUserResponse): void {
|
||||
this.cache.githubUser = {
|
||||
value,
|
||||
timestamp: Date.now(),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user