@flow-conductor/core 1.0.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/README.md +213 -0
- package/build/index.d.ts +674 -0
- package/build/index.js +667 -0
- package/build/index.js.map +1 -0
- package/package.json +84 -0
package/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# @flow-conductor/core
|
|
2
|
+
|
|
3
|
+
Core types and base classes for flow-conductor request adapters.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @flow-conductor/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
This package provides the foundational types and classes for building request adapters and managing request chains. It's the core dependency that all flow-conductor adapters depend on.
|
|
14
|
+
|
|
15
|
+
## What's Included
|
|
16
|
+
|
|
17
|
+
### Classes
|
|
18
|
+
|
|
19
|
+
- **`RequestAdapter`** - Base abstract class for all request adapters
|
|
20
|
+
- **`RequestChain`** - Main class for chaining requests together
|
|
21
|
+
- **`RequestManager`** - Base class for request management (extended by `RequestChain`)
|
|
22
|
+
|
|
23
|
+
### Types
|
|
24
|
+
|
|
25
|
+
- **`IRequestConfig`** - Interface for request configuration
|
|
26
|
+
- **`IRequestConfigFactory`** - Function type for dynamic request configuration
|
|
27
|
+
- **`PipelineRequestStage`** - Interface for individual request stages
|
|
28
|
+
- **`PipelineManagerStage`** - Interface for nested request manager stages
|
|
29
|
+
- **`BasePipelineStage`** - Base interface for pipeline stages
|
|
30
|
+
- **`ErrorHandler`** - Type for error handling functions
|
|
31
|
+
- **`ResultHandler`** - Type for result handling functions
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Basic Usage
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { begin, RequestAdapter, IRequestConfig } from "@flow-conductor/core";
|
|
39
|
+
|
|
40
|
+
// You need to provide an adapter - see adapter packages
|
|
41
|
+
class MyAdapter extends RequestAdapter<Response, IRequestConfig> {
|
|
42
|
+
public async createRequest(config: IRequestConfig): Promise<Response> {
|
|
43
|
+
// Implement your request logic
|
|
44
|
+
return fetch(config.url, { method: config.method });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const adapter = new MyAdapter();
|
|
49
|
+
const result = await begin(
|
|
50
|
+
{
|
|
51
|
+
config: { url: "https://api.example.com/users", method: "GET" }
|
|
52
|
+
},
|
|
53
|
+
adapter
|
|
54
|
+
).execute();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Creating a Custom Adapter
|
|
58
|
+
|
|
59
|
+
To create a custom adapter, extend the `RequestAdapter` class:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { RequestAdapter, IRequestConfig } from "@flow-conductor/core";
|
|
63
|
+
|
|
64
|
+
export default class MyCustomAdapter extends RequestAdapter<
|
|
65
|
+
MyResponseType,
|
|
66
|
+
MyRequestConfig
|
|
67
|
+
> {
|
|
68
|
+
public async createRequest(
|
|
69
|
+
requestConfig: MyRequestConfig
|
|
70
|
+
): Promise<MyResponseType> {
|
|
71
|
+
// Implement your custom request logic
|
|
72
|
+
// This could use axios, node-fetch, or any other HTTP library
|
|
73
|
+
const response = await myHttpLibrary.request({
|
|
74
|
+
url: requestConfig.url,
|
|
75
|
+
method: requestConfig.method,
|
|
76
|
+
data: requestConfig.data
|
|
77
|
+
});
|
|
78
|
+
return response;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public getResult(result: MyResponseType): MyResponseType {
|
|
82
|
+
// Optionally transform the result before it's passed to the next step
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Using RequestChain
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { begin } from "@flow-conductor/core";
|
|
92
|
+
import { MyAdapter } from "./MyAdapter";
|
|
93
|
+
|
|
94
|
+
const adapter = new MyAdapter();
|
|
95
|
+
|
|
96
|
+
// Simple chain
|
|
97
|
+
const result = await begin(
|
|
98
|
+
{
|
|
99
|
+
config: { url: "https://api.example.com/users/1", method: "GET" }
|
|
100
|
+
},
|
|
101
|
+
adapter
|
|
102
|
+
)
|
|
103
|
+
.next({
|
|
104
|
+
config: async (previousResult) => {
|
|
105
|
+
const user = await previousResult.json();
|
|
106
|
+
return {
|
|
107
|
+
url: `https://api.example.com/users/${user.id}/posts`,
|
|
108
|
+
method: "GET"
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
.execute();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Using RequestManager Methods
|
|
116
|
+
|
|
117
|
+
`RequestChain` extends `RequestManager`, which provides additional methods:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { begin } from "@flow-conductor/core";
|
|
121
|
+
|
|
122
|
+
const adapter = new MyAdapter();
|
|
123
|
+
|
|
124
|
+
const chain = begin(
|
|
125
|
+
{ config: { url: "https://api.example.com/users", method: "GET" } },
|
|
126
|
+
adapter
|
|
127
|
+
)
|
|
128
|
+
.withResultHandler((result) => {
|
|
129
|
+
console.log("Success:", result);
|
|
130
|
+
})
|
|
131
|
+
.withErrorHandler((error) => {
|
|
132
|
+
console.error("Error:", error);
|
|
133
|
+
})
|
|
134
|
+
.withFinishHandler(() => {
|
|
135
|
+
console.log("Finished");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Add multiple requests at once
|
|
139
|
+
chain.addAll([
|
|
140
|
+
{ config: { url: "https://api.example.com/posts", method: "GET" } },
|
|
141
|
+
{ config: { url: "https://api.example.com/comments", method: "GET" } }
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
const results = await chain.executeAll();
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Type Definitions
|
|
148
|
+
|
|
149
|
+
### IRequestConfig
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
interface IRequestConfig {
|
|
153
|
+
url: string;
|
|
154
|
+
method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
|
|
155
|
+
data?: any;
|
|
156
|
+
headers?: Record<string, string>;
|
|
157
|
+
[key: string]: any; // Additional adapter-specific options
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### PipelineRequestStage
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
interface PipelineRequestStage<Result, Out = Result, AdapterRequestConfig extends IRequestConfig = IRequestConfig> {
|
|
165
|
+
config: AdapterRequestConfig | IRequestConfigFactory<Result, AdapterRequestConfig>;
|
|
166
|
+
precondition?: () => boolean;
|
|
167
|
+
mapper?: (result: Result) => Out | Promise<Out>;
|
|
168
|
+
resultInterceptor?: (result: Out) => void | Promise<void>; // Optional result interceptor for side effects
|
|
169
|
+
retry?: RetryConfig; // Optional retry configuration
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### RequestAdapter
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
abstract class RequestAdapter<ExecutionResult, RequestConfig extends IRequestConfig = IRequestConfig> {
|
|
177
|
+
abstract createRequest(requestConfig: RequestConfig): Promise<ExecutionResult>;
|
|
178
|
+
getResult<T extends ExecutionResult>(result: ExecutionResult): T;
|
|
179
|
+
executeRequest(requestConfig: RequestConfig): Promise<ExecutionResult>;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Exports
|
|
184
|
+
|
|
185
|
+
### Main Exports
|
|
186
|
+
|
|
187
|
+
- `RequestAdapter` - Base adapter class
|
|
188
|
+
- `RequestManager` - Base manager class
|
|
189
|
+
- `RequestChain` - Main chain class
|
|
190
|
+
- `begin` - Function alternative to `RequestChain.begin`
|
|
191
|
+
|
|
192
|
+
### Type Exports
|
|
193
|
+
|
|
194
|
+
- `IRequestConfig`
|
|
195
|
+
- `IRequestConfigFactory`
|
|
196
|
+
- `PipelineRequestStage`
|
|
197
|
+
- `PipelineManagerStage`
|
|
198
|
+
- `BasePipelineStage`
|
|
199
|
+
- `ErrorHandler`
|
|
200
|
+
- `ResultHandler`
|
|
201
|
+
|
|
202
|
+
## Examples
|
|
203
|
+
|
|
204
|
+
See the [adapter-fetch](../adapter-fetch) package for a complete example of an adapter implementation.
|
|
205
|
+
|
|
206
|
+
## Requirements
|
|
207
|
+
|
|
208
|
+
- TypeScript 5.0+
|
|
209
|
+
- Node.js 18.0+ (for ES modules support)
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
|
|
213
|
+
MIT
|