fix: api typings (#210)

This commit is contained in:
Thomas Klein 2019-10-22 22:31:39 +02:00 committed by Juan Picado @jotadeveloper
parent 5c06ace14a
commit 5d6ad3d783
4 changed files with 4 additions and 16 deletions

View File

@ -139,7 +139,6 @@ export class Search extends Component<RouteComponentProps<{}>, State> {
*/ */
private handleFetchPackages: handleFetchPackages = async ({ value }) => { private handleFetchPackages: handleFetchPackages = async ({ value }) => {
try { try {
// @ts-ignore
const controller = new window.AbortController(); const controller = new window.AbortController();
const signal = controller.signal; const signal = controller.signal;
// Keep track of search requests. // Keep track of search requests.

View File

@ -3,22 +3,12 @@
import { handleResponseType } from '../../src/utils/api'; import { handleResponseType } from '../../src/utils/api';
describe('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', () => { describe('handleResponseType', () => {
test('should handle missing Content-Type', async () => { test('should handle missing Content-Type', async () => {
const response: Response = { const response: Response = {
url: 'http://localhost:8080/-/packages', url: 'http://localhost:8080/-/packages',
ok: false, ok: false,
// @ts-ignore headers: new Headers(),
headers: {
get: () => null,
} as Headers,
} as Response; } as Response;
const handled = await handleResponseType(response); const handled = await handleResponseType(response);
@ -34,7 +24,7 @@ describe('api', () => {
url: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz', url: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz',
blob: () => blobPromise, blob: () => blobPromise,
ok: true, ok: true,
headers, headers: new Headers(),
} as Response; } as Response;
const handled = await handleResponseType(response); const handled = await handleResponseType(response);

View File

@ -6,7 +6,7 @@ import '../../types';
* @param {object} response * @param {object} response
* @returns {promise} * @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) { if (response.headers) {
const contentType = response.headers.get('Content-Type'); const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/pdf')) { if (contentType && contentType.includes('application/pdf')) {
@ -52,7 +52,6 @@ class API {
credentials: 'same-origin', credentials: 'same-origin',
...options, ...options,
}) })
// @ts-ignore
.then(handleResponseType) .then(handleResponseType)
.then(response => { .then(response => {
if (response[0]) { if (response[0]) {

View File

@ -12,7 +12,7 @@ export async function callDetailPage(packageName: string, packageVersion?: strin
return packageMeta; 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 // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#Browser_compatibility
// FUTURE: signal is not well supported for IE and Samsung Browser // FUTURE: signal is not well supported for IE and Samsung Browser
return API.request(`search/${encodeURIComponent(value)}`, 'GET', { signal, headers: {} }); return API.request(`search/${encodeURIComponent(value)}`, 'GET', { signal, headers: {} });