@flowcore/pathways 0.7.0 → 0.9.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/CHANGELOG.md +19 -0
- package/README.md +95 -0
- package/esm/pathways/builder.d.ts +45 -6
- package/esm/pathways/builder.d.ts.map +1 -1
- package/esm/pathways/builder.js +106 -43
- package/esm/pathways/index.d.ts +2 -0
- package/esm/pathways/index.d.ts.map +1 -1
- package/esm/pathways/index.js +2 -0
- package/esm/pathways/kv/kv-adapter.d.ts +59 -2
- package/esm/pathways/kv/kv-adapter.d.ts.map +1 -1
- package/esm/pathways/kv/kv-adapter.js +31 -1
- package/esm/pathways/postgres/postgres-pathway-state.d.ts +131 -1
- package/esm/pathways/postgres/postgres-pathway-state.d.ts.map +1 -1
- package/esm/pathways/postgres/postgres-pathway-state.js +131 -1
- package/esm/pathways/session-pathway.d.ts +99 -0
- package/esm/pathways/session-pathway.d.ts.map +1 -0
- package/esm/pathways/session-pathway.js +138 -0
- package/esm/pathways/types.d.ts +5 -0
- package/esm/pathways/types.d.ts.map +1 -1
- package/esm/router/index.d.ts +83 -2
- package/esm/router/index.d.ts.map +1 -1
- package/esm/router/index.js +83 -2
- package/package.json +1 -1
- package/script/pathways/builder.d.ts +45 -6
- package/script/pathways/builder.d.ts.map +1 -1
- package/script/pathways/builder.js +106 -43
- package/script/pathways/index.d.ts +2 -0
- package/script/pathways/index.d.ts.map +1 -1
- package/script/pathways/index.js +2 -0
- package/script/pathways/kv/kv-adapter.d.ts +59 -2
- package/script/pathways/kv/kv-adapter.d.ts.map +1 -1
- package/script/pathways/kv/kv-adapter.js +31 -1
- package/script/pathways/postgres/postgres-pathway-state.d.ts +131 -1
- package/script/pathways/postgres/postgres-pathway-state.d.ts.map +1 -1
- package/script/pathways/postgres/postgres-pathway-state.js +131 -1
- package/script/pathways/session-pathway.d.ts +99 -0
- package/script/pathways/session-pathway.d.ts.map +1 -0
- package/script/pathways/session-pathway.js +142 -0
- package/script/pathways/types.d.ts +5 -0
- package/script/pathways/types.d.ts.map +1 -1
- package/script/router/index.d.ts +83 -2
- package/script/router/index.d.ts.map +1 -1
- package/script/router/index.js +83 -2
package/esm/router/index.d.ts
CHANGED
|
@@ -11,6 +11,49 @@ import type { PathwaysBuilder } from "../pathways/index.js";
|
|
|
11
11
|
import type { Logger } from "../pathways/logger.js";
|
|
12
12
|
/**
|
|
13
13
|
* Router class that handles directing events to the appropriate pathway handlers
|
|
14
|
+
*
|
|
15
|
+
* The PathwayRouter serves as a bridge between incoming webhook events and the PathwaysBuilder,
|
|
16
|
+
* ensuring events are routed to the correct pathway handlers based on flow type and event type.
|
|
17
|
+
*
|
|
18
|
+
* Key features:
|
|
19
|
+
* - Secure authentication using a secret key
|
|
20
|
+
* - Automatic mapping of events to the correct pathway handlers
|
|
21
|
+
* - Compatibility with both legacy and modern Flowcore event formats
|
|
22
|
+
* - Detailed error handling and logging
|
|
23
|
+
*
|
|
24
|
+
* Use cases:
|
|
25
|
+
* - Building webhook endpoints that receive Flowcore events
|
|
26
|
+
* - Creating API routes that process events from external systems
|
|
27
|
+
* - Implementing event-driven microservices that consume Flowcore events
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // Create a router with authentication
|
|
32
|
+
* const SECRET_KEY = "your-webhook-secret";
|
|
33
|
+
* const router = new PathwayRouter(pathwaysBuilder, SECRET_KEY);
|
|
34
|
+
*
|
|
35
|
+
* // In your HTTP handler:
|
|
36
|
+
* async function handleWebhook(req: Request) {
|
|
37
|
+
* const event = await req.json();
|
|
38
|
+
* const secret = req.headers.get("X-Webhook-Secret");
|
|
39
|
+
*
|
|
40
|
+
* try {
|
|
41
|
+
* const result = await router.processEvent(event, secret);
|
|
42
|
+
* return new Response(JSON.stringify(result), {
|
|
43
|
+
* status: 200,
|
|
44
|
+
* headers: { "Content-Type": "application/json" }
|
|
45
|
+
* });
|
|
46
|
+
* } catch (error) {
|
|
47
|
+
* console.error("Error processing event:", error);
|
|
48
|
+
* return new Response(JSON.stringify({
|
|
49
|
+
* error: error.message
|
|
50
|
+
* }), {
|
|
51
|
+
* status: 401,
|
|
52
|
+
* headers: { "Content-Type": "application/json" }
|
|
53
|
+
* });
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
14
57
|
*/
|
|
15
58
|
export declare class PathwayRouter {
|
|
16
59
|
private readonly pathways;
|
|
@@ -28,10 +71,48 @@ export declare class PathwayRouter {
|
|
|
28
71
|
/**
|
|
29
72
|
* Process an incoming event by routing it to the appropriate pathway handler
|
|
30
73
|
*
|
|
31
|
-
*
|
|
74
|
+
* This method handles the complete lifecycle of an incoming event:
|
|
75
|
+
* 1. Validates the authentication using the provided secret key
|
|
76
|
+
* 2. Maps the event to the correct pathway based on flowType and eventType
|
|
77
|
+
* 3. Delegates processing to the PathwaysBuilder
|
|
78
|
+
* 4. Provides detailed error handling and feedback
|
|
79
|
+
*
|
|
80
|
+
* The method supports both modern Flowcore events and legacy events that used
|
|
81
|
+
* the "aggregator" field instead of "flowType". It automatically converts legacy
|
|
82
|
+
* events to the modern format before processing.
|
|
83
|
+
*
|
|
84
|
+
* @param event The event to process, containing flowType, eventType, and payload
|
|
32
85
|
* @param providedSecret The secret key provided for authentication
|
|
33
86
|
* @returns Result of the event processing with success status and message
|
|
34
|
-
*
|
|
87
|
+
*
|
|
88
|
+
* @throws Error if authentication fails (401 unauthorized)
|
|
89
|
+
* @throws Error if the pathway is not found (404 not found)
|
|
90
|
+
* @throws Error if processing fails (includes the original error message)
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // Basic usage
|
|
95
|
+
* try {
|
|
96
|
+
* const result = await router.processEvent(incomingEvent, secretFromHeader);
|
|
97
|
+
* console.log("Success:", result.message);
|
|
98
|
+
* } catch (error) {
|
|
99
|
+
* console.error("Failed to process event:", error.message);
|
|
100
|
+
* }
|
|
101
|
+
*
|
|
102
|
+
* // With error handling for different error types
|
|
103
|
+
* try {
|
|
104
|
+
* const result = await router.processEvent(event, secret);
|
|
105
|
+
* return { status: 200, body: result };
|
|
106
|
+
* } catch (error) {
|
|
107
|
+
* if (error.message.includes("Invalid secret key")) {
|
|
108
|
+
* return { status: 401, body: { error: "Unauthorized" } };
|
|
109
|
+
* } else if (error.message.includes("not found")) {
|
|
110
|
+
* return { status: 404, body: { error: "Pathway not found" } };
|
|
111
|
+
* } else {
|
|
112
|
+
* return { status: 500, body: { error: "Processing failed" } };
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
35
116
|
*/
|
|
36
117
|
processEvent(event: FlowcoreLegacyEvent, providedSecret: string): Promise<{
|
|
37
118
|
success: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAErE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAGnD
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAErE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAGnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,qBAAa,aAAa;IAatB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAb5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAEhC;;;;;;;OAOG;gBAGgB,QAAQ,EAAE,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EAC9C,SAAS,EAAE,MAAM,EAClC,MAAM,CAAC,EAAE,MAAM;IAajB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6CG;IACG,YAAY,CAAC,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAkDvH"}
|
package/esm/router/index.js
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
import { NoopLogger } from "../pathways/logger.js";
|
|
2
2
|
/**
|
|
3
3
|
* Router class that handles directing events to the appropriate pathway handlers
|
|
4
|
+
*
|
|
5
|
+
* The PathwayRouter serves as a bridge between incoming webhook events and the PathwaysBuilder,
|
|
6
|
+
* ensuring events are routed to the correct pathway handlers based on flow type and event type.
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Secure authentication using a secret key
|
|
10
|
+
* - Automatic mapping of events to the correct pathway handlers
|
|
11
|
+
* - Compatibility with both legacy and modern Flowcore event formats
|
|
12
|
+
* - Detailed error handling and logging
|
|
13
|
+
*
|
|
14
|
+
* Use cases:
|
|
15
|
+
* - Building webhook endpoints that receive Flowcore events
|
|
16
|
+
* - Creating API routes that process events from external systems
|
|
17
|
+
* - Implementing event-driven microservices that consume Flowcore events
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create a router with authentication
|
|
22
|
+
* const SECRET_KEY = "your-webhook-secret";
|
|
23
|
+
* const router = new PathwayRouter(pathwaysBuilder, SECRET_KEY);
|
|
24
|
+
*
|
|
25
|
+
* // In your HTTP handler:
|
|
26
|
+
* async function handleWebhook(req: Request) {
|
|
27
|
+
* const event = await req.json();
|
|
28
|
+
* const secret = req.headers.get("X-Webhook-Secret");
|
|
29
|
+
*
|
|
30
|
+
* try {
|
|
31
|
+
* const result = await router.processEvent(event, secret);
|
|
32
|
+
* return new Response(JSON.stringify(result), {
|
|
33
|
+
* status: 200,
|
|
34
|
+
* headers: { "Content-Type": "application/json" }
|
|
35
|
+
* });
|
|
36
|
+
* } catch (error) {
|
|
37
|
+
* console.error("Error processing event:", error);
|
|
38
|
+
* return new Response(JSON.stringify({
|
|
39
|
+
* error: error.message
|
|
40
|
+
* }), {
|
|
41
|
+
* status: 401,
|
|
42
|
+
* headers: { "Content-Type": "application/json" }
|
|
43
|
+
* });
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
4
47
|
*/
|
|
5
48
|
export class PathwayRouter {
|
|
6
49
|
/**
|
|
@@ -43,10 +86,48 @@ export class PathwayRouter {
|
|
|
43
86
|
/**
|
|
44
87
|
* Process an incoming event by routing it to the appropriate pathway handler
|
|
45
88
|
*
|
|
46
|
-
*
|
|
89
|
+
* This method handles the complete lifecycle of an incoming event:
|
|
90
|
+
* 1. Validates the authentication using the provided secret key
|
|
91
|
+
* 2. Maps the event to the correct pathway based on flowType and eventType
|
|
92
|
+
* 3. Delegates processing to the PathwaysBuilder
|
|
93
|
+
* 4. Provides detailed error handling and feedback
|
|
94
|
+
*
|
|
95
|
+
* The method supports both modern Flowcore events and legacy events that used
|
|
96
|
+
* the "aggregator" field instead of "flowType". It automatically converts legacy
|
|
97
|
+
* events to the modern format before processing.
|
|
98
|
+
*
|
|
99
|
+
* @param event The event to process, containing flowType, eventType, and payload
|
|
47
100
|
* @param providedSecret The secret key provided for authentication
|
|
48
101
|
* @returns Result of the event processing with success status and message
|
|
49
|
-
*
|
|
102
|
+
*
|
|
103
|
+
* @throws Error if authentication fails (401 unauthorized)
|
|
104
|
+
* @throws Error if the pathway is not found (404 not found)
|
|
105
|
+
* @throws Error if processing fails (includes the original error message)
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* // Basic usage
|
|
110
|
+
* try {
|
|
111
|
+
* const result = await router.processEvent(incomingEvent, secretFromHeader);
|
|
112
|
+
* console.log("Success:", result.message);
|
|
113
|
+
* } catch (error) {
|
|
114
|
+
* console.error("Failed to process event:", error.message);
|
|
115
|
+
* }
|
|
116
|
+
*
|
|
117
|
+
* // With error handling for different error types
|
|
118
|
+
* try {
|
|
119
|
+
* const result = await router.processEvent(event, secret);
|
|
120
|
+
* return { status: 200, body: result };
|
|
121
|
+
* } catch (error) {
|
|
122
|
+
* if (error.message.includes("Invalid secret key")) {
|
|
123
|
+
* return { status: 401, body: { error: "Unauthorized" } };
|
|
124
|
+
* } else if (error.message.includes("not found")) {
|
|
125
|
+
* return { status: 404, body: { error: "Pathway not found" } };
|
|
126
|
+
* } else {
|
|
127
|
+
* return { status: 500, body: { error: "Processing failed" } };
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
50
131
|
*/
|
|
51
132
|
async processEvent(event, providedSecret) {
|
|
52
133
|
// Validate secret key
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flowcore/pathways",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "A TypeScript Library for creating Flowcore Pathways, simplifying the integration with the flowcore platform",
|
|
5
5
|
"homepage": "https://github.com/flowcore-io/flowcore-sdk#readme",
|
|
6
6
|
"repository": {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Static, TSchema } from "@sinclair/typebox";
|
|
2
2
|
import type { WebhookSendOptions } from "@flowcore/sdk-transformer-core";
|
|
3
3
|
import type { FlowcoreEvent } from "../contracts/event.js";
|
|
4
|
+
import type { KvAdapter } from "./kv/kv-adapter.js";
|
|
4
5
|
import type { Logger } from "./logger.js";
|
|
5
6
|
import type { EventMetadata, PathwayContract, PathwayKey, PathwayState, PathwayWriteOptions, WritablePathway } from "./types.js";
|
|
6
7
|
/**
|
|
@@ -62,6 +63,7 @@ export declare class PathwaysBuilder<TPathway extends Record<string, unknown> =
|
|
|
62
63
|
private pathwayTimeoutMs;
|
|
63
64
|
private auditHandler?;
|
|
64
65
|
private userIdResolver?;
|
|
66
|
+
private readonly sessionUserResolvers;
|
|
65
67
|
private readonly logger;
|
|
66
68
|
private readonly baseUrl;
|
|
67
69
|
private readonly tenant;
|
|
@@ -76,14 +78,16 @@ export declare class PathwaysBuilder<TPathway extends Record<string, unknown> =
|
|
|
76
78
|
* @param options.apiKey The API key for authentication
|
|
77
79
|
* @param options.pathwayTimeoutMs Optional timeout for pathway processing in milliseconds
|
|
78
80
|
* @param options.logger Optional logger instance
|
|
81
|
+
* @param options.sessionUserResolvers Optional KvAdapter instance for session-specific user resolvers
|
|
79
82
|
*/
|
|
80
|
-
constructor({ baseUrl, tenant, dataCore, apiKey, pathwayTimeoutMs, logger, }: {
|
|
83
|
+
constructor({ baseUrl, tenant, dataCore, apiKey, pathwayTimeoutMs, logger, sessionUserResolvers, }: {
|
|
81
84
|
baseUrl: string;
|
|
82
85
|
tenant: string;
|
|
83
86
|
dataCore: string;
|
|
84
87
|
apiKey: string;
|
|
85
88
|
pathwayTimeoutMs?: number;
|
|
86
89
|
logger?: Logger;
|
|
90
|
+
sessionUserResolvers?: KvAdapter;
|
|
87
91
|
});
|
|
88
92
|
/**
|
|
89
93
|
* Configures the PathwaysBuilder to use a custom pathway state implementation
|
|
@@ -103,6 +107,46 @@ export declare class PathwaysBuilder<TPathway extends Record<string, unknown> =
|
|
|
103
107
|
* @returns The PathwaysBuilder instance with custom user ID resolver configured
|
|
104
108
|
*/
|
|
105
109
|
withUserResolver(resolver: UserIdResolver): PathwaysBuilder<TPathway, TWritablePaths>;
|
|
110
|
+
/**
|
|
111
|
+
* Registers a user resolver for a specific session
|
|
112
|
+
*
|
|
113
|
+
* Session-specific user resolvers allow you to associate different user IDs with different
|
|
114
|
+
* sessions, which is useful in multi-user applications or when tracking user actions across
|
|
115
|
+
* different sessions.
|
|
116
|
+
*
|
|
117
|
+
* The resolver is stored in a key-value store with a TTL (time to live), and will be used
|
|
118
|
+
* to resolve the user ID when operations are performed with the given session ID. If the resolver
|
|
119
|
+
* expires, it will need to be registered again.
|
|
120
|
+
*
|
|
121
|
+
* This feature works in conjunction with the SessionPathwayBuilder to provide a complete
|
|
122
|
+
* session management solution.
|
|
123
|
+
*
|
|
124
|
+
* @param sessionId The session ID to associate with this resolver
|
|
125
|
+
* @param resolver The resolver function that resolves to the user ID for this session
|
|
126
|
+
* @returns The PathwaysBuilder instance for chaining
|
|
127
|
+
*
|
|
128
|
+
* @throws Error if session user resolvers are not configured (sessionUserResolvers not provided in constructor)
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* // Register a resolver for a specific session
|
|
133
|
+
* pathwaysBuilder.withSessionUserResolver("session-123", async () => {
|
|
134
|
+
* return "user-456"; // Return the user ID for this session
|
|
135
|
+
* });
|
|
136
|
+
*
|
|
137
|
+
* // Use with SessionPathwayBuilder
|
|
138
|
+
* const session = new SessionPathwayBuilder(pathwaysBuilder, "session-123");
|
|
139
|
+
* await session.write("user/action", actionData);
|
|
140
|
+
* // The user ID will be automatically included in the metadata
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
withSessionUserResolver(sessionId: string, resolver: UserIdResolver): PathwaysBuilder<TPathway, TWritablePaths>;
|
|
144
|
+
/**
|
|
145
|
+
* Gets a user resolver for a specific session ID
|
|
146
|
+
* @param sessionId The session ID to get the resolver for
|
|
147
|
+
* @returns The resolver function for the session, or undefined if none exists
|
|
148
|
+
*/
|
|
149
|
+
getSessionUserResolver(sessionId: string): UserIdResolver | undefined;
|
|
106
150
|
/**
|
|
107
151
|
* Process a pathway event with error handling and retries
|
|
108
152
|
* @param pathway The pathway to process
|
|
@@ -189,10 +233,5 @@ export declare class PathwaysBuilder<TPathway extends Record<string, unknown> =
|
|
|
189
233
|
* @throws Error if the timeout is exceeded
|
|
190
234
|
*/
|
|
191
235
|
private waitForPathwayToBeProcessed;
|
|
192
|
-
/**
|
|
193
|
-
* Creates a new instance of PathwaysBuilder with the same configuration
|
|
194
|
-
* @returns A new PathwaysBuilder instance with the same type parameters and configuration
|
|
195
|
-
*/
|
|
196
|
-
clone(): PathwaysBuilder<TPathway, TWritablePaths>;
|
|
197
236
|
}
|
|
198
237
|
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/pathways/builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,KAAK,EAAyD,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAG/H,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAE1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,YAAY,EAAE,mBAAmB,EAA6B,eAAe,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/pathways/builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,KAAK,EAAyD,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAG/H,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAE1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,YAAY,EAAE,mBAAmB,EAA6B,eA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AS,CAAC,GAAG;QAAE,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;KAAE,KAAK,IAAI,EACvF,IAAI,GAAE,QAAQ,GAAG,OAAO,GAAG,KAAgB,GAC1C,IAAI;IA4BP;;;;OAIG;IACH,OAAO,CAAC,KAAK,SAAS,MAAM,QAAQ,EAClC,IAAI,EAAE,KAAK,EACX,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,CAAC,GAAG;QAAE,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;KAAE,KAAK,IAAI,GACpG,IAAI;IAmBP;;;OAGG;IACH,UAAU,CACR,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,GACrE,IAAI;IAMP;;;;;;;OAOG;IACG,KAAK,CAAC,KAAK,SAAS,cAAc,EACtC,IAAI,EAAE,KAAK,EACX,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,EACrB,QAAQ,CAAC,EAAE,aAAa,EACxB,OAAO,CAAC,EAAE,mBAAmB,GAC5B,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC;IAuH7B;;;;;;;;;;OAUG;YACW,2BAA2B;CA4C1C"}
|
|
@@ -18,6 +18,10 @@ const DEFAULT_MAX_RETRIES = 3;
|
|
|
18
18
|
* Default delay between retry attempts in milliseconds
|
|
19
19
|
*/
|
|
20
20
|
const DEFAULT_RETRY_DELAY_MS = 500;
|
|
21
|
+
/**
|
|
22
|
+
* Default TTL for session-specific user resolvers in milliseconds (10seconds)
|
|
23
|
+
*/
|
|
24
|
+
const DEFAULT_SESSION_USER_RESOLVER_TTL_MS = 10 * 1000;
|
|
21
25
|
/**
|
|
22
26
|
* Main builder class for creating and managing Flowcore pathways
|
|
23
27
|
*
|
|
@@ -42,8 +46,9 @@ class PathwaysBuilder {
|
|
|
42
46
|
* @param options.apiKey The API key for authentication
|
|
43
47
|
* @param options.pathwayTimeoutMs Optional timeout for pathway processing in milliseconds
|
|
44
48
|
* @param options.logger Optional logger instance
|
|
49
|
+
* @param options.sessionUserResolvers Optional KvAdapter instance for session-specific user resolvers
|
|
45
50
|
*/
|
|
46
|
-
constructor({ baseUrl, tenant, dataCore, apiKey, pathwayTimeoutMs, logger, }) {
|
|
51
|
+
constructor({ baseUrl, tenant, dataCore, apiKey, pathwayTimeoutMs, logger, sessionUserResolvers, }) {
|
|
47
52
|
Object.defineProperty(this, "pathways", {
|
|
48
53
|
enumerable: true,
|
|
49
54
|
configurable: true,
|
|
@@ -153,6 +158,13 @@ class PathwaysBuilder {
|
|
|
153
158
|
writable: true,
|
|
154
159
|
value: void 0
|
|
155
160
|
});
|
|
161
|
+
// Session-specific user resolvers
|
|
162
|
+
Object.defineProperty(this, "sessionUserResolvers", {
|
|
163
|
+
enumerable: true,
|
|
164
|
+
configurable: true,
|
|
165
|
+
writable: true,
|
|
166
|
+
value: null
|
|
167
|
+
});
|
|
156
168
|
// Logger instance (but not using it yet due to TypeScript errors)
|
|
157
169
|
Object.defineProperty(this, "logger", {
|
|
158
170
|
enumerable: true,
|
|
@@ -192,6 +204,9 @@ class PathwaysBuilder {
|
|
|
192
204
|
this.tenant = tenant;
|
|
193
205
|
this.dataCore = dataCore;
|
|
194
206
|
this.apiKey = apiKey;
|
|
207
|
+
if (sessionUserResolvers) {
|
|
208
|
+
this.sessionUserResolvers = sessionUserResolvers;
|
|
209
|
+
}
|
|
195
210
|
this.logger.debug('Initializing PathwaysBuilder', {
|
|
196
211
|
baseUrl,
|
|
197
212
|
tenant,
|
|
@@ -243,6 +258,59 @@ class PathwaysBuilder {
|
|
|
243
258
|
this.userIdResolver = resolver;
|
|
244
259
|
return this;
|
|
245
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Registers a user resolver for a specific session
|
|
263
|
+
*
|
|
264
|
+
* Session-specific user resolvers allow you to associate different user IDs with different
|
|
265
|
+
* sessions, which is useful in multi-user applications or when tracking user actions across
|
|
266
|
+
* different sessions.
|
|
267
|
+
*
|
|
268
|
+
* The resolver is stored in a key-value store with a TTL (time to live), and will be used
|
|
269
|
+
* to resolve the user ID when operations are performed with the given session ID. If the resolver
|
|
270
|
+
* expires, it will need to be registered again.
|
|
271
|
+
*
|
|
272
|
+
* This feature works in conjunction with the SessionPathwayBuilder to provide a complete
|
|
273
|
+
* session management solution.
|
|
274
|
+
*
|
|
275
|
+
* @param sessionId The session ID to associate with this resolver
|
|
276
|
+
* @param resolver The resolver function that resolves to the user ID for this session
|
|
277
|
+
* @returns The PathwaysBuilder instance for chaining
|
|
278
|
+
*
|
|
279
|
+
* @throws Error if session user resolvers are not configured (sessionUserResolvers not provided in constructor)
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* // Register a resolver for a specific session
|
|
284
|
+
* pathwaysBuilder.withSessionUserResolver("session-123", async () => {
|
|
285
|
+
* return "user-456"; // Return the user ID for this session
|
|
286
|
+
* });
|
|
287
|
+
*
|
|
288
|
+
* // Use with SessionPathwayBuilder
|
|
289
|
+
* const session = new SessionPathwayBuilder(pathwaysBuilder, "session-123");
|
|
290
|
+
* await session.write("user/action", actionData);
|
|
291
|
+
* // The user ID will be automatically included in the metadata
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
withSessionUserResolver(sessionId, resolver) {
|
|
295
|
+
if (!this.sessionUserResolvers) {
|
|
296
|
+
throw new Error('Session user resolvers not configured');
|
|
297
|
+
}
|
|
298
|
+
this.logger.debug('Configuring session-specific user resolver', { sessionId });
|
|
299
|
+
this.sessionUserResolvers.set(sessionId, resolver, DEFAULT_SESSION_USER_RESOLVER_TTL_MS);
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Gets a user resolver for a specific session ID
|
|
304
|
+
* @param sessionId The session ID to get the resolver for
|
|
305
|
+
* @returns The resolver function for the session, or undefined if none exists
|
|
306
|
+
*/
|
|
307
|
+
getSessionUserResolver(sessionId) {
|
|
308
|
+
if (!this.sessionUserResolvers) {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
const resolver = this.sessionUserResolvers.get(sessionId);
|
|
312
|
+
return resolver;
|
|
313
|
+
}
|
|
246
314
|
/**
|
|
247
315
|
* Process a pathway event with error handling and retries
|
|
248
316
|
* @param pathway The pathway to process
|
|
@@ -537,7 +605,8 @@ class PathwaysBuilder {
|
|
|
537
605
|
pathway: pathStr,
|
|
538
606
|
metadata,
|
|
539
607
|
options: {
|
|
540
|
-
fireAndForget: options?.fireAndForget
|
|
608
|
+
fireAndForget: options?.fireAndForget,
|
|
609
|
+
sessionId: options?.sessionId
|
|
541
610
|
}
|
|
542
611
|
});
|
|
543
612
|
if (!this.pathways[path]) {
|
|
@@ -561,17 +630,43 @@ class PathwaysBuilder {
|
|
|
561
630
|
}
|
|
562
631
|
// Create a copy of the metadata to avoid modifying the original
|
|
563
632
|
const finalMetadata = metadata ? { ...metadata } : {};
|
|
633
|
+
// Check for session-specific user resolver
|
|
634
|
+
let userId;
|
|
635
|
+
if (options?.sessionId) {
|
|
636
|
+
const sessionUserResolver = this.getSessionUserResolver(options.sessionId);
|
|
637
|
+
if (sessionUserResolver) {
|
|
638
|
+
try {
|
|
639
|
+
userId = await sessionUserResolver();
|
|
640
|
+
this.logger.debug(`Using session-specific user resolver`, {
|
|
641
|
+
pathway: pathStr,
|
|
642
|
+
sessionId: options.sessionId,
|
|
643
|
+
userId
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
catch (error) {
|
|
647
|
+
this.logger.error(`Error resolving session user ID`, error instanceof Error ? error : new Error(String(error)), {
|
|
648
|
+
pathway: pathStr,
|
|
649
|
+
sessionId: options.sessionId
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
564
654
|
// Process audit metadata if audit is configured
|
|
565
655
|
if (this.userIdResolver) {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
656
|
+
// Only use global resolver if we don't already have a user ID from a session resolver
|
|
657
|
+
if (!userId) {
|
|
658
|
+
this.logger.debug(`Resolving user ID for audit metadata`, { pathway: pathStr });
|
|
659
|
+
userId = await this.userIdResolver();
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
// Determine the audit mode: default is "user" unless explicitly specified as "system"
|
|
663
|
+
const auditMode = options?.auditMode ?? "user";
|
|
664
|
+
this.logger.debug(`Adding audit metadata`, {
|
|
665
|
+
pathway: pathStr,
|
|
666
|
+
auditMode,
|
|
667
|
+
userId
|
|
668
|
+
});
|
|
669
|
+
if (userId) {
|
|
575
670
|
// Add appropriate audit metadata based on mode
|
|
576
671
|
if (auditMode === "system") {
|
|
577
672
|
finalMetadata["audit/user-id"] = "system";
|
|
@@ -657,37 +752,5 @@ class PathwaysBuilder {
|
|
|
657
752
|
attempts
|
|
658
753
|
});
|
|
659
754
|
}
|
|
660
|
-
/**
|
|
661
|
-
* Creates a new instance of PathwaysBuilder with the same configuration
|
|
662
|
-
* @returns A new PathwaysBuilder instance with the same type parameters and configuration
|
|
663
|
-
*/
|
|
664
|
-
clone() {
|
|
665
|
-
this.logger.debug('Building new PathwaysBuilder instance with same configuration');
|
|
666
|
-
// Create new instance with same base configuration
|
|
667
|
-
const builder = new PathwaysBuilder({
|
|
668
|
-
baseUrl: this.baseUrl,
|
|
669
|
-
tenant: this.tenant,
|
|
670
|
-
dataCore: this.dataCore,
|
|
671
|
-
apiKey: this.apiKey,
|
|
672
|
-
pathwayTimeoutMs: this.pathwayTimeoutMs,
|
|
673
|
-
logger: this.logger
|
|
674
|
-
});
|
|
675
|
-
// Copy pathway state configuration if custom
|
|
676
|
-
if (!(this.pathwayState instanceof internal_pathway_state_js_1.InternalPathwayState)) {
|
|
677
|
-
builder.withPathwayState(this.pathwayState);
|
|
678
|
-
}
|
|
679
|
-
// Copy audit configuration if present
|
|
680
|
-
if (this.auditHandler) {
|
|
681
|
-
builder.withAudit(this.auditHandler);
|
|
682
|
-
}
|
|
683
|
-
// Copy user resolver if present
|
|
684
|
-
if (this.userIdResolver) {
|
|
685
|
-
builder.withUserResolver(this.userIdResolver);
|
|
686
|
-
}
|
|
687
|
-
// The new instance will have the same type parameters but
|
|
688
|
-
// it will be empty of pathways until they are registered again
|
|
689
|
-
this.logger.info('New PathwaysBuilder instance created');
|
|
690
|
-
return builder;
|
|
691
|
-
}
|
|
692
755
|
}
|
|
693
756
|
exports.PathwaysBuilder = PathwaysBuilder;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Exports all components needed to build and manage pathways including:
|
|
5
5
|
* - Pathway builder
|
|
6
|
+
* - Session pathway builder
|
|
6
7
|
* - State management
|
|
7
8
|
* - Storage adapters (KV, Postgres)
|
|
8
9
|
* - Logging
|
|
@@ -15,5 +16,6 @@ export * from "./internal-pathway.state.js";
|
|
|
15
16
|
export * from "./kv/kv-adapter.js";
|
|
16
17
|
export * from "./logger.js";
|
|
17
18
|
export * from "./postgres/index.js";
|
|
19
|
+
export * from "./session-pathway.js";
|
|
18
20
|
export * from "./types.js";
|
|
19
21
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pathways/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pathways/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,cAAc,cAAc,CAAC;AAC7B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC"}
|
package/script/pathways/index.js
CHANGED
|
@@ -19,6 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
19
19
|
*
|
|
20
20
|
* Exports all components needed to build and manage pathways including:
|
|
21
21
|
* - Pathway builder
|
|
22
|
+
* - Session pathway builder
|
|
22
23
|
* - State management
|
|
23
24
|
* - Storage adapters (KV, Postgres)
|
|
24
25
|
* - Logging
|
|
@@ -31,4 +32,5 @@ __exportStar(require("./internal-pathway.state.js"), exports);
|
|
|
31
32
|
__exportStar(require("./kv/kv-adapter.js"), exports);
|
|
32
33
|
__exportStar(require("./logger.js"), exports);
|
|
33
34
|
__exportStar(require("./postgres/index.js"), exports);
|
|
35
|
+
__exportStar(require("./session-pathway.js"), exports);
|
|
34
36
|
__exportStar(require("./types.js"), exports);
|
|
@@ -2,7 +2,34 @@
|
|
|
2
2
|
* Interface for key-value storage adapters
|
|
3
3
|
*
|
|
4
4
|
* Provides a common interface for different KV storage implementations
|
|
5
|
-
* that can be used for storing pathway state.
|
|
5
|
+
* that can be used for storing pathway state and other application data.
|
|
6
|
+
*
|
|
7
|
+
* This interface abstracts away the details of specific storage backends,
|
|
8
|
+
* allowing the application to work with different storage providers
|
|
9
|
+
* without changing the core logic.
|
|
10
|
+
*
|
|
11
|
+
* The Flowcore Pathways library includes several implementations of this interface:
|
|
12
|
+
* - BunKvAdapter: Uses Bun's built-in KV store
|
|
13
|
+
* - NodeKvAdapter: Uses node-cache for in-memory storage
|
|
14
|
+
* - DenoKvAdapter: Uses Deno's KV store (when available)
|
|
15
|
+
*
|
|
16
|
+
* Custom implementations can be created for other storage backends
|
|
17
|
+
* by implementing this interface.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create a KV adapter
|
|
22
|
+
* const store = await createKvAdapter();
|
|
23
|
+
*
|
|
24
|
+
* // Store a value with a TTL
|
|
25
|
+
* await store.set("session:123", { userId: "user-456" }, 3600000); // 1 hour TTL
|
|
26
|
+
*
|
|
27
|
+
* // Retrieve the value
|
|
28
|
+
* const session = await store.get<{ userId: string }>("session:123");
|
|
29
|
+
* if (session) {
|
|
30
|
+
* console.log("User ID:", session.userId);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
6
33
|
*/
|
|
7
34
|
export interface KvAdapter {
|
|
8
35
|
/**
|
|
@@ -26,9 +53,39 @@ export interface KvAdapter {
|
|
|
26
53
|
/**
|
|
27
54
|
* Creates an appropriate KV adapter based on the runtime environment
|
|
28
55
|
*
|
|
29
|
-
*
|
|
56
|
+
* This function automatically detects the current runtime environment and creates
|
|
57
|
+
* the most suitable KV adapter implementation:
|
|
58
|
+
*
|
|
59
|
+
* - In Bun: Returns a BunKvAdapter using Bun's built-in KV store
|
|
60
|
+
* - In Deno with KV access: Returns a DenoKvAdapter
|
|
61
|
+
* - Otherwise: Returns a NodeKvAdapter using an in-memory cache
|
|
62
|
+
*
|
|
63
|
+
* Using this factory function rather than directly instantiating a specific adapter
|
|
64
|
+
* implementation makes your code more portable across different JavaScript runtimes.
|
|
65
|
+
*
|
|
66
|
+
* The adapter is lazily initialized, so any necessary setup only happens when
|
|
67
|
+
* you first interact with the adapter.
|
|
30
68
|
*
|
|
31
69
|
* @returns A KV adapter instance for the current runtime
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // Create a runtime-specific KV adapter
|
|
74
|
+
* const kv = await createKvAdapter();
|
|
75
|
+
*
|
|
76
|
+
* // Use with PathwaysBuilder for session user resolvers
|
|
77
|
+
* const pathways = new PathwaysBuilder({
|
|
78
|
+
* baseUrl: "https://api.flowcore.io",
|
|
79
|
+
* tenant: "my-tenant",
|
|
80
|
+
* dataCore: "my-data-core",
|
|
81
|
+
* apiKey: "my-api-key",
|
|
82
|
+
* sessionUserResolvers: kv
|
|
83
|
+
* });
|
|
84
|
+
*
|
|
85
|
+
* // Use as a general-purpose key-value store
|
|
86
|
+
* await kv.set("cache:user:123", userData, 60 * 60 * 1000); // 1 hour TTL
|
|
87
|
+
* const cachedUser = await kv.get("cache:user:123");
|
|
88
|
+
* ```
|
|
32
89
|
*/
|
|
33
90
|
export declare function createKvAdapter(): Promise<KvAdapter>;
|
|
34
91
|
//# sourceMappingURL=kv-adapter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv-adapter.d.ts","sourceRoot":"","sources":["../../../src/pathways/kv/kv-adapter.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"kv-adapter.d.ts","sourceRoot":"","sources":["../../../src/pathways/kv/kv-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;;OAMG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;IAEpD;;;;;;;OAOG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;CACzE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,SAAS,CAAC,CAU1D"}
|