0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

dedup Headers types (#4736)

This commit is contained in:
Ryan Dahl 2020-04-13 22:46:23 -04:00 committed by GitHub
parent c915e4d77d
commit 360c05ffe7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 46 deletions

View file

@ -215,7 +215,7 @@ export const windowOrWorkerGlobalScopeProperties = {
EventTarget: nonEnumerable(eventTarget.EventTargetImpl),
URL: nonEnumerable(url.URLImpl),
URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParamsImpl),
Headers: nonEnumerable(headers.Headers),
Headers: nonEnumerable(headers.HeadersImpl),
FormData: nonEnumerable(formData.FormData),
TextEncoder: nonEnumerable(textEncoding.TextEncoder),
TextDecoder: nonEnumerable(textEncoding.TextDecoder),

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts";
import {
unitTest,
assert,
assertEquals,
assertStrContains,
} from "./test_util.ts";
const {
stringifyArgs,
// @ts-ignore TypeScript (as of 3.7) does not support indexing namespaces by symbol
@ -276,9 +281,9 @@ unitTest(function headerParamsArgumentsCheck(): void {
}
}
assertEquals(hasThrown, 2);
assertEquals(
assertStrContains(
errMsg,
`Headers.${method} requires at least 1 argument, but only 0 present`
`${method} requires at least 1 argument, but only 0 present`
);
});
@ -300,9 +305,9 @@ unitTest(function headerParamsArgumentsCheck(): void {
}
}
assertEquals(hasThrown, 2);
assertEquals(
assertStrContains(
errMsg,
`Headers.${method} requires at least 2 arguments, but only 0 present`
`${method} requires at least 2 arguments, but only 0 present`
);
hasThrown = 0;
@ -320,9 +325,9 @@ unitTest(function headerParamsArgumentsCheck(): void {
}
}
assertEquals(hasThrown, 2);
assertEquals(
assertStrContains(
errMsg,
`Headers.${method} requires at least 2 arguments, but only 1 present`
`${method} requires at least 2 arguments, but only 1 present`
);
});
});

View file

@ -1,12 +1,9 @@
import * as formData from "./form_data.ts";
import * as blob from "./blob.ts";
import * as encoding from "./text_encoding.ts";
import * as headers from "./headers.ts";
import * as domTypes from "./dom_types.d.ts";
import { ReadableStream } from "./streams/mod.ts";
const { Headers } = headers;
// only namespace imports work for now, plucking out what we need
const { FormData } = formData;
const { TextEncoder, TextDecoder } = encoding;

View file

@ -17,11 +17,6 @@ and limitations under the License.
/* eslint-disable @typescript-eslint/no-explicit-any */
export type HeadersInit =
| Headers
| Array<[string, string]>
| Record<string, string>;
type BodyInit =
| Blob
| BufferSource
@ -513,23 +508,6 @@ export interface QueuingStrategySizeCallback<T = any> {
(chunk: T): number;
}
export 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;
forEach(
callbackfn: (value: string, key: string, parent: this) => void,
thisArg?: any
): void;
[Symbol.iterator](): IterableIterator<[string, string]>;
entries(): IterableIterator<[string, string]>;
keys(): IterableIterator<string>;
values(): IterableIterator<string>;
}
type RequestCache =
| "default"
| "no-store"

View file

@ -4,7 +4,6 @@ import { isTypedArray } from "./util.ts";
import * as domTypes from "./dom_types.d.ts";
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob.ts";
import { Headers } from "./headers.ts";
import * as io from "../io.ts";
import { read } from "../ops/io.ts";
import { close } from "../ops/resources.ts";
@ -277,8 +276,8 @@ class Body
export class Response implements domTypes.Response {
readonly type: domTypes.ResponseType;
readonly redirected: boolean;
headers: domTypes.Headers;
readonly trailer: Promise<domTypes.Headers>;
headers: Headers;
readonly trailer: Promise<Headers>;
readonly body: Body | null;
constructor(
@ -467,7 +466,7 @@ export class Response implements domTypes.Response {
function sendFetchReq(
url: string,
method: string | null,
headers: domTypes.Headers | null,
headers: Headers | null,
body: ArrayBufferView | undefined
): Promise<FetchResponse> {
let headerArray: Array<[string, string]> = [];
@ -490,7 +489,7 @@ export async function fetch(
): Promise<Response> {
let url: string;
let method: string | null = null;
let headers: domTypes.Headers | null = null;
let headers: Headers | null = null;
let body: ArrayBufferView | undefined;
let redirected = false;
let remRedirectCount = 20; // TODO: use a better way to handle

View file

@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.d.ts";
import { DomIterableMixin } from "./dom_iterable.ts";
import { requiredArguments } from "./util.ts";
import { customInspect } from "./console.ts";
@ -10,7 +9,7 @@ const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isHeaders(value: any): value is domTypes.Headers {
function isHeaders(value: any): value is Headers {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return value instanceof Headers;
}
@ -44,7 +43,7 @@ function validateValue(value: string): void {
class HeadersBase {
[headerMap]: Map<string, string>;
constructor(init?: domTypes.HeadersInit) {
constructor(init?: HeadersInit) {
if (init === null) {
throw new TypeError(
"Failed to construct 'Headers'; The provided value was not valid"
@ -145,7 +144,7 @@ class HeadersBase {
}
// @internal
export class Headers extends DomIterableMixin<
export class HeadersImpl extends DomIterableMixin<
string,
string,
typeof HeadersBase

View file

@ -1,10 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as headers from "./headers.ts";
import * as body from "./body.ts";
import * as domTypes from "./dom_types.d.ts";
import * as streams from "./streams/mod.ts";
const { Headers } = headers;
const { ReadableStream } = streams;
function byteUpperCase(s: string): string {
@ -32,7 +30,7 @@ export class Request extends body.Body implements domTypes.Request {
public method: string;
public url: string;
public credentials?: "omit" | "same-origin" | "include";
public headers: domTypes.Headers;
public headers: Headers;
constructor(input: domTypes.RequestInfo, init?: domTypes.RequestInit) {
if (arguments.length < 1) {
@ -62,7 +60,7 @@ export class Request extends body.Body implements domTypes.Request {
b = "";
}
let headers: domTypes.Headers;
let headers: Headers;
// prefer headers from init
if (init.headers) {