# camelcasetypeform

> An NPM package to easily consume answers from Typeform Webhooks.

Latest version **0.3.1** (published 2018-05-14) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install camelcasetypeform
pnpm add camelcasetypeform
yarn add camelcasetypeform
bun add camelcasetypeform
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2018-05-14 |
| First published | 2018-03-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sam Caplat |
| Maintainers | samcaplat |
| Keywords | typeform, webhooks, survey, answers, json, form, camelcase, questions, results |

## Links

- npm: https://www.npmjs.com/package/camelcasetypeform
- Repository: https://github.com/samcaplat/camelCaseTypeform
- Homepage: https://github.com/samcaplat/camelCaseTypeform#readme
- Issues: https://github.com/samcaplat/camelCaseTypeform/issues
- npm.io page: https://npm.io/package/camelcasetypeform

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.5

## Recent versions

- 0.3.1 (latest) — 2018-05-14
- 0.3.0 — 2018-05-12
- 0.2.1 — 2018-03-16
- 0.1.0 — 2018-03-16

## README

# camelCaseTypeform

An NPM package to easily consume answers from Typeform Webhooks.

## Installation
```
npm install --save camelcasetypeform
```

```javascript
const cct = require('camelcasetypeform')
```

## Usage

Payload:

```json
{
  "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:

```json
{
  "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.

```javascript
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.

```javascript
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.

```javascript
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: 

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

### Properties

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

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

All special characters are removed from the Object name:

Question: `First name?`

```javascript
answers.firstName.answer
```

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

```javascript
answers.whatsTheNameOfTheAnswer76515723.answer
```

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

```javascript
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:

```javascript
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.

```javascript
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```:

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

Payments are also supported:

```javascript
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:

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

<a href="https://standardjs.com"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>

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.

---
_Source: https://npm.io/package/camelcasetypeform · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
