(params: {
req: NextRequest
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
logger: Logger
trackEvent: TrackEventFn
})
| 12 | type ValidField = (typeof VALID_USER_INFO_FIELDS)[number] |
| 13 | |
| 14 | export async function getMe(params: { |
| 15 | req: NextRequest |
| 16 | getUserInfoFromApiKey: GetUserInfoFromApiKeyFn |
| 17 | logger: Logger |
| 18 | trackEvent: TrackEventFn |
| 19 | }) { |
| 20 | const { req, getUserInfoFromApiKey, logger, trackEvent } = params |
| 21 | |
| 22 | const apiKey = extractApiKeyFromHeader(req) |
| 23 | |
| 24 | if (!apiKey) { |
| 25 | return NextResponse.json( |
| 26 | { error: 'Missing or invalid Authorization header' }, |
| 27 | { status: 401 }, |
| 28 | ) |
| 29 | } |
| 30 | |
| 31 | // Parse fields from query parameter |
| 32 | const fieldsParam = req.nextUrl.searchParams.get('fields') |
| 33 | let fields: ValidField[] |
| 34 | if (fieldsParam !== null) { |
| 35 | const requestedFields = fieldsParam |
| 36 | .split(',') |
| 37 | .map((f) => f.trim()) |
| 38 | .filter((f) => f.length > 0) |
| 39 | |
| 40 | // Check if we have any fields after filtering |
| 41 | if (requestedFields.length === 0) { |
| 42 | return NextResponse.json( |
| 43 | { |
| 44 | error: `Invalid fields: empty. Valid fields are: ${VALID_USER_INFO_FIELDS.join(', ')}`, |
| 45 | }, |
| 46 | { status: 400 }, |
| 47 | ) |
| 48 | } |
| 49 | |
| 50 | // Validate that all requested fields are valid |
| 51 | const invalidFields = requestedFields.filter( |
| 52 | (f) => !VALID_USER_INFO_FIELDS.includes(f as ValidField), |
| 53 | ) |
| 54 | if (invalidFields.length > 0) { |
| 55 | trackEvent({ |
| 56 | event: AnalyticsEvent.ME_VALIDATION_ERROR, |
| 57 | userId: 'unknown', |
| 58 | properties: { |
| 59 | invalidFields, |
| 60 | requestedFields, |
| 61 | }, |
| 62 | logger, |
| 63 | }) |
| 64 | return NextResponse.json( |
| 65 | { |
| 66 | error: `Invalid fields: ${invalidFields.join(', ')}. Valid fields are: ${VALID_USER_INFO_FIELDS.join(', ')}`, |
| 67 | }, |
| 68 | { status: 400 }, |
| 69 | ) |
| 70 | } |
| 71 | fields = requestedFields as ValidField[] |
no test coverage detected