Skip to content

Commit 85742c3

Browse files
committed
fix json from stripe
1 parent ee4b509 commit 85742c3

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

pages/o/[orgSlug]/[endpointId]/index.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ const activeMessageHeaders = computed(() => {
105105
106106
const activeMessageBody = computed(() => {
107107
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);
110111
}
111-
return activeMessage.value.body;
112+
return activeMessage.value.body as string;
112113
});
113114
114115
const activeMessageContentType = computed(() => {
115116
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;
119119
return type;
120120
});
121121
@@ -410,7 +410,7 @@ async function getMessageDeliveries() {
410410
square
411411
icon="i-ph-clipboard"
412412
variant="soft"
413-
@click="copy(activeMessage.body)"
413+
@click="copy(activeMessageBody)"
414414
class="active:ring-green-500 active:ring-2"
415415
/>
416416
</UTooltip>

server/db/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ export const messages = pgTable("messages", {
203203
endpointId: uuid("endpoint_id").notNull(),
204204
headers: json("headers").notNull(),
205205
method: varchar("method").notNull(),
206-
body: varchar("body").notNull(),
206+
body: varchar("body"),
207+
bodyJson: json("body_json"),
207208
contentType: varchar("content_type").notNull(),
208209
response: json("response")
209210
.notNull()

server/routes/endpoint/[endpointId].ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,23 @@ export default defineEventHandler(async (event) => {
3939
const payloadBody = await readBody(event);
4040
const requestHost = getRequestHost(event);
4141
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");
4443
if (!endpointResponse) return sendNoContent(event, 404);
4544
if (!payloadBody || !payloadHeaders) return sendNoContent(event, 400);
4645
setResponseStatus(event, endpointResponse.response.code);
4746
await send(event, endpointResponse.response.content, "application/text");
4847

48+
// parse the content type
49+
const contentType = contentTypeUnparsed?.split(";")[0] || "application/json";
50+
const isJson = isBodyJson(contentType);
51+
4952
// save the message to the database
5053
const messageInsert = await db
5154
.insert(messages)
5255
.values({
5356
orgId: endpointResponse.orgId,
5457
headers: payloadHeaders,
55-
body: body,
58+
...(isJson ? { bodyJson: payloadBody } : { body: payloadBody }),
5659
endpointId: endpointId,
5760
origin: requestHost,
5861
method: requestMethod,

server/trpc/routers/messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const messageRouter = router({
2828
headers: true,
2929
origin: true,
3030
body: true,
31+
bodyJson: true,
3132
response: true,
3233
createdAt: true,
3334
contentType: true,

server/utils/contentType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function isBodyJson(contentType: string) {
2+
const splitType = contentType.split(";")[0];
3+
return splitType === "application/json";
4+
}

server/utils/destinationSender.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FetchError } from "ofetch";
22
import { eq } from "drizzle-orm";
33
import { db } from "~/server/db";
44
import { endpoints, messageDeliveries, messages } from "~/server/db/schema";
5+
import { isBodyJson } from "./contentType";
56

67
export async function sendMessageToDestinations(
78
endpointId: string,
@@ -43,6 +44,7 @@ export async function sendMessageToDestinations(
4344
id: true,
4445
headers: true,
4546
body: true,
47+
bodyJson: true,
4648
contentType: true,
4749
endpointId: true,
4850
method: true,
@@ -54,10 +56,9 @@ export async function sendMessageToDestinations(
5456

5557
if (!message || message.orgId !== orgId) return;
5658
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);
6162
if (!body || !payloadHeaders) return;
6263

6364
// Handle Forwarding to the destinations

0 commit comments

Comments
 (0)