0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00

feat: extend app.request paramters (#1442)

* feat: extend app.request paramters

* denoify
This commit is contained in:
hagishi 2023-09-12 08:31:41 +09:00 committed by GitHub
parent c3d984353a
commit 2a13b095c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 6 deletions

View File

@ -374,17 +374,22 @@ class Hono<
return this.dispatch(request, executionCtx, Env, request.method) return this.dispatch(request, executionCtx, Env, request.method)
} }
request = (input: Request | string | URL, requestInit?: RequestInit) => { request = (
input: Request | string | URL,
requestInit?: RequestInit,
Env?: E['Bindings'] | {},
executionCtx?: ExecutionContext
) => {
if (input instanceof Request) { if (input instanceof Request) {
if (requestInit !== undefined) { if (requestInit !== undefined) {
input = new Request(input, requestInit) input = new Request(input, requestInit)
} }
return this.fetch(input) return this.fetch(input, Env, executionCtx)
} }
input = input.toString() input = input.toString()
const path = /^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}` const path = /^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}`
const req = new Request(path, requestInit) const req = new Request(path, requestInit)
return this.fetch(req) return this.fetch(req, Env, executionCtx)
} }
fire = () => { fire = () => {

View File

@ -374,17 +374,22 @@ class Hono<
return this.dispatch(request, executionCtx, Env, request.method) return this.dispatch(request, executionCtx, Env, request.method)
} }
request = (input: Request | string | URL, requestInit?: RequestInit) => { request = (
input: Request | string | URL,
requestInit?: RequestInit,
Env?: E['Bindings'] | {},
executionCtx?: ExecutionContext
) => {
if (input instanceof Request) { if (input instanceof Request) {
if (requestInit !== undefined) { if (requestInit !== undefined) {
input = new Request(input, requestInit) input = new Request(input, requestInit)
} }
return this.fetch(input) return this.fetch(input, Env, executionCtx)
} }
input = input.toString() input = input.toString()
const path = /^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}` const path = /^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}`
const req = new Request(path, requestInit) const req = new Request(path, requestInit)
return this.fetch(req) return this.fetch(req, Env, executionCtx)
} }
fire = () => { fire = () => {

View File

@ -34,6 +34,10 @@ describe('GET Request', () => {
return c.html('<h1>Hono!!!</h1>') return c.html('<h1>Hono!!!</h1>')
}) })
app.get('/hello-env', (c) => {
return c.json(c.env)
})
it('GET http://localhost/hello is ok', async () => { it('GET http://localhost/hello is ok', async () => {
const res = await app.request('http://localhost/hello') const res = await app.request('http://localhost/hello')
expect(res).not.toBeNull() expect(res).not.toBeNull()
@ -77,6 +81,12 @@ describe('GET Request', () => {
expect(res).not.toBeNull() expect(res).not.toBeNull()
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
it('GET /hello-env is ok', async () => {
const res = await app.request('/hello-env', undefined, { HELLO: 'world' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ HELLO: 'world' })
})
}) })
describe('Register handlers without a path', () => { describe('Register handlers without a path', () => {