diff --git a/src/request.ts b/src/request.ts index 8fd890e..4d41ca2 100644 --- a/src/request.ts +++ b/src/request.ts @@ -22,6 +22,16 @@ export const toRequestError = (e: unknown): RequestError => { export const GlobalRequest = global.Request export class Request extends GlobalRequest { + static [Symbol.hasInstance](this: unknown, instance: unknown): boolean { + if (this === Request) { + return ( + instance instanceof GlobalRequest || + Function.prototype[Symbol.hasInstance].call(this, instance) + ) + } + return Function.prototype[Symbol.hasInstance].call(this, instance) + } + constructor(input: string | Request, options?: RequestInit) { if (typeof input === 'object' && getRequestCache in input) { // Match native Request behavior: diff --git a/src/response.ts b/src/response.ts index 2ba92cd..2e45aff 100644 --- a/src/response.ts +++ b/src/response.ts @@ -22,7 +22,17 @@ interface LightResponse { export const GlobalResponse = global.Response export class Response { #body?: BodyInit | null - #init?: ResponseInit; + #init?: ResponseInit + + static [Symbol.hasInstance](this: unknown, instance: unknown): boolean { + if (this === Response) { + return ( + instance instanceof GlobalResponse || + Function.prototype[Symbol.hasInstance].call(this, instance) + ) + } + return Function.prototype[Symbol.hasInstance].call(this, instance) + } [getResponseCache](): globalThis.Response { // If `cacheKey` has been populated with a live `Headers` instance, the @@ -54,7 +64,7 @@ export class Response { this[getResponseCache]() return } - this.#init = init instanceof Response ? init.#init : init + this.#init = #init in init ? init.#init : init headers = new Headers(init.headers) } else { this.#init = init diff --git a/test/request.test.ts b/test/request.test.ts index 5ce7e95..2e38bd9 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -1124,6 +1124,17 @@ describe('Request', () => { expect(req).toBeInstanceOf(GlobalRequest) }) + it('should allow native GlobalRequest to be instance of Request', () => { + const globalReq = new GlobalRequest('http://localhost/') + expect(globalReq).toBeInstanceOf(Request) + + class NextRequest extends Request {} + expect(globalReq).not.toBeInstanceOf(NextRequest) + expect(new Request('http://localhost/')).not.toBeInstanceOf(NextRequest) + expect(new NextRequest('http://localhost/')).toBeInstanceOf(NextRequest) + expect(new NextRequest('http://localhost/')).toBeInstanceOf(Request) + }) + it('should be success to create instance from old light weight instance', async () => { const req = newRequest({ method: 'GET', diff --git a/test/response.test.ts b/test/response.test.ts index 4883542..5c7785e 100644 --- a/test/response.test.ts +++ b/test/response.test.ts @@ -55,10 +55,12 @@ describe('Response', () => { // response prototype chain not changed expect(new Response()).toBeInstanceOf(GlobalResponse) + expect(new GlobalResponse()).toBeInstanceOf(Response) // `fetch()` and `Response` are not changed const fetchRes = await fetch(`http://localhost:${port}`) expect(new Response()).toBeInstanceOf(fetchRes.constructor) + expect(fetchRes).toBeInstanceOf(Response) const resJson = await fetchRes.json() expect(fetchRes.headers.get('content-type')).toEqual('application/json charset=UTF-8') expect(resJson).toEqual({ status: 'ok' }) @@ -76,6 +78,19 @@ describe('Response', () => { // support other class to extends from Response expect(new NextResponse()).toBeInstanceOf(Response) + expect(new GlobalResponse()).not.toBeInstanceOf(NextResponse) + expect(new Response()).not.toBeInstanceOf(NextResponse) + }) + + it('Should use a native Response from fetch() as init', async () => { + const fetchRes = await fetch(`http://localhost:${port}`) + const res = new Response('body', fetchRes) + expect(res.status).toBe(200) + expect(res.headers.get('content-type')).toBe('application/json charset=UTF-8') + expect(await res.text()).toBe('body') + + const created = new Response('body', new GlobalResponse(null, { status: 201 })) + expect(created.status).toBe(201) }) it('Should not lose header data', async () => {