Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Improved Typings for API class #210

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export class Search extends Component<RouteComponentProps<{}>, State> {
*/
private handleFetchPackages: handleFetchPackages = async ({ value }) => {
try {
// @ts-ignore
const controller = new window.AbortController();
const signal = controller.signal;
// Keep track of search requests.
Expand Down
14 changes: 2 additions & 12 deletions src/utils/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@
import { handleResponseType } from '../../src/utils/api';

describe('api', () => {
// no the best mock, but I'd look for a mock library to work with fetch in the future
// @ts-ignore
const headers: Headers = {
// @ts-ignore
get: () => [],
};

describe('handleResponseType', () => {
test('should handle missing Content-Type', async () => {
const response: Response = {
url: 'http://localhost:8080/-/packages',
ok: false,
// @ts-ignore
headers: {
get: () => null,
} as Headers,
headers: new Headers(),
} as Response;

const handled = await handleResponseType(response);
Expand All @@ -34,7 +24,7 @@ describe('api', () => {
url: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz',
blob: () => blobPromise,
ok: true,
headers,
headers: new Headers(),
} as Response;
const handled = await handleResponseType(response);

Expand Down
3 changes: 1 addition & 2 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../../types';
* @param {object} response
* @returns {promise}
*/
export function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
export function handleResponseType(response: Response): Promise<[boolean, Blob | string] | void> {
if (response.headers) {
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/pdf')) {
Expand Down Expand Up @@ -52,7 +52,6 @@ class API {
credentials: 'same-origin',
...options,
})
// @ts-ignore
.then(handleResponseType)
.then(response => {
if (response[0]) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function callDetailPage(packageName: string, packageVersion?: strin
return packageMeta;
}

export function callSearch(value: string, signal: any) {
export function callSearch(value: string, signal: AbortSignal) {
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#Browser_compatibility
// FUTURE: signal is not well supported for IE and Samsung Browser
return API.request(`search/${encodeURIComponent(value)}`, 'GET', { signal, headers: {} });
Expand Down