File tree Expand file tree Collapse file tree 6 files changed +25
-15
lines changed
pages/o/[orgSlug]/[endpointId] Expand file tree Collapse file tree 6 files changed +25
-15
lines changed Original file line number Diff line number Diff line change @@ -105,17 +105,17 @@ const activeMessageHeaders = computed(() => {
105
105
106
106
const activeMessageBody = computed (() => {
107
107
if (! activeMessage .value ) return " " ;
108
- if (activeMessage .value .contentType === " application/json" ) {
109
- return JSON .stringify (JSON .parse (activeMessage .value .body ), null , 2 );
108
+ const split = activeMessage .value .contentType .split (" ;" )[0 ];
109
+ if (split === " application/json" ) {
110
+ return JSON .stringify (activeMessage .value .bodyJson , null , 2 );
110
111
}
111
- return activeMessage .value .body ;
112
+ return activeMessage .value .body as string ;
112
113
});
113
114
114
115
const activeMessageContentType = computed (() => {
115
116
if (! activeMessage .value ) return " " ;
116
- const type = activeMessage .value .contentType .startsWith (" application/" )
117
- ? activeMessage .value .contentType .split (" /" )[1 ]
118
- : activeMessage .value .contentType ;
117
+ const split = activeMessage .value .contentType .split (" ;" )[0 ];
118
+ const type = split .startsWith (" application/" ) ? split .split (" /" )[1 ] : split ;
119
119
return type ;
120
120
});
121
121
@@ -410,7 +410,7 @@ async function getMessageDeliveries() {
410
410
square
411
411
icon =" i-ph-clipboard"
412
412
variant =" soft"
413
- @click =" copy(activeMessage.body )"
413
+ @click =" copy(activeMessageBody )"
414
414
class =" active:ring-green-500 active:ring-2"
415
415
/>
416
416
</UTooltip >
Original file line number Diff line number Diff line change @@ -203,7 +203,8 @@ export const messages = pgTable("messages", {
203
203
endpointId : uuid ( "endpoint_id" ) . notNull ( ) ,
204
204
headers : json ( "headers" ) . notNull ( ) ,
205
205
method : varchar ( "method" ) . notNull ( ) ,
206
- body : varchar ( "body" ) . notNull ( ) ,
206
+ body : varchar ( "body" ) ,
207
+ bodyJson : json ( "body_json" ) ,
207
208
contentType : varchar ( "content_type" ) . notNull ( ) ,
208
209
response : json ( "response" )
209
210
. notNull ( )
Original file line number Diff line number Diff line change @@ -39,20 +39,23 @@ export default defineEventHandler(async (event) => {
39
39
const payloadBody = await readBody ( event ) ;
40
40
const requestHost = getRequestHost ( event ) ;
41
41
const requestMethod = event . node . req . method as string ;
42
- const contentType = getHeader ( event , "content-type" ) ;
43
- const body = JSON . stringify ( payloadBody ) ?? payloadBody . toString ( ) ;
42
+ const contentTypeUnparsed = getHeader ( event , "content-type" ) ;
44
43
if ( ! endpointResponse ) return sendNoContent ( event , 404 ) ;
45
44
if ( ! payloadBody || ! payloadHeaders ) return sendNoContent ( event , 400 ) ;
46
45
setResponseStatus ( event , endpointResponse . response . code ) ;
47
46
await send ( event , endpointResponse . response . content , "application/text" ) ;
48
47
48
+ // parse the content type
49
+ const contentType = contentTypeUnparsed ?. split ( ";" ) [ 0 ] || "application/json" ;
50
+ const isJson = isBodyJson ( contentType ) ;
51
+
49
52
// save the message to the database
50
53
const messageInsert = await db
51
54
. insert ( messages )
52
55
. values ( {
53
56
orgId : endpointResponse . orgId ,
54
57
headers : payloadHeaders ,
55
- body : body ,
58
+ ... ( isJson ? { bodyJson : payloadBody } : { body : payloadBody } ) ,
56
59
endpointId : endpointId ,
57
60
origin : requestHost ,
58
61
method : requestMethod ,
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ export const messageRouter = router({
28
28
headers : true ,
29
29
origin : true ,
30
30
body : true ,
31
+ bodyJson : true ,
31
32
response : true ,
32
33
createdAt : true ,
33
34
contentType : true ,
Original file line number Diff line number Diff line change
1
+ export function isBodyJson ( contentType : string ) {
2
+ const splitType = contentType . split ( ";" ) [ 0 ] ;
3
+ return splitType === "application/json" ;
4
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import type { FetchError } from "ofetch";
2
2
import { eq } from "drizzle-orm" ;
3
3
import { db } from "~/server/db" ;
4
4
import { endpoints , messageDeliveries , messages } from "~/server/db/schema" ;
5
+ import { isBodyJson } from "./contentType" ;
5
6
6
7
export async function sendMessageToDestinations (
7
8
endpointId : string ,
@@ -43,6 +44,7 @@ export async function sendMessageToDestinations(
43
44
id : true ,
44
45
headers : true ,
45
46
body : true ,
47
+ bodyJson : true ,
46
48
contentType : true ,
47
49
endpointId : true ,
48
50
method : true ,
@@ -54,10 +56,9 @@ export async function sendMessageToDestinations(
54
56
55
57
if ( ! message || message . orgId !== orgId ) return ;
56
58
const payloadHeaders = message . headers ;
57
- const body =
58
- message . contentType === "application/json"
59
- ? JSON . parse ( message . body )
60
- : message . body ;
59
+ const body = isBodyJson ( message . contentType )
60
+ ? ( message . bodyJson as JSON )
61
+ : ( message . body as string ) ;
61
62
if ( ! body || ! payloadHeaders ) return ;
62
63
63
64
// Handle Forwarding to the destinations
You can’t perform that action at this time.
0 commit comments