@romstar/gas-client-typescript
Version:
A client-side utility class that can call server-side Google Apps Script functions
48 lines (47 loc) • 2.13 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerProxy = void 0;
var check_allow_list_1 = require("../utils/check-allow-list");
var proxy_handler_1 = require("../utils/proxy-handler");
var function_host_1 = require("../classes/function-host");
var ServerProxy = /** @class */ (function (_super) {
__extends(ServerProxy, _super);
function ServerProxy(_config) {
var _this = _super.call(this) || this;
_this._config = _config;
window.gasStore = {};
window.addEventListener('message', _this.buildMessageListener(), false);
_this._serverFunctions = new Proxy({}, { get: proxy_handler_1.proxyHandler });
return _this;
}
ServerProxy.prototype.buildMessageListener = function () {
var _this = this;
return function (event) {
var _a;
var allowOrigin = check_allow_list_1.checkAllowList(event.origin, (_a = _this._config) === null || _a === void 0 ? void 0 : _a.allowedDevelopmentDomains);
if (!allowOrigin || event.data.type !== 'RESPONSE')
return;
var _b = event.data, response = _b.response, status = _b.status, id = _b.id;
var _c = window.gasStore[id], resolve = _c.resolve, reject = _c.reject;
if (status === 'ERROR') {
reject(response);
}
resolve(response);
};
};
return ServerProxy;
}(function_host_1.FunctionHost));
exports.ServerProxy = ServerProxy;