Skip to content

Commit 56e546d

Browse files
committed
refactor: use octokit client
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 7b3e1b3 commit 56e546d

4 files changed

Lines changed: 8 additions & 57 deletions

File tree

src/renderer/utils/api/client.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ export async function listNotificationsForAuthenticatedUser(
7272
participating: settings.participating,
7373
all: settings.fetchReadNotifications,
7474
per_page: 100,
75-
// TODO - is this the right way to do this
7675
headers: {
77-
'If-None-Match': '', // Prevent caching
76+
'Cache-Control': 'no-cache', // Prevent caching
7877
},
7978
},
8079
);
@@ -86,9 +85,8 @@ export async function listNotificationsForAuthenticatedUser(
8685
participating: settings.participating,
8786
all: settings.fetchReadNotifications,
8887
per_page: 100,
89-
// TODO - is this the right way to do this
9088
headers: {
91-
'If-None-Match': '', // Prevent caching
89+
'Cache-Control': 'no-cache', // Prevent caching
9290
},
9391
});
9492

src/renderer/utils/api/octokit.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { restEndpointMethods } from '@octokit/plugin-rest-endpoint-methods';
55
import type { Account } from '../../types';
66

77
import { getAccountUUID } from '../auth/utils';
8-
import { decryptValue } from '../comms';
8+
import { decryptValue, getAppVersion } from '../comms';
99
import { getGitHubAPIBaseUrl } from './utils';
10+
import { APPLICATION } from '../../../shared/constants';
1011

1112
// Create the Octokit type with plugins
1213
const OctokitWithPlugins = Octokit.plugin(paginateRest, restEndpointMethods);
@@ -15,6 +16,8 @@ export type OctokitClient = InstanceType<typeof OctokitWithPlugins>;
1516
// Cache Octokit clients per account UUID
1617
const octokitClientCache = new Map<string, OctokitClient>();
1718

19+
const version = getAppVersion()
20+
1821
/**
1922
* Clear the Octokit client cache
2023
* Useful when accounts are added/removed or tokens change
@@ -52,6 +55,7 @@ export async function createOctokitClient(
5255
const client = new OctokitWithPlugins({
5356
auth: decryptedToken,
5457
baseUrl,
58+
userAgent: `${APPLICATION.NAME}/${version}`
5559
});
5660

5761
// Cache the client
Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { AxiosResponse } from 'axios';
2-
31
import type { Hostname } from '../../types';
42

53
import {
@@ -46,44 +44,5 @@ describe('renderer/utils/api/utils.ts', () => {
4644
});
4745
});
4846

49-
describe('getNextURLFromLinkHeader', () => {
50-
it('should parse next url from link header', () => {
51-
const mockResponse = {
52-
headers: {
53-
link: '<https://api.github.com/notifications?participating=false&page=2>; rel="next", <https://api.github.com/notifications?participating=false&page=2>; rel="last"',
54-
},
55-
};
56-
57-
const result = getNextURLFromLinkHeader(
58-
mockResponse as unknown as AxiosResponse,
59-
);
60-
expect(result.toString()).toBe(
61-
'https://api.github.com/notifications?participating=false&page=2',
62-
);
63-
});
64-
65-
it('should return null if no next url in link header', () => {
66-
const mockResponse = {
67-
headers: {
68-
link: '<https://api.github.com/notifications?participating=false&page=2>; rel="last"',
69-
},
70-
};
71-
72-
const result = getNextURLFromLinkHeader(
73-
mockResponse as unknown as AxiosResponse,
74-
);
75-
expect(result).toBeNull();
76-
});
77-
78-
it('should return null if no link header exists', () => {
79-
const mockResponse = {
80-
headers: {},
81-
};
82-
83-
const result = getNextURLFromLinkHeader(
84-
mockResponse as unknown as AxiosResponse,
85-
);
86-
expect(result).toBeNull();
87-
});
88-
});
47+
8948
});

src/renderer/utils/api/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { AxiosResponse } from 'axios';
2-
31
import { APPLICATION } from '../../../shared/constants';
42

53
import { Constants } from '../../constants';
@@ -39,14 +37,6 @@ export function getGitHubGraphQLUrl(hostname: Hostname): URL {
3937
return url;
4038
}
4139

42-
export function getNextURLFromLinkHeader(
43-
response: AxiosResponse,
44-
): string | null {
45-
const linkHeader = response.headers.link;
46-
const matches = linkHeader?.match(/<([^<>]+)>;\s*rel="next"/);
47-
return matches ? matches[1] : null;
48-
}
49-
5040
export function getNumberFromUrl(url: string): number {
5141
return Number.parseInt(url.split('/').pop(), 10);
5242
}

0 commit comments

Comments
 (0)