chainflow 0.1.7 → 0.1.8
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 +40 -17
- package/dist/http/endpoint.js +12 -9
- package/dist/http/endpoint.js.map +1 -1
- package/dist/http/utils/client.d.ts +11 -8
- package/dist/http/utils/client.js +23 -9
- package/dist/http/utils/client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<h1 align="center" style="border-bottom: none;">🌊hainflow</h1>
|
|
2
|
-
<h3 align="center">
|
|
2
|
+
<h3 align="center">An Open Source library to create dynamic and composable API call workflows.</h3>
|
|
3
3
|
<div align="center">
|
|
4
4
|
|
|
5
5
|
[](https://github.com/edwinlzs/chainflow/blob/main/LICENSE)
|
|
@@ -11,22 +11,23 @@
|
|
|
11
11
|
[](https://codecov.io/gh/edwinlzs/chainflow)
|
|
12
12
|
</div>
|
|
13
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
14
|
## Documentation
|
|
19
15
|
|
|
20
16
|
Read the guides over at [Chainflow Docs](https://edwinlzs.github.io/chainflow-docs/) to get started!
|
|
21
17
|
|
|
22
|
-
##
|
|
18
|
+
## When might Chainflow be useful?
|
|
19
|
+
|
|
20
|
+
1. **_Setting up demo data_**
|
|
21
|
+
|
|
22
|
+
Say you have an application that you're developing new features for and you'd like to demonstrate those features. You may need your app to be in a certain context and hence your database in a specific state - perhaps a user has to be logged in with certain permissions, and to have already created a "group" in the app and added other users to that group. You may use raw SQL or other DB scripts to put your DB into that state by inserting users, roles, etc.. However, those scripts could miss out on important side effects relevant to the business context of your app that tend to be built into the services exposed by your backend server. Hence, you can use Chainflow to help compose API call workflows to setup the data in your app by calling the revelant service endpoints you have built e.g. `POST /user`, `POST /role`. You can then minimize your use of database scripts to mainly data that is not configurable with existing endpoints.
|
|
23
|
+
|
|
24
|
+
2. **_Speeding up development_**
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
Similar to setting up demo data, often while coding new features you may want to test out how they behave in your app, and again you may want your app to be in a specific state locally for that. You can write API call workflow scripts built with Chainflow to help move your app into those states quickly.
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
4. Test edge cases on backend endpoints with input variations
|
|
28
|
+
3. **_Testing your endpoints_**
|
|
29
|
+
|
|
30
|
+
An API call workflow could behave as if it were a frontend client calling the backend. In that way, you can create UI-agnostic end-to-end testing of backend endpoints by using API call workflows to simulate how a frontend would interact with the backend.
|
|
30
31
|
|
|
31
32
|
## Basic Usage
|
|
32
33
|
|
|
@@ -142,6 +143,27 @@ const origin = originServer('127.0.0.1:3001').headers({ token: 'some-token' });
|
|
|
142
143
|
const getInfo = origin.get('/info'); // getInfo endpoint will have the headers defined above
|
|
143
144
|
```
|
|
144
145
|
|
|
146
|
+
### Default headers
|
|
147
|
+
|
|
148
|
+
Chainflow attaches default headers to all requests made by any endpoint with the value:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
'content-type': 'application/json',
|
|
152
|
+
'User-Agent': 'Chainflow/[major.minor version number]',
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
If you'd like to change this, pass your default headers to the `defaultHeaders` util.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { defaultHeaders } from 'chainflow';
|
|
159
|
+
|
|
160
|
+
defaultHeaders({ 'content-type': 'application/xml' });
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Pass in `true` as the second argument if you want to replace the entire set of default headers. Otherwise, the example above only overwrites the `content-type` default header and keeps `User-Agent`.
|
|
164
|
+
|
|
165
|
+
### Initializing Values
|
|
166
|
+
|
|
145
167
|
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
168
|
|
|
147
169
|
However, you can also use the following features to more flexibly define the values used in a request.
|
|
@@ -361,7 +383,9 @@ flow1.extend(flow2).run(); // calls endpoint 1, 2 and 3
|
|
|
361
383
|
### `config`
|
|
362
384
|
|
|
363
385
|
`respParser`
|
|
364
|
-
By default,
|
|
386
|
+
By default, a chainflow parses response bodies as JSON objects UNLESS the status code is `204` or the `content-type` header does not contain `application/json` (to avoid errors when parsing an empty body), upon which they will instead parse it as text.
|
|
387
|
+
|
|
388
|
+
To set a specific parsing format, 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
389
|
|
|
366
390
|
```typescript
|
|
367
391
|
import { RESP_PARSER } from 'chainflow';
|
|
@@ -385,12 +409,11 @@ There are 4 supported ways to parse response bodies (as provided by the underlyi
|
|
|
385
409
|
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
410
|
|
|
387
411
|
```typescript
|
|
388
|
-
const getUser = origin.get(
|
|
412
|
+
const getUser = origin.get('/user').config({
|
|
389
413
|
respValidator: (resp) => {
|
|
390
|
-
if (resp.statusCode !== 201)
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
return { valid: false, msg: "Response did not provide user ID." };
|
|
414
|
+
if (resp.statusCode !== 201) return { valid: false, msg: 'Failed to retrieve users.' };
|
|
415
|
+
if (!Object.keys(resp.body as Record<string, unknown>).includes('id'))
|
|
416
|
+
return { valid: false, msg: 'Response did not provide user ID.' };
|
|
394
417
|
return { valid: true };
|
|
395
418
|
},
|
|
396
419
|
});
|
package/dist/http/endpoint.js
CHANGED
|
@@ -22,13 +22,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
22
22
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
24
|
};
|
|
25
|
-
var _Endpoint_instances, _Endpoint_addr, _Endpoint_path, _Endpoint_method, _Endpoint_req, _Endpoint_resp, _Endpoint_config, _Endpoint_store, _Endpoint_extractPathParams, _Endpoint_insertPathParams, _Endpoint_insertQueryParams, _Endpoint_validateResp, _Endpoint_findMissingValues,
|
|
25
|
+
var _Endpoint_instances, _Endpoint_addr, _Endpoint_path, _Endpoint_method, _Endpoint_req, _Endpoint_resp, _Endpoint_config, _Endpoint_store, _Endpoint_extractPathParams, _Endpoint_insertPathParams, _Endpoint_insertQueryParams, _Endpoint_validateResp, _Endpoint_findMissingValues, _Endpoint_parseResponseBody;
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
27
|
exports.Endpoint = exports.RESP_PARSER = void 0;
|
|
28
28
|
const id_1 = require("./utils/id");
|
|
29
29
|
const inputNode_1 = require("../core/inputNode");
|
|
30
30
|
const reqBuilder_1 = require("./reqBuilder");
|
|
31
|
-
const client_1 =
|
|
31
|
+
const client_1 = require("./utils/client");
|
|
32
32
|
const errors_1 = require("./errors");
|
|
33
33
|
const deepmerge_1 = __importDefault(require("@fastify/deepmerge"));
|
|
34
34
|
const sourceNode_1 = require("../core/sourceNode");
|
|
@@ -157,7 +157,7 @@ class Endpoint {
|
|
|
157
157
|
headers = deepmerge(headers, opts.headers);
|
|
158
158
|
callPath = __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_insertPathParams).call(this, callPath, pathParams);
|
|
159
159
|
callPath = __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_insertQueryParams).call(this, callPath, queryParams);
|
|
160
|
-
const resp = yield client_1.
|
|
160
|
+
const resp = yield client_1.httpClient.request({
|
|
161
161
|
addr: __classPrivateFieldGet(this, _Endpoint_addr, "f"),
|
|
162
162
|
path: callPath,
|
|
163
163
|
method: __classPrivateFieldGet(this, _Endpoint_method, "f").toUpperCase(),
|
|
@@ -166,7 +166,7 @@ class Endpoint {
|
|
|
166
166
|
});
|
|
167
167
|
if (resp == null)
|
|
168
168
|
throw new errors_1.InvalidResponseError('No response received.');
|
|
169
|
-
const parsedResp = Object.assign(Object.assign({}, resp), { body:
|
|
169
|
+
const parsedResp = Object.assign(Object.assign({}, resp), { body: yield __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_parseResponseBody).call(this, resp) });
|
|
170
170
|
const results = (_c = (_b = (_a = __classPrivateFieldGet(this, _Endpoint_config, "f")).respValidator) === null || _b === void 0 ? void 0 : _b.call(_a, parsedResp)) !== null && _c !== void 0 ? _c : __classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_validateResp).call(this, parsedResp);
|
|
171
171
|
if (!results.valid)
|
|
172
172
|
throw new errors_1.InvalidResponseError(results.msg);
|
|
@@ -229,18 +229,21 @@ _Endpoint_addr = new WeakMap(), _Endpoint_path = new WeakMap(), _Endpoint_method
|
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
return finalMissingValues;
|
|
232
|
-
},
|
|
232
|
+
}, _Endpoint_parseResponseBody = function _Endpoint_parseResponseBody(resp) {
|
|
233
233
|
return __awaiter(this, void 0, void 0, function* () {
|
|
234
234
|
switch (__classPrivateFieldGet(this, _Endpoint_config, "f").respParser) {
|
|
235
235
|
case RESP_PARSER.ARRAY_BUFFER:
|
|
236
|
-
return yield body.arrayBuffer();
|
|
236
|
+
return yield resp.body.arrayBuffer();
|
|
237
237
|
case RESP_PARSER.BLOB:
|
|
238
|
-
return yield body.blob();
|
|
238
|
+
return yield resp.body.blob();
|
|
239
239
|
case RESP_PARSER.TEXT:
|
|
240
|
-
return yield body.text();
|
|
240
|
+
return yield resp.body.text();
|
|
241
241
|
case RESP_PARSER.JSON:
|
|
242
|
+
return yield resp.body.json();
|
|
242
243
|
default:
|
|
243
|
-
|
|
244
|
+
if ((0, client_1.checkJsonSafe)(resp))
|
|
245
|
+
return yield resp.body.json();
|
|
246
|
+
return yield resp.body.text();
|
|
244
247
|
}
|
|
245
248
|
});
|
|
246
249
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../src/http/endpoint.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAA2C;AAC3C,iDAAwE;AACxE,6CAA0C;AAC1C,
|
|
1
|
+
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../src/http/endpoint.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAA2C;AAC3C,iDAAwE;AACxE,6CAA0C;AAC1C,2CAAuF;AAEvF,qCAIkB;AAElB,mEAAgD;AAChD,mDAA4D;AAC5D,mDAA0E;AAC1E,6DAAsD;AACtD,yCAA8C;AAC9C,qCAAgC;AAChC,iDAAwE;AAGxE,MAAM,SAAS,GAAG,IAAA,mBAAc,GAAE,CAAC;AAEnC,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAyB3C,0CAA0C;AAC1C,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,2CAA4B,CAAA;IAC5B,4BAAa,CAAA;IACb,4BAAa,CAAA;IACb,4BAAa,CAAA;AACf,CAAC,EALW,WAAW,2BAAX,WAAW,QAKtB;AAUD;;;GAGG;AACH,MAAa,QAAQ;IAUnB,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAkD;;QARlF,yBAAgB,WAAW,EAAC;QAC5B,iCAAc;QACd,mCAA0B;QAC1B,gCAAiB;QACjB,iCAAkB;QAClB,2BAA0B,EAAE,EAAC;QAC7B,0BAAgB,IAAI,aAAK,EAAE,EAAC;QAG1B,MAAM,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;QAC9B,IAAI,CAAC,6BAAiB,CAAC,QAAQ,CAAC,MAA0B,CAAC;YACzD,MAAM,IAAI,+BAAsB,CAAC,MAAM,CAAC,CAAC;QAC3C,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,IAA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QACX,CAAC;QACD,IAAI,KAAK,GAAG,GAAG,CAAC;QAChB,KAAK,MAAM,QAAQ,IAAI,IAAI,EAAE,CAAC;YAC5B,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;YACxB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;gBACxC,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,kBAAkB,CAAC;AAC5B,CAAC,qEAGwB,IAA6B;;QACpD,QAAQ,uBAAA,IAAI,wBAAQ,CAAC,UAAU,EAAE,CAAC;YAChC,KAAK,WAAW,CAAC,YAAY;gBAC3B,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YACvC,KAAK,WAAW,CAAC,IAAI;gBACnB,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YAChC,KAAK,WAAW,CAAC,IAAI;gBACnB,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YAChC,KAAK,WAAW,CAAC,IAAI;gBACnB,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YAChC;gBACE,IAAI,IAAA,sBAAa,EAAC,IAAI,CAAC;oBAAE,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;gBACvD,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QAClC,CAAC;IACH,CAAC"}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
|
+
import { Dispatcher } from 'undici';
|
|
1
2
|
export type SUPPORTED_METHOD_UPPERCASE = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS';
|
|
2
3
|
/** @todo evaluate if defaults should change */
|
|
3
|
-
export declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
'content-type': string;
|
|
7
|
-
};
|
|
8
|
-
declare const _default: {
|
|
4
|
+
export declare let _defaultHeaders: Record<string, string> | undefined;
|
|
5
|
+
export declare const defaultHeaders: (headers?: Record<string, string>, replace?: boolean) => void;
|
|
6
|
+
export declare const httpClient: {
|
|
9
7
|
request: ({ addr, path, method, body, headers, }: {
|
|
10
8
|
addr: string;
|
|
11
9
|
path: string;
|
|
12
10
|
method: SUPPORTED_METHOD_UPPERCASE;
|
|
13
11
|
body?: any;
|
|
14
12
|
headers?: Record<string, string> | undefined;
|
|
15
|
-
}) => Promise<
|
|
13
|
+
}) => Promise<Dispatcher.ResponseData | null>;
|
|
16
14
|
};
|
|
17
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Check required to avoid errors when attempting `json()` on an empty body.
|
|
17
|
+
* Refer to issue under `ResponseData` at
|
|
18
|
+
* https://github.com/nodejs/undici/blob/main/docs/docs/api/Dispatcher.md
|
|
19
|
+
*/
|
|
20
|
+
export declare const checkJsonSafe: (resp: Dispatcher.ResponseData) => boolean;
|
|
@@ -9,25 +9,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.defaultHeaders = void 0;
|
|
12
|
+
exports.checkJsonSafe = exports.httpClient = exports.defaultHeaders = exports._defaultHeaders = void 0;
|
|
13
13
|
const undici_1 = require("undici");
|
|
14
14
|
const logger_1 = require("../logger");
|
|
15
15
|
/** @todo evaluate if defaults should change */
|
|
16
|
-
exports.
|
|
17
|
-
connection: 'keep-alive',
|
|
18
|
-
accept: '*/*',
|
|
16
|
+
exports._defaultHeaders = {
|
|
19
17
|
'content-type': 'application/json',
|
|
20
|
-
|
|
18
|
+
'User-Agent': 'Chainflow/0.1',
|
|
21
19
|
};
|
|
20
|
+
const defaultHeaders = (headers, replace) => {
|
|
21
|
+
replace
|
|
22
|
+
? (exports._defaultHeaders = headers)
|
|
23
|
+
: (exports._defaultHeaders = Object.assign(Object.assign({}, exports._defaultHeaders), headers));
|
|
24
|
+
};
|
|
25
|
+
exports.defaultHeaders = defaultHeaders;
|
|
22
26
|
/** Sends a HTTP request. */
|
|
23
27
|
const request = ({ addr, path, method, body, headers, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
-
const finalHeaders = Object.assign(Object.assign({}, exports.
|
|
28
|
+
const finalHeaders = Object.assign(Object.assign({}, exports._defaultHeaders), headers);
|
|
25
29
|
(0, logger_1.log)(`[${method}] [${addr}${path}] with headers %O${body ? ' and payload %O' : ''}`, finalHeaders, body !== null && body !== void 0 ? body : '');
|
|
26
30
|
try {
|
|
27
31
|
const resp = yield (0, undici_1.request)(`${addr}${path}`, {
|
|
28
32
|
method,
|
|
29
|
-
body: JSON.stringify(body),
|
|
30
|
-
headers: finalHeaders,
|
|
33
|
+
body: body ? JSON.stringify(body) : null,
|
|
34
|
+
headers: Object.keys(finalHeaders).length > 0 ? finalHeaders : null,
|
|
31
35
|
});
|
|
32
36
|
return resp;
|
|
33
37
|
}
|
|
@@ -36,5 +40,15 @@ const request = ({ addr, path, method, body, headers, }) => __awaiter(void 0, vo
|
|
|
36
40
|
return null;
|
|
37
41
|
}
|
|
38
42
|
});
|
|
39
|
-
exports.
|
|
43
|
+
exports.httpClient = { request };
|
|
44
|
+
/**
|
|
45
|
+
* Check required to avoid errors when attempting `json()` on an empty body.
|
|
46
|
+
* Refer to issue under `ResponseData` at
|
|
47
|
+
* https://github.com/nodejs/undici/blob/main/docs/docs/api/Dispatcher.md
|
|
48
|
+
*/
|
|
49
|
+
const checkJsonSafe = (resp) => {
|
|
50
|
+
var _a;
|
|
51
|
+
return Boolean(resp.statusCode !== 204 && ((_a = resp.headers['content-type']) === null || _a === void 0 ? void 0 : _a.includes('application/json')));
|
|
52
|
+
};
|
|
53
|
+
exports.checkJsonSafe = checkJsonSafe;
|
|
40
54
|
//# sourceMappingURL=client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/http/utils/client.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/http/utils/client.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAA8D;AAC9D,sCAAsC;AAItC,+CAA+C;AACpC,QAAA,eAAe,GAAuC;IAC/D,cAAc,EAAE,kBAAkB;IAClC,YAAY,EAAE,eAAe;CAC9B,CAAC;AAEK,MAAM,cAAc,GAAG,CAAC,OAAgC,EAAE,OAAiB,EAAE,EAAE;IACpF,OAAO;QACL,CAAC,CAAC,CAAC,uBAAe,GAAG,OAAO,CAAC;QAC7B,CAAC,CAAC,CAAC,uBAAe,mCACX,uBAAe,GACf,OAAO,CACX,CAAC,CAAC;AACT,CAAC,CAAC;AAPW,QAAA,cAAc,kBAOzB;AAEF,4BAA4B;AAC5B,MAAM,OAAO,GAAG,CAAO,EACrB,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,IAAI,EACJ,OAAO,GAOR,EAAE,EAAE;IACH,MAAM,YAAY,mCACb,uBAAe,GACf,OAAO,CACX,CAAC;IAEF,IAAA,YAAG,EACD,IAAI,MAAM,MAAM,IAAI,GAAG,IAAI,oBAAoB,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,EAAE,EAC9E,YAAY,EACZ,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CACX,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,IAAA,gBAAa,EAAC,GAAG,IAAI,GAAG,IAAI,EAAE,EAAE;YACjD,MAAM;YACN,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI;YACxC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI;SACpE,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAA,aAAI,EAAC,mBAAmB,GAAG,EAAE,CAAC,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAA,CAAC;AAEW,QAAA,UAAU,GAAG,EAAE,OAAO,EAAE,CAAC;AAEtC;;;;GAIG;AACI,MAAM,aAAa,GAAG,CAAC,IAA6B,EAAW,EAAE;;IACtE,OAAO,OAAO,CACZ,IAAI,CAAC,UAAU,KAAK,GAAG,KAAI,MAAA,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,0CAAE,QAAQ,CAAC,kBAAkB,CAAC,CAAA,CACtF,CAAC;AACJ,CAAC,CAAC;AAJW,QAAA,aAAa,iBAIxB"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.enableLogs = void 0;
|
|
17
|
+
exports.defaultHeaders = exports.enableLogs = void 0;
|
|
18
18
|
__exportStar(require("./core/chainflow"), exports);
|
|
19
19
|
__exportStar(require("./core/utils/initializers"), exports);
|
|
20
20
|
__exportStar(require("./core/inputNode"), exports);
|
|
@@ -25,4 +25,6 @@ __exportStar(require("./http/reqBuilder"), exports);
|
|
|
25
25
|
__exportStar(require("./http/originServer"), exports);
|
|
26
26
|
var logger_1 = require("./http/logger");
|
|
27
27
|
Object.defineProperty(exports, "enableLogs", { enumerable: true, get: function () { return logger_1.enableLogs; } });
|
|
28
|
+
var client_1 = require("./http/utils/client");
|
|
29
|
+
Object.defineProperty(exports, "defaultHeaders", { enumerable: true, get: function () { return client_1.defaultHeaders; } });
|
|
28
30
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,4DAA0C;AAC1C,mDAAiC;AACjC,oDAAkC;AAClC,sDAAoC;AACpC,kDAAgC;AAChC,oDAAkC;AAClC,sDAAoC;AACpC,wCAA2C;AAAlC,oGAAA,UAAU,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,4DAA0C;AAC1C,mDAAiC;AACjC,oDAAkC;AAClC,sDAAoC;AACpC,kDAAgC;AAChC,oDAAkC;AAClC,sDAAoC;AACpC,wCAA2C;AAAlC,oGAAA,UAAU,OAAA;AACnB,8CAAqD;AAA5C,wGAAA,cAAc,OAAA"}
|