npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-gasrun

v1.0.0

Published

Minimalist wrapper for google.scripts.run with native TypeScript support

Downloads

4

Readme

ts-gasrun

Minimalist wrapper for google.script.run with native TypeScript support.

This package promisifies google.script.run, a client-side API of Google Apps Script (GAS) to call server-side GAS functions from HTML-service pages. Wrapped GAS functions are typed in TypeScript with the promisified types of the corresponding GAS functions. This enables type checks and enhances IDE assists.

Installation

Using npm:

npm install ts-gasrun

Using yarn:

yarn add ts-gasrun

Using pnpm:

pnpm add ts-gasrun

How to use

Preparation for use in Typescript

In the server-side code, export functions to be called from the client-side.

// gas-functions.ts: GAS server-side code

export function hello(str: string) {
  return `Hello ${str}!`;
}

export function concat(prop: { a: string; b: unknown }) {
  return { result: prop.a + prop.b };
}

export function throwError() {
  throw new Error("Error in GAS!");
}

Front-end Typescript code

Wrap exported GAS functions in the client-side.

// Frontend code bundled into GAS HTML-service pages
import { wrapGASFunctions } from "ts-gasrun";

// Wrap the all functions exported from a module.
import * as GASFuncs from "./gas-functions";
const gasrun = wrapGASFunctions(GASFuncs);

// Wrap specified functions of a module.
import { hello } from "./gas-functions";
const gasrun = wrapGASFunctions({ hello });

Then, use the wrapped functions with proper types.

// Case of using then & catch
gasrun
  .hello("world") // hello: (str: string) => Promise<string>
  .then((result) => {
    console.log(result);
  });
gasrun.throwError().catch((error) => {
  console.error(error);
});

// Case of using async & await
(async () => {
  const { result } =
    // concat: (prop: { a: string, b: unknown }) => Promise<{ result: string }>
    await gasrun.concat({ a: "string", b: 123 });
  console.log(result);
  try {
    await gasrun.throwError();
  } catch (error) {
    console.error(error);
  }
})();

Client-side in Javascript

You can use this package as an ES module in JavaScript also, like below in a GAS HTML-service page.

<script type="module">
  import { wrapGASFunctions } from "https://unpkg.com/ts-gasrun";

  // Wrap all functions available within google.script.run
  const gasrun = wrapGASFunctions();
  // Or you can specify wrapping functions:
  const gasrun = wrapGASFunctions({ hello: null, concat: null });

  (async () => {
    const { result } = await gasrun.concat({
      a: await gasrun.hello("world"),
      b: 123,
    });
    document.body.textContent = result;
  })();
</script>

Mocking

You can mock GAS functions for local development. Use mockGASFunctions with mock implementations of GAS functions, instead of wrapGASFunctions.

import { wrapGASFunctions, mockGASFunctions } from "ts-gasrun";
import * as GASFuncs from "./gas-functions";

const gasrun =
  process.env.NODE_ENV === "production"
    ? wrapGASFunctions(GASFuncs)
    : mockGASFunctions({
        hello: (str: string) => `Hello, ${str}!`,
        concat: ({ a, b }) => ({ result: `${a}${b}` }),
        throwError: () => {
          throw new Error("Error!");
        },
      });

Acknowledgements

This package is inspired by gas-client.