Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@typedef doGet, doPost event parameter #31

Open
oshliaer opened this issue Apr 4, 2019 · 2 comments
Open

@typedef doGet, doPost event parameter #31

oshliaer opened this issue Apr 4, 2019 · 2 comments
Assignees
Labels

Comments

@oshliaer
Copy link
Owner

oshliaer commented Apr 4, 2019

/**
 * @typedef {Object} HTTPEvent
 * {@link https://developers.google.com/apps-script/guides/web Web Apps}
 * @property {string} queryString The value of the query string portion of
 *   the URL, or null if no query string is specified.
 * @property {object} parameter An object of key/value pairs that correspond to
 *   the request parameters.  Only the first value is returned for parameters
 *   that have multiple values.
 * @property {object} parameters An object similar to e.parameter, but with
 *   an array of values for each key.
 * @property {string} [contextPath=''] Not used, always the empty string.
 * @property {number} contentLength The length of the request body for
 *   POST requests, or -1 for GET requests.
 * @property {HTTPEventPostData} postData postData
 */

/**
 * @typedef {Object} HTTPEventPostData
 * @property {number} length The same as e.contentLength
 * @property {string} type The MIME type of the POST body
 * @property {string} contents The content text of the POST body
 * @property {string} postData Always the value "postData"
 */

/* exported doGet, doPost */
function doGet(e) {}

/**
 *
 * @param {HTTPEvent} e
 */
function doPost(e) {
  console.log(e.postData.contents);
  console.log(JSON.parse(e.postData.contents));
}

function example() {
  // Make a POST request with a JSON payload.
  var data = {
    name: 'Bob Smith',
    age: 35,
    pets: ['fido', 'fluffy']
  };
  var options = {
    method: 'post',
    contentType: 'application/json',
    // Convert the JavaScript object to a JSON string.
    payload: JSON.stringify(data)
  };
  UrlFetchApp.fetch('https://script.google.com/macros/s/xxx/exec', options);
}
@oshliaer oshliaer self-assigned this Apr 4, 2019
@oshliaer
Copy link
Owner Author

oshliaer commented Apr 5, 2019

// Type definitions for Google Apps Script 2019-04-02
// Project: https://developers.google.com/apps-script/
// Definitions by: grant <https://github.com/grant/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/// <reference path="google-apps-script.script.d.ts" />
/// <reference path="google-apps-script.spreadsheet.d.ts" />
/// <reference path="google-apps-script.slides.d.ts" />

declare namespace GoogleAppsScript {
  /**
   * Google Apps Script Events
   * @see https://developers.google.com/apps-script/guides/triggers/events
   */
  export module Events {
    // Internal interfaces
    interface AppsScriptEvent {
      authMode: Script.AuthMode,
      triggerUid: string,
      user: Base.User,
    }
      
    interface HttpRequestEvent {
      parameter: object,
      contextPath: string,
      contentLength: number
      queryString: string,
      parameters: object,
    }

    /**
     * TODO: What about other properties?
     * copyBlob, getAllBlobs, getAs, getBlob, getBytes, getContentType,
     * getDataAsString, getDocId, getImageUrl, getName, getPrimitiveByteArray,
     * isGoogleType, requireContentType, requireData, requireName, setBytes,
     * setContentType, setContentTypeFromExtension, setDataFromString,
     * setDataFromString, setName, toString
     */
    interface HttpRequestEventPostData {
      length: number,
      type: string,
      contents: string,
      name: string // Always the value "postData"
    }

    // External interfaces
    export interface SheetsOnOpen extends AppsScriptEvent {
      source: Spreadsheet.Spreadsheet,
    }

    enum SheetsOnChangeChangeType { EDIT, INSERT_ROW, INSERT_COLUMN, REMOVE_ROW, REMOVE_COLUMN, INSERT_GRID, REMOVE_GRID, FORMAT, OTHER }
    export interface SheetsOnChange extends AppsScriptEvent {
      changeType: SheetsOnChangeChangeType,
    }

    export interface SheetsOnEdit extends AppsScriptEvent {
      oldValue: string,
      range: Spreadsheet.Range,
      source: Spreadsheet.Spreadsheet,
      value: string,
    }

    export interface FormsOnSubmit extends AppsScriptEvent {
      namedValues: { [key: string]: string[]; },
      range: Spreadsheet.Range,
      values: string[],
    }

    export interface DocsOnOpen extends AppsScriptEvent {
      source: Document.Document,
    }

    export interface SlidesOnOpen extends AppsScriptEvent {
      source: Slides.Presentation,
    }

    export interface FormsOnOpen extends AppsScriptEvent {
      source: Forms.Form,
    }

    // TODO: Is there a `user` attribute?
    export interface CalendarEventUpdated extends AppsScriptEvent {
      calendarId: string,
    }

    export interface AddonOnInstall {
      authMode: Script.AuthMode,
    }
    
    export interface DoGet extends HttpRequestEvent {
    }

    export interface DoPost extends HttpRequestEvent {
      postData: HttpRequestEventPostData
    }
  }
}

@oshliaer
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant