@onlive.ai/flow-client 0.0.3 → 0.1.34
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 +236 -0
- package/client/client.service.cjs +747 -0
- package/client/client.service.js +724 -0
- package/client/client.service.spec.cjs +909 -0
- package/client/client.service.spec.js +908 -0
- package/client/client.types.cjs +438 -0
- package/client/client.types.js +399 -0
- package/flow.types.cjs +311 -0
- package/flow.types.js +264 -0
- package/index.cjs +783 -0
- package/index.js +740 -0
- package/package.json +34 -17
- package/schema-generator.cjs +1504 -0
- package/schema-generator.js +1502 -0
- package/tracking/tracking.service.cjs +238 -0
- package/tracking/tracking.service.js +215 -0
- package/tracking/tracking.types.cjs +49 -0
- package/tracking/tracking.types.js +22 -0
- package/dist/index.cjs +0 -1
- package/dist/index.d.ts +0 -1013
- package/dist/index.js +0 -3301
- package/dist/index.umd.cjs +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# Onlive Flow Client
|
|
2
|
+
|
|
3
|
+
The Flow Client package offers a convenient interface for interacting with a multi-step process flow. The client helps you initiate, retrieve, and progress through flow steps while tracking events. It leverages robust type validation and provides a modern TypeScript-first API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### Using pnpm (recommended)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @onlive.ai/flow-client
|
|
11
|
+
# or
|
|
12
|
+
npm install @onlive.ai/flow-client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Package Structure
|
|
16
|
+
|
|
17
|
+
This package is organized into the following main components:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
packages/flow-client/
|
|
21
|
+
├── client/ # Core client implementation
|
|
22
|
+
│ ├── client.service.ts # Main FlowClient class
|
|
23
|
+
│ ├── client.types.ts # Type definitions and schemas
|
|
24
|
+
│ └── client.service.spec.ts # Unit tests
|
|
25
|
+
├── tracking/ # Event tracking functionality
|
|
26
|
+
│ ├── tracking.service.ts # Tracking service implementation
|
|
27
|
+
│ └── tracking.types.ts # Tracking-related types
|
|
28
|
+
├── flow.types.ts # Flow-specific type definitions
|
|
29
|
+
├── index.ts # Main package exports
|
|
30
|
+
├── schema-generator.ts # JSON schema generation utilities
|
|
31
|
+
└── README.md # This documentation
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Main Exports
|
|
35
|
+
|
|
36
|
+
The package exports the following main components:
|
|
37
|
+
|
|
38
|
+
- **FlowClient**: Main client class for flow interactions
|
|
39
|
+
- **Type definitions**: Comprehensive TypeScript types for flows, steps, actions, and events
|
|
40
|
+
- **Event types**: Types for tracking and event handling from `@onlive.ai/tracker`
|
|
41
|
+
|
|
42
|
+
## Dependencies
|
|
43
|
+
|
|
44
|
+
This package depends on:
|
|
45
|
+
|
|
46
|
+
- **@onlive.ai/tracker**: Event tracking functionality
|
|
47
|
+
- **@onlive.ai/calendar**: Calendar-related utilities
|
|
48
|
+
- **@onlive.ai/common-121**: Shared utilities and types
|
|
49
|
+
- **zod**: Runtime type validation and schema definitions
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
### Basic Import
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { FlowClient } from '@onlive.ai/flow-client';
|
|
57
|
+
// or
|
|
58
|
+
import { FlowClient, type FlowContext, type ClientOptions } from '@onlive.ai/flow-client';
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Initialization
|
|
62
|
+
|
|
63
|
+
Create an instance of the FlowClient by passing a valid ClientOptions object. The options must conform to the ClientOptionsSchema. The options include:
|
|
64
|
+
|
|
65
|
+
- **baseUrl**: (string) URL of the service.
|
|
66
|
+
- **flowId**: (string) 24-character identifier.
|
|
67
|
+
- **organizationId**: (string) UUID.
|
|
68
|
+
- **lang**: (string) 2-character language code.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const client = new FlowClient({
|
|
72
|
+
baseUrl: "https://api.example.com",
|
|
73
|
+
flowId: "0123456789abcdef01234567",
|
|
74
|
+
organizationId: "550e8400-e29b-41d4-a716-446655440000",
|
|
75
|
+
lang: "en",
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### API Reference
|
|
80
|
+
|
|
81
|
+
#### FlowClient Class
|
|
82
|
+
|
|
83
|
+
The main class for interacting with flows. All methods are type-safe and include runtime validation.
|
|
84
|
+
|
|
85
|
+
#### Methods
|
|
86
|
+
|
|
87
|
+
##### firstStep(options?: GetStepOptions): Promise<FlowContext>
|
|
88
|
+
|
|
89
|
+
Initiates the first step of the flow.
|
|
90
|
+
|
|
91
|
+
- **Parameters**:
|
|
92
|
+
- **options** (optional): An object conforming to GetStepOptionsSchema.
|
|
93
|
+
- **Response**:
|
|
94
|
+
- Returns a `Promise` that resolves to a `FlowContext` object, representing the flow’s status, the current step (as defined in StepSchema), and flow metadata (FlowSchema).
|
|
95
|
+
- **Process**:
|
|
96
|
+
- Validates the options.
|
|
97
|
+
- Sends a POST request to `flow/{flowId}/first-step`.
|
|
98
|
+
- Analyzes and emits "track" events via the tracking service.
|
|
99
|
+
|
|
100
|
+
##### getStep(stepId: string, options?: GetStepOptions): Promise<FlowContext>
|
|
101
|
+
|
|
102
|
+
Retrieves a specific step in the flow.
|
|
103
|
+
|
|
104
|
+
- **Parameters**:
|
|
105
|
+
- **stepId**: (string) A non-empty string identifier validated by StepIdSchema.
|
|
106
|
+
- **options** (optional): Additional parameters adhering to GetStepOptionsSchema.
|
|
107
|
+
- **Response**:
|
|
108
|
+
- Returns a `Promise` resolving to a FlowContext.
|
|
109
|
+
- **Process**:
|
|
110
|
+
- Validates the stepId.
|
|
111
|
+
- Sends a POST request to `flow/{flowId}/steps/{stepId}`.
|
|
112
|
+
- Tracks request and response events.
|
|
113
|
+
|
|
114
|
+
##### nextStep(trigger: GetStepTrigger, options?: GetStepOptions): Promise<FlowContext>
|
|
115
|
+
|
|
116
|
+
Proceeds to the next step in the flow based on user actions.
|
|
117
|
+
|
|
118
|
+
- **Parameters**:
|
|
119
|
+
- **trigger**: An object conforming to GetStepTriggerSchema containing:
|
|
120
|
+
- `currentStepId` (string): Current step identifier.
|
|
121
|
+
- `actionId` (string): Identifier of the action to trigger.
|
|
122
|
+
- **options** (optional): Configuration for step retrieval following GetStepOptionsSchema.
|
|
123
|
+
- **Response**:
|
|
124
|
+
- Returns a `Promise` that resolves to an updated FlowContext.
|
|
125
|
+
- **Process**:
|
|
126
|
+
- Validates the trigger.
|
|
127
|
+
- Sends a POST request to `flow/{flowId}/steps/{currentStepId}/action/{actionId}`.
|
|
128
|
+
- Analyzes both request and response by emitting tracking events.
|
|
129
|
+
|
|
130
|
+
#### Event Handling
|
|
131
|
+
|
|
132
|
+
##### on(type: EventType, listener: EventListener): void
|
|
133
|
+
|
|
134
|
+
Registers a listener for specific events.
|
|
135
|
+
|
|
136
|
+
- **Parameters**:
|
|
137
|
+
- **type**: A string event type, currently supporting `"track"` as defined by EventTypeSchema.
|
|
138
|
+
- **listener**: Callback accepting the event type and event data.
|
|
139
|
+
- **Usage**:
|
|
140
|
+
Register multiple listener functions to react to tracking events.
|
|
141
|
+
|
|
142
|
+
Internally, FlowClient uses a private `emit` method to deliver events to all registered listeners.
|
|
143
|
+
|
|
144
|
+
## Types & Schemas
|
|
145
|
+
|
|
146
|
+
The methods and the overall flow behavior are governed by types and validation schemas defined in the package:
|
|
147
|
+
|
|
148
|
+
- **FlowContext**: Contains the status of the flow, current step details, and flow metadata.
|
|
149
|
+
- **StepSchema**, **ActionSchema**, **FieldSchema**: Define properties and validation for steps, actions, and form fields.
|
|
150
|
+
- **GetStepOptions** and **GetStepTrigger**: Govern the payload structure for step operations.
|
|
151
|
+
- **EventData**: Structure for tracking events, ensuring consistency with the "track" event type.
|
|
152
|
+
|
|
153
|
+
## Features
|
|
154
|
+
|
|
155
|
+
- **Type Safety**: Full TypeScript support with runtime validation using Zod schemas
|
|
156
|
+
- **Event Tracking**: Built-in event tracking for flow interactions
|
|
157
|
+
- **Multi-format Support**: Supports both ESM and CommonJS
|
|
158
|
+
- **Modern API**: Promise-based API with async/await support
|
|
159
|
+
- **Flexible Configuration**: Configurable client options with validation
|
|
160
|
+
- **Error Handling**: Comprehensive error handling with type-safe error responses
|
|
161
|
+
|
|
162
|
+
## Development
|
|
163
|
+
|
|
164
|
+
### Building the Package
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
pnpm build:package
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Running Tests
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
# Run tests
|
|
174
|
+
pnpm test
|
|
175
|
+
|
|
176
|
+
# Run tests with coverage
|
|
177
|
+
pnpm test:coverage
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Cleaning Build Artifacts
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
pnpm clean
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Examples
|
|
187
|
+
|
|
188
|
+
### Complete Flow Example
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
import { FlowClient } from '@onlive.ai/flow-client';
|
|
192
|
+
|
|
193
|
+
const client = new FlowClient({
|
|
194
|
+
baseUrl: "https://api.example.com",
|
|
195
|
+
flowId: "0123456789abcdef01234567",
|
|
196
|
+
organizationId: "550e8400-e29b-41d4-a716-446655440000",
|
|
197
|
+
lang: "en",
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Set up event tracking
|
|
201
|
+
client.on('track', (eventType, eventData) => {
|
|
202
|
+
console.log('Flow event:', eventType, eventData);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Start the flow
|
|
206
|
+
try {
|
|
207
|
+
const firstStep = await client.firstStep();
|
|
208
|
+
console.log('First step:', firstStep);
|
|
209
|
+
|
|
210
|
+
// Progress through the flow
|
|
211
|
+
const nextStep = await client.nextStep({
|
|
212
|
+
currentStepId: firstStep.step.id,
|
|
213
|
+
actionId: 'continue'
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
console.log('Next step:', nextStep);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error('Flow error:', error);
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Getting a Specific Step
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const specificStep = await client.getStep('step-id-123', {
|
|
226
|
+
// optional additional parameters
|
|
227
|
+
});
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## License
|
|
231
|
+
|
|
232
|
+
This package is part of the Onlive workspace and follows the project's licensing terms.
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
This documentation mirrors common npm package libraries by providing a straightforward interface with strong type validations and event tracking, ensuring that clients can interact with a dynamic flow efficiently.
|