@forge/bridge
5.18.0-next.1-experimental-44b7a125.18.0-next.2
out/invoke/invoke.js~
out/invoke/invoke.jsModified+32−4
Index: package/out/invoke/invoke.js
===================================================================
--- package/out/invoke/invoke.js
+++ package/out/invoke/invoke.js
@@ -11,21 +11,49 @@
if (Object.values(payload).some((val) => typeof val === 'function')) {
throw new errors_1.BridgeAPIError('Passing functions as part of the payload is not supported!');
}
};
-const _invoke = (functionKey, payload) => {
+/**
+ * While the rate limit metadata feature is being rolled out, the bridge may return either
+ * the resolver body directly or an object of shape `{ body, metadata }`, where the
+ * rate limit fields are reported under `metadata`.
+ */
+const isMetadataWrappedResponse = (response) => {
+ if (typeof response !== 'object' || response === null) {
+ return false;
+ }
+ const keys = Object.keys(response);
+ return keys.length === 2 && keys.includes('body') && keys.includes('metadata');
+};
+const _invoke = async (functionKey, payload, metadata) => {
+ var _a;
if (typeof functionKey !== 'string') {
throw new errors_1.BridgeAPIError('functionKey must be a string!');
}
validatePayload(payload);
- return callBridge('invoke', { functionKey, payload });
+ const response = await callBridge('invoke', { functionKey, payload });
+ // If metadata is requested, return the response wrapped in metadata.
+ if (metadata) {
+ // If the bridge response is not wrapped in metadata, return the metadata field as undefined.
+ if (!isMetadataWrappedResponse(response)) {
+ return { body: response, metadata: undefined };
+ }
+ const requestedMetadataKeys = Object.keys(metadata !== null && metadata !== void 0 ? metadata : {}).filter((key) => (metadata === null || metadata === void 0 ? void 0 : metadata[key]) === true);
+ const responseMetadata = {};
+ for (const key of requestedMetadataKeys) {
+ responseMetadata[key] = (_a = response.metadata) === null || _a === void 0 ? void 0 : _a[key];
+ }
+ return { body: response.body, metadata: responseMetadata };
+ }
+ // If metadata is not requested, return the response directly.
+ return (isMetadataWrappedResponse(response) ? response.body : response);
};
const limitedInvoke = (0, utils_1.withRateLimiter)(_invoke, 500, 1000 * 25, 'Resolver calls are rate limited at 500req/25s');
/**
* Calls a backend resolver function by key.
*/
-function invoke(functionKey, payload) {
- return limitedInvoke(functionKey, payload);
+function invoke(functionKey, payload, metadata) {
+ return limitedInvoke(functionKey, payload, metadata);
}
exports.invoke = invoke;
/**
* Specialises the invoke function to a given Definitions type.