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

Commit

Permalink
fix: api typings (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkn authored and juanpicado committed Oct 22, 2019
1 parent 5c06ace commit 5d6ad3d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/components/Search/Search.tsx
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
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
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
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

0 comments on commit 5d6ad3d

Please sign in to comment.