(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>
})
| 31 | }) |
| 32 | |
| 33 | export 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 | }) |
no test coverage detected