import {expectType, expectAssignable} from 'tsd'; import AbortController from 'abort-controller'; import Blob from 'fetch-blob'; import fetch, {Request, Response, Headers, Body, FetchError, AbortError} from '.'; import * as _fetch from '.'; async function run() { const getResponse = await fetch('https://bigfile.com/test.zip'); expectType(getResponse.ok); expectType(getResponse.size); expectType(getResponse.status); expectType(getResponse.statusText); expectType<() => Response>(getResponse.clone); // Test async iterator over body expectType(getResponse.body); if (getResponse.body) { for await (const data of getResponse.body) { expectType(data); } } // Test Buffer expectType(await getResponse.buffer()); // Test arrayBuffer expectType(await getResponse.arrayBuffer()); // Test JSON, returns unknown expectType(await getResponse.json()); // Headers iterable expectType(getResponse.headers); // Post try { const request = new Request('http://byjka.com/buka'); expectType(request.url); expectType(request.headers); const headers = new Headers({byaka: 'buke'}); expectType<(a: string, b: string) => void>(headers.append); expectType<(a: string) => string | null>(headers.get); expectType<(name: string, value: string) => void>(headers.set); expectType<(name: string) => void>(headers.delete); expectType<() => IterableIterator>(headers.keys); expectType<() => IterableIterator<[string, string]>>(headers.entries); expectType<() => IterableIterator<[string, string]>>(headers[Symbol.iterator]); const postResponse = await fetch(request, {method: 'POST', headers}); expectType(await postResponse.blob()); } catch (error: unknown) { if (error instanceof FetchError) { throw new TypeError(error.errno as string | undefined); } if (error instanceof AbortError) { throw error; } } // export * const wildResponse = await _fetch.default('https://google.com'); expectType(wildResponse.ok); expectType(wildResponse.size); expectType(wildResponse.status); expectType(wildResponse.statusText); expectType<() => Response>(wildResponse.clone); // Others const response = new Response(); expectType(response.url); expectAssignable(response); const abortController = new AbortController(); const request = new Request('url', {signal: abortController.signal}); expectAssignable(request); /* eslint-disable no-new */ new Headers({Header: 'value'}); // new Headers(['header', 'value']); // should not work new Headers([['header', 'value']]); new Headers(new Headers()); new Headers([ new Set(['a', '1']), ['b', '2'], new Map([['a', null], ['3', null]]).keys() ]); /* eslint-enable no-new */ } run().finally(() => { console.log('✅'); });