1.0.5 • Published 5 years ago

gforms-submit v1.0.5

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

GForms Submit

Submit form data to Google Forms via JavaScript/TypeScript.

This project can be used to build simple serverless contact forms and mailing lists with very little overhead.

Installation

npm i gforms-submit

Usage

Create a new Google Form, click Preview, inspect the DOM, and retrieve the <form> action URL and form field name= attribute values from your Google Form.

import gformsSubmit from "gforms-submit";

// Replace this with your form's action URL
const gformActionUrl =
  "https://docs.google.com/forms/d/e/abcdefgh12345678-abcdefgh/formResponse";

// Replace the name attribute with your form field's
const emailFormData = {
  nameAttribute: "emailAddress",
  value: "test@example.com"
};

// Replace the name attribute with your form field's
const messageFormData = {
  nameAttribute: "entry.12345678901",
  value: "Hello! This is my message."
};

// You can have as many form data objects as you'd like!
const formDatas = [emailFormData, messageFormData];

gformsSubmit(gformActionUrl, formDatas)
  .then(() => {
    console.log("Sent!");
  })
  .catch(err => {
    console.log(err);
  });

CORS

Responses from Google's servers are blocked by modern browsers due to CORS restrictions. Because of this, there's no way to know whether or not the form submission succeeded, so all submissions are treated as successful. You will see a CORS error in the console, but the form will still receive the submission.

If you use/build a CORS proxy, you can turn on error handling for your request to the proxy server. Error responses can then be handled as you see fit.

You can specify handleErrors to turn on request error handling:

// ...
const handleErrors = true;
// Note: This will always fail if you're not using a proxy!
gformsSubmit(gformActionUrl, formDatas, handleErrors)
  .then(() => {
    console.log("Sent!");
  })
  .catch(err => {
    console.log(err);
  });

Axios

This project uses axios to make requests.

TypeScript Support

This project is written in TypeScript and compiled to JavaScript. Type definitions are available in dist/index.d.ts.

Form data is defined as type GFormData, like so:

import gformsSubmit, { GFormData } from "gforms-submit";

const emailFormData: GFormData = {
  nameAttribute: "emailAddress",
  value: "test@example.com"
};

ISC License

Copyright 2019 Justin Mahar

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago