@lark-apaas/nestjs-common 0.0.1-alpha.4 → 0.1.0

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/dist/index.cjs CHANGED
@@ -22,12 +22,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  CommonModule: () => CommonModule,
25
- OBSERVABLE_SERVICE_TOKEN: () => OBSERVABLE_SERVICE_TOKEN,
25
+ OBSERVABLE_SERVICE: () => OBSERVABLE_SERVICE,
26
+ PLATFORM_HTTP_CLIENT: () => PLATFORM_HTTP_CLIENT,
26
27
  RequestContextService: () => RequestContextService
27
28
  });
28
29
  module.exports = __toCommonJS(index_exports);
29
30
 
30
- // src/request-context.service.ts
31
+ // src/services/request-context.service.ts
31
32
  var import_common = require("@nestjs/common");
32
33
  var import_async_hooks = require("async_hooks");
33
34
  function _ts_decorate(decorators, target, key, desc) {
@@ -92,12 +93,14 @@ CommonModule = _ts_decorate2([
92
93
  })
93
94
  ], CommonModule);
94
95
 
95
- // src/constants.ts
96
- var OBSERVABLE_SERVICE_TOKEN = Symbol("OBSERVABLE_SERVICE_TOKEN");
96
+ // src/tokens.ts
97
+ var OBSERVABLE_SERVICE = /* @__PURE__ */ Symbol("OBSERVABLE_SERVICE");
98
+ var PLATFORM_HTTP_CLIENT = /* @__PURE__ */ Symbol("PLATFORM_HTTP_CLIENT");
97
99
  // Annotate the CommonJS export names for ESM import in node:
98
100
  0 && (module.exports = {
99
101
  CommonModule,
100
- OBSERVABLE_SERVICE_TOKEN,
102
+ OBSERVABLE_SERVICE,
103
+ PLATFORM_HTTP_CLIENT,
101
104
  RequestContextService
102
105
  });
103
106
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/request-context.service.ts","../src/module.ts","../src/constants.ts"],"sourcesContent":["export { RequestContextService, type RequestContextState } from './request-context.service';\nexport { CommonModule } from './module';\nexport * from './constants';\nexport * from './type';","import { Injectable } from '@nestjs/common';\nimport { AsyncLocalStorage } from 'async_hooks';\n\nexport interface RequestContextState {\n requestId?: string;\n path?: string;\n method?: string;\n userId?: string;\n appId?: string;\n tenantId?: string;\n ip?: string;\n [key: string]: unknown;\n}\n\n@Injectable()\nexport class RequestContextService {\n private readonly storage = new AsyncLocalStorage<RequestContextState>();\n\n run<T>(context: RequestContextState, callback: () => T): T {\n const store = { ...context };\n return this.storage.run(store, callback);\n }\n\n setContext(partial: Partial<RequestContextState>): void {\n const store = this.storage.getStore();\n if (!store) {\n return;\n }\n Object.assign(store, partial);\n }\n\n getContext(): RequestContextState | undefined {\n return this.storage.getStore();\n }\n\n get<K extends keyof RequestContextState>(key: K): RequestContextState[K] | undefined {\n return this.storage.getStore()?.[key];\n }\n}\n","import { Global, Module } from '@nestjs/common';\nimport { RequestContextService } from './request-context.service';\n\n@Global()\n@Module({\n providers: [RequestContextService],\n exports: [RequestContextService],\n})\nexport class CommonModule {}\n","export const OBSERVABLE_SERVICE_TOKEN = Symbol('OBSERVABLE_SERVICE_TOKEN');"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,oBAA2B;AAC3B,yBAAkC;;;;;;;;AAc3B,IAAMA,wBAAN,MAAMA;SAAAA;;;EACMC,UAAU,IAAIC,qCAAAA;EAE/BC,IAAOC,SAA8BC,UAAsB;AACzD,UAAMC,QAAQ;MAAE,GAAGF;IAAQ;AAC3B,WAAO,KAAKH,QAAQE,IAAIG,OAAOD,QAAAA;EACjC;EAEAE,WAAWC,SAA6C;AACtD,UAAMF,QAAQ,KAAKL,QAAQQ,SAAQ;AACnC,QAAI,CAACH,OAAO;AACV;IACF;AACAI,WAAOC,OAAOL,OAAOE,OAAAA;EACvB;EAEAI,aAA8C;AAC5C,WAAO,KAAKX,QAAQQ,SAAQ;EAC9B;EAEAI,IAAyCC,KAA4C;AACnF,WAAO,KAAKb,QAAQQ,SAAQ,IAAKK,GAAAA;EACnC;AACF;;;;;;ACtCA,IAAAC,iBAA+B;;;;;;;;AAQxB,IAAMC,eAAN,MAAMA;SAAAA;;;AAAc;;;;IAHzBC,WAAW;MAACC;;IACZC,SAAS;MAACD;;;;;;ACNL,IAAME,2BAA2BC,OAAO,0BAAA;","names":["RequestContextService","storage","AsyncLocalStorage","run","context","callback","store","setContext","partial","getStore","Object","assign","getContext","get","key","import_common","CommonModule","providers","RequestContextService","exports","OBSERVABLE_SERVICE_TOKEN","Symbol"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/services/request-context.service.ts","../src/module.ts","../src/tokens.ts"],"sourcesContent":["export { RequestContextService, type RequestContextState } from './services/request-context.service';\nexport { CommonModule } from './module';\nexport * from './tokens';\nexport * from './type';\n","import { Injectable } from '@nestjs/common';\nimport { AsyncLocalStorage } from 'async_hooks';\n\nexport interface RequestContextState {\n requestId?: string;\n path?: string;\n method?: string;\n userId?: string;\n appId?: string;\n tenantId?: string;\n ip?: string;\n [key: string]: unknown;\n}\n\n@Injectable()\nexport class RequestContextService {\n private readonly storage = new AsyncLocalStorage<RequestContextState>();\n\n run<T>(context: RequestContextState, callback: () => T): T {\n const store = { ...context };\n return this.storage.run(store, callback);\n }\n\n setContext(partial: Partial<RequestContextState>): void {\n const store = this.storage.getStore();\n if (!store) {\n return;\n }\n Object.assign(store, partial);\n }\n\n getContext(): RequestContextState | undefined {\n return this.storage.getStore();\n }\n\n get<K extends keyof RequestContextState>(key: K): RequestContextState[K] | undefined {\n return this.storage.getStore()?.[key];\n }\n}\n","import { Global, Module } from '@nestjs/common';\nimport { RequestContextService } from './services/request-context.service';\n\n@Global()\n@Module({\n providers: [RequestContextService],\n exports: [RequestContextService],\n})\nexport class CommonModule {}\n","/**\n * DI token for ObservableService\n *\n * 注入此 token 获得 ObservableService 实例\n */\nexport const OBSERVABLE_SERVICE = Symbol('OBSERVABLE_SERVICE');\n\n/**\n * DI Token for Platform HttpClient\n *\n * 这些 token 供 PlatformModule 和内部依赖(如 AuthNPaasModule)共享使用,通过创建独立的 nestjs-common 包,避免循环依赖\n *\n * @example\n * ```typescript\n * // nestjs-core 中注册 provider\n * import { PLATFORM_HTTP_CLIENT } from '@lark-apaas/nestjs-common';\n *\n * {\n * provide: PLATFORM_HTTP_CLIENT,\n * useFactory: (svc: PlatformHttpClientService) => svc.instance,\n * inject: [PlatformHttpClientService],\n * }\n * ```\n *\n * @example\n * ```typescript\n * // nestjs-authnpaas 中注入使用\n * import { Injectable, Inject } from '@nestjs/common';\n * import { PLATFORM_HTTP_CLIENT, type SafeHttpClient } from '@lark-apaas/nestjs-common';\n *\n * @Injectable()\n * export class AuthNService {\n * constructor(\n * @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient\n * ) {}\n * }\n * ```\n */\n\n/**\n * DI token for PlatformHttpClient instance\n *\n * 注入此 token 获得 SafeHttpClient 实例(不暴露 interceptors)\n */\nexport const PLATFORM_HTTP_CLIENT = Symbol('PLATFORM_HTTP_CLIENT');\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACAA,oBAA2B;AAC3B,yBAAkC;;;;;;;;AAc3B,IAAMA,wBAAN,MAAMA;SAAAA;;;EACMC,UAAU,IAAIC,qCAAAA;EAE/BC,IAAOC,SAA8BC,UAAsB;AACzD,UAAMC,QAAQ;MAAE,GAAGF;IAAQ;AAC3B,WAAO,KAAKH,QAAQE,IAAIG,OAAOD,QAAAA;EACjC;EAEAE,WAAWC,SAA6C;AACtD,UAAMF,QAAQ,KAAKL,QAAQQ,SAAQ;AACnC,QAAI,CAACH,OAAO;AACV;IACF;AACAI,WAAOC,OAAOL,OAAOE,OAAAA;EACvB;EAEAI,aAA8C;AAC5C,WAAO,KAAKX,QAAQQ,SAAQ;EAC9B;EAEAI,IAAyCC,KAA4C;AACnF,WAAO,KAAKb,QAAQQ,SAAQ,IAAKK,GAAAA;EACnC;AACF;;;;;;ACtCA,IAAAC,iBAA+B;;;;;;;;AAQxB,IAAMC,eAAN,MAAMA;SAAAA;;;AAAc;;;;IAHzBC,WAAW;MAACC;;IACZC,SAAS;MAACD;;;;;;ACDL,IAAME,qBAAqBC,uBAAO,oBAAA;AAuClC,IAAMC,uBAAuBD,uBAAO,sBAAA;","names":["RequestContextService","storage","AsyncLocalStorage","run","context","callback","store","setContext","partial","getStore","Object","assign","getContext","get","key","import_common","CommonModule","providers","RequestContextService","exports","OBSERVABLE_SERVICE","Symbol","PLATFORM_HTTP_CLIENT"]}
package/dist/index.d.cts CHANGED
@@ -19,11 +19,120 @@ declare class RequestContextService {
19
19
  declare class CommonModule {
20
20
  }
21
21
 
22
- declare const OBSERVABLE_SERVICE_TOKEN: unique symbol;
22
+ /**
23
+ * DI token for ObservableService
24
+ *
25
+ * 注入此 token 获得 ObservableService 实例
26
+ */
27
+ declare const OBSERVABLE_SERVICE: unique symbol;
28
+ /**
29
+ * DI Token for Platform HttpClient
30
+ *
31
+ * 这些 token 供 PlatformModule 和内部依赖(如 AuthNPaasModule)共享使用,通过创建独立的 nestjs-common 包,避免循环依赖
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // nestjs-core 中注册 provider
36
+ * import { PLATFORM_HTTP_CLIENT } from '@lark-apaas/nestjs-common';
37
+ *
38
+ * {
39
+ * provide: PLATFORM_HTTP_CLIENT,
40
+ * useFactory: (svc: PlatformHttpClientService) => svc.instance,
41
+ * inject: [PlatformHttpClientService],
42
+ * }
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // nestjs-authnpaas 中注入使用
48
+ * import { Injectable, Inject } from '@nestjs/common';
49
+ * import { PLATFORM_HTTP_CLIENT, type SafeHttpClient } from '@lark-apaas/nestjs-common';
50
+ *
51
+ * @Injectable()
52
+ * export class AuthNService {
53
+ * constructor(
54
+ * @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient
55
+ * ) {}
56
+ * }
57
+ * ```
58
+ */
59
+ /**
60
+ * DI token for PlatformHttpClient instance
61
+ *
62
+ * 注入此 token 获得 SafeHttpClient 实例(不暴露 interceptors)
63
+ */
64
+ declare const PLATFORM_HTTP_CLIENT: unique symbol;
23
65
 
24
66
  interface ObservableService {
25
67
  log(level: string, message: string, attributes: Record<string, unknown>): void;
26
68
  flush(): Promise<void>;
27
69
  }
70
+ /**
71
+ * Safe HttpClient interface (fetch 风格)
72
+ *
73
+ * 只暴露请求方法,不暴露 interceptors
74
+ * 防止业务代码修改全局拦截器,避免相互影响
75
+ *
76
+ * 所有方法返回标准 Response 对象,完美支持流式响应
77
+ *
78
+ * @example
79
+ * 普通 JSON 请求
80
+ * ```typescript
81
+ * @Injectable()
82
+ * export class UserService {
83
+ * constructor(
84
+ * @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient
85
+ * ) {}
86
+ *
87
+ * async getUser() {
88
+ * const response = await this.http.get('/user');
89
+ * return response.json(); // 手动解析 JSON
90
+ * }
91
+ * }
92
+ * ```
93
+ *
94
+ * @example
95
+ * 流式响应(SSE, Streaming API)
96
+ * ```typescript
97
+ * async streamData() {
98
+ * const response = await this.http.post('/stream', { query: 'hello' });
99
+ * const reader = response.body!.getReader();
100
+ * const decoder = new TextDecoder();
101
+ *
102
+ * while (true) {
103
+ * const { done, value } = await reader.read();
104
+ * if (done) break;
105
+ * const text = decoder.decode(value);
106
+ * console.log(text); // 实时处理流数据
107
+ * }
108
+ * }
109
+ * ```
110
+ */
111
+ interface PlatformHttpClient {
112
+ /**
113
+ * 发送 HTTP 请求
114
+ */
115
+ request(config: any): Promise<Response>;
116
+ /**
117
+ * 发送 GET 请求
118
+ */
119
+ get(url: string, config?: any): Promise<Response>;
120
+ /**
121
+ * 发送 POST 请求
122
+ */
123
+ post(url: string, data?: any, config?: any): Promise<Response>;
124
+ /**
125
+ * 发送 PUT 请求
126
+ */
127
+ put(url: string, data?: any, config?: any): Promise<Response>;
128
+ /**
129
+ * 发送 PATCH 请求
130
+ */
131
+ patch(url: string, data?: any, config?: any): Promise<Response>;
132
+ /**
133
+ * 发送 DELETE 请求
134
+ */
135
+ delete(url: string, config?: any): Promise<Response>;
136
+ }
28
137
 
29
- export { CommonModule, OBSERVABLE_SERVICE_TOKEN, type ObservableService, RequestContextService, type RequestContextState };
138
+ export { CommonModule, OBSERVABLE_SERVICE, type ObservableService, PLATFORM_HTTP_CLIENT, type PlatformHttpClient, RequestContextService, type RequestContextState };
package/dist/index.d.ts CHANGED
@@ -19,11 +19,120 @@ declare class RequestContextService {
19
19
  declare class CommonModule {
20
20
  }
21
21
 
22
- declare const OBSERVABLE_SERVICE_TOKEN: unique symbol;
22
+ /**
23
+ * DI token for ObservableService
24
+ *
25
+ * 注入此 token 获得 ObservableService 实例
26
+ */
27
+ declare const OBSERVABLE_SERVICE: unique symbol;
28
+ /**
29
+ * DI Token for Platform HttpClient
30
+ *
31
+ * 这些 token 供 PlatformModule 和内部依赖(如 AuthNPaasModule)共享使用,通过创建独立的 nestjs-common 包,避免循环依赖
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // nestjs-core 中注册 provider
36
+ * import { PLATFORM_HTTP_CLIENT } from '@lark-apaas/nestjs-common';
37
+ *
38
+ * {
39
+ * provide: PLATFORM_HTTP_CLIENT,
40
+ * useFactory: (svc: PlatformHttpClientService) => svc.instance,
41
+ * inject: [PlatformHttpClientService],
42
+ * }
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // nestjs-authnpaas 中注入使用
48
+ * import { Injectable, Inject } from '@nestjs/common';
49
+ * import { PLATFORM_HTTP_CLIENT, type SafeHttpClient } from '@lark-apaas/nestjs-common';
50
+ *
51
+ * @Injectable()
52
+ * export class AuthNService {
53
+ * constructor(
54
+ * @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient
55
+ * ) {}
56
+ * }
57
+ * ```
58
+ */
59
+ /**
60
+ * DI token for PlatformHttpClient instance
61
+ *
62
+ * 注入此 token 获得 SafeHttpClient 实例(不暴露 interceptors)
63
+ */
64
+ declare const PLATFORM_HTTP_CLIENT: unique symbol;
23
65
 
24
66
  interface ObservableService {
25
67
  log(level: string, message: string, attributes: Record<string, unknown>): void;
26
68
  flush(): Promise<void>;
27
69
  }
70
+ /**
71
+ * Safe HttpClient interface (fetch 风格)
72
+ *
73
+ * 只暴露请求方法,不暴露 interceptors
74
+ * 防止业务代码修改全局拦截器,避免相互影响
75
+ *
76
+ * 所有方法返回标准 Response 对象,完美支持流式响应
77
+ *
78
+ * @example
79
+ * 普通 JSON 请求
80
+ * ```typescript
81
+ * @Injectable()
82
+ * export class UserService {
83
+ * constructor(
84
+ * @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient
85
+ * ) {}
86
+ *
87
+ * async getUser() {
88
+ * const response = await this.http.get('/user');
89
+ * return response.json(); // 手动解析 JSON
90
+ * }
91
+ * }
92
+ * ```
93
+ *
94
+ * @example
95
+ * 流式响应(SSE, Streaming API)
96
+ * ```typescript
97
+ * async streamData() {
98
+ * const response = await this.http.post('/stream', { query: 'hello' });
99
+ * const reader = response.body!.getReader();
100
+ * const decoder = new TextDecoder();
101
+ *
102
+ * while (true) {
103
+ * const { done, value } = await reader.read();
104
+ * if (done) break;
105
+ * const text = decoder.decode(value);
106
+ * console.log(text); // 实时处理流数据
107
+ * }
108
+ * }
109
+ * ```
110
+ */
111
+ interface PlatformHttpClient {
112
+ /**
113
+ * 发送 HTTP 请求
114
+ */
115
+ request(config: any): Promise<Response>;
116
+ /**
117
+ * 发送 GET 请求
118
+ */
119
+ get(url: string, config?: any): Promise<Response>;
120
+ /**
121
+ * 发送 POST 请求
122
+ */
123
+ post(url: string, data?: any, config?: any): Promise<Response>;
124
+ /**
125
+ * 发送 PUT 请求
126
+ */
127
+ put(url: string, data?: any, config?: any): Promise<Response>;
128
+ /**
129
+ * 发送 PATCH 请求
130
+ */
131
+ patch(url: string, data?: any, config?: any): Promise<Response>;
132
+ /**
133
+ * 发送 DELETE 请求
134
+ */
135
+ delete(url: string, config?: any): Promise<Response>;
136
+ }
28
137
 
29
- export { CommonModule, OBSERVABLE_SERVICE_TOKEN, type ObservableService, RequestContextService, type RequestContextState };
138
+ export { CommonModule, OBSERVABLE_SERVICE, type ObservableService, PLATFORM_HTTP_CLIENT, type PlatformHttpClient, RequestContextService, type RequestContextState };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
- // src/request-context.service.ts
4
+ // src/services/request-context.service.ts
5
5
  import { Injectable } from "@nestjs/common";
6
6
  import { AsyncLocalStorage } from "async_hooks";
7
7
  function _ts_decorate(decorators, target, key, desc) {
@@ -66,11 +66,13 @@ CommonModule = _ts_decorate2([
66
66
  })
67
67
  ], CommonModule);
68
68
 
69
- // src/constants.ts
70
- var OBSERVABLE_SERVICE_TOKEN = Symbol("OBSERVABLE_SERVICE_TOKEN");
69
+ // src/tokens.ts
70
+ var OBSERVABLE_SERVICE = /* @__PURE__ */ Symbol("OBSERVABLE_SERVICE");
71
+ var PLATFORM_HTTP_CLIENT = /* @__PURE__ */ Symbol("PLATFORM_HTTP_CLIENT");
71
72
  export {
72
73
  CommonModule,
73
- OBSERVABLE_SERVICE_TOKEN,
74
+ OBSERVABLE_SERVICE,
75
+ PLATFORM_HTTP_CLIENT,
74
76
  RequestContextService
75
77
  };
76
78
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/request-context.service.ts","../src/module.ts","../src/constants.ts"],"sourcesContent":["import { Injectable } from '@nestjs/common';\nimport { AsyncLocalStorage } from 'async_hooks';\n\nexport interface RequestContextState {\n requestId?: string;\n path?: string;\n method?: string;\n userId?: string;\n appId?: string;\n tenantId?: string;\n ip?: string;\n [key: string]: unknown;\n}\n\n@Injectable()\nexport class RequestContextService {\n private readonly storage = new AsyncLocalStorage<RequestContextState>();\n\n run<T>(context: RequestContextState, callback: () => T): T {\n const store = { ...context };\n return this.storage.run(store, callback);\n }\n\n setContext(partial: Partial<RequestContextState>): void {\n const store = this.storage.getStore();\n if (!store) {\n return;\n }\n Object.assign(store, partial);\n }\n\n getContext(): RequestContextState | undefined {\n return this.storage.getStore();\n }\n\n get<K extends keyof RequestContextState>(key: K): RequestContextState[K] | undefined {\n return this.storage.getStore()?.[key];\n }\n}\n","import { Global, Module } from '@nestjs/common';\nimport { RequestContextService } from './request-context.service';\n\n@Global()\n@Module({\n providers: [RequestContextService],\n exports: [RequestContextService],\n})\nexport class CommonModule {}\n","export const OBSERVABLE_SERVICE_TOKEN = Symbol('OBSERVABLE_SERVICE_TOKEN');"],"mappings":";;;;AAAA,SAASA,kBAAkB;AAC3B,SAASC,yBAAyB;;;;;;;;AAc3B,IAAMC,wBAAN,MAAMA;SAAAA;;;EACMC,UAAU,IAAIC,kBAAAA;EAE/BC,IAAOC,SAA8BC,UAAsB;AACzD,UAAMC,QAAQ;MAAE,GAAGF;IAAQ;AAC3B,WAAO,KAAKH,QAAQE,IAAIG,OAAOD,QAAAA;EACjC;EAEAE,WAAWC,SAA6C;AACtD,UAAMF,QAAQ,KAAKL,QAAQQ,SAAQ;AACnC,QAAI,CAACH,OAAO;AACV;IACF;AACAI,WAAOC,OAAOL,OAAOE,OAAAA;EACvB;EAEAI,aAA8C;AAC5C,WAAO,KAAKX,QAAQQ,SAAQ;EAC9B;EAEAI,IAAyCC,KAA4C;AACnF,WAAO,KAAKb,QAAQQ,SAAQ,IAAKK,GAAAA;EACnC;AACF;;;;;;ACtCA,SAASC,QAAQC,cAAc;;;;;;;;AAQxB,IAAMC,eAAN,MAAMA;SAAAA;;;AAAc;;;;IAHzBC,WAAW;MAACC;;IACZC,SAAS;MAACD;;;;;;ACNL,IAAME,2BAA2BC,OAAO,0BAAA;","names":["Injectable","AsyncLocalStorage","RequestContextService","storage","AsyncLocalStorage","run","context","callback","store","setContext","partial","getStore","Object","assign","getContext","get","key","Global","Module","CommonModule","providers","RequestContextService","exports","OBSERVABLE_SERVICE_TOKEN","Symbol"]}
1
+ {"version":3,"sources":["../src/services/request-context.service.ts","../src/module.ts","../src/tokens.ts"],"sourcesContent":["import { Injectable } from '@nestjs/common';\nimport { AsyncLocalStorage } from 'async_hooks';\n\nexport interface RequestContextState {\n requestId?: string;\n path?: string;\n method?: string;\n userId?: string;\n appId?: string;\n tenantId?: string;\n ip?: string;\n [key: string]: unknown;\n}\n\n@Injectable()\nexport class RequestContextService {\n private readonly storage = new AsyncLocalStorage<RequestContextState>();\n\n run<T>(context: RequestContextState, callback: () => T): T {\n const store = { ...context };\n return this.storage.run(store, callback);\n }\n\n setContext(partial: Partial<RequestContextState>): void {\n const store = this.storage.getStore();\n if (!store) {\n return;\n }\n Object.assign(store, partial);\n }\n\n getContext(): RequestContextState | undefined {\n return this.storage.getStore();\n }\n\n get<K extends keyof RequestContextState>(key: K): RequestContextState[K] | undefined {\n return this.storage.getStore()?.[key];\n }\n}\n","import { Global, Module } from '@nestjs/common';\nimport { RequestContextService } from './services/request-context.service';\n\n@Global()\n@Module({\n providers: [RequestContextService],\n exports: [RequestContextService],\n})\nexport class CommonModule {}\n","/**\n * DI token for ObservableService\n *\n * 注入此 token 获得 ObservableService 实例\n */\nexport const OBSERVABLE_SERVICE = Symbol('OBSERVABLE_SERVICE');\n\n/**\n * DI Token for Platform HttpClient\n *\n * 这些 token 供 PlatformModule 和内部依赖(如 AuthNPaasModule)共享使用,通过创建独立的 nestjs-common 包,避免循环依赖\n *\n * @example\n * ```typescript\n * // nestjs-core 中注册 provider\n * import { PLATFORM_HTTP_CLIENT } from '@lark-apaas/nestjs-common';\n *\n * {\n * provide: PLATFORM_HTTP_CLIENT,\n * useFactory: (svc: PlatformHttpClientService) => svc.instance,\n * inject: [PlatformHttpClientService],\n * }\n * ```\n *\n * @example\n * ```typescript\n * // nestjs-authnpaas 中注入使用\n * import { Injectable, Inject } from '@nestjs/common';\n * import { PLATFORM_HTTP_CLIENT, type SafeHttpClient } from '@lark-apaas/nestjs-common';\n *\n * @Injectable()\n * export class AuthNService {\n * constructor(\n * @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient\n * ) {}\n * }\n * ```\n */\n\n/**\n * DI token for PlatformHttpClient instance\n *\n * 注入此 token 获得 SafeHttpClient 实例(不暴露 interceptors)\n */\nexport const PLATFORM_HTTP_CLIENT = Symbol('PLATFORM_HTTP_CLIENT');\n"],"mappings":";;;;AAAA,SAASA,kBAAkB;AAC3B,SAASC,yBAAyB;;;;;;;;AAc3B,IAAMC,wBAAN,MAAMA;SAAAA;;;EACMC,UAAU,IAAIC,kBAAAA;EAE/BC,IAAOC,SAA8BC,UAAsB;AACzD,UAAMC,QAAQ;MAAE,GAAGF;IAAQ;AAC3B,WAAO,KAAKH,QAAQE,IAAIG,OAAOD,QAAAA;EACjC;EAEAE,WAAWC,SAA6C;AACtD,UAAMF,QAAQ,KAAKL,QAAQQ,SAAQ;AACnC,QAAI,CAACH,OAAO;AACV;IACF;AACAI,WAAOC,OAAOL,OAAOE,OAAAA;EACvB;EAEAI,aAA8C;AAC5C,WAAO,KAAKX,QAAQQ,SAAQ;EAC9B;EAEAI,IAAyCC,KAA4C;AACnF,WAAO,KAAKb,QAAQQ,SAAQ,IAAKK,GAAAA;EACnC;AACF;;;;;;ACtCA,SAASC,QAAQC,cAAc;;;;;;;;AAQxB,IAAMC,eAAN,MAAMA;SAAAA;;;AAAc;;;;IAHzBC,WAAW;MAACC;;IACZC,SAAS;MAACD;;;;;;ACDL,IAAME,qBAAqBC,uBAAO,oBAAA;AAuClC,IAAMC,uBAAuBD,uBAAO,sBAAA;","names":["Injectable","AsyncLocalStorage","RequestContextService","storage","AsyncLocalStorage","run","context","callback","store","setContext","partial","getStore","Object","assign","getContext","get","key","Global","Module","CommonModule","providers","RequestContextService","exports","OBSERVABLE_SERVICE","Symbol","PLATFORM_HTTP_CLIENT"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/nestjs-common",
3
- "version": "0.0.1-alpha.4",
3
+ "version": "0.1.0",
4
4
  "description": "Common NestJS utilities",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -42,4 +42,4 @@
42
42
  "tsup": "^8.0.0",
43
43
  "typescript": "^5.2.0"
44
44
  }
45
- }
45
+ }