mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-20 02:05:21 -04:00

* Add caching of user-data for 600 seconds * Make cache-entry interface commonly usable * Extract revision types * Remove revision-cache rule * Use seconds as cache-time interval (Date.now uses milliseconds) * Fix import error * Extract cache logic into common cache-class * Add cache class that was forgotten to commit in last commit * Start adding unit tests * Fix bug detected during unit-testing * Add unit tests for cache * Made entry-limit test more explicit * Renamed files to lower-case starting letter
18 lines
605 B
TypeScript
18 lines
605 B
TypeScript
import { Cache } from '../../components/common/cache/cache'
|
|
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
|
import { UserResponse } from './types'
|
|
|
|
const cache = new Cache<string, UserResponse>(600)
|
|
|
|
export const getUserById = async (userid: string): Promise<UserResponse> => {
|
|
if (cache.has(userid)) {
|
|
return cache.get(userid)
|
|
}
|
|
const response = await fetch(`${getApiUrl()}/users/${userid}`, {
|
|
...defaultFetchConfig
|
|
})
|
|
expectResponseCode(response)
|
|
const userData = (await response.json()) as UserResponse
|
|
cache.put(userid, userData)
|
|
return userData
|
|
}
|