@devmoods/express-extras 0.25.0 → 0.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/auth.js +2 -2
- package/dist/cache.d.ts +14 -0
- package/dist/cache.js +64 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/logger.d.ts +3 -1
- package/dist/logger.js +4 -2
- package/dist/middleware.js +3 -1
- package/package.json +4 -4
package/README.md
CHANGED
package/dist/auth.js
CHANGED
|
@@ -190,8 +190,8 @@ export function createAuth({ getUserByToken, filterUser = (x) => x, redis, provi
|
|
|
190
190
|
const token = createAuthToken(rest);
|
|
191
191
|
await redis.set(token, '1', { EX: TOKEN_EXPIRES_IN });
|
|
192
192
|
setAccessTokenCookie(res, token);
|
|
193
|
-
logger.info('refreshed token'
|
|
194
|
-
return { token };
|
|
193
|
+
logger.info('refreshed token');
|
|
194
|
+
return { token, user: filterUser(user) };
|
|
195
195
|
}));
|
|
196
196
|
for (const provider of providers) {
|
|
197
197
|
provider.setupRoutes(router, providerContext);
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type RedisClient } from './redis.js';
|
|
2
|
+
export interface CacheAdapter {
|
|
3
|
+
get(key: string): Promise<any>;
|
|
4
|
+
set(key: string, value: any, expires?: number | null | undefined): Promise<void>;
|
|
5
|
+
clear(key?: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function createRedisCacheAdapter(redis: RedisClient): CacheAdapter;
|
|
8
|
+
export declare function createMemoryCacheAdapter(): CacheAdapter;
|
|
9
|
+
export declare function createCache(adapter: CacheAdapter): {
|
|
10
|
+
set: (key: string, value: any, expires?: number | null | undefined) => Promise<void>;
|
|
11
|
+
get: (key: string) => Promise<any>;
|
|
12
|
+
clear: (key?: string | undefined) => Promise<void>;
|
|
13
|
+
wrap: <T>(key: string, expiresMs: number | null | undefined, fn: () => Promise<T>) => Promise<T>;
|
|
14
|
+
};
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function createRedisCacheAdapter(redis) {
|
|
2
|
+
return {
|
|
3
|
+
async get(key) {
|
|
4
|
+
const value = await redis.get(key);
|
|
5
|
+
if (value == null) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
return JSON.parse(value);
|
|
9
|
+
},
|
|
10
|
+
async set(key, value, expires) {
|
|
11
|
+
await redis.set(key, JSON.stringify(value), { PX: expires });
|
|
12
|
+
},
|
|
13
|
+
async clear(key) {
|
|
14
|
+
if (key) {
|
|
15
|
+
await redis.del(key);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function createMemoryCacheAdapter() {
|
|
21
|
+
const cache = new Map();
|
|
22
|
+
return {
|
|
23
|
+
async get(key) {
|
|
24
|
+
const entry = cache.get(key);
|
|
25
|
+
if (entry == null) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const [value, expires] = entry;
|
|
29
|
+
if (expires != null && Date.now() >= expires) {
|
|
30
|
+
cache.delete(key);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
},
|
|
35
|
+
async set(key, value, expires) {
|
|
36
|
+
cache.set(key, [value, expires == null ? null : Date.now() + expires]);
|
|
37
|
+
},
|
|
38
|
+
async clear(key) {
|
|
39
|
+
if (key) {
|
|
40
|
+
cache.delete(key);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
cache.clear();
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function createCache(adapter) {
|
|
49
|
+
async function cached(key, expiresMs, fn) {
|
|
50
|
+
const cachedValue = await adapter.get(key);
|
|
51
|
+
if (cachedValue != null) {
|
|
52
|
+
return cachedValue;
|
|
53
|
+
}
|
|
54
|
+
const result = await fn();
|
|
55
|
+
await adapter.set(key, result, expiresMs);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
set: adapter.set.bind(adapter),
|
|
60
|
+
get: adapter.get.bind(adapter),
|
|
61
|
+
clear: adapter.clear.bind(adapter),
|
|
62
|
+
wrap: cached,
|
|
63
|
+
};
|
|
64
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/logger.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
1
2
|
import type { CreateSentry } from './sentry.js';
|
|
2
3
|
export type LoggerContext = Record<string, any>;
|
|
3
4
|
export interface LoggerOptions {
|
|
@@ -13,11 +14,12 @@ export interface LoggerOptions {
|
|
|
13
14
|
* Defaults to `process.env.NODE_ENV === 'test'`
|
|
14
15
|
*/
|
|
15
16
|
silent?: () => boolean;
|
|
17
|
+
output?: NodeJS.WriteStream;
|
|
16
18
|
}
|
|
17
19
|
export declare function attachSentryClientToLoggers(sentryClient: CreateSentry['client']): void;
|
|
18
20
|
export declare function setGlobalLoggerContext(getContext: () => LoggerContext): void;
|
|
19
21
|
export type Logger = ReturnType<typeof createLogger>;
|
|
20
|
-
export declare function createLogger({ name, getContext, silent, }: LoggerOptions): {
|
|
22
|
+
export declare function createLogger({ name, getContext, silent, output, }: LoggerOptions): {
|
|
21
23
|
debug(message: string, context?: LoggerContext): void;
|
|
22
24
|
info(message: string, context?: LoggerContext): void;
|
|
23
25
|
warn(message: string, context?: LoggerContext): void;
|
package/dist/logger.js
CHANGED
|
@@ -24,12 +24,13 @@ function defaultGetContext() {
|
|
|
24
24
|
..._globalGetContext?.(),
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
export function createLogger({ name, getContext, silent = () => process.env.NODE_ENV === 'test', }) {
|
|
27
|
+
export function createLogger({ name, getContext, silent = () => process.env.NODE_ENV === 'test', output = process.stdout, }) {
|
|
28
28
|
if (_loggers.has(name)) {
|
|
29
29
|
throw new Error(`A logger named ${name} already exists`);
|
|
30
30
|
}
|
|
31
31
|
const hostname = os.hostname();
|
|
32
32
|
const pid = process.pid;
|
|
33
|
+
const env = process.env.NODE_ENV;
|
|
33
34
|
const logLevel = silent() ? LEVELS.FATAL + 1 : LEVELS.INFO;
|
|
34
35
|
function getFinalContext(context) {
|
|
35
36
|
return {
|
|
@@ -41,6 +42,7 @@ export function createLogger({ name, getContext, silent = () => process.env.NODE
|
|
|
41
42
|
function log(level, message, context) {
|
|
42
43
|
const payload = JSON.stringify({
|
|
43
44
|
level: LEVELS_TO_NAME[level],
|
|
45
|
+
env,
|
|
44
46
|
hostname,
|
|
45
47
|
pid,
|
|
46
48
|
name,
|
|
@@ -50,7 +52,7 @@ export function createLogger({ name, getContext, silent = () => process.env.NODE
|
|
|
50
52
|
v: 1,
|
|
51
53
|
});
|
|
52
54
|
if (level >= logLevel) {
|
|
53
|
-
|
|
55
|
+
output.write(payload + '\n');
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
const api = {
|
package/dist/middleware.js
CHANGED
|
@@ -104,7 +104,7 @@ export function after() {
|
|
|
104
104
|
* Alias of `route()`
|
|
105
105
|
*/
|
|
106
106
|
export function asyncMiddleware(fn) {
|
|
107
|
-
|
|
107
|
+
const handler = (req, res, next) => {
|
|
108
108
|
return Promise.resolve(fn(req, res, next))
|
|
109
109
|
.then((result) => {
|
|
110
110
|
if (Array.isArray(result) &&
|
|
@@ -125,4 +125,6 @@ export function asyncMiddleware(fn) {
|
|
|
125
125
|
})
|
|
126
126
|
.catch(next);
|
|
127
127
|
};
|
|
128
|
+
Object.defineProperty(handler, 'name', { value: fn.name });
|
|
129
|
+
return handler;
|
|
128
130
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devmoods/express-extras",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.1",
|
|
4
4
|
"description": "Kubernetes-ready utilities for rapid Node.js application development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@devmoods/eslint-config": "^1.2.0",
|
|
27
|
-
"@sentry/types": "^7.
|
|
27
|
+
"@sentry/types": "^7.64.0",
|
|
28
28
|
"@types/express-serve-static-core": "^4.17.35",
|
|
29
|
-
"@types/node": "20.
|
|
29
|
+
"@types/node": "20.5.0",
|
|
30
30
|
"@types/supertest": "^2.0.12",
|
|
31
31
|
"@typescript-eslint/eslint-plugin": "^6.3.0",
|
|
32
32
|
"@typescript-eslint/parser": "^6.3.0",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@devmoods/fetch": "^3.3.0",
|
|
53
53
|
"@godaddy/terminus": "^4.12.1",
|
|
54
|
-
"@sentry/node": "^7.
|
|
54
|
+
"@sentry/node": "^7.64.0",
|
|
55
55
|
"@types/bcrypt": "^5.0.0",
|
|
56
56
|
"@types/body-parser": "^1.19.2",
|
|
57
57
|
"@types/cookie-parser": "^1.4.3",
|