MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / parseJsonBody

Function parseJsonBody

web/src/app/api/v1/_helpers.ts:34–83  ·  view source on GitHub ↗
(params: {
  req: NextRequest
  schema: ZodType<T>
  logger: Logger
  trackEvent: TrackEventFn
  validationErrorEvent: AnalyticsEvent
  userId?: string
})

Source from the content-addressed store, hash-verified

32 | { ok: false; response: NextResponse }
33
34export const parseJsonBody = async <T>(params: {
35 req: NextRequest
36 schema: ZodType<T>
37 logger: Logger
38 trackEvent: TrackEventFn
39 validationErrorEvent: AnalyticsEvent
40 userId?: string
41}): Promise<HandlerResult<T>> => {
42 const { req, schema, logger, trackEvent, validationErrorEvent, userId } =
43 params
44 const trackingUserId = userId ?? 'unknown'
45
46 let json: unknown
47 try {
48 json = await req.json()
49 } catch {
50 trackEvent({
51 event: validationErrorEvent,
52 userId: trackingUserId,
53 properties: { error: 'Invalid JSON' },
54 logger,
55 })
56 return {
57 ok: false,
58 response: NextResponse.json(
59 { error: 'Invalid JSON in request body' },
60 { status: 400 },
61 ),
62 }
63 }
64
65 const parsed = schema.safeParse(json)
66 if (!parsed.success) {
67 trackEvent({
68 event: validationErrorEvent,
69 userId: trackingUserId,
70 properties: { issues: parsed.error.format() },
71 logger,
72 })
73 return {
74 ok: false,
75 response: NextResponse.json(
76 { error: 'Invalid request body', details: parsed.error.format() },
77 { status: 400 },
78 ),
79 }
80 }
81
82 return { ok: true, data: parsed.data }
83}
84
85export const requireUserFromApiKey = async (params: {
86 req: NextRequest

Callers 5

postGravityIndexFunction · 0.90
postTokenCountFunction · 0.90
postFeedbackFunction · 0.90
postWebSearchFunction · 0.90
postDocsSearchFunction · 0.90

Calls 1

trackEventFunction · 0.50

Tested by

no test coverage detected