chainflow 0.1.5 → 0.1.7

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.
Files changed (41) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +542 -487
  3. package/dist/core/chainflow.d.ts +15 -13
  4. package/dist/core/chainflow.js +14 -11
  5. package/dist/core/chainflow.js.map +1 -1
  6. package/dist/core/inputNode.d.ts +2 -2
  7. package/dist/core/inputNode.js +37 -38
  8. package/dist/core/inputNode.js.map +1 -1
  9. package/dist/core/logger.d.ts +1 -0
  10. package/dist/core/logger.js +6 -2
  11. package/dist/core/logger.js.map +1 -1
  12. package/dist/core/sourceNode.d.ts +5 -5
  13. package/dist/core/sourceNode.js +5 -5
  14. package/dist/core/sourceNode.js.map +1 -1
  15. package/dist/core/utils/constants.d.ts +2 -2
  16. package/dist/core/utils/constants.js +3 -3
  17. package/dist/core/utils/constants.js.map +1 -1
  18. package/dist/core/utils/symbols.d.ts +1 -1
  19. package/dist/core/utils/symbols.js +2 -2
  20. package/dist/http/endpoint.d.ts +13 -5
  21. package/dist/http/endpoint.js +8 -4
  22. package/dist/http/endpoint.js.map +1 -1
  23. package/dist/http/errors.d.ts +1 -1
  24. package/dist/http/errors.js +2 -2
  25. package/dist/http/errors.js.map +1 -1
  26. package/dist/http/logger.d.ts +1 -0
  27. package/dist/http/logger.js +8 -2
  28. package/dist/http/logger.js.map +1 -1
  29. package/dist/http/utils/id.d.ts +5 -0
  30. package/dist/http/utils/id.js +9 -0
  31. package/dist/http/utils/id.js.map +1 -0
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.js +3 -0
  34. package/dist/index.js.map +1 -1
  35. package/package.json +5 -3
  36. package/dist/core/utils/source.d.ts +0 -14
  37. package/dist/core/utils/source.js +0 -19
  38. package/dist/core/utils/source.js.map +0 -1
  39. package/dist/http/utils/hash.d.ts +0 -4
  40. package/dist/http/utils/hash.js +0 -8
  41. package/dist/http/utils/hash.js.map +0 -1
package/README.md CHANGED
@@ -1,487 +1,542 @@
1
- <h1 align="center" style="border-bottom: none;">🌊hainflow</h1>
2
- <h3 align="center">A library to create dynamic and composable API call workflows.</h3>
3
- <div align="center">
4
-
5
- [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/edwinlzs/chainflow/blob/main/LICENSE)
6
- &nbsp;
7
- [![NPM version](https://img.shields.io/npm/v/chainflow.svg?style=flat-square)](https://www.npmjs.com/package/chainflow)
8
- &nbsp;
9
- [![GitHub Actions](https://img.shields.io/github/actions/workflow/status/edwinlzs/chainflow/on-push.yml?style=flat-square&branch=main)](https://github.com/edwinlzs/chainflow/actions)
10
- &nbsp;
11
- </div>
12
-
13
- ## Not Released Yet
14
-
15
- Hi! If you are here, you're a bit early. I'm still setting up some stuff for the first release. Check back in later!
16
-
17
- ## Documentation
18
-
19
- Read the guides over at <https://edwinlzs.github.io/chainflow-docs/> to get started!
20
-
21
- ## Use Cases
22
-
23
- Create multiple sets of API call workflows with this library that can be used to:
24
-
25
- 1. Insert demo data via your app's APIs (instead of SQL/db scripts)
26
- 2. Simulate frontend interactions with backend APIs
27
- 3. UI-agnostic end-to-end testing of backend APIs
28
- 4. Test edge cases on backend endpoints with input variations
29
-
30
- ## Basic Usage
31
-
32
- ```console
33
- npm install --save-dev chainflow
34
- ```
35
-
36
- Use `originServer` to define your endpoints and their request/response signatures with the `endpoint` method.
37
-
38
- ```typescript
39
- import { originServer } from chainflow;
40
-
41
- const origin = originServer('127.0.0.1:5000');
42
-
43
- const createUser = origin.post('/user').body({
44
- name: 'Tom',
45
- details: {
46
- age: 40,
47
- },
48
- });
49
-
50
- const createRole = origin.post('/role').body({
51
- type: 'Engineer',
52
- userId: createUser.resp.body.id,
53
- });
54
-
55
- const getUser = origin.get('/user').query({
56
- roleType: createRole.resp.body.type,
57
- });
58
- ```
59
-
60
- Use a `chainflow` to define a sequence of endpoint calls that take advantage of the values and links provided above.
61
-
62
- ```typescript
63
- import { chainflow } from Chainflow;
64
-
65
- const flow = chainflow()
66
- .call(createUser)
67
- .call(createRole)
68
- .call(getUser);
69
-
70
- flow.run();
71
- ```
72
-
73
- ---
74
-
75
- \
76
- The above setup will result in the following API calls:
77
-
78
- 1. `POST` Request to `/user` with body:
79
-
80
- ```json
81
- {
82
- "name": "Tom",
83
- "details": {
84
- "age": 40
85
- }
86
- }
87
- ```
88
-
89
- 2. `POST` Request to `/role` with body:
90
-
91
- ```json
92
- {
93
- "type": "Engineer",
94
- "userId": "['userId' from response to step 1]"
95
- }
96
- ```
97
-
98
- 3. `GET` Request to `/user?roleType=['type' from response to step 2]`
99
-
100
- &nbsp;
101
-
102
- ## More Features
103
-
104
- ### Query params
105
-
106
- Define query params with the `query` method on an endpoint.
107
-
108
- ```typescript
109
- const getUsersInGroup = origin.get('/user').query({ groupId: 'some-id' });
110
- ```
111
-
112
- ### Path params
113
-
114
- Define path params by wrapping a param name with `{}` in the endpoint path.
115
-
116
- ```typescript
117
- const getGroupsWithUser = origin.get('/groups/{userId}');
118
- ```
119
-
120
- You can specify values for your path params by calling `pathParams`. Note that path params which do not actually exist in the path will be discarded.
121
-
122
- ```typescript
123
- const getGroupsWithUser = origin.get('/groups/{userId}').pathParams({
124
- userId: 'user123',
125
- });
126
- ```
127
-
128
- ### Headers
129
-
130
- Specify headers with `headers` method on endpoints.
131
-
132
- ```typescript
133
- const getInfo = origin.get('/info').headers({ token: 'some-token' });
134
- ```
135
-
136
- You can also use `headers` on an `OriginServer` to have all endpoints made for that origin bear those headers.
137
-
138
- ```typescript
139
- const origin = originServer('127.0.0.1:3001').headers({ token: 'some-token' });
140
-
141
- const getInfo = origin.get('/info'); // getInfo endpoint will have the headers defined above
142
- ```
143
-
144
- The request payloads under `Basic Usage` are defined with only _default_ values - i.e. the values which a Chainflow use if there are no response values from other endpoint calls linked to it.
145
-
146
- However, you can also use the following features to more flexibly define the values used in a request.
147
-
148
- ### `required`
149
-
150
- Marks a value as required but without a default. The chainflow will expect this value to be sourced from another node. If no such source is available, the endpoint call will throw an error.
151
-
152
- ```typescript
153
- const createUser = origin.post('/user').body({
154
- name: required(),
155
- });
156
- ```
157
-
158
- ### `gen`
159
-
160
- Provide a callback that generates values for building requests.
161
-
162
- ```typescript
163
- const randAge = () => Math.floor(Math.random() * 100);
164
-
165
- const createUser = origin.post('/user').body({
166
- name: 'Tom',
167
- details: {
168
- age: gen(randAge),
169
- },
170
- });
171
- ```
172
-
173
- ### `link`
174
-
175
- You can use the `link` function to specify a callback to transform the response value before it is passed to the input node.
176
-
177
- ```typescript
178
- const addGreeting = (name: string) => `Hello ${name}`;
179
-
180
- const createMessage = origin.post('message').body({
181
- msg: link(getUser.resp.body.name, addGreeting);
182
- });
183
- ```
184
-
185
- ### `set`
186
-
187
- The `link` has another function signature.
188
-
189
- You can use the `set` method on an endpoint to expose its input nodes, then use the 2nd function signature of `link` as shown below: pass in the input node first (`msg`), then the source node second and optionally a callback third.
190
-
191
- ```typescript
192
- createMessage.set(({ body: { msg } }) => {
193
- link(msg, getUser.resp.body.name);
194
- link(msg, createUser.resp.body.name);
195
- });
196
- ```
197
-
198
- With a callback:
199
-
200
- ```typescript
201
- createMessage.set(({ body: { msg } }) => {
202
- link(msg, getUser.resp.body.name, addGreeting);
203
- link(msg, createUser.resp.body.name, addGreeting);
204
- });
205
- ```
206
-
207
- ### `linkMerge`
208
-
209
- Link multiple response values to a single request node with an optional callback to merge the values into a single input value. This has 4 function signatures:
210
-
211
- For the argument containing the source nodes, you can either pass an _array_ of SourceNodes:
212
-
213
- ```typescript
214
- // note the callback has an array parameter
215
- const mergeValues = ([name, favAnimal]: [string, string]) =>
216
- `${name} likes ${favAnimal}.`;
217
-
218
- const createMessage = origin.post('message').body({
219
- msg: linkMerge(
220
- // array of source nodes
221
- [getUser.resp.body.name, getFavAnimal.resp.body.favAnimal],
222
- mergeValues,
223
- );
224
- });
225
- ```
226
-
227
- or you can pass an _object_ with SourceNodes as the values:
228
-
229
- ```typescript
230
- // note the callback has an object parameter
231
- const mergeValues = ({
232
- userName,
233
- favAnimal,
234
- }: {
235
- userName: string;
236
- favAnimal: string;
237
- }) => `${userName} likes ${favAnimal}.`;
238
-
239
-
240
- const createMessage = origin.post('message').body({
241
- msg: linkMerge(
242
- // object of source nodes
243
- {
244
- userName: getUser.resp.body.name,
245
- favAnimal: getFavAnimal.resp.body.favAnimal,
246
- },
247
- mergeValues,
248
- );
249
- });
250
- ```
251
-
252
- alternatively, you can use the `set` method in addition with the other function signature of `linkMerge` (similar to how `link` above has overloads to work with `set`).
253
-
254
- with array:
255
-
256
- ```typescript
257
- createMessage.set(({ body: { msg } }) => {
258
- linkMerge(
259
- msg, // the input node
260
- [getUser.resp.body.name, getFavAnimal.resp.body.favAnimal],
261
- mergeValues,
262
- );
263
- });
264
- ```
265
-
266
- with object:
267
-
268
- ```typescript
269
- createMessage.set(({ body: { msg } }) => {
270
- linkMerge(
271
- msg, // the input node
272
- {
273
- userName: getUser.resp.body.name,
274
- favAnimal: getFavAnimal.resp.body.favAnimal,
275
- },
276
- mergeValues,
277
- );
278
- });
279
- ```
280
-
281
- Note that the merging link created by this method will only be used if ALL the source nodes specified are available i.e. if either one of `getUser.resp.body.name` or `getFavAnimal.resp.body.favAnimal` does not have a value, this link will not be used at all.
282
-
283
- ### Call Options
284
-
285
- You can declare manual values for an endpoint call in the chainflow itself, should you need to do so, by passing in a Call Options object as a second argument in the `call` method.
286
-
287
- `body`, `pathParams`, `query` and `headers` can be set this way.
288
-
289
- ```typescript
290
- const createUser = origin.post('/user').body({
291
- name: 'Tom',
292
- });
293
-
294
- chainflow()
295
- .call(createUser, { body: { name: 'Harry' } })
296
- .run();
297
- ```
298
-
299
- ### `seed`
300
-
301
- You can specify request nodes to take values from the chainflow 'seed' by importing the `seed` object and linking nodes to it. Provide actual seed values by calling the `seed` method on a chainflow before you `run` it, like below.
302
-
303
- ```typescript
304
- import { chainflow, link seed, } from 'chainflow';
305
-
306
- const createUser = origin.post('/user').body({
307
- name: required(),
308
- });
309
-
310
- createUser.set(({ body: { name }}) => {
311
- link(name, seed.username);
312
- });
313
-
314
- chainflow()
315
- .call()
316
- .seed({ username: 'Tom' })
317
- .run();
318
- ```
319
-
320
- ### Allow Undefined Sources Values
321
-
322
- By default, an input node will reject and skip a source node's value if it is unavailable or `undefined`. However, you can change this by passing a source node into the `config` utility function and passing an options object as the second parameter like below. This informs an input node to use the source node's value regardless of whether the value is `undefined` or not.
323
-
324
- ```typescript
325
- import { config } from 'chainflow';
326
-
327
- createUser.set(({ body: { name } }) => {
328
- link(name, config(seed.username, { allowUndefined: true }));
329
- });
330
- ```
331
-
332
- This has important implications - it means that as long as the source (e.g. a response from an endpoint call) is available, then the linked source node's value will be taken and used (even if that value is unavailable, which would be taken as `undefined`). Therefore, any other linked sources will not be used UNLESS 1. they have a higher priority or 2. the source providing the linked node that allows `undefined` is unavailable.
333
-
334
- &nbsp;
335
-
336
- ### `clone`
337
-
338
- You can create chainflow "templates" with the use of `clone` to create a copy of a chainflow and its endpoint callqueue. The clone can have endpoint calls added to it without modifying the initial flow.
339
-
340
- ```typescript
341
- const initialFlow = chainflow().call(endpoint1).call(endpoint2);
342
-
343
- const clonedFlow = initialFlow.clone();
344
-
345
- clonedFlow.call(endpoint3).run(); // calls endpoint 1, 2 and 3
346
- initialFlow.call(endpoint4).run(); // calls endpoint 1, 2 and 4
347
- ```
348
-
349
- ### `extend`
350
-
351
- You can connect multiple different chainflows together into a longer chainflow using `extend`.
352
-
353
- ```typescript
354
- const flow1 = chainflow().call(endpoint1).call(endpoint2);
355
- const flow2 = chainflow().call(endpoint3);
356
-
357
- flow1.extend(flow2).run(); // calls endpoint 1, 2 and 3
358
- ```
359
-
360
- ### `config`
361
-
362
- `respParser`
363
- By default, Chainflows will parse response bodies as JSON objects. To change this, you can call `.config` to change that configuration on an `endpoint` (or on an `OriginServer`, to apply it to all endpoints created from it) like so:
364
-
365
- ```typescript
366
- import { RESP_PARSER } from 'chainflow';
367
-
368
- const getUser = origin.get('/user').config({
369
- respParser: RESP_PARSER.TEXT,
370
- });
371
- ```
372
-
373
- or with camelcase in JavaScript:
374
-
375
- ```javascript
376
- const getUser = origin.get('/user').config({
377
- respParser: 'text',
378
- });
379
- ```
380
-
381
- There are 4 supported ways to parse response bodies (as provided by the underlying HTTP client, `undici`): `arrayBuffer`, `blob`, `json` and `text`.
382
-
383
- `respValidator`
384
- Another configuration option is how to validate the response to an endpoint. By default, Chainflow rejects responses that have HTTP status code 400 and above and throws an error. You can pass in a custom `respValidator` to change when a response is rejected.
385
-
386
- ```typescript
387
- const getUser = origin.get("/user").config({
388
- respValidator: (resp) => {
389
- if (resp.statusCode !== 201)
390
- return { valid: false, msg: "Failed to retrieve users." };
391
- if (!Object.keys(resp.body as Record<string, unknown>).includes("id"))
392
- return { valid: false, msg: "Response did not provide user ID." };
393
- return { valid: true };
394
- },
395
- });
396
- ```
397
-
398
- Your custom validator callback should have a return type:
399
-
400
- ```typescript
401
- {
402
- valid: boolean; // false if response should be rejected
403
- msg?: string; // error message
404
- }
405
- ```
406
-
407
- ### `store`
408
-
409
- Instead of direct links between endpoints, you can use a central store to keep values from some endpoints and have other endpoints take from it via the special `store` object.
410
-
411
- ```typescript
412
- import { store } from 'chainflow';
413
-
414
- const createUser = origin
415
- .post('/user')
416
- .body({
417
- name: 'Tom',
418
- })
419
- .store((resp) => ({
420
- // this endpoint will store `id` from a response to `userId` in the store
421
- userId: resp.body.id,
422
- }));
423
-
424
- const addRole = origin.post('/role').body({
425
- // this endpoint will take `userId` from the store, if available
426
- userId: store.userId,
427
- role: 'Engineer',
428
- });
429
-
430
- chainflow().call(createUser).call(addRole).run();
431
- ```
432
-
433
- This is usually useful when you have endpoints that could take a value from any one of many other endpoints for the same input node. Having a store to centralise these many-to-many relationships (like an API Gateway) can improve the developer experience.
434
-
435
- ### `continuesFrom` - transferring Chainflow states
436
-
437
- Say we have 2 endpoints, `login` and `createGroup`. We want to login as a user once, then proceed to proceed 3 groups as that same user without having to login 3 times.
438
-
439
- ```typescript
440
- const createGroup = origin
441
- .post('/group')
442
- .headers({
443
- Authorization: login.resp.body.authToken,
444
- })
445
- .body({
446
- groupName: seed.groupName,
447
- });
448
-
449
- // loggedInFlow will contain a response from the `login` endpoint
450
- const loggedInFlow = chainflow().call(login).run();
451
-
452
- // createGroupFlow will take the response that
453
- // loggedInFlow received and carry on from there
454
- const createGroupFlow = chainflow().call(createGroup).continuesFrom(loggedInFlow);
455
-
456
- const groupNames = ['RapGPT', 'Averageexpedition', 'Shaky Osmosis'];
457
- for (const groupName in groupNames) {
458
- createGroupFlow.seed({ groupName }).run();
459
- }
460
- ```
461
-
462
- We run a chainflow that calls `login` first to get a response from the login endpoint.
463
-
464
- Using the `continuesFrom` method, `createGroupFlow` will copy the state of source values (i.e. responses) from `loggedInFlow`. This means `createGroupFlow` will now have the logged in user's `authToken` received from calling `login`, and will use it when calling `createGroup` thrice for each group name in the `groupNames` array.
465
-
466
- ### `logging`
467
-
468
- Enable logs from Chainflow by setting `ENABLE_CHAINFLOW_LOGS=true` in your environment variables.
469
-
470
- ## Future Updates
471
-
472
- Below features are currently not yet supported but are planned in future releases.
473
-
474
- 1. More flexibility to log and return responses
475
- 2. API performance testing
476
- 3. (Exploratory) Possibly some sort of UI/diagram generation
477
-
478
- ## Development
479
-
480
- Run specific test files:
481
-
482
- `pnpm run test:file ./src/**/chainflow.test.ts`
483
-
484
- ### Trivia
485
-
486
- - You probably noticed that I enjoy using the Builder pattern for its clarity.
487
- - I'm praying the wave 🌊 emoji remains sufficiently shaped like a "C" to avoid confusion. Please let me know if there is some system where it does not!
1
+ <h1 align="center" style="border-bottom: none;">🌊hainflow</h1>
2
+ <h3 align="center">A library to create dynamic and composable API call workflows.</h3>
3
+ <div align="center">
4
+
5
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/edwinlzs/chainflow/blob/main/LICENSE)
6
+ &nbsp;
7
+ [![NPM version](https://img.shields.io/npm/v/chainflow.svg?style=flat-square)](https://www.npmjs.com/package/chainflow)
8
+ &nbsp;
9
+ [![GitHub Actions](https://img.shields.io/github/actions/workflow/status/edwinlzs/chainflow/ci.yml?style=flat-square&branch=main)](https://github.com/edwinlzs/chainflow/actions)
10
+ &nbsp;
11
+ [![codecov](https://img.shields.io/codecov/c/gh/edwinlzs/chainflow?token=O55JNRTCM5&style=flat-square&color=23a133)](https://codecov.io/gh/edwinlzs/chainflow)
12
+ </div>
13
+
14
+ ## Not Released Yet
15
+
16
+ Hi! If you are here, you're a bit early. I'm still setting up some stuff for the first release. Check back in later!
17
+
18
+ ## Documentation
19
+
20
+ Read the guides over at [Chainflow Docs](https://edwinlzs.github.io/chainflow-docs/) to get started!
21
+
22
+ ## Use Cases
23
+
24
+ Create multiple sets of API call workflows with this library that can be used to:
25
+
26
+ 1. Insert demo data via your app's APIs (instead of SQL/db scripts)
27
+ 2. Simulate frontend interactions with backend APIs
28
+ 3. UI-agnostic end-to-end testing of backend APIs
29
+ 4. Test edge cases on backend endpoints with input variations
30
+
31
+ ## Basic Usage
32
+
33
+ ```console
34
+ npm install --save-dev chainflow
35
+ ```
36
+
37
+ Use `originServer` to define your endpoints and their request/response signatures with the `endpoint` method.
38
+
39
+ ```typescript
40
+ import { originServer } from chainflow;
41
+
42
+ const origin = originServer('127.0.0.1:5000');
43
+
44
+ const createUser = origin.post('/user').body({
45
+ name: 'Tom',
46
+ details: {
47
+ age: 40,
48
+ },
49
+ });
50
+
51
+ const createRole = origin.post('/role').body({
52
+ type: 'Engineer',
53
+ userId: createUser.resp.body.id,
54
+ });
55
+
56
+ const getUser = origin.get('/user').query({
57
+ roleType: createRole.resp.body.type,
58
+ });
59
+ ```
60
+
61
+ Use a `chainflow` to define a sequence of endpoint calls that take advantage of the values and links provided above.
62
+
63
+ ```typescript
64
+ import { chainflow } from Chainflow;
65
+
66
+ const flow = chainflow()
67
+ .call(createUser)
68
+ .call(createRole)
69
+ .call(getUser);
70
+
71
+ flow.run();
72
+ ```
73
+
74
+ ---
75
+
76
+ \
77
+ The above setup will result in the following API calls:
78
+
79
+ 1. `POST` Request to `/user` with body:
80
+
81
+ ```json
82
+ {
83
+ "name": "Tom",
84
+ "details": {
85
+ "age": 40
86
+ }
87
+ }
88
+ ```
89
+
90
+ 2. `POST` Request to `/role` with body:
91
+
92
+ ```json
93
+ {
94
+ "type": "Engineer",
95
+ "userId": "['userId' from response to step 1]"
96
+ }
97
+ ```
98
+
99
+ 3. `GET` Request to `/user?roleType=['type' from response to step 2]`
100
+
101
+ &nbsp;
102
+
103
+ ## More Features
104
+
105
+ ### Query params
106
+
107
+ Define query params with the `query` method on an endpoint.
108
+
109
+ ```typescript
110
+ const getUsersInGroup = origin.get('/user').query({ groupId: 'some-id' });
111
+ ```
112
+
113
+ ### Path params
114
+
115
+ Define path params by wrapping a param name with `{}` in the endpoint path.
116
+
117
+ ```typescript
118
+ const getGroupsWithUser = origin.get('/groups/{userId}');
119
+ ```
120
+
121
+ You can specify values for your path params by calling `pathParams`. Note that path params which do not actually exist in the path will be discarded.
122
+
123
+ ```typescript
124
+ const getGroupsWithUser = origin.get('/groups/{userId}').pathParams({
125
+ userId: 'user123',
126
+ });
127
+ ```
128
+
129
+ ### Headers
130
+
131
+ Specify headers with `headers` method on endpoints.
132
+
133
+ ```typescript
134
+ const getInfo = origin.get('/info').headers({ token: 'some-token' });
135
+ ```
136
+
137
+ You can also use `headers` on an `OriginServer` to have all endpoints made for that origin bear those headers.
138
+
139
+ ```typescript
140
+ const origin = originServer('127.0.0.1:3001').headers({ token: 'some-token' });
141
+
142
+ const getInfo = origin.get('/info'); // getInfo endpoint will have the headers defined above
143
+ ```
144
+
145
+ The request payloads under `Basic Usage` are defined with only _default_ values - i.e. the values which a Chainflow use if there are no response values from other endpoint calls linked to it.
146
+
147
+ However, you can also use the following features to more flexibly define the values used in a request.
148
+
149
+ ### `required`
150
+
151
+ Marks a value as required but without a default. The chainflow will expect this value to be sourced from another node. If no such source is available, the endpoint call will throw an error.
152
+
153
+ ```typescript
154
+ const createUser = origin.post('/user').body({
155
+ name: required(),
156
+ });
157
+ ```
158
+
159
+ ### `gen`
160
+
161
+ Provide a callback that generates values for building requests.
162
+
163
+ ```typescript
164
+ const randAge = () => Math.floor(Math.random() * 100);
165
+
166
+ const createUser = origin.post('/user').body({
167
+ name: 'Tom',
168
+ details: {
169
+ age: gen(randAge),
170
+ },
171
+ });
172
+ ```
173
+
174
+ ### `link`
175
+
176
+ You can use the `link` function to specify a callback to transform the response value before it is passed to the input node.
177
+
178
+ ```typescript
179
+ const addGreeting = (name: string) => `Hello ${name}`;
180
+
181
+ const createMessage = origin.post('message').body({
182
+ msg: link(getUser.resp.body.name, addGreeting);
183
+ });
184
+ ```
185
+
186
+ ### `set`
187
+
188
+ The `link` has another function signature.
189
+
190
+ You can use the `set` method on an endpoint to expose its input nodes, then use the 2nd function signature of `link` as shown below: pass in the input node first (`msg`), then the source node second and optionally a callback third.
191
+
192
+ ```typescript
193
+ createMessage.set(({ body: { msg } }) => {
194
+ link(msg, getUser.resp.body.name);
195
+ link(msg, createUser.resp.body.name);
196
+ });
197
+ ```
198
+
199
+ With a callback:
200
+
201
+ ```typescript
202
+ createMessage.set(({ body: { msg } }) => {
203
+ link(msg, getUser.resp.body.name, addGreeting);
204
+ link(msg, createUser.resp.body.name, addGreeting);
205
+ });
206
+ ```
207
+
208
+ ### `linkMerge`
209
+
210
+ Link multiple response values to a single request node with an optional callback to merge the values into a single input value. This has 4 function signatures:
211
+
212
+ For the argument containing the source nodes, you can either pass an _array_ of SourceNodes:
213
+
214
+ ```typescript
215
+ // note the callback has an array parameter
216
+ const mergeValues = ([name, favAnimal]: [string, string]) =>
217
+ `${name} likes ${favAnimal}.`;
218
+
219
+ const createMessage = origin.post('message').body({
220
+ msg: linkMerge(
221
+ // array of source nodes
222
+ [getUser.resp.body.name, getFavAnimal.resp.body.favAnimal],
223
+ mergeValues,
224
+ );
225
+ });
226
+ ```
227
+
228
+ or you can pass an _object_ with SourceNodes as the values:
229
+
230
+ ```typescript
231
+ // note the callback has an object parameter
232
+ const mergeValues = ({
233
+ userName,
234
+ favAnimal,
235
+ }: {
236
+ userName: string;
237
+ favAnimal: string;
238
+ }) => `${userName} likes ${favAnimal}.`;
239
+
240
+
241
+ const createMessage = origin.post('message').body({
242
+ msg: linkMerge(
243
+ // object of source nodes
244
+ {
245
+ userName: getUser.resp.body.name,
246
+ favAnimal: getFavAnimal.resp.body.favAnimal,
247
+ },
248
+ mergeValues,
249
+ );
250
+ });
251
+ ```
252
+
253
+ alternatively, you can use the `set` method in addition with the other function signature of `linkMerge` (similar to how `link` above has overloads to work with `set`).
254
+
255
+ with array:
256
+
257
+ ```typescript
258
+ createMessage.set(({ body: { msg } }) => {
259
+ linkMerge(
260
+ msg, // the input node
261
+ [getUser.resp.body.name, getFavAnimal.resp.body.favAnimal],
262
+ mergeValues,
263
+ );
264
+ });
265
+ ```
266
+
267
+ with object:
268
+
269
+ ```typescript
270
+ createMessage.set(({ body: { msg } }) => {
271
+ linkMerge(
272
+ msg, // the input node
273
+ {
274
+ userName: getUser.resp.body.name,
275
+ favAnimal: getFavAnimal.resp.body.favAnimal,
276
+ },
277
+ mergeValues,
278
+ );
279
+ });
280
+ ```
281
+
282
+ Note that the merging link created by this method will only be used if ALL the source nodes specified are available i.e. if either one of `getUser.resp.body.name` or `getFavAnimal.resp.body.favAnimal` does not have a value, this link will not be used at all.
283
+
284
+ ### Call Options
285
+
286
+ You can declare manual values for an endpoint call in the chainflow itself, should you need to do so, by passing in a Call Options object as a second argument in the `call` method.
287
+
288
+ `body`, `pathParams`, `query` and `headers` can be set this way.
289
+
290
+ ```typescript
291
+ const createUser = origin.post('/user').body({
292
+ name: 'Tom',
293
+ });
294
+
295
+ chainflow()
296
+ .call(createUser, { body: { name: 'Harry' } })
297
+ .run();
298
+ ```
299
+
300
+ ### `seed`
301
+
302
+ You can specify request nodes to take values from the chainflow 'seed' by importing the `seed` object and linking nodes to it. Provide actual seed values by calling the `seed` method on a chainflow before you `run` it, like below.
303
+
304
+ ```typescript
305
+ import { chainflow, link seed, } from 'chainflow';
306
+
307
+ const createUser = origin.post('/user').body({
308
+ name: required(),
309
+ });
310
+
311
+ createUser.set(({ body: { name }}) => {
312
+ link(name, seed.username);
313
+ });
314
+
315
+ chainflow()
316
+ .call()
317
+ .seed({ username: 'Tom' })
318
+ .run();
319
+ ```
320
+
321
+ ### Allow Undefined Sources Values
322
+
323
+ By default, an input node will reject and skip a source node's value if it is unavailable or `undefined`. However, you can change this by passing a source node into the `config` utility function and passing an options object as the second parameter like below. This informs an input node to use the source node's value regardless of whether the value is `undefined` or not.
324
+
325
+ ```typescript
326
+ import { config } from 'chainflow';
327
+
328
+ createUser.set(({ body: { name } }) => {
329
+ link(name, config(seed.username, { allowUndefined: true }));
330
+ });
331
+ ```
332
+
333
+ This has important implications - it means that as long as the source (e.g. a response from an endpoint call) is available, then the linked source node's value will be taken and used (even if that value is unavailable, which would be taken as `undefined`). Therefore, any other linked sources will not be used UNLESS 1. they have a higher priority or 2. the source providing the linked node that allows `undefined` is unavailable.
334
+
335
+ &nbsp;
336
+
337
+ ### `clone`
338
+
339
+ You can create chainflow "templates" with the use of `clone` to create a copy of a chainflow and its endpoint callqueue. The clone can have endpoint calls added to it without modifying the initial flow.
340
+
341
+ ```typescript
342
+ const initialFlow = chainflow().call(endpoint1).call(endpoint2);
343
+
344
+ const clonedFlow = initialFlow.clone();
345
+
346
+ clonedFlow.call(endpoint3).run(); // calls endpoint 1, 2 and 3
347
+ initialFlow.call(endpoint4).run(); // calls endpoint 1, 2 and 4
348
+ ```
349
+
350
+ ### `extend`
351
+
352
+ You can connect multiple different chainflows together into a longer chainflow using `extend`.
353
+
354
+ ```typescript
355
+ const flow1 = chainflow().call(endpoint1).call(endpoint2);
356
+ const flow2 = chainflow().call(endpoint3);
357
+
358
+ flow1.extend(flow2).run(); // calls endpoint 1, 2 and 3
359
+ ```
360
+
361
+ ### `config`
362
+
363
+ `respParser`
364
+ By default, Chainflows will parse response bodies as JSON objects. To change this, you can call `.config` to change that configuration on an `endpoint` (or on an `OriginServer`, to apply it to all endpoints created from it) like so:
365
+
366
+ ```typescript
367
+ import { RESP_PARSER } from 'chainflow';
368
+
369
+ const getUser = origin.get('/user').config({
370
+ respParser: RESP_PARSER.TEXT,
371
+ });
372
+ ```
373
+
374
+ or with camelcase in JavaScript:
375
+
376
+ ```javascript
377
+ const getUser = origin.get('/user').config({
378
+ respParser: 'text',
379
+ });
380
+ ```
381
+
382
+ There are 4 supported ways to parse response bodies (as provided by the underlying HTTP client, `undici`): `arrayBuffer`, `blob`, `json` and `text`.
383
+
384
+ `respValidator`
385
+ Another configuration option is how to validate the response to an endpoint. By default, Chainflow rejects responses that have HTTP status code 400 and above and throws an error. You can pass in a custom `respValidator` to change when a response is rejected.
386
+
387
+ ```typescript
388
+ const getUser = origin.get("/user").config({
389
+ respValidator: (resp) => {
390
+ if (resp.statusCode !== 201)
391
+ return { valid: false, msg: "Failed to retrieve users." };
392
+ if (!Object.keys(resp.body as Record<string, unknown>).includes("id"))
393
+ return { valid: false, msg: "Response did not provide user ID." };
394
+ return { valid: true };
395
+ },
396
+ });
397
+ ```
398
+
399
+ Your custom validator callback should have a return type:
400
+
401
+ ```typescript
402
+ {
403
+ valid: boolean; // false if response should be rejected
404
+ msg?: string; // error message
405
+ }
406
+ ```
407
+
408
+ ### `store`
409
+
410
+ Instead of direct links between endpoints, you can use a central store to keep values from some endpoints and have other endpoints take from it via the special `store` object.
411
+
412
+ ```typescript
413
+ import { store } from 'chainflow';
414
+
415
+ const createUser = origin
416
+ .post('/user')
417
+ .body({
418
+ name: 'Tom',
419
+ })
420
+ .store((resp) => ({
421
+ // this endpoint will store `id` from a response to `userId` in the store
422
+ userId: resp.body.id,
423
+ }));
424
+
425
+ const addRole = origin.post('/role').body({
426
+ // this endpoint will take `userId` from the store, if available
427
+ userId: store.userId,
428
+ role: 'Engineer',
429
+ });
430
+
431
+ chainflow().call(createUser).call(addRole).run();
432
+ ```
433
+
434
+ This is usually useful when you have endpoints that could take a value from any one of many other endpoints for the same input node. Having a store to centralise these many-to-many relationships (like an API Gateway) can improve the developer experience.
435
+
436
+ ### `continuesFrom` - transferring Chainflow states
437
+
438
+ Say we have 2 endpoints, `login` and `createGroup`. We want to login as a user once, then proceed to proceed 3 groups as that same user without having to login 3 times.
439
+
440
+ ```typescript
441
+ const createGroup = origin
442
+ .post('/group')
443
+ .headers({
444
+ Authorization: login.resp.body.authToken,
445
+ })
446
+ .body({
447
+ groupName: seed.groupName,
448
+ });
449
+
450
+ // loggedInFlow will contain a response from the `login` endpoint
451
+ const loggedInFlow = chainflow().call(login).run();
452
+
453
+ // createGroupFlow will take the response that
454
+ // loggedInFlow received and carry on from there
455
+ const createGroupFlow = chainflow().call(createGroup).continuesFrom(loggedInFlow);
456
+
457
+ const groupNames = ['RapGPT', 'Averageexpedition', 'Shaky Osmosis'];
458
+ for (const groupName in groupNames) {
459
+ createGroupFlow.seed({ groupName }).run();
460
+ }
461
+ ```
462
+
463
+ We run a chainflow that calls `login` first to get a response from the login endpoint.
464
+
465
+ Using the `continuesFrom` method, `createGroupFlow` will copy the state of source values (i.e. responses) from `loggedInFlow`. This means `createGroupFlow` will now have the logged in user's `authToken` received from calling `login`, and will use it when calling `createGroup` thrice for each group name in the `groupNames` array.
466
+
467
+ ### `responses`
468
+
469
+ After running a chainflow, you can retrieve the responses received from endpoint calls via the `responses` property on that chainflow.
470
+
471
+ ```typescript
472
+ const flow = chainflow().run(createUser).run(getRoles);
473
+
474
+ const responses = flow.responses;
475
+ ```
476
+
477
+ The responses will look something like:
478
+
479
+ ```typescript
480
+ [
481
+ {
482
+ details: '[POST] /user' // identifies the endpoint called
483
+ val: { // the response to createUser
484
+ statusCode: 200,
485
+ body: ...,
486
+ headers: ...,
487
+ ...
488
+ }
489
+ },
490
+ {
491
+ details: '[GET] /roles'
492
+ val: ... // the response to getRoles
493
+ }
494
+ ]
495
+ ```
496
+
497
+ The responses in the array follow the order in which the respective endpoints are called.
498
+
499
+ ### `logging`
500
+
501
+ Enable logs from Chainflow by setting `ENABLE_CHAINFLOW_LOGS=true` in your environment variables, or by simply importing and calling the `enableLogs` function.
502
+
503
+ ### Misc Behaviors
504
+
505
+ - If you have multiple endpoint calls to the same endpoint on one chainflow and they are linked to other endpoints' input nodes further down the flow, the latest endpoint call's values will be used.
506
+
507
+ For example:
508
+
509
+ ```typescript
510
+ chainflow().call(getUser).call(addRole).call(getUser).call(createGroup);
511
+ ```
512
+
513
+ If an input node on `createGroup` requires a value from a response to `getUser`, then `createGroup` will take that value from the last call to `getUser` (i.e. from the response to the 2nd call to `getUser` that happens _after_ the call to `addRole`).
514
+
515
+ ## Future Updates
516
+
517
+ Below features are currently not yet supported but are planned in future releases.
518
+
519
+ 1. More flexibility to log and return responses
520
+ 2. Conditional calls - execute an endpoint call only if some condition is met.
521
+ 3. (Exploratory) API performance measurement
522
+ 4. (Exploratory) Possibly some sort of UI/diagram generation
523
+
524
+ ## Development
525
+
526
+ ### Areas that could be better (non-exhaustive)
527
+
528
+ #### _Encoding endpoint IDs_
529
+
530
+ - Currently assumes that URLs of endpoints do not contain unencoded `|` and `[]` characters. `[]` used to wrap around HTTP method in the encoded ID. Linkmerge uses `|` to separate different encoded IDs.
531
+ - Current implementation also leads to ID collision if multiple endpoints with the same method and path are created (but perhaps with different configuration) and are called on the same chainflow.
532
+ - Idea: Have a centralized service to issue unique IDs to deconflict endpoints - but still somehow encode the method/path info of an endpoint into it.
533
+
534
+ #### _Logging_
535
+
536
+ - Should further explore appropriate degree of detail for logging
537
+ - Truncation of requests/responses with extremely large payloads
538
+
539
+ ### Trivia
540
+
541
+ - You probably noticed that I enjoy using the Builder pattern for its clarity.
542
+ - I'm praying the wave 🌊 emoji remains sufficiently shaped like a "C" to avoid confusion. Please let me know if there is some system where it does not!