@context-action/core 0.0.1 → 0.0.3
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 +269 -66
- package/dist/index.d.cts +205 -30
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +205 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +261 -66
- package/dist/index.js.map +1 -1
- package/package.json +16 -28
- package/LICENSE +0 -187
- package/README.ko.md +0 -328
- package/README.md +0 -195
package/dist/index.cjs
CHANGED
|
@@ -24,8 +24,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
}) : target, mod));
|
|
25
25
|
|
|
26
26
|
//#endregion
|
|
27
|
-
const react = __toESM(require("react"));
|
|
28
|
-
const react_jsx_runtime = __toESM(require("react/jsx-runtime"));
|
|
29
27
|
|
|
30
28
|
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js
|
|
31
29
|
var require_typeof = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js"(exports, module) {
|
|
@@ -73,7 +71,7 @@ var require_toPropertyKey = __commonJS({ "../../node_modules/.pnpm/@oxc-project+
|
|
|
73
71
|
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js
|
|
74
72
|
var require_defineProperty = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js"(exports, module) {
|
|
75
73
|
var toPropertyKey = require_toPropertyKey();
|
|
76
|
-
function _defineProperty$
|
|
74
|
+
function _defineProperty$2(e, r, t) {
|
|
77
75
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
78
76
|
value: t,
|
|
79
77
|
enumerable: !0,
|
|
@@ -81,68 +79,311 @@ var require_defineProperty = __commonJS({ "../../node_modules/.pnpm/@oxc-project
|
|
|
81
79
|
writable: !0
|
|
82
80
|
}) : e[r] = t, e;
|
|
83
81
|
}
|
|
84
|
-
module.exports = _defineProperty$
|
|
82
|
+
module.exports = _defineProperty$2, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
85
83
|
} });
|
|
86
84
|
|
|
87
85
|
//#endregion
|
|
88
|
-
//#region src/
|
|
86
|
+
//#region src/logger.ts
|
|
87
|
+
var import_defineProperty$1 = __toESM(require_defineProperty(), 1);
|
|
88
|
+
/**
|
|
89
|
+
* Log levels in order of severity (lowest to highest)
|
|
90
|
+
*/
|
|
91
|
+
let LogLevel = /* @__PURE__ */ function(LogLevel$1) {
|
|
92
|
+
LogLevel$1[LogLevel$1["TRACE"] = 0] = "TRACE";
|
|
93
|
+
LogLevel$1[LogLevel$1["DEBUG"] = 1] = "DEBUG";
|
|
94
|
+
LogLevel$1[LogLevel$1["INFO"] = 2] = "INFO";
|
|
95
|
+
LogLevel$1[LogLevel$1["WARN"] = 3] = "WARN";
|
|
96
|
+
LogLevel$1[LogLevel$1["ERROR"] = 4] = "ERROR";
|
|
97
|
+
LogLevel$1[LogLevel$1["FATAL"] = 5] = "FATAL";
|
|
98
|
+
return LogLevel$1;
|
|
99
|
+
}({});
|
|
100
|
+
/**
|
|
101
|
+
* Default console logger implementation
|
|
102
|
+
*/
|
|
103
|
+
var ConsoleLogger = class {
|
|
104
|
+
constructor(level = LogLevel.ERROR) {
|
|
105
|
+
this.level = level;
|
|
106
|
+
}
|
|
107
|
+
shouldLog(level) {
|
|
108
|
+
return level >= this.level;
|
|
109
|
+
}
|
|
110
|
+
formatMessage(level, message) {
|
|
111
|
+
return `[${level.toUpperCase()}] ${message}`;
|
|
112
|
+
}
|
|
113
|
+
trace(message, ...args) {
|
|
114
|
+
if (this.shouldLog(LogLevel.TRACE)) console.trace(this.formatMessage("trace", message), ...args);
|
|
115
|
+
}
|
|
116
|
+
debug(message, ...args) {
|
|
117
|
+
if (this.shouldLog(LogLevel.DEBUG)) console.debug(this.formatMessage("debug", message), ...args);
|
|
118
|
+
}
|
|
119
|
+
info(message, ...args) {
|
|
120
|
+
if (this.shouldLog(LogLevel.INFO)) console.info(this.formatMessage("info", message), ...args);
|
|
121
|
+
}
|
|
122
|
+
warn(message, ...args) {
|
|
123
|
+
if (this.shouldLog(LogLevel.WARN)) console.warn(this.formatMessage("warn", message), ...args);
|
|
124
|
+
}
|
|
125
|
+
error(message, ...args) {
|
|
126
|
+
if (this.shouldLog(LogLevel.ERROR)) console.error(this.formatMessage("error", message), ...args);
|
|
127
|
+
}
|
|
128
|
+
fatal(message, ...args) {
|
|
129
|
+
if (this.shouldLog(LogLevel.FATAL)) console.error(this.formatMessage("fatal", message), ...args);
|
|
130
|
+
}
|
|
131
|
+
setLevel(level) {
|
|
132
|
+
this.level = level;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Parse log level from string
|
|
137
|
+
*/
|
|
138
|
+
function parseLogLevel(level) {
|
|
139
|
+
const upperLevel = level.toUpperCase();
|
|
140
|
+
switch (upperLevel) {
|
|
141
|
+
case "TRACE": return LogLevel.TRACE;
|
|
142
|
+
case "DEBUG": return LogLevel.DEBUG;
|
|
143
|
+
case "INFO": return LogLevel.INFO;
|
|
144
|
+
case "WARN": return LogLevel.WARN;
|
|
145
|
+
case "ERROR": return LogLevel.ERROR;
|
|
146
|
+
case "FATAL": return LogLevel.FATAL;
|
|
147
|
+
default: return LogLevel.ERROR;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Get log level from environment variable or default to ERROR
|
|
152
|
+
*/
|
|
153
|
+
function getLogLevelFromEnv() {
|
|
154
|
+
if (typeof process !== "undefined" && process.env) {
|
|
155
|
+
const envLevel = process.env.LOG_LEVEL || process.env.ACTION_LOG_LEVEL;
|
|
156
|
+
if (envLevel) return parseLogLevel(envLevel);
|
|
157
|
+
}
|
|
158
|
+
return LogLevel.TRACE;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Extract trace ID from payload if it exists
|
|
162
|
+
*/
|
|
163
|
+
function extractTraceIdFromPayload(payload) {
|
|
164
|
+
if (payload && typeof payload === "object") return payload._traceId || payload.traceId || payload.trace_id;
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Extract session ID from payload if it exists
|
|
169
|
+
*/
|
|
170
|
+
function extractSessionIdFromPayload(payload) {
|
|
171
|
+
if (payload && typeof payload === "object") return payload._sessionId || payload.sessionId || payload.session_id;
|
|
172
|
+
return void 0;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create OTEL context from payload
|
|
176
|
+
*/
|
|
177
|
+
function createOtelContextFromPayload(payload) {
|
|
178
|
+
return {
|
|
179
|
+
traceId: extractTraceIdFromPayload(payload),
|
|
180
|
+
sessionId: extractSessionIdFromPayload(payload),
|
|
181
|
+
metadata: payload
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* OpenTelemetry-aware console logger implementation
|
|
186
|
+
*/
|
|
187
|
+
var OtelConsoleLogger = class extends ConsoleLogger {
|
|
188
|
+
constructor(level = LogLevel.ERROR) {
|
|
189
|
+
super(level);
|
|
190
|
+
(0, import_defineProperty$1.default)(this, "context", {});
|
|
191
|
+
}
|
|
192
|
+
setContext(context) {
|
|
193
|
+
this.context = {
|
|
194
|
+
...this.context,
|
|
195
|
+
...context
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
getContext() {
|
|
199
|
+
return { ...this.context };
|
|
200
|
+
}
|
|
201
|
+
clearContext() {
|
|
202
|
+
this.context = {};
|
|
203
|
+
}
|
|
204
|
+
formatWithContext(level, message) {
|
|
205
|
+
const contextParts = [];
|
|
206
|
+
if (this.context.sessionId) contextParts.push(`session=${this.context.sessionId}`);
|
|
207
|
+
if (this.context.traceId) contextParts.push(`trace=${this.context.traceId}`);
|
|
208
|
+
if (this.context.spanId) contextParts.push(`span=${this.context.spanId}`);
|
|
209
|
+
const contextStr = contextParts.length > 0 ? ` [${contextParts.join(", ")}]` : "";
|
|
210
|
+
return `[${level.toUpperCase()}]${contextStr} ${message}`;
|
|
211
|
+
}
|
|
212
|
+
logWithContext(level, message, ...args) {
|
|
213
|
+
const levelName = LogLevel[level].toLowerCase();
|
|
214
|
+
const formattedMessage = this.formatWithContext(levelName, message);
|
|
215
|
+
switch (level) {
|
|
216
|
+
case LogLevel.TRACE:
|
|
217
|
+
if (this.shouldLog(LogLevel.TRACE)) console.trace(formattedMessage, ...args);
|
|
218
|
+
break;
|
|
219
|
+
case LogLevel.DEBUG:
|
|
220
|
+
if (this.shouldLog(LogLevel.DEBUG)) console.debug(formattedMessage, ...args);
|
|
221
|
+
break;
|
|
222
|
+
case LogLevel.INFO:
|
|
223
|
+
if (this.shouldLog(LogLevel.INFO)) console.info(formattedMessage, ...args);
|
|
224
|
+
break;
|
|
225
|
+
case LogLevel.WARN:
|
|
226
|
+
if (this.shouldLog(LogLevel.WARN)) console.warn(formattedMessage, ...args);
|
|
227
|
+
break;
|
|
228
|
+
case LogLevel.ERROR:
|
|
229
|
+
if (this.shouldLog(LogLevel.ERROR)) console.error(formattedMessage, ...args);
|
|
230
|
+
break;
|
|
231
|
+
case LogLevel.FATAL:
|
|
232
|
+
if (this.shouldLog(LogLevel.FATAL)) console.error(formattedMessage, ...args);
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
trace(message, ...args) {
|
|
237
|
+
this.logWithContext(LogLevel.TRACE, message, ...args);
|
|
238
|
+
}
|
|
239
|
+
debug(message, ...args) {
|
|
240
|
+
this.logWithContext(LogLevel.DEBUG, message, ...args);
|
|
241
|
+
}
|
|
242
|
+
info(message, ...args) {
|
|
243
|
+
this.logWithContext(LogLevel.INFO, message, ...args);
|
|
244
|
+
}
|
|
245
|
+
warn(message, ...args) {
|
|
246
|
+
this.logWithContext(LogLevel.WARN, message, ...args);
|
|
247
|
+
}
|
|
248
|
+
error(message, ...args) {
|
|
249
|
+
this.logWithContext(LogLevel.ERROR, message, ...args);
|
|
250
|
+
}
|
|
251
|
+
fatal(message, ...args) {
|
|
252
|
+
this.logWithContext(LogLevel.FATAL, message, ...args);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/ActionRegister.ts
|
|
89
258
|
var import_defineProperty = __toESM(require_defineProperty(), 1);
|
|
259
|
+
/**
|
|
260
|
+
* Core action pipeline management system
|
|
261
|
+
* @template T - Action payload map defining available actions and their payload types
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* interface AppActions extends ActionPayloadMap {
|
|
265
|
+
* increment: void;
|
|
266
|
+
* setCount: number;
|
|
267
|
+
* }
|
|
268
|
+
*
|
|
269
|
+
* const actionRegister = new ActionRegister<AppActions>();
|
|
270
|
+
*
|
|
271
|
+
* // Register handlers
|
|
272
|
+
* actionRegister.register('increment', () => console.log('Incremented'));
|
|
273
|
+
* actionRegister.register('setCount', (count) => console.log(`Count: ${count}`));
|
|
274
|
+
*
|
|
275
|
+
* // Dispatch actions
|
|
276
|
+
* await actionRegister.dispatch('increment');
|
|
277
|
+
* await actionRegister.dispatch('setCount', 42);
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
90
280
|
var ActionRegister = class {
|
|
91
|
-
constructor() {
|
|
281
|
+
constructor(config) {
|
|
92
282
|
(0, import_defineProperty.default)(this, "pipelines", /* @__PURE__ */ new Map());
|
|
93
283
|
(0, import_defineProperty.default)(this, "atomSetters", /* @__PURE__ */ new Map());
|
|
284
|
+
(0, import_defineProperty.default)(this, "handlerCounter", 0);
|
|
285
|
+
(0, import_defineProperty.default)(this, "logger", void 0);
|
|
286
|
+
const envLogLevel = getLogLevelFromEnv();
|
|
287
|
+
const configLogLevel = config?.logLevel ?? envLogLevel;
|
|
288
|
+
if (config?.logger) this.logger = config.logger;
|
|
289
|
+
else if (config?.useOtel) this.logger = new OtelConsoleLogger(configLogLevel);
|
|
290
|
+
else this.logger = new ConsoleLogger(configLogLevel);
|
|
291
|
+
if (config?.otelContext && this.logger instanceof OtelConsoleLogger) this.logger.setContext(config.otelContext);
|
|
292
|
+
this.logger.debug("ActionRegister initialized", {
|
|
293
|
+
logLevel: configLogLevel,
|
|
294
|
+
useOtel: config?.useOtel ?? false,
|
|
295
|
+
hasOtelContext: !!config?.otelContext
|
|
296
|
+
});
|
|
94
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Register a handler for an action in the pipeline
|
|
300
|
+
* @param action - The action name to handle
|
|
301
|
+
* @param handler - The handler function to execute
|
|
302
|
+
* @param config - Optional configuration for the handler
|
|
303
|
+
* @returns Unregister function to remove the handler
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* const unregister = actionRegister.register('increment', () => {
|
|
307
|
+
* console.log('Incremented!');
|
|
308
|
+
* }, { priority: 10 });
|
|
309
|
+
*
|
|
310
|
+
* // Later, remove the handler
|
|
311
|
+
* unregister();
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
95
314
|
register(action, handler, config = {}) {
|
|
96
|
-
if (!this.pipelines.has(action))
|
|
315
|
+
if (!this.pipelines.has(action)) {
|
|
316
|
+
this.pipelines.set(action, /* @__PURE__ */ new Map());
|
|
317
|
+
this.logger.debug(`Created new pipeline for action: ${String(action)}`);
|
|
318
|
+
}
|
|
97
319
|
const pipeline = this.pipelines.get(action);
|
|
98
|
-
const handlerId = config.id || `handler_${
|
|
320
|
+
const handlerId = config.id || `handler_${++this.handlerCounter}`;
|
|
99
321
|
if (pipeline.has(handlerId)) {
|
|
100
|
-
|
|
322
|
+
this.logger.warn(`Handler with id ${handlerId} already exists for action: ${String(action)}`);
|
|
101
323
|
return () => {};
|
|
102
324
|
}
|
|
103
325
|
pipeline.set(handlerId, {
|
|
104
326
|
handler,
|
|
105
327
|
config
|
|
106
328
|
});
|
|
329
|
+
this.logger.debug(`Registered handler for action: ${String(action)}`, {
|
|
330
|
+
handlerId,
|
|
331
|
+
priority: config.priority ?? 0,
|
|
332
|
+
blocking: config.blocking ?? false
|
|
333
|
+
});
|
|
107
334
|
this.sortPipeline(action);
|
|
108
335
|
return () => {
|
|
109
336
|
pipeline.delete(handlerId);
|
|
337
|
+
this.logger.debug(`Unregistered handler: ${handlerId} for action: ${String(action)}`);
|
|
110
338
|
};
|
|
111
339
|
}
|
|
112
340
|
registerAtomSetter(name, setter) {
|
|
113
341
|
this.atomSetters.set(name, setter);
|
|
342
|
+
this.logger.debug(`Registered atom setter: ${name}`);
|
|
114
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Internal dispatch implementation
|
|
346
|
+
* @internal
|
|
347
|
+
*/
|
|
115
348
|
async dispatch(action, payload) {
|
|
116
349
|
const pipeline = this.pipelines.get(action);
|
|
350
|
+
if (this.logger instanceof OtelConsoleLogger && payload) {
|
|
351
|
+
const otelContext = createOtelContextFromPayload(payload);
|
|
352
|
+
if (otelContext.traceId || otelContext.sessionId) this.logger.setContext(otelContext);
|
|
353
|
+
}
|
|
354
|
+
this.logger.debug(`Dispatching action: ${String(action)}`, { payload });
|
|
117
355
|
if (!pipeline || pipeline.size === 0) {
|
|
118
|
-
|
|
356
|
+
this.logger.warn(`No handlers registered for action: ${String(action)}`);
|
|
119
357
|
return;
|
|
120
358
|
}
|
|
121
359
|
let modifiedPayload = payload;
|
|
122
360
|
const handlers = Array.from(pipeline.values());
|
|
361
|
+
let shouldContinue = true;
|
|
362
|
+
this.logger.trace(`Executing pipeline for action: ${String(action)}`, { handlerCount: handlers.length });
|
|
123
363
|
for (const { handler, config } of handlers) {
|
|
124
|
-
|
|
364
|
+
if (!shouldContinue) break;
|
|
125
365
|
const controller = {
|
|
126
366
|
next: () => {
|
|
127
367
|
shouldContinue = true;
|
|
128
368
|
},
|
|
129
369
|
abort: (reason) => {
|
|
130
370
|
shouldContinue = false;
|
|
131
|
-
|
|
371
|
+
this.logger.warn(`Pipeline aborted: ${reason}`);
|
|
132
372
|
},
|
|
133
373
|
modifyPayload: (modifier) => {
|
|
134
374
|
modifiedPayload = modifier(modifiedPayload);
|
|
375
|
+
this.logger.trace(`Payload modified for action: ${String(action)}`);
|
|
135
376
|
}
|
|
136
377
|
};
|
|
137
378
|
try {
|
|
138
379
|
if (config.blocking) await handler(modifiedPayload, controller);
|
|
139
380
|
else handler(modifiedPayload, controller);
|
|
140
|
-
if (!shouldContinue) break;
|
|
141
381
|
} catch (error) {
|
|
142
|
-
|
|
382
|
+
this.logger.error(`Error in pipeline handler for action: ${String(action)}`, error);
|
|
143
383
|
if (config.blocking) throw error;
|
|
144
384
|
}
|
|
145
385
|
}
|
|
386
|
+
this.logger.debug(`Completed dispatching action: ${String(action)}`);
|
|
146
387
|
}
|
|
147
388
|
sortPipeline(action) {
|
|
148
389
|
const pipeline = this.pipelines.get(action);
|
|
@@ -154,11 +395,15 @@ var ActionRegister = class {
|
|
|
154
395
|
});
|
|
155
396
|
pipeline.clear();
|
|
156
397
|
sorted.forEach(([id, data]) => pipeline.set(id, data));
|
|
398
|
+
this.logger.trace(`Sorted pipeline for action: ${String(action)}`, {
|
|
399
|
+
handlerCount: sorted.length,
|
|
400
|
+
priorities: sorted.map(([, data]) => data.config.priority ?? 0)
|
|
401
|
+
});
|
|
157
402
|
}
|
|
158
403
|
};
|
|
159
404
|
|
|
160
405
|
//#endregion
|
|
161
|
-
//#region src/
|
|
406
|
+
//#region src/types.ts
|
|
162
407
|
function createAction(type, payload) {
|
|
163
408
|
return {
|
|
164
409
|
type,
|
|
@@ -169,57 +414,15 @@ function isAction(action, type) {
|
|
|
169
414
|
return action?.type === type;
|
|
170
415
|
}
|
|
171
416
|
|
|
172
|
-
//#endregion
|
|
173
|
-
//#region src/react/ActionContext.tsx
|
|
174
|
-
/**
|
|
175
|
-
* ActionRegister를 Context로 공유할 수 있는 헬퍼 함수
|
|
176
|
-
* @returns Provider, hooks를 포함한 객체
|
|
177
|
-
*/
|
|
178
|
-
function createActionContext() {
|
|
179
|
-
const ActionContext = (0, react.createContext)(null);
|
|
180
|
-
const Provider = ({ children }) => {
|
|
181
|
-
const actionRegisterRef = (0, react.useRef)(new ActionRegister());
|
|
182
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ActionContext.Provider, {
|
|
183
|
-
value: { actionRegisterRef },
|
|
184
|
-
children
|
|
185
|
-
});
|
|
186
|
-
};
|
|
187
|
-
const useActionContext = () => {
|
|
188
|
-
const context = (0, react.useContext)(ActionContext);
|
|
189
|
-
if (!context) throw new Error("useActionContext must be used within Provider");
|
|
190
|
-
return context;
|
|
191
|
-
};
|
|
192
|
-
const useAction = () => {
|
|
193
|
-
const { actionRegisterRef } = useActionContext();
|
|
194
|
-
return actionRegisterRef.current;
|
|
195
|
-
};
|
|
196
|
-
const useActionHandler = (action, handler, config) => {
|
|
197
|
-
const actionRegister = useAction();
|
|
198
|
-
const componentId = (0, react.useId)();
|
|
199
|
-
(0, react.useEffect)(() => {
|
|
200
|
-
const unregister = actionRegister.register(action, handler, {
|
|
201
|
-
...config,
|
|
202
|
-
id: config?.id || componentId
|
|
203
|
-
});
|
|
204
|
-
return unregister;
|
|
205
|
-
}, [
|
|
206
|
-
action,
|
|
207
|
-
handler,
|
|
208
|
-
config,
|
|
209
|
-
componentId,
|
|
210
|
-
actionRegister
|
|
211
|
-
]);
|
|
212
|
-
};
|
|
213
|
-
return {
|
|
214
|
-
Provider,
|
|
215
|
-
useActionContext,
|
|
216
|
-
useAction,
|
|
217
|
-
useActionHandler
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
417
|
//#endregion
|
|
222
418
|
exports.ActionRegister = ActionRegister;
|
|
419
|
+
exports.ConsoleLogger = ConsoleLogger;
|
|
420
|
+
exports.LogLevel = LogLevel;
|
|
421
|
+
exports.OtelConsoleLogger = OtelConsoleLogger;
|
|
223
422
|
exports.createAction = createAction;
|
|
224
|
-
exports.
|
|
225
|
-
exports.
|
|
423
|
+
exports.createOtelContextFromPayload = createOtelContextFromPayload;
|
|
424
|
+
exports.extractSessionIdFromPayload = extractSessionIdFromPayload;
|
|
425
|
+
exports.extractTraceIdFromPayload = extractTraceIdFromPayload;
|
|
426
|
+
exports.getLogLevelFromEnv = getLogLevelFromEnv;
|
|
427
|
+
exports.isAction = isAction;
|
|
428
|
+
exports.parseLogLevel = parseLogLevel;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,30 +1,230 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
//#region src/logger.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Log levels in order of severity (lowest to highest)
|
|
4
|
+
*/
|
|
5
|
+
declare enum LogLevel {
|
|
6
|
+
TRACE = 0,
|
|
7
|
+
DEBUG = 1,
|
|
8
|
+
INFO = 2,
|
|
9
|
+
WARN = 3,
|
|
10
|
+
ERROR = 4,
|
|
11
|
+
FATAL = 5,
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Logger interface for custom logger implementations
|
|
15
|
+
*/
|
|
16
|
+
interface Logger {
|
|
17
|
+
trace(message: string, ...args: any[]): void;
|
|
18
|
+
debug(message: string, ...args: any[]): void;
|
|
19
|
+
info(message: string, ...args: any[]): void;
|
|
20
|
+
warn(message: string, ...args: any[]): void;
|
|
21
|
+
error(message: string, ...args: any[]): void;
|
|
22
|
+
fatal(message: string, ...args: any[]): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Default console logger implementation
|
|
26
|
+
*/
|
|
27
|
+
declare class ConsoleLogger implements Logger {
|
|
28
|
+
private level;
|
|
29
|
+
constructor(level?: LogLevel);
|
|
30
|
+
protected shouldLog(level: LogLevel): boolean;
|
|
31
|
+
private formatMessage;
|
|
32
|
+
trace(message: string, ...args: any[]): void;
|
|
33
|
+
debug(message: string, ...args: any[]): void;
|
|
34
|
+
info(message: string, ...args: any[]): void;
|
|
35
|
+
warn(message: string, ...args: any[]): void;
|
|
36
|
+
error(message: string, ...args: any[]): void;
|
|
37
|
+
fatal(message: string, ...args: any[]): void;
|
|
38
|
+
setLevel(level: LogLevel): void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parse log level from string
|
|
42
|
+
*/
|
|
43
|
+
declare function parseLogLevel(level: string): LogLevel;
|
|
44
|
+
/**
|
|
45
|
+
* Get log level from environment variable or default to ERROR
|
|
46
|
+
*/
|
|
47
|
+
declare function getLogLevelFromEnv(): LogLevel;
|
|
48
|
+
/**
|
|
49
|
+
* Extract trace ID from payload if it exists
|
|
50
|
+
*/
|
|
51
|
+
declare function extractTraceIdFromPayload(payload: any): string | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Extract session ID from payload if it exists
|
|
54
|
+
*/
|
|
55
|
+
declare function extractSessionIdFromPayload(payload: any): string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Create OTEL context from payload
|
|
58
|
+
*/
|
|
59
|
+
declare function createOtelContextFromPayload(payload: any): OtelContext;
|
|
60
|
+
/**
|
|
61
|
+
* OpenTelemetry context interface for tracing
|
|
62
|
+
*/
|
|
63
|
+
interface OtelContext {
|
|
64
|
+
/** Session ID for tracking user sessions */
|
|
65
|
+
sessionId?: string;
|
|
66
|
+
/** Trace ID for distributed tracing */
|
|
67
|
+
traceId?: string;
|
|
68
|
+
/** Span ID for current operation */
|
|
69
|
+
spanId?: string;
|
|
70
|
+
/** Parent span ID for operation hierarchy */
|
|
71
|
+
parentSpanId?: string;
|
|
72
|
+
/** Additional context metadata */
|
|
73
|
+
metadata?: Record<string, any>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Extended logger interface with OpenTelemetry support
|
|
77
|
+
*/
|
|
78
|
+
interface OtelLogger extends Logger {
|
|
79
|
+
/** Set OpenTelemetry context */
|
|
80
|
+
setContext(context: OtelContext): void;
|
|
81
|
+
/** Get current OpenTelemetry context */
|
|
82
|
+
getContext(): OtelContext;
|
|
83
|
+
/** Clear OpenTelemetry context */
|
|
84
|
+
clearContext(): void;
|
|
85
|
+
/** Log with OpenTelemetry context */
|
|
86
|
+
logWithContext(level: LogLevel, message: string, ...args: any[]): void;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* OpenTelemetry-aware console logger implementation
|
|
90
|
+
*/
|
|
91
|
+
declare class OtelConsoleLogger extends ConsoleLogger implements OtelLogger {
|
|
92
|
+
private context;
|
|
93
|
+
constructor(level?: LogLevel);
|
|
94
|
+
setContext(context: OtelContext): void;
|
|
95
|
+
getContext(): OtelContext;
|
|
96
|
+
clearContext(): void;
|
|
97
|
+
private formatWithContext;
|
|
98
|
+
logWithContext(level: LogLevel, message: string, ...args: any[]): void;
|
|
99
|
+
trace(message: string, ...args: any[]): void;
|
|
100
|
+
debug(message: string, ...args: any[]): void;
|
|
101
|
+
info(message: string, ...args: any[]): void;
|
|
102
|
+
warn(message: string, ...args: any[]): void;
|
|
103
|
+
error(message: string, ...args: any[]): void;
|
|
104
|
+
fatal(message: string, ...args: any[]): void;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/ActionRegister.d.ts
|
|
109
|
+
/**
|
|
110
|
+
* Controller object provided to action handlers for pipeline management
|
|
111
|
+
* @template T - The type of the payload being processed
|
|
112
|
+
*/
|
|
4
113
|
type PipelineController<T = any> = {
|
|
114
|
+
/** Continue to the next handler in the pipeline */
|
|
5
115
|
next: () => void;
|
|
116
|
+
/** Abort the pipeline execution with an optional reason */
|
|
6
117
|
abort: (reason?: string) => void;
|
|
118
|
+
/** Modify the payload that will be passed to subsequent handlers */
|
|
7
119
|
modifyPayload: (modifier: (payload: T) => T) => void;
|
|
8
120
|
};
|
|
121
|
+
/**
|
|
122
|
+
* Action handler function that processes actions in the pipeline
|
|
123
|
+
* @template T - The type of the payload
|
|
124
|
+
* @param payload - The data passed to the handler
|
|
125
|
+
* @param controller - Pipeline controller for flow management
|
|
126
|
+
* @returns void or Promise<void> for async handlers
|
|
127
|
+
*/
|
|
9
128
|
type ActionHandler<T = any> = (payload: T, controller: PipelineController<T>) => void | Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Configuration options for action handlers
|
|
131
|
+
*/
|
|
10
132
|
type HandlerConfig = {
|
|
133
|
+
/** Priority level (higher numbers execute first). Default: 0 */
|
|
11
134
|
priority?: number;
|
|
135
|
+
/** Unique identifier for the handler. Auto-generated if not provided */
|
|
12
136
|
id?: string;
|
|
137
|
+
/** Whether to wait for async handlers to complete. Default: false */
|
|
13
138
|
blocking?: boolean;
|
|
14
139
|
};
|
|
140
|
+
/**
|
|
141
|
+
* Base interface for defining action payload mappings
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* interface MyActions extends ActionPayloadMap {
|
|
145
|
+
* increment: void;
|
|
146
|
+
* setCount: number;
|
|
147
|
+
* updateUser: { id: string; name: string };
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
15
151
|
interface ActionPayloadMap {}
|
|
152
|
+
/**
|
|
153
|
+
* Configuration options for ActionRegister
|
|
154
|
+
*/
|
|
155
|
+
interface ActionRegisterConfig {
|
|
156
|
+
/** Custom logger implementation. Defaults to ConsoleLogger */
|
|
157
|
+
logger?: Logger;
|
|
158
|
+
/** Log level for the logger. Defaults to ERROR if not provided */
|
|
159
|
+
logLevel?: LogLevel;
|
|
160
|
+
/** OpenTelemetry context for tracing */
|
|
161
|
+
otelContext?: OtelContext;
|
|
162
|
+
/** Whether to use OTEL-aware logger. Defaults to false */
|
|
163
|
+
useOtel?: boolean;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Core action pipeline management system
|
|
167
|
+
* @template T - Action payload map defining available actions and their payload types
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* interface AppActions extends ActionPayloadMap {
|
|
171
|
+
* increment: void;
|
|
172
|
+
* setCount: number;
|
|
173
|
+
* }
|
|
174
|
+
*
|
|
175
|
+
* const actionRegister = new ActionRegister<AppActions>();
|
|
176
|
+
*
|
|
177
|
+
* // Register handlers
|
|
178
|
+
* actionRegister.register('increment', () => console.log('Incremented'));
|
|
179
|
+
* actionRegister.register('setCount', (count) => console.log(`Count: ${count}`));
|
|
180
|
+
*
|
|
181
|
+
* // Dispatch actions
|
|
182
|
+
* await actionRegister.dispatch('increment');
|
|
183
|
+
* await actionRegister.dispatch('setCount', 42);
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
16
186
|
declare class ActionRegister<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
17
187
|
private pipelines;
|
|
18
188
|
private atomSetters;
|
|
189
|
+
private handlerCounter;
|
|
190
|
+
readonly logger: Logger;
|
|
191
|
+
constructor(config?: ActionRegisterConfig);
|
|
192
|
+
/**
|
|
193
|
+
* Register a handler for an action in the pipeline
|
|
194
|
+
* @param action - The action name to handle
|
|
195
|
+
* @param handler - The handler function to execute
|
|
196
|
+
* @param config - Optional configuration for the handler
|
|
197
|
+
* @returns Unregister function to remove the handler
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* const unregister = actionRegister.register('increment', () => {
|
|
201
|
+
* console.log('Incremented!');
|
|
202
|
+
* }, { priority: 10 });
|
|
203
|
+
*
|
|
204
|
+
* // Later, remove the handler
|
|
205
|
+
* unregister();
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
19
208
|
register<K extends keyof T>(action: K, handler: ActionHandler<T[K]>, config?: HandlerConfig): () => void;
|
|
20
209
|
registerAtomSetter(name: string, setter: Function): void;
|
|
210
|
+
/**
|
|
211
|
+
* Dispatch an action through the pipeline (for actions without payload)
|
|
212
|
+
* @param action - The action to dispatch
|
|
213
|
+
* @returns Promise that resolves when all handlers complete
|
|
214
|
+
*/
|
|
21
215
|
dispatch<K extends keyof T>(action: T[K] extends void ? K : never): Promise<void>;
|
|
216
|
+
/**
|
|
217
|
+
* Dispatch an action through the pipeline (for actions with payload)
|
|
218
|
+
* @param action - The action to dispatch
|
|
219
|
+
* @param payload - The payload data to pass to handlers
|
|
220
|
+
* @returns Promise that resolves when all handlers complete
|
|
221
|
+
*/
|
|
22
222
|
dispatch<K extends keyof T>(action: K, payload: T[K]): Promise<void>;
|
|
23
223
|
private sortPipeline;
|
|
24
224
|
}
|
|
25
225
|
//# sourceMappingURL=ActionRegister.d.ts.map
|
|
26
226
|
//#endregion
|
|
27
|
-
//#region src/
|
|
227
|
+
//#region src/types.d.ts
|
|
28
228
|
interface BaseActionPayloadMap {}
|
|
29
229
|
type ActionType<T extends Record<string, any>> = keyof T;
|
|
30
230
|
type ActionPayload<T extends Record<string, any>, K extends keyof T> = T[K];
|
|
@@ -38,32 +238,7 @@ declare function isAction<T extends Record<string, any>, K extends keyof T>(acti
|
|
|
38
238
|
payload: T[K];
|
|
39
239
|
};
|
|
40
240
|
//# sourceMappingURL=types.d.ts.map
|
|
41
|
-
//#endregion
|
|
42
|
-
//#region src/react/ActionContext.d.ts
|
|
43
|
-
/**
|
|
44
|
-
* Context type for ActionRegister
|
|
45
|
-
*/
|
|
46
|
-
interface ActionContextType<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
47
|
-
actionRegisterRef: React.RefObject<ActionRegister<T>>;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Return type for createActionContext
|
|
51
|
-
*/
|
|
52
|
-
interface ActionContextReturn<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
53
|
-
Provider: React.FC<{
|
|
54
|
-
children: ReactNode;
|
|
55
|
-
}>;
|
|
56
|
-
useActionContext: () => ActionContextType<T>;
|
|
57
|
-
useAction: () => ActionRegister<T>;
|
|
58
|
-
useActionHandler: <K extends keyof T>(action: K, handler: ActionHandler<T[K]>, config?: HandlerConfig) => void;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* ActionRegister를 Context로 공유할 수 있는 헬퍼 함수
|
|
62
|
-
* @returns Provider, hooks를 포함한 객체
|
|
63
|
-
*/
|
|
64
|
-
declare function createActionContext<T extends ActionPayloadMap = ActionPayloadMap>(): ActionContextReturn<T>;
|
|
65
|
-
//# sourceMappingURL=ActionContext.d.ts.map
|
|
66
241
|
|
|
67
242
|
//#endregion
|
|
68
|
-
export { ActionHandler, ActionHandlerMap, ActionPayload, ActionPayloadMap, ActionRegister, ActionType, BaseActionPayloadMap, HandlerConfig, PipelineController, createAction,
|
|
243
|
+
export { ActionHandler, ActionHandlerMap, ActionPayload, ActionPayloadMap, ActionRegister, ActionRegisterConfig, ActionType, BaseActionPayloadMap, ConsoleLogger, HandlerConfig, LogLevel, Logger, OtelConsoleLogger, OtelContext, OtelLogger, PipelineController, createAction, createOtelContextFromPayload, extractSessionIdFromPayload, extractTraceIdFromPayload, getLogLevelFromEnv, isAction, parseLogLevel };
|
|
69
244
|
//# sourceMappingURL=index.d.cts.map
|