@context-action/core 0.0.2 → 0.0.4

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 ADDED
@@ -0,0 +1,272 @@
1
+ # @context-action/core
2
+
3
+ Type-safe action pipeline management library for JavaScript/TypeScript applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @context-action/core dotenv
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ### 1. Setup Environment Configuration
14
+
15
+ ```bash
16
+ # Copy sample configuration
17
+ cp .env.sample .env
18
+
19
+ # Quick setup for maximum debugging detail
20
+ echo "NODE_ENV=development" >> .env
21
+ echo "CONTEXT_ACTION_TRACE=true" >> .env
22
+ echo "CONTEXT_ACTION_DEBUG=true" >> .env
23
+ echo "CONTEXT_ACTION_LOGGER_NAME=MyApp" >> .env
24
+ ```
25
+
26
+ ### 2. Basic Usage
27
+
28
+ ```typescript
29
+ // Load environment variables first
30
+ import 'dotenv/config';
31
+ import { ActionRegister } from '@context-action/core';
32
+
33
+ // Define your action types
34
+ interface MyActions {
35
+ increment: void;
36
+ setCount: number;
37
+ updateUser: { id: string; name: string };
38
+ }
39
+
40
+ // Create action register
41
+ const actions = new ActionRegister<MyActions>();
42
+
43
+ // Register handlers
44
+ actions.register('increment', (_, controller) => {
45
+ console.log('Increment called');
46
+ controller.next();
47
+ });
48
+
49
+ actions.register('setCount', (count, controller) => {
50
+ console.log(`Setting count to: ${count}`);
51
+ controller.next();
52
+ });
53
+
54
+ // Dispatch actions
55
+ await actions.dispatch('increment');
56
+ await actions.dispatch('setCount', 42);
57
+ ```
58
+
59
+ ## Environment Variables
60
+
61
+ | Variable | Description | Default | Max Detail |
62
+ |----------|-------------|---------|------------|
63
+ | `CONTEXT_ACTION_TRACE` | Enable detailed trace logging | `false` | `true` |
64
+ | `CONTEXT_ACTION_DEBUG` | Enable debug mode | `false` | `true` |
65
+ | `CONTEXT_ACTION_LOG_LEVEL` | Set specific log level (`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `NONE`) | `ERROR` | `TRACE` |
66
+ | `CONTEXT_ACTION_LOGGER_NAME` | Custom logger name | `ActionRegister` | `YourAppName` |
67
+ | `NODE_ENV` | Auto-configure logging (`development` = DEBUG, `production` = ERROR) | - | `development` |
68
+
69
+ **💡 Tip**: For maximum debugging detail, set all variables in the "Max Detail" column.
70
+
71
+ ## Features
72
+
73
+ - **Type-safe actions** - Full TypeScript support with compile-time checking
74
+ - **Priority-based execution** - Control handler execution order
75
+ - **Pipeline control** - Abort, modify payloads, conditional execution
76
+ - **Event system** - Listen to action lifecycle events
77
+ - **Comprehensive logging** - Detailed trace logging for debugging
78
+ - **Environment-based configuration** - Easy .env setup
79
+
80
+ ## Advanced Usage
81
+
82
+ ### Handler Configuration
83
+
84
+ ```typescript
85
+ actions.register('myAction', handler, {
86
+ priority: 10, // Higher priority executes first
87
+ blocking: true, // Wait for async handlers
88
+ once: true, // Remove after first execution
89
+ condition: () => shouldRun, // Conditional execution
90
+ id: 'my-handler' // Custom handler ID
91
+ });
92
+ ```
93
+
94
+ ### Pipeline Control
95
+
96
+ ```typescript
97
+ actions.register('validate', (data, controller) => {
98
+ if (!data.isValid) {
99
+ controller.abort('Validation failed');
100
+ return;
101
+ }
102
+
103
+ // Modify data for next handlers
104
+ controller.modifyPayload(data => ({ ...data, validated: true }));
105
+ controller.next();
106
+ });
107
+ ```
108
+
109
+ ### Event Listeners
110
+
111
+ ```typescript
112
+ actions.on('action:start', ({ action, payload }) => {
113
+ console.log(`Starting ${action}`, payload);
114
+ });
115
+
116
+ actions.on('action:complete', ({ action, metrics }) => {
117
+ console.log(`Completed ${action} in ${metrics.executionTime}ms`);
118
+ });
119
+ ```
120
+
121
+ ## Debugging
122
+
123
+ ### Maximum Detail Logging
124
+
125
+ For the most comprehensive debugging experience, use this .env configuration:
126
+
127
+ ```bash
128
+ # .env - Maximum detail logging configuration
129
+ NODE_ENV=development
130
+ CONTEXT_ACTION_TRACE=true
131
+ CONTEXT_ACTION_DEBUG=true
132
+ CONTEXT_ACTION_LOGGER_NAME=DetailedApp
133
+
134
+ # This configuration will show:
135
+ # - Every function call and return
136
+ # - Handler registration and execution details
137
+ # - Pipeline flow and state changes
138
+ # - Payload modifications and conditions
139
+ # - Performance metrics and timing
140
+ # - Error details and stack traces
141
+ ```
142
+
143
+ ### Common Debug Configurations
144
+
145
+ ```bash
146
+ # Development - Balanced detail
147
+ NODE_ENV=development
148
+ CONTEXT_ACTION_DEBUG=true
149
+ CONTEXT_ACTION_LOGGER_NAME=DevApp
150
+
151
+ # Production Debug - Errors only with context
152
+ NODE_ENV=production
153
+ CONTEXT_ACTION_LOG_LEVEL=ERROR
154
+ CONTEXT_ACTION_DEBUG=true
155
+ CONTEXT_ACTION_LOGGER_NAME=ProdApp
156
+
157
+ # Issue Investigation - Specific level
158
+ CONTEXT_ACTION_LOG_LEVEL=DEBUG
159
+ CONTEXT_ACTION_DEBUG=true
160
+ CONTEXT_ACTION_LOGGER_NAME=InvestigationSession
161
+ ```
162
+
163
+ ### Test Your Configuration
164
+
165
+ After setting up your .env file, test that maximum detail logging is working:
166
+
167
+ ```bash
168
+ # Create a quick test file
169
+ echo "import 'dotenv/config';
170
+ import { ActionRegister } from '@context-action/core';
171
+
172
+ const actions = new ActionRegister();
173
+ actions.register('test', (payload, controller) => {
174
+ console.log('Handler executed:', payload);
175
+ controller.next();
176
+ });
177
+ await actions.dispatch('test', { message: 'Hello!' });" > test-logging.js
178
+
179
+ # Run the test
180
+ node test-logging.js
181
+ ```
182
+
183
+ You should see detailed TRACE and DEBUG output if configured correctly.
184
+
185
+ ### Troubleshooting
186
+
187
+ **Not seeing trace output?**
188
+
189
+ 1. **Check dependencies**: Make sure `dotenv` is installed
190
+ ```bash
191
+ npm install dotenv
192
+ # or
193
+ pnpm install dotenv
194
+ ```
195
+
196
+ 2. **Verify .env file**: Confirm your .env file exists and has the correct settings
197
+ ```bash
198
+ cat .env
199
+ # Should show: CONTEXT_ACTION_TRACE=true
200
+ ```
201
+
202
+ 3. **Check import order**: `dotenv/config` must be imported first
203
+ ```typescript
204
+ import 'dotenv/config'; // MUST be first
205
+ import { ActionRegister } from '@context-action/core';
206
+ ```
207
+
208
+ 4. **Rebuild if needed**: After installing dotenv, rebuild the project if using a bundler
209
+
210
+ For detailed debugging information, see [TRACE_LOGGING.md](./TRACE_LOGGING.md).
211
+
212
+ ## API Reference
213
+
214
+ ### ActionRegister<T>
215
+
216
+ Main class for managing action pipelines.
217
+
218
+ - `register<K>(action, handler, config?)` - Register action handler
219
+ - `dispatch<K>(action, payload?)` - Dispatch action through pipeline
220
+ - `getHandlerCount(action)` - Get number of handlers for action
221
+ - `hasHandlers(action)` - Check if action has handlers
222
+ - `clearAction(action)` - Remove all handlers for action
223
+ - `clearAll()` - Remove all handlers
224
+ - `on(event, handler)` - Add event listener
225
+
226
+ ### Configuration Options
227
+
228
+ ```typescript
229
+ interface ActionRegisterConfig {
230
+ logger?: Logger; // Custom logger implementation
231
+ logLevel?: LogLevel; // Log filtering level
232
+ name?: string; // Logger name
233
+ debug?: boolean; // Enable debug mode
234
+ }
235
+ ```
236
+
237
+ ## TypeScript Support
238
+
239
+ Full TypeScript support with compile-time type checking:
240
+
241
+ ```typescript
242
+ interface AppActions {
243
+ // Action without payload
244
+ reset: void;
245
+
246
+ // Action with payload
247
+ setUser: { id: string; name: string };
248
+
249
+ // Action with optional payload
250
+ navigate: string | undefined;
251
+ }
252
+
253
+ const actions = new ActionRegister<AppActions>();
254
+
255
+ // ✅ Type-safe dispatch
256
+ await actions.dispatch('reset');
257
+ await actions.dispatch('setUser', { id: '1', name: 'John' });
258
+
259
+ // ❌ TypeScript error - missing payload
260
+ await actions.dispatch('setUser');
261
+
262
+ // ❌ TypeScript error - wrong payload type
263
+ await actions.dispatch('setUser', 'invalid');
264
+ ```
265
+
266
+ ## License
267
+
268
+ Apache-2.0
269
+
270
+ ## Contributing
271
+
272
+ See the main [repository](https://github.com/mineclover/context-action) for contribution guidelines.