Giter VIP home page Giter VIP logo

google-script-dts-generator's Introduction

google-script-dts-generator NPM version

Generate TypeScript declaration (.d.ts) file for google.script.run(Client-side API).

About

google-script-dts-generator generates a client-side TypeScript declaration (.d.ts) for the Apps Script function from a server-side TypeScript source that matchs the following conditions:

  • function assignment expressions to global object.
  • named export functions (when using namedExportsFiles option)

Example

See example.

server.ts:

global.echo = (message: string) => {
  return message;
};

generate:

$ google-script-dts-generator --sourcesDir server/ --outputDir client/

generated google-script.d.ts:

declare namespace google {
    /**
     * Methods available to Google Apps Script
     */
    namespace script {
        interface IRun {
            echo(message: string): void;
            /**
             * Sets a callback function to run if the server-side function throws an exception. Without a failure handler, failures are logged to the JavaScript console. To override this, call withFailureHandler(null) or supply a failure handler that does nothing.
             * @param callback a client-side callback function to run if the server-side function throws an exception; the Error object is passed to the function as the first argument, and the user object (if any) is passed as a second argument
             */
            withFailureHandler(callback: (error: Error, object?: any)=>void): IRun;
            /**
             * Sets a callback function to run if the server-side function returns successfully.
             * @param callback a client-side callback function to run if the server-side function returns successfully; the server's return value is passed to the function as the first argument, and the user object (if any) is passed as a second argument
             */
            withSuccessHandler(callback: (value: any, object?: any)=>void): IRun;
            /**
             * Sets an object to pass as a second parameter to the success and failure handlers.
             * @param {Object} object an object to pass as a second parameter to the success and failure handlers; because user objects are not sent to the server, they are not subject to the restrictions on parameters and return values for server calls. User objects cannot, however, be objects constructed with the new operator
             */
            withUserObject(object: object): IRun;
        }
        
        const run: IRun;

        // omitted below
}

Installation

$ npm install gas-webpack-plugin --save-dev

or

$ yarn add google-script-dts-generator

Usage

CLI

$ google-script-dts-generator --sourcesDir server --outputDir client

Options

--sourcesDir <sources>

Path to TypeScript sources directory.

$ google-script-dts-generator --outputDir ./example/client --sourcesDir ./example/server

--outputDir <outputDir>

Path to a generated d.ts file output directory.

$ google-script-dts-generator --outputDir ./example/client --sourcesDir ./example/server

--namedExportsFiles <glob>

A glob path pattern to generates a client-side TypeScript declaration (.d.ts) from named exports.

$ google-script-dts-generator --outputDir ./example/client --sourcesDir ./example/server --namedExportsFiles './example/server/**/*.ts'

--endpointsOnly

When use @types/google.script.client-side, User needs define specify functions as PublicEndpoints. google-script-dts-generator support generate only PublicEndpoint interfaces.

yarn google-script-dts-generator -s ./src/server/ -o ./src/client/ --namedExportsFiles "./src/server/**/*.ts" --endpointsOnly

Output:

declare namespace google {
  namespace script {
    interface PublicEndpoints {
        doGet(): void;
        myFunction(param1: string, param2: boolean): void;
    }
  }
}

--nonVoidReturnType

generate return type of server-side function as return type of client-side function. This option assumed that using in combination with gas-client.

Note: Use [email protected] or later

yarn google-script-dts-generator -s ./src/server/ -o ./src/client/ --namedExportsFiles "./src/server/**/*.ts" --endpointsOnly --nonVoidReturnType

Output:

declare namespace google {
    /**
     * Methods available to Google Apps Script
     */
    namespace script {
        interface PublicEndpoints {
            helloWorld(): string;
            echo(message: string): string;
        }
    }
}

client.ts

import { GASClient } from 'gas-client';

const { serverFunctions } = new GASClient<google.script.PublicEndpoints>();
serverFunctions.echo("hello gas")
    .then((s) => console.log(s))
    .catch(e => console.error(e));

google-script-dts-generator's People

Contributors

fossamagna avatar renovate-bot avatar renovate[bot] avatar jpoehnelt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.