Skip to content

refactor: use URL object instead of strings #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/utils/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('utils/api/client.ts', () => {
await getRootHypermediaLinks(mockEnterpriseHostname, mockToken);

expect(axios).toHaveBeenCalledWith({
url: 'https://example.com/api/v3',
url: 'https://example.com/api/v3/',
method: 'GET',
data: {},
});
Expand Down
42 changes: 22 additions & 20 deletions src/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import type {
RootHypermediaLinks,
UserDetails,
} from '../../typesGitHub';
import { getGitHubAPIBaseUrl } from '../helpers';
import { apiRequestAuth } from './request';

import { print } from 'graphql/language/printer';
import Constants from '../constants';
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
import { formatSearchQueryString } from './utils';
import { formatSearchQueryString, getGitHubAPIBaseUrl } from './utils';

/**
* Get Hypermedia links to resources accessible in GitHub's REST API
Expand All @@ -32,8 +31,7 @@ export function getRootHypermediaLinks(
hostname: string,
token: string,
): AxiosPromise<RootHypermediaLinks> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(baseUrl);
const url = getGitHubAPIBaseUrl(hostname);
return apiRequestAuth(url.toString(), 'GET', token);
}

Expand All @@ -46,8 +44,9 @@ export function getAuthenticatedUser(
hostname: string,
token: string,
): AxiosPromise<UserDetails> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(`${baseUrl}/user`);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += 'user';

return apiRequestAuth(url.toString(), 'GET', token);
}

Expand All @@ -56,8 +55,9 @@ export function headNotifications(
hostname: string,
token: string,
): AxiosPromise<void> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(`${baseUrl}/notifications`);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += 'notifications';

return apiRequestAuth(url.toString(), 'HEAD', token);
}

Expand All @@ -71,8 +71,8 @@ export function listNotificationsForAuthenticatedUser(
token: string,
settings: SettingsState,
): AxiosPromise<Notification[]> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(`${baseUrl}/notifications`);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += 'notifications';
url.searchParams.append('participating', String(settings.participating));

return apiRequestAuth(url.toString(), 'GET', token);
Expand All @@ -89,8 +89,9 @@ export function markNotificationThreadAsRead(
hostname: string,
token: string,
): AxiosPromise<void> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(`${baseUrl}/notifications/threads/${threadId}`);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `notifications/threads/${threadId}`;

return apiRequestAuth(url.toString(), 'PATCH', token, {});
}

Expand All @@ -105,8 +106,9 @@ export function markNotificationThreadAsDone(
hostname: string,
token: string,
): AxiosPromise<void> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(`${baseUrl}/notifications/threads/${threadId}`);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `notifications/threads/${threadId}`;

return apiRequestAuth(url.toString(), 'DELETE', token, {});
}

Expand All @@ -120,10 +122,9 @@ export function ignoreNotificationThreadSubscription(
hostname: string,
token: string,
): AxiosPromise<NotificationThreadSubscription> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(
`${baseUrl}/notifications/threads/${threadId}/subscription`,
);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `notifications/threads/${threadId}/subscription`;

return apiRequestAuth(url.toString(), 'PUT', token, { ignored: true });
}

Expand All @@ -140,8 +141,9 @@ export function markRepositoryNotificationsAsRead(
hostname: string,
token: string,
): AxiosPromise<void> {
const baseUrl = getGitHubAPIBaseUrl(hostname);
const url = new URL(`${baseUrl}/repos/${repoSlug}/notifications`);
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `repos/${repoSlug}/notifications`;

return apiRequestAuth(url.toString(), 'PUT', token, {});
}

Expand Down
32 changes: 24 additions & 8 deletions src/utils/api/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { addHours, formatSearchQueryString } from './utils';
import {
addHours,
formatSearchQueryString,
getGitHubAPIBaseUrl,
} from './utils';

describe('utils/api/utils.ts', () => {
describe('addHours', () => {
test('adds hours correctly for positive values', () => {
const result = addHours('2024-02-20T12:00:00.000Z', 3);
expect(result).toBe('2024-02-20T15:00:00.000Z');
describe('generateGitHubAPIUrl', () => {
it('should generate a GitHub API url - non enterprise', () => {
const result = getGitHubAPIBaseUrl('github.com');
expect(result.toString()).toBe('https://api.github.com/');
});

test('adds hours correctly for negative values', () => {
const result = addHours('2024-02-20T12:00:00.000Z', -2);
expect(result).toBe('2024-02-20T10:00:00.000Z');
it('should generate a GitHub API url - enterprise', () => {
const result = getGitHubAPIBaseUrl('github.manos.im');
expect(result.toString()).toBe('https://github.manos.im/api/v3/');
});
});

Expand All @@ -26,4 +30,16 @@ describe('utils/api/utils.ts', () => {
);
});
});

describe('addHours', () => {
test('adds hours correctly for positive values', () => {
const result = addHours('2024-02-20T12:00:00.000Z', 3);
expect(result).toBe('2024-02-20T15:00:00.000Z');
});

test('adds hours correctly for negative values', () => {
const result = addHours('2024-02-20T12:00:00.000Z', -2);
expect(result).toBe('2024-02-20T10:00:00.000Z');
});
});
});
13 changes: 13 additions & 0 deletions src/utils/api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import Constants from '../constants';
import { isEnterpriseHost } from '../helpers';

export function getGitHubAPIBaseUrl(hostname: string): URL {
const url = new URL(Constants.GITHUB_API_BASE_URL);

if (isEnterpriseHost(hostname)) {
url.hostname = hostname;
url.pathname = '/api/v3/';
}
return url;
}

export function formatSearchQueryString(
repo: string,
title: string,
Expand Down
51 changes: 0 additions & 51 deletions src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
import type { SubjectType } from '../typesGitHub';
import * as apiRequests from './api/request';
import {
addNotificationReferrerIdToUrl,
formatForDisplay,
generateGitHubWebUrl,
generateNotificationReferrerId,
getGitHubAPIBaseUrl,
isEnterpriseHost,
isGitHubLoggedIn,
} from './helpers';
Expand All @@ -39,43 +37,6 @@ describe('utils/helpers.ts', () => {
});
});

describe('addNotificationReferrerIdToUrl', () => {
it('should add notification_referrer_id to the URL', () => {
// Mock data
const url = 'https://github.com/gitify-app/notifications-test';
const notificationId = '123';
const userId = 456;

const result = addNotificationReferrerIdToUrl(
url,
notificationId,
userId,
);

expect(result).toEqual(
'https://github.com/gitify-app/notifications-test?notification_referrer_id=MDE4Ok5vdGlmaWNhdGlvblRocmVhZDEyMzo0NTY%3D',
);
});

it('should add notification_referrer_id to the URL, preserving anchor tags', () => {
// Mock data
const url =
'https://github.com/gitify-app/notifications-test/pull/123#issuecomment-1951055051';
const notificationId = '123';
const userId = 456;

const result = addNotificationReferrerIdToUrl(
url,
notificationId,
userId,
);

expect(result).toEqual(
'https://github.com/gitify-app/notifications-test/pull/123?notification_referrer_id=MDE4Ok5vdGlmaWNhdGlvblRocmVhZDEyMzo0NTY%3D#issuecomment-1951055051',
);
});
});

describe('generateNotificationReferrerId', () => {
it('should generate the notification_referrer_id', () => {
const referrerId = generateNotificationReferrerId(
Expand All @@ -88,18 +49,6 @@ describe('utils/helpers.ts', () => {
});
});

describe('generateGitHubAPIUrl', () => {
it('should generate a GitHub API url - non enterprise', () => {
const result = getGitHubAPIBaseUrl('github.com');
expect(result).toBe('https://api.github.com');
});

it('should generate a GitHub API url - enterprise', () => {
const result = getGitHubAPIBaseUrl('github.manos.im');
expect(result).toBe('https://github.manos.im/api/v3');
});
});

describe('generateGitHubWebUrl', () => {
const mockedHtmlUrl =
'https://github.com/gitify-app/notifications-test/issues/785';
Expand Down
Loading