(params: {
req: NextRequest
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
logger: Logger
loggerWithContext: LoggerWithContextFn
trackEvent: TrackEventFn
getUserUsageData: GetUserUsageDataFn
consumeCreditsWithFallback: ConsumeCreditsWithFallbackFn
fetch: typeof globalThis.fetch
ensureSubscriberBlockGrant?: (params: { userId: string; logger: Logger }) => Promise<BlockGrantResult | null>
})
| 31 | }) |
| 32 | |
| 33 | export async function postDocsSearch(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 | ensureSubscriberBlockGrant?: (params: { userId: string; logger: Logger }) => Promise<BlockGrantResult | null> |
| 43 | }) { |
| 44 | const { |
| 45 | req, |
| 46 | getUserInfoFromApiKey, |
| 47 | loggerWithContext, |
| 48 | trackEvent, |
| 49 | getUserUsageData, |
| 50 | consumeCreditsWithFallback, |
| 51 | fetch, |
| 52 | ensureSubscriberBlockGrant, |
| 53 | } = params |
| 54 | const baseLogger = params.logger |
| 55 | |
| 56 | const parsedBody = await parseJsonBody({ |
| 57 | req, |
| 58 | schema: bodySchema, |
| 59 | logger: baseLogger, |
| 60 | trackEvent, |
| 61 | validationErrorEvent: AnalyticsEvent.DOCS_SEARCH_VALIDATION_ERROR, |
| 62 | }) |
| 63 | if (!parsedBody.ok) return parsedBody.response |
| 64 | |
| 65 | const { libraryTitle, topic, maxTokens, repoUrl } = parsedBody.data |
| 66 | |
| 67 | const authed = await requireUserFromApiKey({ |
| 68 | req, |
| 69 | getUserInfoFromApiKey, |
| 70 | logger: baseLogger, |
| 71 | loggerWithContext, |
| 72 | trackEvent, |
| 73 | authErrorEvent: AnalyticsEvent.DOCS_SEARCH_AUTH_ERROR, |
| 74 | }) |
| 75 | if (!authed.ok) return authed.response |
| 76 | |
| 77 | const { userId, logger } = authed.data |
| 78 | |
| 79 | // Track request |
| 80 | trackEvent({ |
| 81 | event: AnalyticsEvent.DOCS_SEARCH_REQUEST, |
| 82 | userId, |
| 83 | properties: { libraryTitle, hasTopic: !!topic, hasRepoUrl: !!repoUrl }, |
| 84 | logger, |
| 85 | }) |
| 86 | |
| 87 | // Temporarily free - charge 0 credits |
| 88 | const creditsToCharge = 0 |
| 89 | |
| 90 | const credits = await checkCreditsAndCharge({ |
no test coverage detected