-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathoctokit.ts
More file actions
101 lines (86 loc) · 2.89 KB
/
octokit.ts
File metadata and controls
101 lines (86 loc) · 2.89 KB
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
import { Octokit } from '@octokit/core';
import { paginateRest } from '@octokit/plugin-paginate-rest';
import { restEndpointMethods } from '@octokit/plugin-rest-endpoint-methods';
import { retry } from '@octokit/plugin-retry';
import { APPLICATION } from '../../../shared/constants';
import type { Account } from '../../types';
import type { APIClientType } from './types';
import { getAccountUUID } from '../auth/utils';
import { decryptValue, getAppVersion } from '../comms';
import { getGitHubAPIBaseUrl } from './utils';
// Create the Octokit type with plugins
const OctokitWithPlugins = Octokit.plugin(
paginateRest,
restEndpointMethods,
retry,
);
export type OctokitClient = InstanceType<typeof OctokitWithPlugins>;
// Cache Octokit clients per account UUID + type (rest|graphql)
const octokitClientCache = new Map<string, OctokitClient>();
/**
* Clear the Octokit client cache
* Useful when accounts are added/removed or tokens change
*/
export function clearOctokitClientCache(): void {
octokitClientCache.clear();
}
/**
* Create an authenticated Octokit client instance with caching
* Clients are cached to avoid recreating them for every API call
*
* @param account The account to create the client for
* @param type The api client type (rest | graphql)
* @returns A cached authenticated Octokit instance
*/
export async function createOctokitClient(
account: Account,
type: APIClientType,
): Promise<OctokitClient> {
const cacheKey = getClientCacheKey(account, type);
// Return cached client if it exists
const cachedClient = octokitClientCache.get(cacheKey);
if (cachedClient) {
return cachedClient;
}
const client = await createOctokitClientUncached(account, type);
octokitClientCache.set(cacheKey, client);
return client;
}
/**
* Create an authenticated Octokit client instance without caching
* Useful when fresh data is needed (e.g., user details during account setup)
*
* @param account The account to create the client for
* @param type The api client type (rest | graphql)
* @returns A fresh authenticated Octokit instance
*/
export async function createOctokitClientUncached(
account: Account,
type: APIClientType,
): Promise<OctokitClient> {
const decryptedToken = await decryptValue(account.token);
const version = await getAppVersion();
const userAgent = `${APPLICATION.NAME}/${version}`;
const baseUrl = getGitHubAPIBaseUrl(account.hostname, type)
.toString()
.replace(/\/$/, '');
return new OctokitWithPlugins({
auth: decryptedToken,
baseUrl: baseUrl,
userAgent: userAgent,
retry: {
retries: 1,
},
});
}
/**
* Calculate client cache key for account and api type
*
* @param account The Gitify account
* @param type The API client type
* @returns cache key
*/
export function getClientCacheKey(account: Account, type: APIClientType) {
const accountUUID = getAccountUUID(account);
return `${accountUUID}:${type}`;
}