@skalfa/skalfa-api-core 1.0.25 → 1.0.26

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.
@@ -1,7 +1,8 @@
1
- export type AppContext = {
1
+ export interface AppContext {
2
2
  user_id?: number;
3
- };
3
+ }
4
4
  export declare const context: {
5
- run<T>(ctx: AppContext, fn: () => T): T;
5
+ init(ctx: AppContext): void;
6
6
  get<K extends keyof AppContext>(key: K): AppContext[K];
7
+ set<K extends keyof AppContext>(key: K, value: AppContext[K]): void;
7
8
  };
@@ -1,17 +1,17 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  const storage = new AsyncLocalStorage();
3
3
  export const context = {
4
- // =====================================>
5
- // ## Context: run a function within context
6
- // =====================================>
7
- run(ctx, fn) {
8
- return storage.run(ctx, fn);
4
+ init(ctx) {
5
+ storage.enterWith(ctx);
9
6
  },
10
- // =====================================>
11
- // ## Context: retrieve context property value
12
- // =====================================>
13
7
  get(key) {
14
8
  return storage.getStore()?.[key];
15
9
  },
10
+ set(key, value) {
11
+ const store = storage.getStore();
12
+ if (store) {
13
+ store[key] = value;
14
+ }
15
+ }
16
16
  };
17
17
  //# sourceMappingURL=context.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/context/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAMpD,MAAM,OAAO,GAAG,IAAI,iBAAiB,EAAc,CAAA;AAEnD,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,yCAAyC;IACzC,4CAA4C;IAC5C,yCAAyC;IACzC,GAAG,CAAI,GAAe,EAAE,EAAW;QACjC,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IAC7B,CAAC;IAED,yCAAyC;IACzC,8CAA8C;IAC9C,yCAAyC;IACzC,GAAG,CAA6B,GAAM;QACpC,OAAO,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,GAAG,CAAC,CAAA;IAClC,CAAC;CACF,CAAA"}
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/context/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAMpD,MAAM,OAAO,GAAG,IAAI,iBAAiB,EAAc,CAAA;AAEnD,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,IAAI,CAAC,GAAe;QAClB,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;IAED,GAAG,CAA6B,GAAM;QACpC,OAAO,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,GAAG,CAAC,CAAA;IAClC,CAAC;IAED,GAAG,CAA6B,GAAM,EAAE,KAAoB;QAC1D,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAA;QAChC,IAAI,KAAK,EAAE,CAAC;YACV,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QACpB,CAAC;IACH,CAAC;CACF,CAAA"}
@@ -21,9 +21,9 @@ export declare const ContextMiddleware: (app: Elysia) => Elysia<"", {
21
21
  standaloneSchema: {};
22
22
  response: {};
23
23
  }, {
24
- derive: {};
24
+ derive: Partial<void & {}>;
25
25
  resolve: {};
26
26
  schema: {};
27
27
  standaloneSchema: {};
28
- response: import("elysia").ExtractErrorFromHandle<{}>;
28
+ response: import("elysia").ExtractErrorFromHandle<void>;
29
29
  }>;
@@ -1,8 +1,8 @@
1
1
  import { context } from '@utils';
2
2
  export const ContextMiddleware = (app) => app.derive(async ({ store }) => {
3
3
  const userId = store?.user?.id;
4
- return context.run({
4
+ context.init({
5
5
  user_id: userId,
6
- }, () => ({}));
6
+ });
7
7
  });
8
8
  //# sourceMappingURL=context.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/middleware/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAEhC,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE;IAC/E,MAAM,MAAM,GAAI,KAAa,EAAE,IAAI,EAAE,EAAE,CAAA;IAEvC,OAAO,OAAO,CAAC,GAAG,CAAC;QACf,OAAO,EAAE,MAAM;KAChB,EAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CACb,CAAA;AACH,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/middleware/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAEhC,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE;IAC/E,MAAM,MAAM,GAAI,KAAa,EAAE,IAAI,EAAE,EAAE,CAAA;IAEvC,OAAO,CAAC,IAAI,CAAC;QACX,OAAO,EAAK,MAAM;KACnB,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -275,10 +275,10 @@ export declare const middleware: {
275
275
  standaloneSchema: {};
276
276
  response: {};
277
277
  }, {
278
- derive: {};
278
+ derive: Partial<void & {}>;
279
279
  resolve: {};
280
280
  schema: {};
281
281
  standaloneSchema: {};
282
- response: import("elysia").ExtractErrorFromHandle<{}>;
282
+ response: import("elysia").ExtractErrorFromHandle<void>;
283
283
  }>;
284
284
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skalfa/skalfa-api-core",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "description": "Core and Foundational utilities for the Skalfa API Framework.",
5
5
  "keywords": [
6
6
  "skalfa",
@@ -1,23 +1,24 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks'
2
2
 
3
- export type AppContext = {
4
- user_id ?: number,
3
+ export interface AppContext {
4
+ user_id?: number
5
5
  }
6
6
 
7
7
  const storage = new AsyncLocalStorage<AppContext>()
8
8
 
9
9
  export const context = {
10
- // =====================================>
11
- // ## Context: run a function within context
12
- // =====================================>
13
- run<T>(ctx: AppContext, fn: () => T) {
14
- return storage.run(ctx, fn)
10
+ init(ctx: AppContext) {
11
+ storage.enterWith(ctx)
15
12
  },
16
13
 
17
- // =====================================>
18
- // ## Context: retrieve context property value
19
- // =====================================>
20
14
  get<K extends keyof AppContext>(key: K): AppContext[K] {
21
15
  return storage.getStore()?.[key]
22
16
  },
17
+
18
+ set<K extends keyof AppContext>(key: K, value: AppContext[K]) {
19
+ const store = storage.getStore()
20
+ if (store) {
21
+ store[key] = value
22
+ }
23
+ }
23
24
  }
@@ -4,8 +4,7 @@ import { context } from '@utils'
4
4
  export const ContextMiddleware = (app: Elysia) => app.derive(async ({ store }) => {
5
5
  const userId = (store as any)?.user?.id
6
6
 
7
- return context.run({
8
- user_id: userId,
9
- },() => ({})
10
- )
7
+ context.init({
8
+ user_id : userId,
9
+ })
11
10
  })