0.2.1 • Published 1 month ago

@gasstack/http v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

@gasstack/http

The package is http api microframework, inspired by Express.js, meant to allow the implementation of REST APIs with Google Apps Script.

Description

The package allows create an http API.

Usage

The library expose an appBuilder function which allows you to create and configure an http response handler for the special function events doGet and doPost of the Google Apps Script environment.

You can create and configure an app function and use it to handle both special events.

const app = appBuilder((builder) => {
  builder
    .use((req, res) => {
      console.log(req.contextPath);
    })
    .get("/api/:name", (req, res) => {
      respond(res).text(`Hello ${req.pathParams!.name}`);
    })
    .all("/view/:name", (req, res) => {
      respond(res).view("index.e2e", { name: req.pathParams!.name });
    });
});

function doGet(e: GoogleAppsScript.Events.DoGet) {
  return app(e);
}

function doPost(e: GoogleAppsScript.Events.DoPost) {
  return app(e);
}

// Or you can install it directly
installHttpApp(app);

It is possible to insert special segments in the mapping url pattern in the form of :name which will be matched against the real request setting the keys of the params member of the request object.

To build the response an helper is available wich allows and simplifies the production of text, downloadable content, json and templated html views.

type HttpResponseHelper = {
  ok(): void;
  view(path: string, params: ViewParams): void;
  json(content: any): void;
  text(content: string): void;
  download(content: string, filename: string): void;
  error(error: string): void;
};

Example

Have a look to the e2e test.

API

API Reference

0.2.1

1 month ago

0.2.0

3 months ago

0.1.0

4 months ago