UNPKG

3.7 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __assign = (this && this.__assign) || function () {
16 __assign = Object.assign || function(t) {
17 for (var s, i = 1, n = arguments.length; i < n; i++) {
18 s = arguments[i];
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20 t[p] = s[p];
21 }
22 return t;
23 };
24 return __assign.apply(this, arguments);
25};
26var __importStar = (this && this.__importStar) || function (mod) {
27 if (mod && mod.__esModule) return mod;
28 var result = {};
29 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
30 result["default"] = mod;
31 return result;
32};
33Object.defineProperty(exports, "__esModule", { value: true });
34var React = __importStar(require("react"));
35var bearer_provider_1 = require("./bearer-provider");
36exports.withInvoke = function (integrationId, functionName) {
37 return function (WrappedComponent) { var _a; return _a = /** @class */ (function (_super) {
38 __extends(FetcherComponent, _super);
39 function FetcherComponent(props) {
40 var _this = _super.call(this, props) || this;
41 _this.invoke = function (params) {
42 if (params === void 0) { params = {}; }
43 _this.setState(__assign({}, _this.state, { error: null, loading: true }));
44 return _this.context.bearer
45 .invoke(integrationId, functionName, params)
46 .then(function (_a) {
47 var data = _a.data;
48 _this.setState(__assign({}, _this.state, { data: data }));
49 if (_this.props.onSuccess) {
50 _this.props.onSuccess(data);
51 }
52 })
53 .catch(function (_a) {
54 var error = _a.error;
55 _this.setState(__assign({}, _this.state, { error: error }));
56 if (_this.props.onFail) {
57 _this.props.onFail(error);
58 }
59 })
60 .then(function () {
61 _this.setState(__assign({}, _this.state, { loading: false }));
62 });
63 };
64 _this.state = {
65 loading: false
66 };
67 return _this;
68 }
69 FetcherComponent.prototype.render = function () {
70 return (React.createElement(WrappedComponent, __assign({}, this.props, { error: this.state.error, data: this.state.data, invoke: this.invoke, loading: this.state.loading })));
71 };
72 return FetcherComponent;
73 }(React.Component)),
74 _a.displayName = "withBearerInvoke(" + functionName + ")(" + getDisplayName(WrappedComponent) + ")",
75 _a.contextType = bearer_provider_1.BearerContext,
76 _a; };
77};
78// TODO: add typing
79function getDisplayName(WrappedComponent) {
80 return WrappedComponent.displayName || WrappedComponent.name || 'Component';
81}