ruvector/studio/data/api-keys/temp-api-keys-utils.ts
rUv 814f595995 feat(studio): Add complete RuVector Studio application
Major additions:
- Complete Next.js studio application with 1600+ components
- Docker support (Dockerfile.combined, docker-compose.yml)
- GCP deployment documentation and benchmarks
- SQL benchmark scripts for performance testing
- Sentry integration for monitoring
- Comprehensive test suite and mocks

Studio features:
- Dashboard and admin interfaces
- Data visualization components
- Authentication and user management
- API integration with RuVector backend
- Static data and public assets

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-06 23:04:48 +00:00

54 lines
1.5 KiB
TypeScript

import { getTemporaryAPIKey } from './temp-api-keys-query'
type ProjectRef = string
const projectApiKeys = new Map<ProjectRef, Promise<TemporaryApiKey>>()
export interface TemporaryApiKey {
apiKey: string
expiryTimeMs: number
}
export function createTemporaryApiKey(apiKey: string, expiryInSeconds: number): TemporaryApiKey {
return {
apiKey,
expiryTimeMs: Date.now() + expiryInSeconds * 1000,
}
}
export function isTemporaryApiKeyValid(
key: TemporaryApiKey | null | undefined
): key is TemporaryApiKey {
if (!key) return false
const now = Date.now()
const timeRemaining = key.expiryTimeMs - now
return timeRemaining > 20_000 // More than 20 seconds remaining
}
const checkOrRefreshTemporaryApiKey = async (
projectRef: ProjectRef,
existingKey: Promise<TemporaryApiKey> | undefined
): Promise<TemporaryApiKey> => {
const resolvedKey = await existingKey
if (isTemporaryApiKeyValid(resolvedKey)) {
return resolvedKey
}
const expiryInSeconds = 60
const fetchedKey = getTemporaryAPIKey({
projectRef,
expiry: expiryInSeconds,
}).then((data) => createTemporaryApiKey(data.api_key, expiryInSeconds))
return fetchedKey
}
// This function should never be marked as async, it should always return a promise.
export function getOrRefreshTemporaryApiKey(projectRef: ProjectRef): Promise<TemporaryApiKey> {
const existingKey = projectApiKeys.get(projectRef)
const data = checkOrRefreshTemporaryApiKey(projectRef, existingKey)
projectApiKeys.set(projectRef, data)
return data
}