@zajno/common 2.5.0 → 2.6.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/cjs/api/builder.js +30 -0
- package/cjs/api/builder.js.map +1 -0
- package/cjs/api/call.js +14 -7
- package/cjs/api/call.js.map +1 -1
- package/cjs/api/endpoint.js +65 -58
- package/cjs/api/endpoint.js.map +1 -1
- package/cjs/api/endpoint.types.js +0 -8
- package/cjs/api/endpoint.types.js.map +1 -1
- package/cjs/api/error.js +12 -0
- package/cjs/api/error.js.map +1 -1
- package/cjs/api/extensions/contentType.js +39 -0
- package/cjs/api/extensions/contentType.js.map +1 -0
- package/cjs/api/extensions/index.js +6 -0
- package/cjs/api/extensions/index.js.map +1 -0
- package/cjs/api/extensions/validation.js +27 -0
- package/cjs/api/extensions/validation.js.map +1 -0
- package/cjs/api/helpers.js +3 -2
- package/cjs/api/helpers.js.map +1 -1
- package/cjs/api/logging.js +23 -0
- package/cjs/api/logging.js.map +1 -1
- package/esm/api/builder.js +26 -0
- package/esm/api/builder.js.map +1 -0
- package/esm/api/call.js +14 -7
- package/esm/api/call.js.map +1 -1
- package/esm/api/endpoint.js +65 -57
- package/esm/api/endpoint.js.map +1 -1
- package/esm/api/endpoint.types.js +1 -7
- package/esm/api/endpoint.types.js.map +1 -1
- package/esm/api/error.js +12 -0
- package/esm/api/error.js.map +1 -1
- package/esm/api/extensions/contentType.js +36 -0
- package/esm/api/extensions/contentType.js.map +1 -0
- package/esm/api/extensions/index.js +3 -0
- package/esm/api/extensions/index.js.map +1 -0
- package/esm/api/extensions/validation.js +24 -0
- package/esm/api/extensions/validation.js.map +1 -0
- package/esm/api/helpers.js +3 -2
- package/esm/api/helpers.js.map +1 -1
- package/esm/api/logging.js +23 -0
- package/esm/api/logging.js.map +1 -1
- package/package.json +9 -16
- package/tsconfig.cjs.tsbuildinfo +1 -1
- package/tsconfig.esm.tsbuildinfo +1 -1
- package/tsconfig.types.tsbuildinfo +1 -1
- package/types/api/builder.d.ts +16 -0
- package/types/api/call.d.ts +17 -4
- package/types/api/endpoint.d.ts +39 -30
- package/types/api/endpoint.types.d.ts +23 -12
- package/types/api/error.d.ts +2 -0
- package/types/api/extensions/contentType.d.ts +22 -0
- package/types/api/extensions/index.d.ts +2 -0
- package/types/api/extensions/validation.d.ts +12 -0
- package/types/api/helpers.d.ts +1 -1
- package/types/api/logging.d.ts +27 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildApi = buildApi;
|
|
4
|
+
exports.createEndpointCallable = createEndpointCallable;
|
|
5
|
+
const endpoint_js_1 = require("./endpoint.js");
|
|
6
|
+
const assert_js_1 = require("../functions/assert.js");
|
|
7
|
+
/** Converts an composite API endpoints object to the same tree of functions */
|
|
8
|
+
function buildApi(api, caller) {
|
|
9
|
+
(0, assert_js_1.assert)(!!api && typeof api === 'object', 'API definition must be an object');
|
|
10
|
+
(0, assert_js_1.assert)(!!caller && typeof caller === 'function', 'Caller must be a function');
|
|
11
|
+
if (endpoint_js_1.ApiEndpoint.isEndpoint(api)) {
|
|
12
|
+
return createEndpointCallable(api, caller);
|
|
13
|
+
}
|
|
14
|
+
return Object.entries(api).reduce((acc, [key, value]) => {
|
|
15
|
+
const next = value;
|
|
16
|
+
acc[key] = buildApi(next, caller);
|
|
17
|
+
return acc;
|
|
18
|
+
}, {});
|
|
19
|
+
}
|
|
20
|
+
/** Partial application: binding an endpoint to callApi, so only input data and extra are passed to a newly created function. */
|
|
21
|
+
function createEndpointCallable(endpoint, caller) {
|
|
22
|
+
const name = `${endpoint.displayName || '?'}_${endpoint.method}_${endpoint.path.template()}`;
|
|
23
|
+
const fn = {
|
|
24
|
+
[name]: function (data, extra) { return caller(endpoint, data, extra); },
|
|
25
|
+
}[name]; // this sets the name of the function
|
|
26
|
+
const result = fn;
|
|
27
|
+
Object.assign(result, { Endpoint: endpoint });
|
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AAO,MAAM,CAAY,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5I,CAAC,IAAI,CAAC,CAAC,CAAC,qCAAqC;IAE9C,MAAM,MAAM,GAAG,EAAwC,CAAC;IACxD,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC9C,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
package/cjs/api/call.js
CHANGED
|
@@ -4,22 +4,24 @@ exports.buildApiCaller = buildApiCaller;
|
|
|
4
4
|
const register_js_1 = require("./register.js");
|
|
5
5
|
const helpers_js_1 = require("./helpers.js");
|
|
6
6
|
function buildApiCaller(options) {
|
|
7
|
-
const {
|
|
7
|
+
const { request, hooks = {} } = options;
|
|
8
8
|
return async function callApi(api, data, extra) {
|
|
9
9
|
const { headers, log = 'res', noLoader, ...restExtra } = extra || {};
|
|
10
10
|
const resultInput = data && { ...data };
|
|
11
11
|
const pathInputs = {};
|
|
12
|
+
const queryInputs = {};
|
|
12
13
|
let queryStr = '';
|
|
13
|
-
|
|
14
|
+
// extract path inputs from data
|
|
15
|
+
const pathKeys = api.path.args;
|
|
14
16
|
if (resultInput && pathKeys?.length) {
|
|
15
17
|
for (const key of pathKeys) {
|
|
16
18
|
pathInputs[key] = resultInput[key];
|
|
17
19
|
delete resultInput[key];
|
|
18
20
|
}
|
|
19
21
|
}
|
|
22
|
+
// extract query inputs from data
|
|
20
23
|
const queryKeysExpected = api.queryKeys;
|
|
21
24
|
if (resultInput && queryKeysExpected?.length) {
|
|
22
|
-
const queryInputs = {};
|
|
23
25
|
let empty = true;
|
|
24
26
|
for (const key of queryKeysExpected) {
|
|
25
27
|
const v = resultInput[key];
|
|
@@ -29,13 +31,15 @@ function buildApiCaller(options) {
|
|
|
29
31
|
}
|
|
30
32
|
delete resultInput[key];
|
|
31
33
|
}
|
|
34
|
+
// compile query string
|
|
32
35
|
if (!empty) {
|
|
33
36
|
const params = new URLSearchParams(queryInputs);
|
|
34
37
|
queryStr = '?' + params.toString();
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
|
-
if (
|
|
38
|
-
|
|
40
|
+
if (hooks.beforeConfig) {
|
|
41
|
+
// copy all inputs to avoid mutation
|
|
42
|
+
await hooks.beforeConfig(api, { ...resultInput }, { ...pathInputs }, { ...queryInputs });
|
|
39
43
|
}
|
|
40
44
|
const sendingData = resultInput && Object.keys(resultInput).length > 0
|
|
41
45
|
? resultInput
|
|
@@ -51,8 +55,11 @@ function buildApiCaller(options) {
|
|
|
51
55
|
_log: log,
|
|
52
56
|
_extra: restExtra,
|
|
53
57
|
};
|
|
54
|
-
if (
|
|
55
|
-
|
|
58
|
+
if (hooks.beforeRequest) {
|
|
59
|
+
const result = await hooks.beforeRequest(config);
|
|
60
|
+
if (result && typeof result === 'object') {
|
|
61
|
+
Object.assign(config, result);
|
|
62
|
+
}
|
|
56
63
|
}
|
|
57
64
|
const response = await request(config);
|
|
58
65
|
return register_js_1.PostProcessors.process(api, response?.data);
|
package/cjs/api/call.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"call.js","sourceRoot":"","sources":["../../../src/api/call.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"call.js","sourceRoot":"","sources":["../../../src/api/call.ts"],"names":[],"mappings":";;AAoEA,wCA2FC;AA/JD,+CAA8D;AAK9D,6CAAuC;AA+DvC,SAAgB,cAAc,CAA8C,OAA8B;IAEtG,MAAM,EAAE,OAAO,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IAExC,OAAO,KAAK,UAAU,OAAO,CACzB,GAAM,EACN,IAAyB,EACzB,KAAgC;QAMhC,MAAM,EACF,OAAO,EACP,GAAG,GAAG,KAAK,EACX,QAAQ,EACR,GAAG,SAAS,EACf,GAAG,KAAK,IAAI,EAAE,CAAC;QAEhB,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,GAAG,I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EAAE,GAAG,UAAU,EAAkC,EACjD,EAAE,GAAG,WAAW,EAAmC,CACtD,CAAC;QACN,CAAC;QAED,MAAM,WAAW,GAAG,WAAW,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,GAAG,CAAC;YAClE,CAAC,CAAC,WAAW;YACb,CAAC,CAAC,SAAS,CAAC;QAEhB,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,KAAK,CAAC;QACnC,MAAM,MAAM,GAAyC;YACjD,MAAM;YACN,GAAG,EAAE,IAAA,oBAAO,EAAC,GAAG,EAAE,UAA0C,CAAC,GAAG,QAAQ;YACxE,IAAI,EAAE,2BAAa,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,CAAC,IAAI,SAAS;YAC1D,OAAO,EAAE,OAAoB,IAAI,EAAE;YACnC,IAAI,EAAE,GAAG;YAET,SAAS,EAAE,QAAQ,IAAI,MAAgB,KAAK,KAAK;YACjD,IAAI,EAAE,GAAG;YACT,MAAM,EAAE,SAAmB;SAC9B,CAAC;QAEF,IAAI,KAAK,CAAC,aAAa,EAAE,CAAC;YACtB,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YACjD,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACvC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAClC,CAAC;QACL,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,MAAM,CAAmB,CAAC;QACzD,OAAO,4BAAc,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IACvD,CAAC,CAAC;AACN,CAAC"}
|
package/cjs/api/endpoint.js
CHANGED
|
@@ -3,67 +3,74 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ApiEndpoint = void 0;
|
|
4
4
|
const index_js_1 = require("../structures/path/index.js");
|
|
5
5
|
const methods_js_1 = require("./methods.js");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
out;
|
|
13
|
-
queryKeys;
|
|
14
|
-
queryTemplate;
|
|
15
|
-
errorProcessor;
|
|
16
|
-
headers;
|
|
17
|
-
get pathBuilder() { return this.path.as(); }
|
|
18
|
-
static construct(method = methods_js_1.EndpointMethods.GET, displayName) {
|
|
19
|
-
const res = new ApiEndpoint();
|
|
20
|
-
res.method = method;
|
|
21
|
-
res.displayName = displayName;
|
|
22
|
-
return res;
|
|
23
|
-
}
|
|
24
|
-
/** Creates an instance and applies 'GET' method for it. */
|
|
25
|
-
static get(displayName) {
|
|
26
|
-
return ApiEndpoint.construct(methods_js_1.EndpointMethods.GET, displayName);
|
|
27
|
-
}
|
|
28
|
-
/** Creates an instance and applies 'POST' method for it. */
|
|
29
|
-
static post(displayName) {
|
|
30
|
-
return ApiEndpoint.construct(methods_js_1.EndpointMethods.POST, displayName);
|
|
31
|
-
}
|
|
32
|
-
/** Creates an instance and applies 'DELETE' method for it. */
|
|
33
|
-
static delete(displayName) {
|
|
34
|
-
return ApiEndpoint.construct(methods_js_1.EndpointMethods.DELETE, displayName);
|
|
6
|
+
var ApiEndpoint;
|
|
7
|
+
(function (ApiEndpoint) {
|
|
8
|
+
/** Since we don't use class anymore, we need another way to determine an object to be an Endpoint instance. */
|
|
9
|
+
const ENDPOINT_OBJ_MARKER = '_ENDPOINT_OBJ_MARKER';
|
|
10
|
+
function isEndpoint(obj) {
|
|
11
|
+
return obj[ENDPOINT_OBJ_MARKER] === true;
|
|
35
12
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
13
|
+
ApiEndpoint.isEndpoint = isEndpoint;
|
|
14
|
+
function createBase(displayName) {
|
|
15
|
+
const data = {
|
|
16
|
+
path: index_js_1.Path.Empty,
|
|
17
|
+
displayName,
|
|
18
|
+
method: methods_js_1.EndpointMethods.GET,
|
|
19
|
+
};
|
|
20
|
+
data[ENDPOINT_OBJ_MARKER] = true;
|
|
21
|
+
const res = data;
|
|
22
|
+
// only extension methods
|
|
23
|
+
const ext = {
|
|
24
|
+
withMethod(method) {
|
|
25
|
+
data.method = method;
|
|
26
|
+
return res;
|
|
27
|
+
},
|
|
28
|
+
get() {
|
|
29
|
+
data.method = methods_js_1.EndpointMethods.GET;
|
|
30
|
+
return res;
|
|
31
|
+
},
|
|
32
|
+
put() {
|
|
33
|
+
data.method = methods_js_1.EndpointMethods.PUT;
|
|
34
|
+
return res;
|
|
35
|
+
},
|
|
36
|
+
post() {
|
|
37
|
+
data.method = methods_js_1.EndpointMethods.POST;
|
|
38
|
+
return res;
|
|
39
|
+
},
|
|
40
|
+
delete() {
|
|
41
|
+
data.method = methods_js_1.EndpointMethods.DELETE;
|
|
42
|
+
return res;
|
|
43
|
+
},
|
|
44
|
+
withPath(...path) {
|
|
45
|
+
const p = index_js_1.Path.construct(...path);
|
|
46
|
+
data.path = p;
|
|
47
|
+
return res;
|
|
48
|
+
},
|
|
49
|
+
withQuery(...queryKeys) {
|
|
50
|
+
data.queryKeys = queryKeys;
|
|
51
|
+
return res;
|
|
52
|
+
},
|
|
53
|
+
withErrors(errorProcessor) {
|
|
54
|
+
data.errorProcessor = errorProcessor;
|
|
55
|
+
return res;
|
|
56
|
+
},
|
|
57
|
+
withHeaders() {
|
|
58
|
+
return res;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
Object.assign(res, ext);
|
|
41
62
|
return res;
|
|
42
63
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
64
|
+
function createBuilder(baseFactory, extender) {
|
|
65
|
+
const res = function (displayName) {
|
|
66
|
+
const baseRes = baseFactory(displayName);
|
|
67
|
+
return extender ? extender(baseRes) : baseRes;
|
|
68
|
+
};
|
|
69
|
+
res.extend = (extender) => {
|
|
70
|
+
return createBuilder(res, extender);
|
|
71
|
+
};
|
|
47
72
|
return res;
|
|
48
73
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const res = this;
|
|
52
|
-
res.errorProcessor = errorProcessor;
|
|
53
|
-
return res;
|
|
54
|
-
}
|
|
55
|
-
/** Applies headers type. */
|
|
56
|
-
withHeaders(_headersMarker) {
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
/** Marks this instance as one has to be sent as form.
|
|
60
|
-
*
|
|
61
|
-
* TODO: introduce more content types support.
|
|
62
|
-
*/
|
|
63
|
-
asForm() {
|
|
64
|
-
this.isForm = true;
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
exports.ApiEndpoint = ApiEndpoint;
|
|
74
|
+
ApiEndpoint.create = createBuilder(createBase);
|
|
75
|
+
})(ApiEndpoint || (exports.ApiEndpoint = ApiEndpoint = {}));
|
|
69
76
|
//# sourceMappingURL=endpoint.js.map
|
package/cjs/api/endpoint.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../../src/api/endpoint.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../../src/api/endpoint.ts"],"names":[],"mappings":";;;AAAA,0DAAmD;AACnD,6CAA+C;AA+C/C,IAAiB,WAAW,CAyG3B;AAzGD,WAAiB,WAAW;IAExB,+GAA+G;IAC/G,MAAM,mBAAmB,GAAG,sBAAsB,CAAC;IAEnD,SAAgB,UAAU,CAAC,GAAQ;QAC/B,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,IAAI,CAAC;IAC7C,CAAC;IAFe,sBAAU,aAEzB,CAAA;IAED,SAAS,UAAU,CAAC,WAAoB;QAEpC,MAAM,IAAI,GAAG;YACT,IAAI,EAAE,eAAI,CAAC,KAAK;YAChB,WAAW;YACX,MAAM,EAAE,4BAAe,CAAC,GAAG;SAC9B,C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,aAAa,CAClB,WAA4C,EAC5C,QAA6B;QAE7B,MAAM,GAAG,GAAG,UAAU,WAAoB;YACtC,MAAM,OAAO,GAAG,WAAW,CAAC,WAAW,CAAC,CAAC;YACzC,OAAO,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;QAClD,CAAwB,CAAC;QAEzB,GAAG,CAAC,MAAM,GAAG,CAAC,QAAQ,EAAE,EAAE;YACtB,OAAO,aAAa,CAChB,GAAG,EACH,QAAQ,CACX,CAAC;QACN,CAAC,CAAC;QAEF,OAAO,GAAG,CAAC;IACf,CAAC;IAEY,kBAAM,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC;AACpD,CAAC,EAzGgB,WAAW,2BAAX,WAAW,QAyG3B"}
|
|
@@ -1,11 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IEndpointInfo = void 0;
|
|
4
|
-
var IEndpointInfo;
|
|
5
|
-
(function (IEndpointInfo) {
|
|
6
|
-
class IOut {
|
|
7
|
-
out;
|
|
8
|
-
}
|
|
9
|
-
IEndpointInfo.IOut = IOut;
|
|
10
|
-
})(IEndpointInfo || (exports.IEndpointInfo = IEndpointInfo = {}));
|
|
11
3
|
//# sourceMappingURL=endpoint.types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.types.js","sourceRoot":"","sources":["../../../src/api/endpoint.types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"endpoint.types.js","sourceRoot":"","sources":["../../../src/api/endpoint.types.ts"],"names":[],"mappings":""}
|
package/cjs/api/error.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
|
|
4
|
+
This module is mostly an example of how an API error data structures may look like.
|
|
5
|
+
|
|
6
|
+
It implies that API endpoints have a standardized data structure for errors, and that the error handling is done in a centralized way.
|
|
7
|
+
|
|
8
|
+
`ApiErrorResponse` is a base DTO for errors in responses, can be a combination of an error code, message, and a cause.
|
|
9
|
+
|
|
10
|
+
`ApiError` is an Error class that can be thrown and caught, and is basically a wrapper around the `ApiErrorResponse` DTO.
|
|
11
|
+
|
|
12
|
+
*/
|
|
2
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
14
|
exports.ApiError = exports.ApiErrorResponse = void 0;
|
|
4
15
|
var ApiErrorResponse;
|
|
@@ -8,6 +19,7 @@ var ApiErrorResponse;
|
|
|
8
19
|
}
|
|
9
20
|
ApiErrorResponse.create = create;
|
|
10
21
|
})(ApiErrorResponse || (exports.ApiErrorResponse = ApiErrorResponse = {}));
|
|
22
|
+
/** An Error to be thrown as an API error, and be caught */
|
|
11
23
|
class ApiError extends Error {
|
|
12
24
|
status;
|
|
13
25
|
code;
|
package/cjs/api/error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/api/error.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/api/error.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;EAUE;;;AAUF,IAAiB,gBAAgB,CAIhC;AAJD,WAAiB,gBAAgB;IAC7B,SAAgB,MAAM,CAA4C,IAAa,EAAE,OAAgB,EAAE,KAAc;QAC7G,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IACpC,CAAC;IAFe,uBAAM,SAErB,CAAA;AACL,CAAC,EAJgB,gBAAgB,gCAAhB,gBAAgB,QAIhC;AAED,2DAA2D;AAC3D,MAAa,QAIX,SAAQ,KAAK;IAKS;IACA;IAJX,KAAK,CAAU;IAExB,YACoB,MAAc,EACd,IAAa,EAC7B,OAAe,EACf,KAAc;QAEd,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QALV,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAAS;QAK7B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QAEnB,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;IAEM,MAAM,CAAC,YAAY,CACtB,MAAc,EACd,YAAqB;QAErB,MAAM,QAAQ,GAAG,YAAkD,CAAC;QACpE,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC;YACrC,OAAO,IAAI,QAAQ,CACf,MAAM,EACN,CAAC,EACD;gBACI,wDAAwD;gBACxD,QAAQ,EAAE,OAAO,IAAI,YAAY,QAAQ,CAAC,OAAO,EAAE;aACtD,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAC/B,CAAC;QACN,CAAC;QAED,OAAO,IAAI,QAAQ,CACf,MAAM,EACN,QAAQ,CAAC,IAAI,EACb,QAAQ,CAAC,OAAO,IAAI,cAAc,EAClC,QAAQ,CAAC,KAAK,CACjB,CAAC;IACN,CAAC;CACJ;AA5CD,4BA4CC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IEndpointInputContentType = void 0;
|
|
4
|
+
var IEndpointInputContentType;
|
|
5
|
+
(function (IEndpointInputContentType) {
|
|
6
|
+
IEndpointInputContentType.extender = (base) => {
|
|
7
|
+
const ext = {
|
|
8
|
+
contentType: undefined,
|
|
9
|
+
withContentType(contentType) {
|
|
10
|
+
this.contentType = contentType;
|
|
11
|
+
return this;
|
|
12
|
+
},
|
|
13
|
+
asJson() {
|
|
14
|
+
this.contentType = 'application/json';
|
|
15
|
+
return this;
|
|
16
|
+
},
|
|
17
|
+
asMultipartForm() {
|
|
18
|
+
this.contentType = 'multipart/form-data';
|
|
19
|
+
return this;
|
|
20
|
+
},
|
|
21
|
+
asUrlEncoded() {
|
|
22
|
+
this.contentType = 'application/x-www-form-urlencoded';
|
|
23
|
+
return this;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
return Object.assign(base, ext);
|
|
27
|
+
};
|
|
28
|
+
function guard(api) {
|
|
29
|
+
return 'contentType' in api;
|
|
30
|
+
}
|
|
31
|
+
IEndpointInputContentType.guard = guard;
|
|
32
|
+
function tryApplyContentType(api, headers) {
|
|
33
|
+
if (guard(api) && api.contentType) {
|
|
34
|
+
headers['Content-Type'] = api.contentType;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
IEndpointInputContentType.tryApplyContentType = tryApplyContentType;
|
|
38
|
+
})(IEndpointInputContentType || (exports.IEndpointInputContentType = IEndpointInputContentType = {}));
|
|
39
|
+
//# sourceMappingURL=contentType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contentType.js","sourceRoot":"","sources":["../../../../src/api/extensions/contentType.ts"],"names":[],"mappings":";;;AAuBA,IAAiB,yBAAyB,CAiCzC;AAjCD,WAAiB,yBAAyB;IACzB,kCAAQ,GAA4D,CAAwB,IAAO,EAAE,EAAE;QAChH,MAAM,GAAG,GAAG;YACR,WAAW,EAAE,SAAS;YACtB,eAAe,CAAgC,WAAmB;gBAC9D,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;gBAC/B,OAAO,IAAI,CAAC;YAChB,CAAC;YACD,MAAM;gBACF,IAAI,CAAC,WAAW,GAAG,kBAAkB,CAAC;gBACtC,OAAO,IAAI,CAAC;YAChB,CAAC;YACD,eAAe;gBACX,IAAI,CAAC,WAAW,GAAG,qBAAqB,CAAC;gBACzC,OAAO,IAAI,CAAC;YAChB,CAAC;YACD,YAAY;gBACR,IAAI,CAAC,WAAW,GAAG,mCAAmC,CAAC;gBACvD,OAAO,IAAI,CAAC;YAChB,CAAC;SACyB,CAAC;QAC/B,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACpC,CAAC,CAAC;IAEF,SAAgB,KAAK,CAAC,GAAkB;QACpC,OAAO,aAAa,IAAI,GAAG,CAAC;IAChC,CAAC;IAFe,+BAAK,QAEpB,CAAA;IAED,SAAgB,mBAAmB,CAAC,GAAkB,EAAE,OAA+B;QACnF,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC;YAChC,OAAO,CAAC,cAAc,CAAC,GAAG,GAAG,CAAC,WAAW,CAAC;QAC9C,CAAC;IACL,CAAC;IAJe,6CAAmB,sBAIlC,CAAA;AACL,CAAC,EAjCgB,yBAAyB,yCAAzB,yBAAyB,QAiCzC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./contentType.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./validation.js"), exports);
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/api/extensions/index.ts"],"names":[],"mappings":";;;AAAA,2DAAiC;AACjC,0DAAgC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IEndpointInputValidation = void 0;
|
|
4
|
+
var IEndpointInputValidation;
|
|
5
|
+
(function (IEndpointInputValidation) {
|
|
6
|
+
IEndpointInputValidation.extender = (base) => {
|
|
7
|
+
const ext = {
|
|
8
|
+
validate: undefined,
|
|
9
|
+
withValidation(validate) {
|
|
10
|
+
this.validate = validate;
|
|
11
|
+
return this;
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
return Object.assign(base, ext);
|
|
15
|
+
};
|
|
16
|
+
function guard(api) {
|
|
17
|
+
return 'validate' in api;
|
|
18
|
+
}
|
|
19
|
+
IEndpointInputValidation.guard = guard;
|
|
20
|
+
function tryValidate(api, input) {
|
|
21
|
+
if (guard(api) && api.validate) {
|
|
22
|
+
return api.validate(input);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
IEndpointInputValidation.tryValidate = tryValidate;
|
|
26
|
+
})(IEndpointInputValidation || (exports.IEndpointInputValidation = IEndpointInputValidation = {}));
|
|
27
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../../../src/api/extensions/validation.ts"],"names":[],"mappings":";;;AASA,IAAiB,wBAAwB,CAwBxC;AAxBD,WAAiB,wBAAwB;IAGxB,iCAAQ,GAA2D,CAAwB,IAAO,EAAE,EAAE;QAE/G,MAAM,GAAG,GAAG;YACR,QAAQ,EAAE,SAAS;YACnB,cAAc,CAA8B,QAA+C;gBACvF,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;gBACzB,OAAO,IAAoC,CAAC;YAChD,CAAC;SACwB,CAAC;QAC9B,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACpC,CAAC,CAAC;IAEF,SAAgB,KAAK,CAAC,GAAkB;QACpC,OAAO,UAAU,IAAI,GAAG,CAAC;IAC7B,CAAC;IAFe,8BAAK,QAEpB,CAAA;IAED,SAAgB,WAAW,CAAC,GAAkB,EAAE,KAA6C;QACzF,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC;YAC7B,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC;IACL,CAAC;IAJe,oCAAW,cAI1B,CAAA;AACL,CAAC,EAxBgB,wBAAwB,wCAAxB,wBAAwB,QAwBxC"}
|
package/cjs/api/helpers.js
CHANGED
|
@@ -14,10 +14,11 @@ function setDefaults(settings) {
|
|
|
14
14
|
}
|
|
15
15
|
const getPrefix = (prefix) => typeof prefix === 'string' ? prefix : (prefix ? exports.DefaultSettings.basePrefix : false);
|
|
16
16
|
function getPath(endpoint, pathArgs, prefix = true) {
|
|
17
|
-
|
|
17
|
+
const path = endpoint.path;
|
|
18
|
+
return path.build(pathArgs || undefined, { addStart: getPrefix(prefix) });
|
|
18
19
|
}
|
|
19
20
|
function getTemplate(endpoint, prefix = true) {
|
|
20
|
-
return endpoint.
|
|
21
|
+
return endpoint.path.template(exports.DefaultSettings.templateArgPrefix, { addStart: getPrefix(prefix) });
|
|
21
22
|
}
|
|
22
23
|
function getFormattedDisplayName(endpoint) {
|
|
23
24
|
const template = getTemplate(endpoint);
|
package/cjs/api/helpers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/api/helpers.ts"],"names":[],"mappings":";;;AAQA,kCAEC;AAKD,
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/api/helpers.ts"],"names":[],"mappings":";;;AAQA,kCAEC;AAKD,0BAOC;AAED,kCAEC;AAED,0DAMC;AA/BY,QAAA,eAAe,GAAG;IAC3B,iBAAiB,EAAE,GAAoC;IACvD,UAAU,EAAE,GAAG;CAClB,CAAC;AAEF,SAAgB,WAAW,CAAC,QAAyC;IACjE,MAAM,CAAC,MAAM,CAAC,uBAAe,EAAE,QAAQ,CAAC,CAAC;AAC7C,CAAC;AAGD,MAAM,SAAS,GAAG,CAAC,MAAqB,EAAE,EAAE,CAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,uBAAe,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAEjI,SAAgB,OAAO,CACnB,QAAW,EACX,QAAsC,EACtC,SAA2B,IAAI;IAE/B,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAqB,CAAC;IAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,SAAS,EAAE,EAAE,QAAQ,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAC9E,CAAC;AAED,SAAgB,WAAW,CAA0B,QAAW,EAAE,SAA2B,IAAI;IAC7F,OAAO,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,uBAAe,CAAC,iBAAiB,EAAE,EAAE,QAAQ,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AACtG,CAAC;AAED,SAAgB,uBAAuB,CAAC,QAAuB;IAC3D,MAAM,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;IACvC,MAAM,MAAM,GAAG,QAAQ,CAAC,WAAW;QAC/B,CAAC,CAAC,IAAI,QAAQ,CAAC,WAAW,IAAI;QAC9B,CAAC,CAAC,EAAE,CAAC;IACT,OAAO,MAAM,GAAG,QAAQ,CAAC;AAC7B,CAAC"}
|
package/cjs/api/logging.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LogTypes = void 0;
|
|
4
|
+
const methods_js_1 = require("./methods.js");
|
|
4
5
|
var LogTypes;
|
|
5
6
|
(function (LogTypes) {
|
|
7
|
+
/**
|
|
8
|
+
* Get the enabled state of a log type.
|
|
9
|
+
* @param type Log type to check.
|
|
10
|
+
* @param dir Direction of log, either request or response.
|
|
11
|
+
*/
|
|
6
12
|
function getIsEnabled(type, dir) {
|
|
7
13
|
if (type) {
|
|
8
14
|
if (type === true || type === 'full' || type === dir) {
|
|
@@ -24,5 +30,22 @@ var LogTypes;
|
|
|
24
30
|
return { enabled: false };
|
|
25
31
|
}
|
|
26
32
|
LogTypes.getIsEnabled = getIsEnabled;
|
|
33
|
+
function logCall(logger, cfg, dir, data) {
|
|
34
|
+
if (!cfg._api || (dir === 'req' && cfg._api.method === methods_js_1.EndpointMethods.GET && cfg.data == null)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const info = LogTypes.getIsEnabled(cfg._log, dir);
|
|
38
|
+
if (!info.enabled) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const dataLogged = info.formatter
|
|
42
|
+
? info.formatter(data ?? cfg.data)
|
|
43
|
+
: (data ?? cfg.data);
|
|
44
|
+
const prefix = dir === 'req'
|
|
45
|
+
? 'REQ ====>'
|
|
46
|
+
: 'RES <====';
|
|
47
|
+
logger.log(prefix, cfg._api.method, cfg.url, dataLogged);
|
|
48
|
+
}
|
|
49
|
+
LogTypes.logCall = logCall;
|
|
27
50
|
})(LogTypes || (exports.LogTypes = LogTypes = {}));
|
|
28
51
|
//# sourceMappingURL=logging.js.map
|
package/cjs/api/logging.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../../../src/api/logging.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../../../src/api/logging.ts"],"names":[],"mappings":";;;AAGA,6CAA+C;AAW/C,IAAiB,QAAQ,CAwExB;AAxED,WAAiB,QAAQ;IAIrB;;;;MAIE;IACF,SAAgB,YAAY,CAAc,IAAwB,EAAE,GAAQ;QACxE,IAAI,IAAI,EAAE,CAAC;YACP,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;gBACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAC7B,CAAC;YAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;gBACtB,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;oBACf,OAAO,EAAE,OAAO,EAAE,IAAI,E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
|
|
1
|
+
import { ApiEndpoint } from './endpoint.js';
|
|
2
|
+
import { assert } from '../functions/assert.js';
|
|
3
|
+
/** Converts an composite API endpoints object to the same tree of functions */
|
|
4
|
+
export function buildApi(api, caller) {
|
|
5
|
+
assert(!!api && typeof api === 'object', 'API definition must be an object');
|
|
6
|
+
assert(!!caller && typeof caller === 'function', 'Caller must be a function');
|
|
7
|
+
if (ApiEndpoint.isEndpoint(api)) {
|
|
8
|
+
return createEndpointCallable(api, caller);
|
|
9
|
+
}
|
|
10
|
+
return Object.entries(api).reduce((acc, [key, value]) => {
|
|
11
|
+
const next = value;
|
|
12
|
+
acc[key] = buildApi(next, caller);
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
}
|
|
16
|
+
/** Partial application: binding an endpoint to callApi, so only input data and extra are passed to a newly created function. */
|
|
17
|
+
export function createEndpointCallable(endpoint, caller) {
|
|
18
|
+
const name = `${endpoint.displayName || '?'}_${endpoint.method}_${endpoint.path.template()}`;
|
|
19
|
+
const fn = {
|
|
20
|
+
[name]: function (data, extra) { return caller(endpoint, data, extra); },
|
|
21
|
+
}[name]; // this sets the name of the function
|
|
22
|
+
const result = fn;
|
|
23
|
+
Object.assign(result, { Endpoint: endpoint });
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../src/api/builder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAgBhD,+EAA+E;AAC/E,MAAM,UAAU,QAAQ,CACpB,GAAS,EACT,MAAgC;IAEhC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,kCAAkC,CAAC,CAAC;IAC7E,MAAM,CAAC,CAAC,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,2BAA2B,CAAC,CAAC;IAE9E,IAAI,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC9B,OAAO,sBAAsB,CAAC,GAAG,EAAE,MAA0B,CAAkC,CAAC;IACpG,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;QACpD,MAAM,IAAI,GAAG,KAAsB,CAAC;QACpC,GAAG,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAClC,OAAO,GAAG,CAAC;IACf,CAAC,EAAE,EAA4C,CAA4B,CAAC;AAChF,CAAC;AAED,gIAAgI;AAChI,MAAM,UAAU,sBAAsB,CAIpC,QAAmB,EAAE,MAAe;IAClC,MAAM,IAAI,GAAG,GAAG,QAAQ,CAAC,WAAW,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;IAE7F,MAAM,EAAE,GAAG;QACP,CAAC,IAAI,CAAC,EAAE,UAAU,IAAiC,EAAE,KAA8B,IAAI,OAAO,MAAM,CAAY,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5I,CAAC,IAAI,CAAC,CAAC,CAAC,qCAAqC;IAE9C,MAAM,MAAM,GAAG,EAAwC,CAAC;IACxD,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC9C,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
package/esm/api/call.js
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { PostProcessors, PreProcessors } from './register.js';
|
|
2
2
|
import { getPath } from './helpers.js';
|
|
3
3
|
export function buildApiCaller(options) {
|
|
4
|
-
const {
|
|
4
|
+
const { request, hooks = {} } = options;
|
|
5
5
|
return async function callApi(api, data, extra) {
|
|
6
6
|
const { headers, log = 'res', noLoader, ...restExtra } = extra || {};
|
|
7
7
|
const resultInput = data && { ...data };
|
|
8
8
|
const pathInputs = {};
|
|
9
|
+
const queryInputs = {};
|
|
9
10
|
let queryStr = '';
|
|
10
|
-
|
|
11
|
+
// extract path inputs from data
|
|
12
|
+
const pathKeys = api.path.args;
|
|
11
13
|
if (resultInput && pathKeys?.length) {
|
|
12
14
|
for (const key of pathKeys) {
|
|
13
15
|
pathInputs[key] = resultInput[key];
|
|
14
16
|
delete resultInput[key];
|
|
15
17
|
}
|
|
16
18
|
}
|
|
19
|
+
// extract query inputs from data
|
|
17
20
|
const queryKeysExpected = api.queryKeys;
|
|
18
21
|
if (resultInput && queryKeysExpected?.length) {
|
|
19
|
-
const queryInputs = {};
|
|
20
22
|
let empty = true;
|
|
21
23
|
for (const key of queryKeysExpected) {
|
|
22
24
|
const v = resultInput[key];
|
|
@@ -26,13 +28,15 @@ export function buildApiCaller(options) {
|
|
|
26
28
|
}
|
|
27
29
|
delete resultInput[key];
|
|
28
30
|
}
|
|
31
|
+
// compile query string
|
|
29
32
|
if (!empty) {
|
|
30
33
|
const params = new URLSearchParams(queryInputs);
|
|
31
34
|
queryStr = '?' + params.toString();
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
|
-
if (
|
|
35
|
-
|
|
37
|
+
if (hooks.beforeConfig) {
|
|
38
|
+
// copy all inputs to avoid mutation
|
|
39
|
+
await hooks.beforeConfig(api, { ...resultInput }, { ...pathInputs }, { ...queryInputs });
|
|
36
40
|
}
|
|
37
41
|
const sendingData = resultInput && Object.keys(resultInput).length > 0
|
|
38
42
|
? resultInput
|
|
@@ -48,8 +52,11 @@ export function buildApiCaller(options) {
|
|
|
48
52
|
_log: log,
|
|
49
53
|
_extra: restExtra,
|
|
50
54
|
};
|
|
51
|
-
if (
|
|
52
|
-
|
|
55
|
+
if (hooks.beforeRequest) {
|
|
56
|
+
const result = await hooks.beforeRequest(config);
|
|
57
|
+
if (result && typeof result === 'object') {
|
|
58
|
+
Object.assign(config, result);
|
|
59
|
+
}
|
|
53
60
|
}
|
|
54
61
|
const response = await request(config);
|
|
55
62
|
return PostProcessors.process(api, response?.data);
|
package/esm/api/call.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"call.js","sourceRoot":"","sources":["../../../src/api/call.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAK9D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"call.js","sourceRoot":"","sources":["../../../src/api/call.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAK9D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AA+DvC,MAAM,UAAU,cAAc,CAA8C,OAA8B;IAEtG,MAAM,EAAE,OAAO,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IAExC,OAAO,KAAK,UAAU,OAAO,CACzB,GAAM,EACN,IAAyB,EACzB,KAAgC;QAMhC,MAAM,EACF,OAAO,EACP,GAAG,GAAG,KAAK,EACX,QAAQ,EACR,GAAG,SAAS,EACf,GAAG,KAAK,IAAI,EAAE,CAAC;QAEhB,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,GAAG,IAAI,EAAS,CAAC;QAC/C,MAAM,UAAU,GAAoC,EAAE,CAAC;QACvD,MAAM,WAAW,GAAc,EAAE,CAAC;QAClC,IAAI,QAAQ,GAAW,EAAE,CAAC;QAE1B,gCAAgC;QAChC,MAAM,QAAQ,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;QAC/B,IAAI,WAAW,IAAI,QAAQ,EAAE,MAAM,EAAE,CAAC;YAClC,KAAK,MAA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