0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

fetch support URL instance as input (#3496)

This commit is contained in:
Axetroy 2019-12-14 20:49:30 +08:00 committed by Ry Dahl
parent 7e116dd70d
commit 83f95fb8df
3 changed files with 21 additions and 13 deletions

View file

@ -13,6 +13,7 @@ import * as io from "./io.ts";
import { read, close } from "./files.ts"; import { read, close } from "./files.ts";
import { Buffer } from "./buffer.ts"; import { Buffer } from "./buffer.ts";
import { FormData } from "./form_data.ts"; import { FormData } from "./form_data.ts";
import { URL } from "./url.ts";
import { URLSearchParams } from "./url_search_params.ts"; import { URLSearchParams } from "./url_search_params.ts";
import * as dispatch from "./dispatch.ts"; import * as dispatch from "./dispatch.ts";
import { sendAsync } from "./dispatch_json.ts"; import { sendAsync } from "./dispatch_json.ts";
@ -367,7 +368,7 @@ async function sendFetchReq(
/** Fetch a resource from the network. */ /** Fetch a resource from the network. */
export async function fetch( export async function fetch(
input: domTypes.Request | string, input: domTypes.Request | URL | string,
init?: domTypes.RequestInit init?: domTypes.RequestInit
): Promise<Response> { ): Promise<Response> {
let url: string; let url: string;
@ -377,8 +378,8 @@ export async function fetch(
let redirected = false; let redirected = false;
let remRedirectCount = 20; // TODO: use a better way to handle let remRedirectCount = 20; // TODO: use a better way to handle
if (typeof input === "string") { if (typeof input === "string" || input instanceof URL) {
url = input; url = typeof input === "string" ? (input as string) : (input as URL).href;
if (init != null) { if (init != null) {
method = init.method || null; method = init.method || null;
if (init.headers) { if (init.headers) {

View file

@ -42,6 +42,13 @@ testPerm({ net: true }, async function fetchUrl(): Promise<void> {
assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json"); assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json");
}); });
testPerm({ net: true }, async function fetchURL(): Promise<void> {
const response = await fetch(
new URL("http://localhost:4545/cli/tests/fixture.json")
);
assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json");
});
testPerm({ net: true }, async function fetchHeaders(): Promise<void> { testPerm({ net: true }, async function fetchHeaders(): Promise<void> {
const response = await fetch("http://localhost:4545/cli/tests/fixture.json"); const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
const headers = response.headers; const headers = response.headers;

View file

@ -2481,7 +2481,7 @@ declare namespace __fetch {
} }
/** Fetch a resource from the network. */ /** Fetch a resource from the network. */
export function fetch( export function fetch(
input: __domTypes.Request | string, input: __domTypes.Request | __url.URL | string,
init?: __domTypes.RequestInit init?: __domTypes.RequestInit
): Promise<Response>; ): Promise<Response>;
} }
@ -2658,11 +2658,7 @@ declare namespace __urlSearchParams {
declare namespace __url { declare namespace __url {
// @url js/url.d.ts // @url js/url.d.ts
export interface URL {
export class URL {
private _parts;
private _searchParams;
private _updateSearchParams;
hash: string; hash: string;
host: string; host: string;
hostname: string; hostname: string;
@ -2673,14 +2669,18 @@ declare namespace __url {
port: string; port: string;
protocol: string; protocol: string;
search: string; search: string;
username: string;
readonly searchParams: __urlSearchParams.URLSearchParams; readonly searchParams: __urlSearchParams.URLSearchParams;
constructor(url: string, base?: string | URL); username: string;
toString(): string; toString(): string;
toJSON(): 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 { declare namespace __workers {