Skip to content

Commit 9cc054c

Browse files
committed
feat: axios-cache-interceptor
Signed-off-by: Adam Setch <[email protected]>
1 parent 6b246e5 commit 9cc054c

File tree

9 files changed

+61
-73
lines changed

9 files changed

+61
-73
lines changed

src/renderer/hooks/useNotifications.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ export const useNotifications = (): NotificationsState => {
111111
try {
112112
await Promise.all(
113113
readNotifications.map((notification) =>
114-
markNotificationThreadAsRead(
115-
notification.id,
116-
notification.account.hostname,
117-
notification.account.token,
118-
),
114+
markNotificationThreadAsRead(notification.account, notification.id),
119115
),
120116
);
121117

@@ -149,9 +145,8 @@ export const useNotifications = (): NotificationsState => {
149145
await Promise.all(
150146
doneNotifications.map((notification) =>
151147
markNotificationThreadAsDone(
148+
notification.account,
152149
notification.id,
153-
notification.account.hostname,
154-
notification.account.token,
155150
),
156151
),
157152
);
@@ -184,9 +179,8 @@ export const useNotifications = (): NotificationsState => {
184179

185180
try {
186181
await ignoreNotificationThreadSubscription(
182+
notification.account,
187183
notification.id,
188-
notification.account.hostname,
189-
notification.account.token,
190184
);
191185

192186
if (state.settings.markAsDoneOnUnsubscribe) {

src/renderer/utils/api/client.test.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
mockGitHubEnterpriseServerAccount,
66
mockToken,
77
} from '../../__mocks__/state-mocks';
8-
import type { Hostname, Link, SettingsState, Token } from '../../types';
8+
import type { Hostname, Link, SettingsState } from '../../types';
99
import * as logger from '../../utils/logger';
1010
import {
1111
getAuthenticatedUser,
@@ -172,11 +172,7 @@ describe('renderer/utils/api/client.ts', () => {
172172

173173
describe('markNotificationThreadAsRead', () => {
174174
it('should mark notification thread as read - github', async () => {
175-
await markNotificationThreadAsRead(
176-
mockThreadId,
177-
mockGitHubHostname,
178-
mockToken,
179-
);
175+
await markNotificationThreadAsRead(mockGitHubCloudAccount, mockThreadId);
180176

181177
expect(axios).toHaveBeenCalledWith({
182178
url: `https://api.github.com/notifications/threads/${mockThreadId}`,
@@ -193,13 +189,12 @@ describe('renderer/utils/api/client.ts', () => {
193189

194190
it('should mark notification thread as read - enterprise', async () => {
195191
await markNotificationThreadAsRead(
192+
mockGitHubEnterpriseServerAccount,
196193
mockThreadId,
197-
mockEnterpriseHostname,
198-
mockToken,
199194
);
200195

201196
expect(axios).toHaveBeenCalledWith({
202-
url: `https://example.com/api/v3/notifications/threads/${mockThreadId}`,
197+
url: `https://github.gitify.io/api/v3/notifications/threads/${mockThreadId}`,
203198
headers: {
204199
Accept: 'application/json',
205200
Authorization: 'token decrypted',
@@ -214,11 +209,7 @@ describe('renderer/utils/api/client.ts', () => {
214209

215210
describe('markNotificationThreadAsDone', () => {
216211
it('should mark notification thread as done - github', async () => {
217-
await markNotificationThreadAsDone(
218-
mockThreadId,
219-
mockGitHubHostname,
220-
mockToken,
221-
);
212+
await markNotificationThreadAsDone(mockGitHubCloudAccount, mockThreadId);
222213

223214
expect(axios).toHaveBeenCalledWith({
224215
url: `https://api.github.com/notifications/threads/${mockThreadId}`,
@@ -235,9 +226,8 @@ describe('renderer/utils/api/client.ts', () => {
235226

236227
it('should mark notification thread as done - enterprise', async () => {
237228
await markNotificationThreadAsDone(
229+
mockGitHubEnterpriseServerAccount,
238230
mockThreadId,
239-
mockEnterpriseHostname,
240-
mockToken,
241231
);
242232

243233
expect(axios).toHaveBeenCalledWith({
@@ -257,9 +247,8 @@ describe('renderer/utils/api/client.ts', () => {
257247
describe('ignoreNotificationThreadSubscription', () => {
258248
it('should ignore notification thread subscription - github', async () => {
259249
await ignoreNotificationThreadSubscription(
250+
mockGitHubCloudAccount,
260251
mockThreadId,
261-
mockGitHubHostname,
262-
mockToken,
263252
);
264253

265254
expect(axios).toHaveBeenCalledWith({
@@ -277,13 +266,12 @@ describe('renderer/utils/api/client.ts', () => {
277266

278267
it('should ignore notification thread subscription - enterprise', async () => {
279268
await ignoreNotificationThreadSubscription(
269+
mockGitHubEnterpriseServerAccount,
280270
mockThreadId,
281-
mockEnterpriseHostname,
282-
mockToken,
283271
);
284272

285273
expect(axios).toHaveBeenCalledWith({
286-
url: `https://example.com/api/v3/notifications/threads/${mockThreadId}/subscription`,
274+
url: `https://github.gitify.io/api/v3/notifications/threads/${mockThreadId}/subscription`,
287275
headers: {
288276
Accept: 'application/json',
289277
Authorization: 'token decrypted',
@@ -312,8 +300,8 @@ describe('renderer/utils/api/client.ts', () => {
312300
apiRequestAuthMock.mockResolvedValue(requestPromise);
313301

314302
const result = await getHtmlUrl(
303+
mockGitHubCloudAccount,
315304
'https://api.github.com/repos/gitify-app/notifications-test/issues/785' as Link,
316-
'123' as Token,
317305
);
318306
expect(result).toBe(
319307
'https://github.com/gitify-app/notifications-test/issues/785',
@@ -332,8 +320,8 @@ describe('renderer/utils/api/client.ts', () => {
332320
apiRequestAuthMock.mockRejectedValue(mockError);
333321

334322
await getHtmlUrl(
323+
mockGitHubCloudAccount,
335324
'https://api.github.com/repos/gitify-app/gitify/issues/785' as Link,
336-
'123' as Token,
337325
);
338326

339327
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);

src/renderer/utils/api/client.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function listNotificationsForAuthenticatedUser(
7171
const url = getGitHubAPIBaseUrl(account.hostname);
7272
url.pathname += 'notifications';
7373
url.searchParams.append('participating', String(settings.participating));
74+
7475
return apiRequestAuth(
7576
url.toString() as Link,
7677
'GET',
@@ -87,14 +88,13 @@ export function listNotificationsForAuthenticatedUser(
8788
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-read
8889
*/
8990
export function markNotificationThreadAsRead(
91+
account: Account,
9092
threadId: string,
91-
hostname: Hostname,
92-
token: Token,
9393
): AxiosPromise<void> {
94-
const url = getGitHubAPIBaseUrl(hostname);
94+
const url = getGitHubAPIBaseUrl(account.hostname);
9595
url.pathname += `notifications/threads/${threadId}`;
9696

97-
return apiRequestAuth(url.toString() as Link, 'PATCH', token, {});
97+
return apiRequestAuth(url.toString() as Link, 'PATCH', account.token, {});
9898
}
9999

100100
/**
@@ -106,13 +106,13 @@ export function markNotificationThreadAsRead(
106106
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-done
107107
*/
108108
export function markNotificationThreadAsDone(
109+
account: Account,
109110
threadId: string,
110-
hostname: Hostname,
111-
token: Token,
112111
): AxiosPromise<void> {
113-
const url = getGitHubAPIBaseUrl(hostname);
112+
const url = getGitHubAPIBaseUrl(account.hostname);
114113
url.pathname += `notifications/threads/${threadId}`;
115-
return apiRequestAuth(url.toString() as Link, 'DELETE', token, {});
114+
115+
return apiRequestAuth(url.toString() as Link, 'DELETE', account.token, {});
116116
}
117117

118118
/**
@@ -121,14 +121,13 @@ export function markNotificationThreadAsDone(
121121
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#delete-a-thread-subscription
122122
*/
123123
export function ignoreNotificationThreadSubscription(
124+
account: Account,
124125
threadId: string,
125-
hostname: Hostname,
126-
token: Token,
127126
): AxiosPromise<NotificationThreadSubscription> {
128-
const url = getGitHubAPIBaseUrl(hostname);
127+
const url = getGitHubAPIBaseUrl(account.hostname);
129128
url.pathname += `notifications/threads/${threadId}/subscription`;
130129

131-
return apiRequestAuth(url.toString() as Link, 'PUT', token, {
130+
return apiRequestAuth(url.toString() as Link, 'PUT', account.token, {
132131
ignored: true,
133132
});
134133
}
@@ -138,8 +137,8 @@ export function ignoreNotificationThreadSubscription(
138137
*
139138
* Endpoint documentation: https://docs.github.com/en/rest/commits/commits#get-a-commit
140139
*/
141-
export function getCommit(url: Link, token: Token): AxiosPromise<Commit> {
142-
return apiRequestAuth(url, 'GET', token);
140+
export function getCommit(account: Account, url: Link): AxiosPromise<Commit> {
141+
return apiRequestAuth(url, 'GET', account.token);
143142
}
144143

145144
/**
@@ -149,19 +148,19 @@ export function getCommit(url: Link, token: Token): AxiosPromise<Commit> {
149148
150149
*/
151150
export function getCommitComment(
151+
account: Account,
152152
url: Link,
153-
token: Token,
154153
): AxiosPromise<CommitComment> {
155-
return apiRequestAuth(url, 'GET', token);
154+
return apiRequestAuth(url, 'GET', account.token);
156155
}
157156

158157
/**
159158
* Get details of an issue.
160159
*
161160
* Endpoint documentation: https://docs.github.com/en/rest/issues/issues#get-an-issue
162161
*/
163-
export function getIssue(url: Link, token: Token): AxiosPromise<Issue> {
164-
return apiRequestAuth(url, 'GET', token);
162+
export function getIssue(account: Account, url: Link): AxiosPromise<Issue> {
163+
return apiRequestAuth(url, 'GET', account.token);
165164
}
166165

167166
/**
@@ -171,10 +170,10 @@ export function getIssue(url: Link, token: Token): AxiosPromise<Issue> {
171170
* Endpoint documentation: https://docs.github.com/en/rest/issues/comments#get-an-issue-comment
172171
*/
173172
export function getIssueOrPullRequestComment(
173+
account: Account,
174174
url: Link,
175-
token: Token,
176175
): AxiosPromise<IssueOrPullRequestComment> {
177-
return apiRequestAuth(url, 'GET', token);
176+
return apiRequestAuth(url, 'GET', account.token);
178177
}
179178

180179
/**
@@ -183,10 +182,10 @@ export function getIssueOrPullRequestComment(
183182
* Endpoint documentation: https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request
184183
*/
185184
export function getPullRequest(
185+
account: Account,
186186
url: Link,
187-
token: Token,
188187
): AxiosPromise<PullRequest> {
189-
return apiRequestAuth(url, 'GET', token);
188+
return apiRequestAuth(url, 'GET', account.token);
190189
}
191190

192191
/**
@@ -195,27 +194,27 @@ export function getPullRequest(
195194
* Endpoint documentation: https://docs.github.com/en/rest/pulls/reviews#list-reviews-for-a-pull-request
196195
*/
197196
export function getPullRequestReviews(
197+
account: Account,
198198
url: Link,
199-
token: Token,
200199
): AxiosPromise<PullRequestReview[]> {
201-
return apiRequestAuth(url, 'GET', token);
200+
return apiRequestAuth(url, 'GET', account.token);
202201
}
203202

204203
/**
205204
* Gets a public release with the specified release ID.
206205
*
207206
* Endpoint documentation: https://docs.github.com/en/rest/releases/releases#get-a-release
208207
*/
209-
export function getRelease(url: Link, token: Token): AxiosPromise<Release> {
210-
return apiRequestAuth(url, 'GET', token);
208+
export function getRelease(account: Account, url: Link): AxiosPromise<Release> {
209+
return apiRequestAuth(url, 'GET', account.token);
211210
}
212211

213212
/**
214213
* Get the `html_url` from the GitHub response
215214
*/
216-
export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
215+
export async function getHtmlUrl(account: Account, url: Link): Promise<string> {
217216
try {
218-
const response = (await apiRequestAuth(url, 'GET', token)).data;
217+
const response = (await apiRequestAuth(url, 'GET', account.token)).data;
219218
return response.html_url;
220219
} catch (err) {
221220
rendererLogError(

src/renderer/utils/api/request.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Axios, {
33
type AxiosResponse,
44
type Method,
55
} from 'axios';
6-
import { setupCache } from 'axios-cache-interceptor';
6+
import { buildKeyGenerator, setupCache } from 'axios-cache-interceptor';
77

88
import type { Link, Token } from '../../types';
99
import { decryptValue } from '../comms';
@@ -13,13 +13,20 @@ import { getNextURLFromLinkHeader } from './utils';
1313
const instance = Axios.create();
1414
const axios = setupCache(instance, {
1515
location: 'client',
16+
1617
cachePredicate: {
1718
ignoreUrls: [
1819
'/login/oauth/access_token',
19-
'/notifications',
20-
'/api/v3/notifications',
20+
// '/notifications',
21+
// '/api/v3/notifications',
2122
],
2223
},
24+
25+
generateKey: buildKeyGenerator((request) => ({
26+
method: request.method,
27+
url: request.url,
28+
custom: request.auth,
29+
})),
2330
});
2431

2532
/**

src/renderer/utils/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ export async function generateGitHubWebUrl(
117117
try {
118118
if (notification.subject.latest_comment_url) {
119119
url.href = await getHtmlUrl(
120+
notification.account,
120121
notification.subject.latest_comment_url,
121-
notification.account.token,
122122
);
123123
} else if (notification.subject.url) {
124124
url.href = await getHtmlUrl(
125+
notification.account,
125126
notification.subject.url,
126-
notification.account.token,
127127
);
128128
} else {
129129
// Perform any specific notification type handling (only required for a few special notification scenarios)

src/renderer/utils/notifications/handlers/commit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class CommitHandler extends DefaultHandler {
3535
if (notification.subject.latest_comment_url) {
3636
const commitComment = (
3737
await getCommitComment(
38+
notification.account,
3839
notification.subject.latest_comment_url,
39-
notification.account.token,
4040
)
4141
).data;
4242

4343
user = commitComment.user;
4444
} else {
4545
const commit = (
46-
await getCommit(notification.subject.url, notification.account.token)
46+
await getCommit(notification.account, notification.subject.url)
4747
).data;
4848

4949
user = commit.author;

src/renderer/utils/notifications/handlers/issue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class IssueHandler extends DefaultHandler {
2929
settings: SettingsState,
3030
): Promise<GitifySubject> {
3131
const issue = (
32-
await getIssue(notification.subject.url, notification.account.token)
32+
await getIssue(notification.account, notification.subject.url)
3333
).data;
3434

3535
const issueState = issue.state_reason ?? issue.state;
@@ -44,8 +44,8 @@ class IssueHandler extends DefaultHandler {
4444
if (notification.subject.latest_comment_url) {
4545
const issueComment = (
4646
await getIssueOrPullRequestComment(
47+
notification.account,
4748
notification.subject.latest_comment_url,
48-
notification.account.token,
4949
)
5050
).data;
5151
issueCommentUser = issueComment.user;

0 commit comments

Comments
 (0)