Skip to content

Commit

Permalink
fetch support URL instance as input (denoland#3496)
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and ry committed Dec 14, 2019
1 parent 7e116dd commit 83f95fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
7 changes: 4 additions & 3 deletions cli/js/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as io from "./io.ts";
import { read, close } from "./files.ts";
import { Buffer } from "./buffer.ts";
import { FormData } from "./form_data.ts";
import { URL } from "./url.ts";
import { URLSearchParams } from "./url_search_params.ts";
import * as dispatch from "./dispatch.ts";
import { sendAsync } from "./dispatch_json.ts";
Expand Down Expand Up @@ -367,7 +368,7 @@ async function sendFetchReq(

/** Fetch a resource from the network. */
export async function fetch(
input: domTypes.Request | string,
input: domTypes.Request | URL | string,
init?: domTypes.RequestInit
): Promise<Response> {
let url: string;
Expand All @@ -377,8 +378,8 @@ export async function fetch(
let redirected = false;
let remRedirectCount = 20; // TODO: use a better way to handle

if (typeof input === "string") {
url = input;
if (typeof input === "string" || input instanceof URL) {
url = typeof input === "string" ? (input as string) : (input as URL).href;
if (init != null) {
method = init.method || null;
if (init.headers) {
Expand Down
7 changes: 7 additions & 0 deletions cli/js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ testPerm({ net: true }, async function fetchUrl(): Promise<void> {
assertEquals(response.url, "http:https://localhost:4545/cli/tests/fixture.json");
});

testPerm({ net: true }, async function fetchURL(): Promise<void> {
const response = await fetch(
new URL("http:https://localhost:4545/cli/tests/fixture.json")
);
assertEquals(response.url, "http:https://localhost:4545/cli/tests/fixture.json");
});

testPerm({ net: true }, async function fetchHeaders(): Promise<void> {
const response = await fetch("http:https://localhost:4545/cli/tests/fixture.json");
const headers = response.headers;
Expand Down
20 changes: 10 additions & 10 deletions cli/js/lib.deno_runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2481,7 +2481,7 @@ declare namespace __fetch {
}
/** Fetch a resource from the network. */
export function fetch(
input: __domTypes.Request | string,
input: __domTypes.Request | __url.URL | string,
init?: __domTypes.RequestInit
): Promise<Response>;
}
Expand Down Expand Up @@ -2658,11 +2658,7 @@ declare namespace __urlSearchParams {

declare namespace __url {
// @url js/url.d.ts

export class URL {
private _parts;
private _searchParams;
private _updateSearchParams;
export interface URL {
hash: string;
host: string;
hostname: string;
Expand All @@ -2673,14 +2669,18 @@ declare namespace __url {
port: string;
protocol: string;
search: string;
username: string;
readonly searchParams: __urlSearchParams.URLSearchParams;
constructor(url: string, base?: string | URL);
username: string;
toString(): string;
toJSON(): string;
static createObjectURL(b: __domTypes.Blob): string;
static revokeObjectURL(url: string): void;
}

export const URL: {
prototype: URL;
new (url: string, base?: string | URL): URL;
createObjectURL(object: __domTypes.Blob): string;
revokeObjectURL(url: string): void;
};
}

declare namespace __workers {
Expand Down

0 comments on commit 83f95fb

Please sign in to comment.