0.3.1 • Published 6 years ago

camelcasetypeform v0.3.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

camelCaseTypeform

An NPM package to easily consume answers from Typeform Webhooks.

Installation

npm install --save camelcasetypeform
const cct = require('camelcasetypeform')

Usage

Payload:

{
  "event_id": "09GHYHGG4566543FGHHJ",
  "event_type": "form_response",
  "form_response": {
    "form_id": "id",
    "token": "token",
    "submitted_at": "2018-04-27T08:50:43Z",
    "definition": {
      "id": "id",
      "title": "Account Application",
      "fields": [
        {
          "id": "cIgKDPbAVkpr",
          "title": "First name?",
          "type": "short_text",
          "ref": "123456",
          "allow_multiple_selections": false,
          "allow_other_choice": false
        },
        {
          "id": "qp8TwTpRoN1W",
          "title": "What city do you live in?",
          "type": "short_text",
          "ref": "654321",
          "allow_multiple_selections": false,
          "allow_other_choice": false
        }
      ]
    },
    "answers": [
      {
        "type": "text",
        "text": "Sam",
        "field": {
          "id": "cIgKDPbAVkpr",
          "type": "short_text"
        }
      },
      {
        "type": "text",
        "text": "London",
        "field": {
          "id": "qp8TwTpRoN1W",
          "type": "short_text"
        }
      }
    ]
  }
}

Becomes:

{
  "firstName": {
    "answerType": "text",
    "answer": "Sam",
    "id": "cIgKDPbAVkpr",
    "questionType": "short_text"
  },
  "whatCityDoYouLiveIn": {
    "answerType": "text",
    "answer": "London",
    "id": "qp8TwTpRoN1W",
    "questionType": "short_text"
  }
}

Functions

The .consume() function accepts JSON as a parameter, and assigns a list of Objects to a variable.

let answers = cct.consume(json)

If you want to log the answers to the console at runtime, you can also pass true as the second parameter.

let answers = cct.consume(json, true)

Because Typeform only includes completed questions in its payload, you'll want to check if an optional question has been completed. camelCaseTypeform stores the answers so you can check to see if it's been completed.

You can use the .in() function to do this.

cct.in('firstName') // Returns true
cct.in('lastName') // Returns false

if (cct.in('firstName')) {
    // do something with 'firstName'
}

If .in() is called before .consume(), the following error will occur:

Error: camelCaseTypeform - Must populate results before checking if an answer exists.

Properties

Answers can be accessed by calling .answer on an Object.

answers.firstName.answer // Returns 'Sam'
answers.whatCityDoYouLiveIn.answer // Returns 'London'

All special characters are removed from the Object name:

Question: First name?

answers.firstName.answer

Question: What's the name of the {{answer_76515723}}

answers.whatsTheNameOfTheAnswer76515723.answer

You can also access all of the other values included in the payload:

answers.howManyCarsDoYouOwn.answerType // Returns 'number'
answers.howManyCarsDoYouOwn.id // Returns '{answer_id}'
answers.howManyCarsDoYouOwn.questionType // Returns 'short_text'

Typeform defines multiple-choice answers differently depending on how the answers have been chosen. If the user has selected multiple options, the answers are put into a list:

answers.whatsYourPerfectSunday.answer.label[0] // Returns 'Breakfast in bed'
answers.whatsYourPerfectSunday.answer.label[1] // Returns 'Going for a run'

If only one option has been selected, answer.label will not be a list, and will only return one value.

answers.favouritePasstime.answer.label // Returns 'Video games'

If no choices were selected, and only the 'Other' field was completed, answer.label will return null.

If an 'Other' answer has been provided, this can be accessed by using answer.other. Otherwise it will return null:

answers.whichBeerDoYouEnjoyMost.answer.other // Returns 'Homebrewed, like a boss!'

Payments are also supported:

answers.payForYourOrder.answer.amount // Returns '{amount_paid}'
answers.payForYourOrder.answer.last4 // Returns '{last_4_card_numbers}'
answers.payForYourOrder.answer.name // Returns '{name_on_card}'
answers.payForYourOrder.answer.success // Returns 'true' or 'false'

I hope you find camelCaseTypeform useful :blush:

answers.whoAreYou.answer // Sam Caplat
answers.whereDoYouLive.answer // London, UK
answers.areYouAvailableForHire.answer // true
answers.howCanWeContactYou.answer // sam@capl.at

License

MIT License

Copyright (c) 2018 Sam Caplat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.1.0

6 years ago