@guilhermetod/gas-client
Advanced tools
Comparing version 0.2.0-alpha.0 to 0.2.0-alpha.1
@@ -1,6 +0,6 @@ | ||
import { ServerConfig } from './types/config'; | ||
import { ServerFunctions, ServerFunctionsMap } from './types/functions'; | ||
declare class GasClient<F extends ServerFunctionsMap = {}> { | ||
import { ServerConfig } from '@src/types/config'; | ||
import { ServerFunctions, FunctionMap } from '@src/types/functions'; | ||
declare class GASClient<FM extends FunctionMap = {}> { | ||
private _config?; | ||
private _serverFunctions; | ||
private _functionHost; | ||
/** | ||
@@ -12,7 +12,5 @@ * Accepts a single `config` object | ||
constructor(_config?: ServerConfig | undefined); | ||
get serverFunctions(): ServerFunctions<F>; | ||
private promisifyGasFunctions; | ||
private setupProxy; | ||
private buildMessageListener; | ||
get serverFunctions(): ServerFunctions<FM>; | ||
} | ||
export { GasClient, ServerFunctions }; | ||
export { GASClient, ServerFunctions }; | ||
export { DevServerRequestEvent, GASDevServerIFrame } from '@src/types/dev-server'; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -17,9 +6,7 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GasClient = void 0; | ||
var check_allow_list_1 = __importDefault(require("./utils/check-allow-list")); | ||
var ignored_function_names_1 = __importDefault(require("./utils/ignored-function-names")); | ||
var should_setup_proxy_1 = __importDefault(require("./utils/should-setup-proxy")); | ||
var proxy_handler_1 = __importDefault(require("./utils/proxy-handler")); | ||
var promisify_1 = __importDefault(require("./utils/promisify")); | ||
var GasClient = /** @class */ (function () { | ||
exports.GASClient = void 0; | ||
var is_gas_environment_1 = __importDefault(require("@src/utils/is-gas-environment")); | ||
var gas_promises_1 = require("@src/classes/gas-promises"); | ||
var server_proxy_1 = require("@src/classes/server-proxy"); | ||
var GASClient = /** @class */ (function () { | ||
/** | ||
@@ -30,17 +17,14 @@ * Accepts a single `config` object | ||
*/ | ||
function GasClient(_config) { | ||
function GASClient(_config) { | ||
this._config = _config; | ||
this._serverFunctions = {}; | ||
try { | ||
this.promisifyGasFunctions(); | ||
if (is_gas_environment_1.default()) { | ||
this._functionHost = new gas_promises_1.GASPromises(); | ||
} | ||
catch (err) { | ||
if (should_setup_proxy_1.default(err)) { | ||
this.setupProxy(); | ||
} | ||
else { | ||
this._functionHost = new server_proxy_1.ServerProxy(this._config); | ||
} | ||
} | ||
Object.defineProperty(GasClient.prototype, "serverFunctions", { | ||
Object.defineProperty(GASClient.prototype, "serverFunctions", { | ||
get: function () { | ||
return this._serverFunctions; | ||
return this._functionHost.serverFunctions; | ||
}, | ||
@@ -50,38 +34,4 @@ enumerable: false, | ||
}); | ||
GasClient.prototype.promisifyGasFunctions = function () { | ||
// get the names of all of our publicly accessible server functions | ||
this._serverFunctions = Object.keys(google.script.run).reduce(function (acc, functionName) { | ||
var _a; | ||
// filter out the reserved names -- we don't want those | ||
return ignored_function_names_1.default.includes(functionName) | ||
? acc | ||
: __assign(__assign({}, acc), (_a = {}, _a[functionName] = promisify_1.default(functionName), _a)); | ||
}, {}); | ||
}; | ||
GasClient.prototype.setupProxy = function () { | ||
window.gasStore = {}; | ||
window.addEventListener('message', this.buildMessageListener(), false); | ||
this._serverFunctions = new Proxy({}, { get: proxy_handler_1.default }); | ||
}; | ||
GasClient.prototype.buildMessageListener = function () { | ||
var _this = this; | ||
return function (event) { | ||
var _a; | ||
// check the allow list for the receiving origin | ||
var allowOrigin = check_allow_list_1.default(event.origin, (_a = _this._config) === null || _a === void 0 ? void 0 : _a.allowedDevelopmentDomains); | ||
// we only care about the type: 'RESPONSE' messages here | ||
if (!allowOrigin || event.data.type !== 'RESPONSE') | ||
return; | ||
var _b = event.data, response = _b.response, status = _b.status, id = _b.id; | ||
// look up the saved resolve and reject functions in our global store based | ||
// on the response id, and call the function depending on the response status | ||
var _c = window.gasStore[id], resolve = _c.resolve, reject = _c.reject; | ||
if (status === 'ERROR') { | ||
reject(response); | ||
} | ||
resolve(response); | ||
}; | ||
}; | ||
return GasClient; | ||
return GASClient; | ||
}()); | ||
exports.GasClient = GasClient; | ||
exports.GASClient = GASClient; |
@@ -1,4 +0,4 @@ | ||
export declare type ServerFunctionsMap = Record<string, (...args: any[]) => any>; | ||
export declare type FunctionMap = Record<string, (...args: any[]) => any>; | ||
declare type Promisified<F extends (...args: any[]) => any> = (...params: Parameters<F>) => Promise<ReturnType<F>>; | ||
declare type RecognizedServerFunctions<R extends ServerFunctionsMap> = { | ||
declare type RecognizedServerFunctions<R extends FunctionMap> = { | ||
[Name in keyof R]: Promisified<R[Name]>; | ||
@@ -9,3 +9,3 @@ }; | ||
}; | ||
export declare type ServerFunctions<R extends ServerFunctionsMap> = RecognizedServerFunctions<R> & UnrecognizedServerFunctions; | ||
export declare type ServerFunctions<FM extends FunctionMap> = RecognizedServerFunctions<FM> & UnrecognizedServerFunctions; | ||
export {}; |
@@ -10,5 +10,3 @@ "use strict"; | ||
if (typeof allowedDevelopmentDomains === 'string') { | ||
return allowedDevelopmentDomains | ||
.split(' ') | ||
.some(function (permittedOrigin) { return permittedOrigin === eventOrigin; }); | ||
return allowedDevelopmentDomains.split(' ').some(function (permittedOrigin) { return permittedOrigin === eventOrigin; }); | ||
} | ||
@@ -15,0 +13,0 @@ if (typeof allowedDevelopmentDomains === 'function') { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// skip the reserved names: https://developers.google.com/apps-script/guides/html/reference/run | ||
exports.default = [ | ||
'withFailureHandler', | ||
'withLogger', | ||
'withSuccessHandler', | ||
'withUserObject', | ||
]; | ||
exports.default = ['withFailureHandler', 'withLogger', 'withSuccessHandler', 'withUserObject']; |
{ | ||
"name": "@guilhermetod/gas-client", | ||
"version": "0.2.0-alpha.0", | ||
"version": "0.2.0-alpha.1", | ||
"description": "A client-side utility class that can call server-side Google Apps Script functions", | ||
@@ -11,2 +11,5 @@ "main": "build/index.js", | ||
"author": "Elisha Nuchi", | ||
"contributors": [ | ||
"Guilherme Tod <guilhermetod@gmail.com> (https://github.com/guilhermetod)" | ||
], | ||
"license": "MIT", | ||
@@ -13,0 +16,0 @@ "scripts": { |
@@ -20,4 +20,4 @@ # gas-client | ||
```javascript | ||
import { GasClient } from 'gas-client'; | ||
const { serverFunctions } = new GasClient(); | ||
import { GASClient } from 'gas-client'; | ||
const { serverFunctions } = new GASClient(); | ||
@@ -36,5 +36,5 @@ // We now have access to all our server functions, which return promises | ||
```javascript | ||
import { GasClient } from 'gas-client'; | ||
import { GASClient } from 'gas-client'; | ||
const { serverFunctions } = new GasClient({ | ||
const { serverFunctions } = new GASClient({ | ||
allowedDevelopmentDomains: 'https://localhost:3000', | ||
@@ -67,4 +67,4 @@ }); | ||
// With this package we can now do this: | ||
import { GasClient } from 'gas-client'; | ||
const { serverFunctions } = new GasClient(); | ||
import { GASClient } from 'gas-client'; | ||
const { serverFunctions } = new GASClient(); | ||
@@ -129,7 +129,7 @@ // We now have access to all our server functions, which return promises | ||
import { GasClient } from 'gas-client'; | ||
import { GASClient } from 'gas-client'; | ||
import { showUserPrompt } from './show-user-prompt'; | ||
import * as server from '../server'; | ||
const { serverFunctions } = new GasClient<typeof server>(); | ||
const { serverFunctions } = new GASClient<typeof server>(); | ||
@@ -160,3 +160,3 @@ const promptUser = async (): Promise<void> => { | ||
In the normal Google Apps Script production environment, `new GasClient()` will have one available method: | ||
In the normal Google Apps Script production environment, `new GASClient()` will have one available method: | ||
@@ -171,4 +171,4 @@ - `serverFunctions`: an object containing all publicly exposed server functions (see example above). | ||
Calling `new GasClient({ allowedDevelopmentDomains })` will create an instance with the following method in development mode: | ||
Calling `new GASClient({ allowedDevelopmentDomains })` will create an instance with the following method in development mode: | ||
- `serverFunctions`: a proxy object, used for development purposes, that mimics calling `google.script.run`. It will dispatch a message to the parent iframe (our custom Dev Server), which will call an app that actually interacts with the `google.script.run` API. Development mode will also handle the response and resolve or reject based on the response type. See the implementation for details on the event signature. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22563
27
309
1