Skip to content

Commit 261606e

Browse files
committed
perf: export createResponseProxy function
1 parent 757a21d commit 261606e

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export { createRequest } from './create-request.js'
22
export { Keq } from './keq.js'
33
export { request } from './request.js'
4+
45
export { composeMiddleware } from './util/compose-middleware.js'
56
export { composeRoute } from './util/compose-route.js'
7+
export { createResponseProxy } from './util/create-response-proxy.js'
68

79
export type { KeqContext, KeqContextOptions } from './types/keq-context.js'
810
export type { KeqMiddleware } from './types/keq-middleware.js'
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createResponseProxy } from '~/util/create-response-proxy.js'
12
import type { KeqMiddleware } from '../types/keq-middleware.js'
23

34

@@ -7,36 +8,6 @@ export function proxyResponseMiddleware(): KeqMiddleware {
78

89
const res = ctx.res
910

10-
if (res) {
11-
ctx.response = new Proxy(res, {
12-
get(res, prop) {
13-
if (typeof prop === 'string') {
14-
if (['json', 'text', 'arrayBuffer', 'blob', 'buffer', 'formData'].includes(prop)) {
15-
/**
16-
* clone when invoking body, json, text, arrayBuffer, blob, buffer, formData
17-
* to avoid time-consuming cloning
18-
*/
19-
return new Proxy(res[prop], {
20-
apply(target, thisArg, argArray) {
21-
const mirror = res.clone()
22-
return mirror[prop](...argArray)
23-
},
24-
})
25-
}
26-
27-
if (prop === 'body') {
28-
const mirror = res.clone()
29-
return mirror.body
30-
}
31-
}
32-
33-
if (typeof res[prop] === 'function') {
34-
return res[prop].bind(res)
35-
}
36-
37-
return res[prop]
38-
},
39-
})
40-
}
11+
if (res) ctx.response = createResponseProxy(res)
4112
}
4213
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test } from '@jest/globals'
2+
import { createResponseProxy } from './create-response-proxy'
3+
4+
5+
test('createResponseProxy(new Response())', async () => {
6+
const res = new Response('hello world')
7+
const proxy = createResponseProxy(res)
8+
9+
expect(proxy).toBeInstanceOf(Response)
10+
expect(proxy).not.toBe(res)
11+
expect(await proxy.text()).toBe('hello world')
12+
})

src/util/create-response-proxy.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export function createResponseProxy(res: Response): Response {
2+
return new Proxy(res, {
3+
get(res, prop) {
4+
if (typeof prop === 'string') {
5+
if (['json', 'text', 'arrayBuffer', 'blob', 'buffer', 'formData'].includes(prop)) {
6+
/**
7+
* clone when invoking body, json, text, arrayBuffer, blob, buffer, formData
8+
* to avoid time-consuming cloning
9+
*/
10+
return new Proxy(res[prop], {
11+
apply(target, thisArg, argArray) {
12+
const mirror = res.clone()
13+
return mirror[prop](...argArray)
14+
},
15+
})
16+
}
17+
18+
if (prop === 'body') {
19+
const mirror = res.clone()
20+
return mirror.body
21+
}
22+
}
23+
24+
if (typeof res[prop] === 'function') {
25+
return res[prop].bind(res)
26+
}
27+
28+
return res[prop]
29+
},
30+
})
31+
}

0 commit comments

Comments
 (0)