Skip to content

Commit 28f06bd

Browse files
authored
refactor: extract api calls into client with types (#1056)
* reafactor: api client * reafactor: api client * refactor: api client * refactor: api client * refactor: api client
1 parent 92a842c commit 28f06bd

17 files changed

+820
-176
lines changed

src/context/App.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useContext } from 'react';
44
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
55
import { useNotifications } from '../hooks/useNotifications';
66
import type { AuthState, SettingsState } from '../types';
7-
import * as apiRequests from '../utils/api-requests';
7+
import * as apiRequests from '../utils/api/request';
88
import * as comms from '../utils/comms';
99
import Constants from '../utils/constants';
1010
import * as notifications from '../utils/notifications';

src/context/App.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import {
1818
type SettingsState,
1919
Theme,
2020
} from '../types';
21-
import { apiRequestAuth } from '../utils/api-requests';
21+
import { headNotifications } from '../utils/api/client';
2222
import { addAccount, authGitHub, getToken, getUserData } from '../utils/auth';
2323
import { setAutoLaunch, updateTrayTitle } from '../utils/comms';
2424
import Constants from '../utils/constants';
25-
import { generateGitHubAPIUrl } from '../utils/helpers';
2625
import { getNotificationCount } from '../utils/notifications';
2726
import { clearState, loadState, saveState } from '../utils/storage';
2827
import { setTheme } from '../utils/theme';
@@ -166,11 +165,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
166165

167166
const validateToken = useCallback(
168167
async ({ token, hostname }: AuthTokenOptions) => {
169-
await apiRequestAuth(
170-
`${generateGitHubAPIUrl(hostname)}notifications`,
171-
'HEAD',
172-
token,
173-
);
168+
await headNotifications(hostname, token);
169+
174170
const user = await getUserData(token, hostname);
175171
const updatedAccounts = addAccount(accounts, token, hostname, user);
176172
setAccounts(updatedAccounts);

src/hooks/useNotifications.ts

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios, { type AxiosError, type AxiosPromise } from 'axios';
1+
import axios, { type AxiosError } from 'axios';
22
import { useCallback, useState } from 'react';
33

44
import type {
@@ -8,11 +8,16 @@ import type {
88
SettingsState,
99
} from '../types';
1010
import type { GitHubRESTError, Notification } from '../typesGitHub';
11-
import { apiRequestAuth } from '../utils/api-requests';
11+
import {
12+
ignoreNotificationThreadSubscription,
13+
listNotificationsForAuthenticatedUser,
14+
markNotificationThreadAsDone,
15+
markNotificationThreadAsRead,
16+
markRepositoryNotificationsAsRead,
17+
} from '../utils/api/client';
1218
import { determineFailureType } from '../utils/api/errors';
1319
import Constants from '../utils/constants';
1420
import {
15-
generateGitHubAPIUrl,
1621
getEnterpriseAccountToken,
1722
getTokenForHost,
1823
isEnterpriseHost,
@@ -74,28 +79,25 @@ export const useNotifications = (): NotificationsState => {
7479

7580
const fetchNotifications = useCallback(
7681
async (accounts: AuthState, settings: SettingsState) => {
77-
function getNotifications(
78-
hostname: string,
79-
token: string,
80-
): AxiosPromise<Notification[]> {
81-
const endpointSuffix = `notifications?participating=${settings.participating}`;
82-
const url = `${generateGitHubAPIUrl(hostname)}${endpointSuffix}`;
83-
return apiRequestAuth(url, 'GET', token);
84-
}
85-
8682
function getGitHubNotifications() {
8783
if (!isGitHubLoggedIn(accounts)) {
8884
return;
8985
}
90-
return getNotifications(
86+
87+
return listNotificationsForAuthenticatedUser(
9188
Constants.DEFAULT_AUTH_OPTIONS.hostname,
9289
accounts.token,
90+
settings,
9391
);
9492
}
9593

9694
function getEnterpriseNotifications() {
9795
return accounts.enterpriseAccounts.map((account) => {
98-
return getNotifications(account.hostname, account.token);
96+
return listNotificationsForAuthenticatedUser(
97+
account.hostname,
98+
account.token,
99+
settings,
100+
);
99101
});
100102
}
101103

@@ -220,12 +222,7 @@ export const useNotifications = (): NotificationsState => {
220222
: accounts.token;
221223

222224
try {
223-
await apiRequestAuth(
224-
`${generateGitHubAPIUrl(hostname)}notifications/threads/${id}`,
225-
'PATCH',
226-
token,
227-
{},
228-
);
225+
await markNotificationThreadAsRead(id, hostname, token);
229226

230227
const updatedNotifications = removeNotification(
231228
id,
@@ -253,12 +250,7 @@ export const useNotifications = (): NotificationsState => {
253250
: accounts.token;
254251

255252
try {
256-
await apiRequestAuth(
257-
`${generateGitHubAPIUrl(hostname)}notifications/threads/${id}`,
258-
'DELETE',
259-
token,
260-
{},
261-
);
253+
await markNotificationThreadAsDone(id, hostname, token);
262254

263255
const updatedNotifications = removeNotification(
264256
id,
@@ -286,14 +278,7 @@ export const useNotifications = (): NotificationsState => {
286278
: accounts.token;
287279

288280
try {
289-
await apiRequestAuth(
290-
`${generateGitHubAPIUrl(
291-
hostname,
292-
)}notifications/threads/${id}/subscription`,
293-
'PUT',
294-
token,
295-
{ ignored: true },
296-
);
281+
await ignoreNotificationThreadSubscription(id, hostname, token);
297282
await markNotificationRead(accounts, id, hostname);
298283
} catch (err) {
299284
setIsFetching(false);
@@ -312,13 +297,7 @@ export const useNotifications = (): NotificationsState => {
312297
: accounts.token;
313298

314299
try {
315-
await apiRequestAuth(
316-
`${generateGitHubAPIUrl(hostname)}repos/${repoSlug}/notifications`,
317-
'PUT',
318-
token,
319-
{},
320-
);
321-
300+
await markRepositoryNotificationsAsRead(repoSlug, hostname, token);
322301
const updatedNotifications = removeNotifications(
323302
repoSlug,
324303
notifications,

src/routes/Settings.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { AxiosResponse } from 'axios';
88
import { shell } from 'electron';
99
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
1010
import { AppContext } from '../context/App';
11-
import * as apiRequests from '../utils/api-requests';
11+
import * as apiRequests from '../utils/api/request';
1212
import Constants from '../utils/constants';
1313
import { SettingsRoute } from './Settings';
1414

src/routes/Settings.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ import { Checkbox } from '../components/fields/Checkbox';
2121
import { RadioGroup } from '../components/fields/RadioGroup';
2222
import { AppContext } from '../context/App';
2323
import { Theme } from '../types';
24-
import { apiRequestAuth } from '../utils/api-requests';
24+
import { getRootHypermediaLinks } from '../utils/api/client';
2525
import {
2626
openExternalLink,
2727
updateTrayIcon,
2828
updateTrayTitle,
2929
} from '../utils/comms';
3030
import Constants from '../utils/constants';
31-
import { generateGitHubAPIUrl } from '../utils/helpers';
3231
import { setTheme } from '../utils/theme';
3332

3433
export const SettingsRoute: FC = () => {
@@ -74,9 +73,8 @@ export const SettingsRoute: FC = () => {
7473

7574
useMemo(() => {
7675
(async () => {
77-
const response = await apiRequestAuth(
78-
`${generateGitHubAPIUrl(Constants.DEFAULT_AUTH_OPTIONS.hostname)}`,
79-
'GET',
76+
const response = await getRootHypermediaLinks(
77+
Constants.DEFAULT_AUTH_OPTIONS.hostname,
8078
accounts.token,
8179
);
8280

src/typesGitHub.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export interface UserProfile {
107107
two_factor_authentication: boolean;
108108
plan: Plan;
109109
}
110+
110111
export interface Plan {
111112
name: string;
112113
space: number;
@@ -330,7 +331,8 @@ interface CommitFiles {
330331
contents_url: string;
331332
patch: string;
332333
}
333-
export interface CommitComments {
334+
335+
export interface CommitComment {
334336
url: string;
335337
html_url: string;
336338
issue_url: string;
@@ -365,7 +367,7 @@ export interface Issue {
365367
state_reason: IssueStateReasonType | null;
366368
}
367369

368-
export interface IssueComments {
370+
export interface IssueOrPullRequestComment {
369371
url: string;
370372
html_url: string;
371373
issue_url: string;
@@ -377,20 +379,22 @@ export interface IssueComments {
377379
body: string;
378380
}
379381

380-
export interface ReleaseComments {
382+
export interface Release {
381383
url: string;
382384
assets_url: string;
385+
upload_url: string;
383386
html_url: string;
384387
id: number;
385388
author: User;
386389
node_id: string;
387390
tag_name: string;
388-
name: string;
391+
target_commitish: string;
392+
name: string | null;
393+
body: string | null;
389394
draft: boolean;
390395
prerelease: boolean;
391396
created_at: string;
392-
published_at: string;
393-
body: string;
397+
published_at: string | null;
394398
}
395399

396400
export interface GraphQLSearch<T> {
@@ -444,3 +448,47 @@ export interface GitHubRESTError {
444448
message: string;
445449
documentation_url: string;
446450
}
451+
452+
export interface NotificationThreadSubscription {
453+
subscribed: boolean;
454+
ignored: boolean;
455+
reason: string | null;
456+
created_at: string;
457+
url: string;
458+
thread_url: string;
459+
}
460+
461+
export interface RootHypermediaLinks {
462+
current_user_url: string;
463+
current_user_authorizations_html_url: string;
464+
authorizations_url: string;
465+
code_search_url: string;
466+
commit_search_url: string;
467+
emails_url: string;
468+
emojis_url: string;
469+
events_url: string;
470+
feeds_url: string;
471+
followers_url: string;
472+
following_url: string;
473+
gists_url: string;
474+
hub_url: string;
475+
issue_search_url: string;
476+
issues_url: string;
477+
keys_url: string;
478+
notifications_url: string;
479+
organization_url: string;
480+
organization_repositories_url: string;
481+
organization_teams_url: string;
482+
public_gists_url: string;
483+
rate_limit_url: string;
484+
repository_url: string;
485+
repository_search_url: string;
486+
current_user_repositories_url: string;
487+
starred_url: string;
488+
starred_gists_url: string;
489+
topic_search_url: string;
490+
user_url: string;
491+
user_organizations_url: string;
492+
user_repositories_url: string;
493+
user_search_url: string;
494+
}

0 commit comments

Comments
 (0)