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

Function postWebSearch

web/src/app/api/v1/web-search/_post.ts:33–162  ·  view source on GitHub ↗
(params: {
  req: NextRequest
  getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
  logger: Logger
  loggerWithContext: LoggerWithContextFn
  trackEvent: TrackEventFn
  getUserUsageData: GetUserUsageDataFn
  consumeCreditsWithFallback: ConsumeCreditsWithFallbackFn
  fetch: typeof globalThis.fetch
  serverEnv: SerperEnv
  ensureSubscriberBlockGrant?: (params: {
    userId: string
    logger: Logger
  }) => Promise<BlockGrantResult | null>
})

Source from the content-addressed store, hash-verified

31})
32
33export async function postWebSearch(params: {
34 req: NextRequest
35 getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
36 logger: Logger
37 loggerWithContext: LoggerWithContextFn
38 trackEvent: TrackEventFn
39 getUserUsageData: GetUserUsageDataFn
40 consumeCreditsWithFallback: ConsumeCreditsWithFallbackFn
41 fetch: typeof globalThis.fetch
42 serverEnv: SerperEnv
43 ensureSubscriberBlockGrant?: (params: {
44 userId: string
45 logger: Logger
46 }) => Promise<BlockGrantResult | null>
47}) {
48 const {
49 req,
50 getUserInfoFromApiKey,
51 loggerWithContext,
52 trackEvent,
53 getUserUsageData,
54 consumeCreditsWithFallback,
55 fetch,
56 serverEnv,
57 ensureSubscriberBlockGrant,
58 } = params
59 const baseLogger = params.logger
60
61 const parsedBody = await parseJsonBody({
62 req,
63 schema: bodySchema,
64 logger: baseLogger,
65 trackEvent,
66 validationErrorEvent: AnalyticsEvent.WEB_SEARCH_VALIDATION_ERROR,
67 })
68 if (!parsedBody.ok) return parsedBody.response
69
70 const { query, depth, repoUrl } = parsedBody.data
71
72 const authed = await requireUserFromApiKey({
73 req,
74 getUserInfoFromApiKey,
75 logger: baseLogger,
76 loggerWithContext,
77 trackEvent,
78 authErrorEvent: AnalyticsEvent.WEB_SEARCH_AUTH_ERROR,
79 })
80 if (!authed.ok) return authed.response
81
82 const { userId, logger } = authed.data
83
84 // Track request
85 trackEvent({
86 event: AnalyticsEvent.WEB_SEARCH_REQUEST,
87 userId,
88 properties: { depth, hasRepoUrl: !!repoUrl },
89 logger,
90 })

Callers 2

POSTFunction · 0.90
web-search.test.tsFile · 0.90

Calls 6

parseJsonBodyFunction · 0.90
requireUserFromApiKeyFunction · 0.90
checkCreditsAndChargeFunction · 0.90
sleepFunction · 0.90
searchWebFunction · 0.90
trackEventFunction · 0.50

Tested by

no test coverage detected