diff --git a/src/event/_polyfills.ts b/src/event/_polyfills.ts new file mode 100644 index 00000000..a8e7dc6b --- /dev/null +++ b/src/event/_polyfills.ts @@ -0,0 +1,11 @@ +/** + * @deprecated Please use native web Headers + * https://developer.mozilla.org/en-US/docs/Web/API/Headers + */ +export const H3Headers = globalThis.Headers; + +/** + * @deprecated Please use native web Headers + * https://developer.mozilla.org/en-US/docs/Web/API/Headers + */ +export const H3Response = globalThis.Response; diff --git a/src/event/index.ts b/src/event/index.ts index 798f32de..68571eb1 100644 --- a/src/event/index.ts +++ b/src/event/index.ts @@ -1,6 +1,3 @@ export * from "./event"; export * from "./utils"; - -// TODO: Drop in next major version -export * from "./polyfills/headers"; -export * from "./polyfills/response"; +export * from "./_polyfills"; diff --git a/src/event/polyfills/headers.ts b/src/event/polyfills/headers.ts deleted file mode 100644 index 7a18681d..00000000 --- a/src/event/polyfills/headers.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * @deprecated Please use native web Headers - * https://developer.mozilla.org/en-US/docs/Web/API/Headers - */ -export class H3Headers implements Headers { - _headers: Record; - - constructor(init?: HeadersInit) { - if (!init) { - this._headers = {}; - } else if (Array.isArray(init)) { - this._headers = Object.fromEntries( - init.map(([key, value]) => [key.toLowerCase(), value]), - ); - } else if (init && "append" in init) { - this._headers = Object.fromEntries((init as any).entries()); - } else { - this._headers = Object.fromEntries( - Object.entries(init).map(([key, value]) => [key.toLowerCase(), value]), - ); - } - } - - [Symbol.iterator](): IterableIterator<[string, string]> { - return this.entries(); - } - - entries(): IterableIterator<[string, string]> { - throw Object.entries(this._headers)[Symbol.iterator](); - } - - keys(): IterableIterator { - return Object.keys(this._headers)[Symbol.iterator](); - } - - values(): IterableIterator { - throw Object.values(this._headers)[Symbol.iterator](); - } - - append(name: string, value: string): void { - const _name = name.toLowerCase(); - this.set(_name, [this.get(_name), value].filter(Boolean).join(", ")); - } - - delete(name: string): void { - delete this._headers[name.toLowerCase()]; - } - - get(name: string): string | null { - return this._headers[name.toLowerCase()]; - } - - has(name: string): boolean { - return name.toLowerCase() in this._headers; - } - - set(name: string, value: string): void { - this._headers[name.toLowerCase()] = String(value); - } - - forEach( - callbackfn: (value: string, key: string, parent: Headers) => void, - ): void { - for (const [key, value] of Object.entries(this._headers)) { - callbackfn(value, key, this); - } - } -} diff --git a/src/event/polyfills/response.ts b/src/event/polyfills/response.ts deleted file mode 100644 index 2e366a71..00000000 --- a/src/event/polyfills/response.ts +++ /dev/null @@ -1,63 +0,0 @@ -import type { EventHandlerResponse } from "../../types"; -import { H3Headers } from "./headers"; - -/** - * @deprecated Please use native web Response - * https://developer.mozilla.org/en-US/docs/Web/API/Response - */ -export class H3Response implements Response { - readonly headers: H3Headers; - readonly status: number; - readonly statusText: string; - readonly redirected: boolean; - readonly ok: boolean; - readonly url: string; - _body: string | ArrayBuffer | Uint8Array; - - // TODO: yet to implement - readonly body: ReadableStream | null = null; - readonly type: ResponseType = "default"; - readonly bodyUsed = false; - - constructor( - body: BodyInit | EventHandlerResponse | null = null, - init: ResponseInit = {}, - ) { - this.headers = new H3Headers(init.headers); - this.status = init.status ?? 200; - this.statusText = init.statusText || ""; - this.redirected = - !!init.status && [301, 302, 307, 308].includes(init.status); - this._body = body; - this.url = ""; - this.ok = this.status < 300 && this.status > 199; - } - - clone(): H3Response { - return new H3Response(this.body, { - headers: this.headers, - status: this.status, - statusText: this.statusText, - }); - } - - arrayBuffer(): Promise { - return Promise.resolve(this._body as unknown as ArrayBuffer); - } - - blob(): Promise { - return Promise.resolve(this._body as unknown as Blob); - } - - formData(): Promise { - return Promise.resolve(this._body as unknown as FormData); - } - - json(): Promise { - return Promise.resolve(this._body as unknown as T); - } - - text(): Promise { - return Promise.resolve(this._body as unknown as string); - } -}