-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelpers.ts
More file actions
30 lines (24 loc) · 777 Bytes
/
Helpers.ts
File metadata and controls
30 lines (24 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import NodeCache from "node-cache";
// This file is full of things I don't want the library to export.
const cache = new NodeCache({
deleteOnExpire: true,
});
export async function GetOrAddToCache<T>(key: string, timeToLiveSeconds: number, addItemFunc: () => T | Promise<T>): Promise<T> {
let object = cache.get(key) as T;
if (!object) {
object = await addItemFunc();
cache.set(key, object, timeToLiveSeconds);
}
return object;
}
export function getHeader(req, headerName: string): string | null {
var header = req.headers[headerName];
return Array.isArray(header) ? header[0]: header;
}
export function throwError(error: Error, next: Function) {
if (next) {
next(error)
} else {
throw error;
}
}