kuwan-expresspack-core 0.1.16 → 0.1.17

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.
@@ -0,0 +1,5 @@
1
+ import { AsyncLocalStorage } from 'async_hooks';
2
+
3
+ var a=new AsyncLocalStorage;function n(e){return {get(t){return e.data.get(t)},set(t,o){e.data.set(t,o);},has(t){return e.data.has(t)},delete(t){return e.data.delete(t)},clear(){e.data.clear();}}}function c(){let e=a.getStore();if(!e)throw new Error("No context available. Ensure context middleware is registered before accessing context.");return n(e)}function i(){let e=a.getStore();return e?n(e):void 0}function y(e){let t={data:new Map};return a.run(t,e)}
4
+
5
+ export { a, n as b, c, i as d, y as e };
@@ -0,0 +1,3 @@
1
+ function r(e){let n=(t,o)=>{e.appContext||(e.appContext={}),e.appContext[t]=o;};function a(t){return e.appContext?.[t]}return {set:n,get:a}}
2
+
3
+ export { r as a };
@@ -0,0 +1,140 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+
3
+ /**
4
+ * Application context with typed access methods
5
+ */
6
+ interface AppContext<TData extends Record<string, any> = Record<string, any>> {
7
+ /**
8
+ * Get a value from the context store
9
+ *
10
+ * @param key - The key to retrieve
11
+ * @returns The stored value or undefined
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * interface MyContext {
16
+ * user: { id: number; name: string };
17
+ * tenantId: string;
18
+ * }
19
+ *
20
+ * const ctx = getAppContext<MyContext>();
21
+ * const user = ctx.get('user'); // type is { id: number; name: string } | undefined
22
+ * ```
23
+ */
24
+ get<K extends keyof TData>(key: K): TData[K] | undefined;
25
+ /**
26
+ * Set a value in the context store
27
+ *
28
+ * @param key - The key to store the value under
29
+ * @param value - The value to store
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const ctx = getAppContext<MyContext>();
34
+ * ctx.set('user', { id: 1, name: 'John' }); // fully typed
35
+ * ```
36
+ */
37
+ set<K extends keyof TData>(key: K, value: TData[K]): void;
38
+ /**
39
+ * Check if a key exists in the context store
40
+ *
41
+ * @param key - The key to check
42
+ * @returns True if the key exists
43
+ */
44
+ has<K extends keyof TData>(key: K): boolean;
45
+ /**
46
+ * Delete a value from the context store
47
+ *
48
+ * @param key - The key to delete
49
+ * @returns True if the key existed and was deleted
50
+ */
51
+ delete<K extends keyof TData>(key: K): boolean;
52
+ /**
53
+ * Clear all values from the context store
54
+ */
55
+ clear(): void;
56
+ }
57
+ /**
58
+ * Internal context structure
59
+ */
60
+ interface InternalContext<TData extends Record<string, any> = Record<string, any>> {
61
+ data: Map<keyof TData, any>;
62
+ }
63
+ /**
64
+ * AsyncLocalStorage instance for managing context
65
+ */
66
+ declare const asyncLocalStorage: AsyncLocalStorage<InternalContext<any>>;
67
+ /**
68
+ * Create a typed context proxy from internal context
69
+ */
70
+ declare function createContextProxy<TData extends Record<string, any>>(internal: InternalContext<TData>): AppContext<TData>;
71
+ /**
72
+ * Get the current application context with custom type support
73
+ *
74
+ * @throws {Error} If called outside of context
75
+ * @returns {AppContext<TData>} The current context
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * import { getAppContext } from 'expresspack';
80
+ *
81
+ * interface MyContext {
82
+ * user: { id: number; name: string };
83
+ * tenantId: string;
84
+ * }
85
+ *
86
+ * export function someHandler(req, res) {
87
+ * const ctx = getAppContext<MyContext>();
88
+ *
89
+ * // Fully typed access
90
+ * const user = ctx.get('user'); // type: { id: number; name: string } | undefined
91
+ *
92
+ * // Fully typed set
93
+ * ctx.set('user', { id: 1, name: 'John' });
94
+ * }
95
+ * ```
96
+ */
97
+ declare function getAppContext<TData extends Record<string, any> = Record<string, any>>(): AppContext<TData>;
98
+ /**
99
+ * Get the current application context, returns undefined if not available
100
+ * Useful for optional context access
101
+ *
102
+ * @returns {AppContext<TData> | undefined} The current context or undefined
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * import { tryGetAppContext } from 'expresspack';
107
+ *
108
+ * export function someUtility() {
109
+ * const ctx = tryGetAppContext<MyContext>();
110
+ * if (ctx) {
111
+ * const user = ctx.get('user');
112
+ * }
113
+ * }
114
+ * ```
115
+ */
116
+ declare function tryGetAppContext<TData extends Record<string, any> = Record<string, any>>(): AppContext<TData> | undefined;
117
+ /**
118
+ * Run a function within a context (useful for testing)
119
+ *
120
+ * @param fn - The function to execute
121
+ * @returns The result of the function
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * import { runInContext } from 'expresspack';
126
+ *
127
+ * interface MyContext {
128
+ * userId: number;
129
+ * }
130
+ *
131
+ * const result = runInContext(() => {
132
+ * const ctx = getAppContext<MyContext>();
133
+ * ctx.set('userId', 123);
134
+ * return someFunction();
135
+ * });
136
+ * ```
137
+ */
138
+ declare function runInContext<T>(fn: () => T): T;
139
+
140
+ export { type AppContext, type InternalContext, asyncLocalStorage, createContextProxy, getAppContext, runInContext, tryGetAppContext };
@@ -0,0 +1 @@
1
+ export { a as asyncLocalStorage, b as createContextProxy, c as getAppContext, e as runInContext, d as tryGetAppContext } from '../../chunk-55RBYZEZ.mjs';
@@ -0,0 +1,2 @@
1
+ export { getAppContext, tryGetAppContext } from './context-store-internal.js';
2
+ import 'node:async_hooks';
@@ -0,0 +1 @@
1
+ export { c as getAppContext, d as tryGetAppContext } from '../../chunk-55RBYZEZ.mjs';
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,16 @@
1
+ import { Request } from 'express';
2
+
3
+ /**
4
+ * Simple Context - Use when AsyncLocalStorage is not needed or not available.
5
+ *
6
+ */
7
+
8
+ /**
9
+ * Helper function to set/get values in Request.
10
+ */
11
+ declare function useAppContext<TData extends Record<never, never> = Record<never, never>>(req: Request): {
12
+ set: (key: string, value: any) => void;
13
+ get: <K extends keyof TData>(key: K) => TData[K] | undefined;
14
+ };
15
+
16
+ export { useAppContext };
@@ -0,0 +1 @@
1
+ export { a as useAppContext } from '../chunk-FFCH7NYQ.mjs';
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { a as a$1 } from './chunk-JKH5TYED.mjs';
2
1
  import { e } from './chunk-OXUMMLIO.mjs';
2
+ import { a as a$1 } from './chunk-JKH5TYED.mjs';
3
3
  import { a as a$2 } from './chunk-VSPZT4K6.mjs';
4
4
  import { a, f } from './chunk-CZGPAGDK.mjs';
5
5
  import { Router } from 'express';
@@ -0,0 +1,48 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+
3
+ /**
4
+ * Configuration options for context middleware
5
+ */
6
+ interface ContextStoreConfig {
7
+ /**
8
+ * Whether to enable the context middleware
9
+ * @default true
10
+ */
11
+ enabled?: boolean;
12
+ }
13
+ /**
14
+ * Express middleware that initializes async context for each request
15
+ * This middleware must be registered before any code that uses getAppContext()
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * import express from 'express';
20
+ * import { contextStoreMiddleware } from 'expresspack';
21
+ *
22
+ * const app = express();
23
+ *
24
+ * // Register as early as possible
25
+ * app.use(contextStoreMiddleware());
26
+ * ```
27
+ */
28
+ declare function contextStoreMiddleware(): (req: Request, res: Response, next: NextFunction) => void;
29
+ /**
30
+ * For types you need to augment the Request type to include appContext
31
+ * and also include the type for appContext.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * declare global {
36
+ * namespace Express {
37
+ * interface Request {
38
+ * appContext: {
39
+ * user: User;
40
+ * session: Session;
41
+ * };
42
+ * }
43
+ * }
44
+ * ```
45
+ */
46
+ declare function simpleContextMiddleware(): (req: Request, res: Response, next: NextFunction) => void;
47
+
48
+ export { type ContextStoreConfig, contextStoreMiddleware, simpleContextMiddleware };
@@ -0,0 +1,6 @@
1
+ import { a } from '../../chunk-55RBYZEZ.mjs';
2
+ import { a as a$1 } from '../../chunk-FFCH7NYQ.mjs';
3
+
4
+ function u(){return (e,p,t)=>{let n={data:new Map};a.run(n,()=>{t();});}}function c(){return (e,p,t)=>{let n=a$1(e);e.appContext=n,t();}}
5
+
6
+ export { u as contextStoreMiddleware, c as simpleContextMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kuwan-expresspack-core",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -57,6 +57,18 @@
57
57
  "types": "./dist/middlewares/error-handler/index.d.ts",
58
58
  "import": "./dist/middlewares/error-handler/index.mjs"
59
59
  },
60
+ "./middlewares/app-context": {
61
+ "types": "./dist/middlewares/app-context/index.d.ts",
62
+ "import": "./dist/middlewares/app-context/index.mjs"
63
+ },
64
+ "./context-store": {
65
+ "types": "./dist/context/context-store/index.d.ts",
66
+ "import": "./dist/context/context-store/index.mjs"
67
+ },
68
+ "./simple-context": {
69
+ "types": "./dist/context/simple-context.d.ts",
70
+ "import": "./dist/context/simple-context.mjs"
71
+ },
60
72
  "./services/*": {
61
73
  "types": "./dist/services/*/index.d.ts",
62
74
  "import": "./dist/services/*/index.mjs"