From 0f352905554977dc24e13330b095f65a08c81a8b Mon Sep 17 00:00:00 2001 From: Niklas Lindgren Date: Thu, 11 Apr 2019 11:05:47 +0100 Subject: [PATCH 1/4] style: prettier --- types/node-fetch/index.d.ts | 7 +- types/node-fetch/node-fetch-tests.ts | 131 +++++++++++++++------------ 2 files changed, 81 insertions(+), 57 deletions(-) diff --git a/types/node-fetch/index.d.ts b/types/node-fetch/index.d.ts index a480fb00e4..c7433ccd1e 100644 --- a/types/node-fetch/index.d.ts +++ b/types/node-fetch/index.d.ts @@ -174,7 +174,12 @@ export interface ResponseInit { export type HeadersInit = Headers | string[][] | { [key: string]: string }; // HeaderInit is exported to support backwards compatibility. See PR #34382 export type HeaderInit = HeadersInit; -export type BodyInit = ArrayBuffer | ArrayBufferView | NodeJS.ReadableStream | string | URLSearchParams; +export type BodyInit = + ArrayBuffer + | ArrayBufferView + | NodeJS.ReadableStream + | string + | URLSearchParams; export type RequestInfo = string | Request; declare function fetch( diff --git a/types/node-fetch/node-fetch-tests.ts b/types/node-fetch/node-fetch-tests.ts index 165ac24800..98cdf9acfb 100644 --- a/types/node-fetch/node-fetch-tests.ts +++ b/types/node-fetch/node-fetch-tests.ts @@ -1,81 +1,100 @@ -import fetch, { Headers, Request, RequestInit, Response, FetchError } from 'node-fetch'; +import fetch, { + Headers, + Request, + RequestInit, + Response, + FetchError +} from "node-fetch"; import { Agent } from "http"; function test_fetchUrlWithOptions() { - const headers = new Headers(); - headers.append("Content-Type", "application/json"); - const requestOptions: RequestInit = { - compress: true, - follow: 10, - headers, - method: "POST", - redirect: 'manual', - size: 100, - timeout: 5000, - }; - handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); + const headers = new Headers(); + headers.append("Content-Type", "application/json"); + const requestOptions: RequestInit = { + compress: true, + follow: 10, + headers, + method: "POST", + redirect: "manual", + size: 100, + timeout: 5000 + }; + handlePromise( + fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions) + ); } function test_fetchUrlWithHeadersObject() { - const requestOptions: RequestInit = { - headers: { - 'Content-Type': 'application/json' - }, - method: "POST", - }; - handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); + const requestOptions: RequestInit = { + headers: { + "Content-Type": "application/json" + }, + method: "POST" + }; + handlePromise( + fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions) + ); } function test_fetchUrl() { - handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php")); + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php")); } function test_fetchUrlArrayBuffer() { - handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php"), true); + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php"), true); } function test_fetchUrlWithRequestObject() { - const requestOptions: RequestInit = { - method: "POST", - headers: { - 'Content-Type': 'application/json' - } - }; - const request: Request = new Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); - const timeout: number = request.timeout; - const size: number = request.size; - const agent: Agent | undefined = request.agent; - const protocol: string = request.protocol; + const requestOptions: RequestInit = { + method: "POST", + headers: { + "Content-Type": "application/json" + } + }; + const request: Request = new Request( + "http://www.andlabs.net/html5/uCOR.php", + requestOptions + ); + const timeout: number = request.timeout; + const size: number = request.size; + const agent: Agent | undefined = request.agent; + const protocol: string = request.protocol; - handlePromise(fetch(request)); + handlePromise(fetch(request)); } function test_globalFetchVar() { - fetch('http://test.com', {}) - .then(response => { - // for test only - }); + fetch("http://test.com", {}).then(response => { + // for test only + }); } -function handlePromise(promise: Promise, isArrayBuffer: boolean = false) { - promise.then((response): Promise => { - if (response.type === 'basic') { - // for test only - } - if (isArrayBuffer) { - return response.arrayBuffer(); - } else { - return response.text(); - } - }).then((text: string | ArrayBuffer) => { - console.log(text); - }); +function handlePromise( + promise: Promise, + isArrayBuffer: boolean = false +) { + promise + .then( + (response): Promise => { + if (response.type === "basic") { + // for test only + } + if (isArrayBuffer) { + return response.arrayBuffer(); + } else { + return response.text(); + } + } + ) + .then((text: string | ArrayBuffer) => { + console.log(text); + }); } function test_headersRaw() { - const headers = new Headers(); - const myHeader = 'foo'; - headers.raw()[myHeader]; // $ExpectType string[] + const headers = new Headers(); + const myHeader = "foo"; + headers.raw()[myHeader]; // $ExpectType string[] } function test_isRedirect() { @@ -84,6 +103,6 @@ function test_isRedirect() { } function test_FetchError() { - new FetchError('message', 'type', 'systemError'); - new FetchError('message', 'type'); + new FetchError("message", "type", "systemError"); + new FetchError("message", "type"); } From 27b2c263b15c0f8a0837abf0906297b52f44bfd6 Mon Sep 17 00:00:00 2001 From: Niklas Lindgren Date: Thu, 11 Apr 2019 11:05:56 +0100 Subject: [PATCH 2/4] feat: type definition for the `Blob` constructor arguments --- types/node-fetch/index.d.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/node-fetch/index.d.ts b/types/node-fetch/index.d.ts index c7433ccd1e..b269e4d465 100644 --- a/types/node-fetch/index.d.ts +++ b/types/node-fetch/index.d.ts @@ -116,7 +116,15 @@ export class Headers implements Iterable<[string, string]> { [Symbol.iterator](): Iterator<[string, string]>; } +type BlobPart = ArrayBuffer | ArrayBufferView | Blob | string; + +interface BlobOptions { + type?: string; + endings?: "transparent" | "native"; +} + export class Blob { + constructor(blobParts?: BlobPart[], options?: BlobOptions); type: string; size: number; slice(start?: number, end?: number): Blob; From fc2189e2f3d0319838d4c1be82872434fb9e5495 Mon Sep 17 00:00:00 2001 From: Niklas Lindgren Date: Thu, 11 Apr 2019 11:25:54 +0100 Subject: [PATCH 3/4] test: Blob constructor --- types/node-fetch/node-fetch-tests.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/node-fetch/node-fetch-tests.ts b/types/node-fetch/node-fetch-tests.ts index 98cdf9acfb..92c5354c1e 100644 --- a/types/node-fetch/node-fetch-tests.ts +++ b/types/node-fetch/node-fetch-tests.ts @@ -1,4 +1,5 @@ import fetch, { + Blob, Headers, Request, RequestInit, @@ -106,3 +107,10 @@ function test_FetchError() { new FetchError("message", "type", "systemError"); new FetchError("message", "type"); } + +function test_Blob() { + new Blob(); + new Blob(["beep", "boop"]); + new Blob(["beep", "boop"], { endings: "native" }); + new Blob(["beep", "boop"], { type: "text/plain" }); +} From 255907fef757741b68838084fb1337c9429d2f93 Mon Sep 17 00:00:00 2001 From: Niklas Lindgren Date: Thu, 11 Apr 2019 11:35:02 +0100 Subject: [PATCH 4/4] fix: `type` and `size` are `readonly` properties --- types/node-fetch/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/node-fetch/index.d.ts b/types/node-fetch/index.d.ts index b269e4d465..78363bfb7d 100644 --- a/types/node-fetch/index.d.ts +++ b/types/node-fetch/index.d.ts @@ -125,8 +125,8 @@ interface BlobOptions { export class Blob { constructor(blobParts?: BlobPart[], options?: BlobOptions); - type: string; - size: number; + readonly type: string; + readonly size: number; slice(start?: number, end?: number): Blob; }