File tree Expand file tree Collapse file tree 1 file changed +21
-9
lines changed Expand file tree Collapse file tree 1 file changed +21
-9
lines changed Original file line number Diff line number Diff line change @@ -28,20 +28,32 @@ It's recommended to use transform for the following:
28
28
Below is an example of using mapResponse to provide Response compression.
29
29
30
30
``` typescript
31
- import { Elysia , mapResponse } from ' elysia'
32
- import { gzipSync } from ' bun'
31
+ import { Elysia } from ' elysia'
33
32
34
33
new Elysia ()
35
- .mapResponse (({ response }) => {
34
+ .mapResponse (({ response , set }) => {
35
+ const isJson = typeof response === ' object'
36
+
37
+ const text = isJson
38
+ ? JSON .stringify (response )
39
+ : response ?.toString () ?? ' '
40
+
41
+ set .headers [' Content-Encoding' ] = ' gzip'
42
+
36
43
return new Response (
37
- gzipSync (
38
- typeof response === ' object'
39
- ? JSON .stringify (response )
40
- : response .toString ()
41
- )
44
+ Bun .gzipSync (new TextEncoder (' utf-8' ).encode (text )),
45
+ {
46
+ headers: {
47
+ ' Content-Type' : ` ${
48
+ isJson ? ' application/json' : ' text/plain'
49
+ }; charset=utf-8 `
50
+ }
51
+ }
42
52
)
43
53
})
44
- .listen (3000 )
54
+ .get (' /text' , () => ' mapResponse' )
55
+ .get (' /json' , () => ({ map: ' response' }))
56
+ .listen (8080 )
45
57
```
46
58
47
59
Like ** parse** and ** beforeHandle** , after a value is returned, the next iteration of afterHandle will be skipped.
You can’t perform that action at this time.
0 commit comments