Skip to content

Added the fetch api #12493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion Gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,16 @@ const es2017LibrarySourceMap = es2017LibrarySource.map(function(source) {
});

const hostsLibrarySources = ["dom.generated.d.ts", "webworker.importscripts.d.ts", "scripthost.d.ts"];
const es2015HostLibrarySources = ["dom.iterable.d.ts", "streams.d.ts", "fetch.d.ts"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not think this should be added to the es6 library. it is not related to the ES6 spec, it is an HTML standard.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should this be handled? dom.iterable.d.ts would still have to depend on iterables, which will depend on ES6 Symbol, which will depend on ES6 Object...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file should have /// references on all parts of the es2015 it depends on.


const librarySourceMap = [
// Host library
{ target: "lib.dom.d.ts", sources: ["header.d.ts", "dom.generated.d.ts"] },
{ target: "lib.dom.iterable.d.ts", sources: ["header.d.ts", "dom.iterable.d.ts"] },
{ target: "lib.webworker.d.ts", sources: ["header.d.ts", "webworker.generated.d.ts"] },
{ target: "lib.scripthost.d.ts", sources: ["header.d.ts", "scripthost.d.ts"] },
{ target: "lib.streams.d.ts", sources: ["header.d.ts", "streams.d.ts"] },
{ target: "lib.fetch.d.ts", sources: ["header.d.ts", "fetch.d.ts"] },

// JavaScript library
{ target: "lib.es5.d.ts", sources: ["header.d.ts", "es5.d.ts"] },
Expand All @@ -153,7 +156,7 @@ const librarySourceMap = [

// JavaScript + all host library
{ target: "lib.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(hostsLibrarySources) },
{ target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, "dom.iterable.d.ts") }
{ target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, es2015HostLibrarySources) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to add entries in commandlineparser.ts for the two new lib targets.

].concat(es2015LibrarySourceMap, es2016LibrarySourceMap, es2017LibrarySourceMap);

const libraryTargets = librarySourceMap.map(function(f) {
Expand Down
5 changes: 4 additions & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,16 @@ var es2017LibrarySourceMap = es2017LibrarySource.map(function (source) {
});

var hostsLibrarySources = ["dom.generated.d.ts", "webworker.importscripts.d.ts", "scripthost.d.ts"];
var es2015HostLibrarySources = ["dom.iterable.d.ts", "streams.d.ts", "fetch.d.ts"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.


var librarySourceMap = [
// Host library
{ target: "lib.dom.d.ts", sources: ["header.d.ts", "dom.generated.d.ts"] },
{ target: "lib.dom.iterable.d.ts", sources: ["header.d.ts", "dom.iterable.d.ts"] },
{ target: "lib.webworker.d.ts", sources: ["header.d.ts", "webworker.generated.d.ts"] },
{ target: "lib.scripthost.d.ts", sources: ["header.d.ts", "scripthost.d.ts"] },
{ target: "lib.streams.d.ts", sources: ["header.d.ts", "streams.d.ts"]},
{ target: "lib.fetch.d.ts", sources: ["header.d.ts", "fetch.d.ts"]},

// JavaScript library
{ target: "lib.es5.d.ts", sources: ["header.d.ts", "es5.d.ts"] },
Expand All @@ -328,7 +331,7 @@ var librarySourceMap = [

// JavaScript + all host library
{ target: "lib.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(hostsLibrarySources) },
{ target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, "dom.iterable.d.ts") }
{ target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, es2015HostLibrarySources) }
].concat(es2015LibrarySourceMap, es2016LibrarySourceMap, es2017LibrarySourceMap);

var libraryTargets = librarySourceMap.map(function (f) {
Expand Down
129 changes: 129 additions & 0 deletions src/lib/fetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/// The fetch API standard can be found at https://fetch.spec.whatwg.org/
/// Definitions by: Ryan Graham <https://github.com/ryan-codingintrigue>, Kagami Sascha Rosylight <https://github.com/saschanaz>, Robin Van den Broeck <https://github.com/gamesmaxed>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have not included any similar headers in the other lib files. please remove that.


/// <reference path="lib.streams.d.ts" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add a reference to lib.es2015.iterables

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and lib.es2015.promise


interface Window {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have always been curious why fetch has been under window - window.fetch(). Why not just make it so that fetch() works out of the box? Or might be could support both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prabirshrestha also works without window as #12493 (diff) has declare var fetch: typeof window.fetch; 🌹

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, fetch should be available in non window contexts like Service Workers.

fetch(url: RequestInfo, init?: RequestInit): Promise<Response>;
}
declare var fetch: typeof window.fetch;

declare type HeadersInit = Headers | string[][] | { [key: string]: string };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ([string, string])[] instead of string[][]?

I also checked and was slightly disappointed to see it cannot accept arbitrary Iterable<[string, string]> :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor

@mohsen1 mohsen1 Jan 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For autocomplete maybe we add list of known headers

type KnownHeaders = 'Accept' | 'Accept-Charset' //...

declare class Headers {
constructor(init?: HeadersInit);

append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | null;
has(name: string): boolean;
set(name: string, value: string): void;

// WebIDL pair iterator: iterable<ByteString, ByteString>
entries(): IterableIterator<[string, string]>;
forEach(callback: (value: string, index: number, headers: Headers) => void, thisArg?: any): void;
keys(): IterableIterator<string>;
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;
}

declare type BodyInit = Blob | ArrayBufferView | ArrayBuffer | FormData | URLSearchParams | string;
interface Body {
readonly bodyUsed: boolean;
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
json(): Promise<any>;
text(): Promise<string>;
}

declare type RequestInfo = Request | string;
declare class Request {
constructor(input: RequestInfo, init?: RequestInit);

readonly method: string;
readonly url: string;
readonly headers: Headers;

readonly type: RequestType
readonly destination: RequestDestination;
readonly referrer: string;
readonly referrerPolicy: ReferrerPolicy;
readonly mode: RequestMode;
readonly credentials: RequestCredentials;
readonly cache: RequestCache;
readonly redirect: RequestRedirect;
readonly integrity: string;
readonly keepalive: boolean;

clone(): Request;
}

interface Request extends Body { }
interface RequestInit {
method?: string;
headers?: HeadersInit;
body?: BodyInit;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
mode?: RequestMode;
credentials?: RequestCredentials;
cache?: RequestCache;
redirect?: RequestRedirect;
integrity?: string;
window?: any;
}

type RequestType = "" | "audio" | "font" | "image" | "script" | "style" | "track" | "video";
type RequestDestination = "" | "document" | "embed" | "font" | "image" | "manifest" | "media" | "object" | "report" | "script" | "serviceworker" | "sharedworker" | "style" | "worker" | "xslt";
type RequestMode = "navigate" | "same-origin" | "no-cors" | "cors";
type RequestCredentials = "omit" | "same-origin" | "include";
type RequestCache = "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached";
type RequestRedirect = "follow" | "error" | "manual";
type ReferrerPolicy = "" | "no-referrer" | "no-referrer-when-downgrade" | "same-origin" | "origin" | "strict-origin" | "origin-when-cross-origin" | "strict-origin-when-cross-origin" | "unsafe-url";

declare class Response {
constructor(body?: BodyInit, init?: ResponseInit);

static error(): Response;
static redirect(url: string, status?: number): Response;

readonly type: ResponseType;
readonly url: string;
readonly redirected: boolean;
readonly status: number;
readonly ok: boolean;
readonly statusText: string;
readonly headers: Headers;
// TODO: Readable stream implementation (I could not find the definitions of this type)
readonly body: ReadableStream | null;
readonly trailer: Promise<Headers>;

clone(): Response;
}
interface Response extends Body { }
interface ResponseInit {
status?: number;
statusText?: string;
headers?: HeadersInit;
}

type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect";

declare class URLSearchParams {
constructor(init?: string);

append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | void;
getAll(name: string): string[];
has(name: string): boolean;
set(name: string, value: string): void;

entries(): IterableIterator<[string, string]>;
forEach(callback: (value: string, index: number, params: URLSearchParams) => void, thisArg?: any): void;
keys(): IterableIterator<string>;
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[string, string]>;

// TODO: stringifier
}
131 changes: 131 additions & 0 deletions src/lib/streams.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/// Definitions by: Kagami Sascha Rosylight <https://github.com/saschanaz>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove header.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a reference to lib.es2015.promise


interface ReadableStreamSource {
start?(controller: ReadableStreamDefaultController): void | Promise<void>;
pull?(controller: ReadableStreamDefaultController): void | Promise<void>;
cancel?(reason: string): void | Promise<void>;
}

interface ReadableByteStreamSource {
start?(controller: ReadableByteStreamController): void | Promise<void>;
pull?(controller: ReadableByteStreamController): void | Promise<void>;
cancel?(reason: string): void | Promise<void>;

type: "bytes";
}

interface QueuingStrategy {
highWaterMark?: number;
size?(chunk: ArrayBufferView): number;
}

declare class ReadableStream {
constructor(underlyingSource?: ReadableStreamSource, strategy?: QueuingStrategy);
constructor(underlyingSource?: ReadableByteStreamSource, strategy?: QueuingStrategy);

locked: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly ?


cancel(reason: string): Promise<void>;
getReader(): ReadableStreamDefaultReader;
getReader({ mode }: { mode: "byob" }): ReadableStreamBYOBReader;
pipeThrough<T extends ReadableStream>({ writable, readable }: { writable: WritableStream, readable: T }): T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seem from the docs, this should take an additional options argument.

pipeTo(dest: WritableStream, { preventClose, preventAbort, preventCancel }: { preventClose?: boolean, preventAbort?: boolean, preventCancel?: boolean }): Promise<void>;
tee(): [ReadableStream, ReadableStream];
}

declare class ReadableStreamDefaultReader {
constructor(stream: ReadableStream);

closed: Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


cancel(reason: string): Promise<void>;
read(): Promise<IteratorResult<ArrayBufferView>>;
releaseLock(): void;
}

declare class ReadableStreamBYOBReader {
constructor(stream: ReadableStream);

closed: Promise<boolean>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


cancel(reason: string): Promise<void>;
read(view: ArrayBufferView): Promise<IteratorResult<ArrayBufferView>>;
releaseLock(): void;
}

declare class ReadableStreamDefaultController {
constructor(stream: ReadableStream, underlyingSource: ReadableStreamSource, size: number, highWaterMark: number);

desiredSize: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


close(): void;
enqueue(chunk: ArrayBufferView): number;
error(e: any): void;
}

declare class ReadableByteStreamController {
constructor(stream: ReadableStream, underlyingSource: ReadableStreamSource, highWaterMark: number);

byobRequest: ReadableStreamBYOBRequest;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly

desiredSize: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


close(): void;
enqueue(chunk: ArrayBufferView): number;
error(e: any): void;
}

declare class ReadableStreamBYOBRequest {
constructor(controller: ReadableByteStreamController, view: ArrayBufferView);

view: ArrayBufferView;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


respond(bytesWritten: number): void;
respondWithNewView(view: ArrayBufferView): void;
}

interface WritableStreamSink {
start?(controller: WritableStreamDefaultController): void | Promise<void>;
write?(chunk: any): void | Promise<void>;
close?(): void | Promise<void>;
abort?(reason: string): void | Promise<void>;
}

declare class WritableStream {
constructor(underlyingSink?: WritableStreamSink, strategy?: QueuingStrategy);

locked: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


abort(reason: string): Promise<void>;
getWriter(): WritableStreamDefaultWriter;
}

declare class WritableStreamDefaultWriter {
constructor(stream: WritableStream);

closed: Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly

desiredSize: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly

ready: Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly


abort(reason: string): Promise<void>;
close(): Promise<void>;
releaseLock(): void;
write(chunk: any): Promise<void>;
}

declare class WritableStreamDefaultController {
constructor(stream: WritableStream, underlyingSink: WritableStreamSink, size: number, highWaterMark: number);

error(e: any): void;
}

declare class ByteLengthQueuingStrategy {
constructor({ highWaterMark }: { highWaterMark: number });

size(chunk: ArrayBufferView): number;
}

declare class CountQueuingStrategy {
constructor({ highWaterMark }: { highWaterMark: number });

size(): number; // 1;
}