npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typeform-payload-helpers

v0.1.1

Published

Helper functions to crawl form payload responses data from Typeform

Downloads

28

Readme

typeform-payload-helpers

Build Status

Helper functions to crawl form payload responses data from Typeform.

This library is in beta since it needs stronger tests around different Typeform block types.

Table of contents

Motivations

The payload of a webhook from Typeform contains a definition property which holds an array of field descriptors and an array of the answers from the respondent.

A field descriptor looks similar to this:

payload object

{
  "definition": {
    "fields": [
      {
        "id": "JwWggjAKtOkA",
        "title": "What is your first name?",
        "type": "short_text",
        "ref": "fhg908-ewb047-gfp261-mxw563",
        "allow_multiple_selections": false,
        "allow_other_choice": false
      }
    ]
  },
  "answers": [ ]
}

The answer to that field will contain the type, value and the reference of the field:

payload object

{
  "definition": {
    "fields": [ ]
  },
  "answers": [
    {
      "type": "text",
      "text": "Laura",
      "field": {
        "id": "JwWggjAKtOkA",
        "type": "short_text"
      }
    }
  ]
}

When processing these responses, it can be difficult to identify to which question the answer belongs, but we can use Typeform's Create API to PUT a new version of the form definition with more readable ref values we can access to.

Read more about this in Typeform's developer portal.

Once we have more readable refs in our form definition we can use it to get the field id from it, and with that id get the answer block.

In order to make this process less repetitive and avoid copy pasting the same approach when processing responses, this library provides a set of helpers available for web or node applications.

Installation

Add it to your application using a package manager.

# npm
npm i typeform-payload-helpers --save

# yarn
yarn add typeform-payload-helpers

Main helpers

getAnswerValueFromRef

Given a ref string and a payload object containing both the definition and the answers arrays, returns the value of the corresponding answer.

import { getAnswerValueFromRef } from 'typeform-payload-helpers';

getAnswerValueFromRef('first_name', payload);
// 'Phoebe'

getAnswerValueFromId

In case you rely on an id mapping instead of refs, you can use this method. Given an id string and a payload object containing both the definition and the answers arrays, returns the value of the corresponding answer.

import { getAnswerValueFromId } from 'typeform-payload-helpers';

getAnswerValueFromId('JjpjdSGicVx1', payload);
// 'Phoebe'

Extended helpers

Though these methods are used internally by getAnswerValueFromRef and getAnswerValueFromId to get answer values, they are available in case a developer finds them useful.

getIdFromRef

Given a ref string and an object containing the form definition returns the corresponding field id.

import { getIdFromRef } from 'typeform-payload-helpers';

getIdFromRef('first_name', payload);
// returns 'JwWggjAKtOkA'

getAnswerFromId

Given an id and an object containing the form definition and an answers array returns the corresponding answer block.

import { getAnswerFromId } from 'typeform-payload-helpers';

getAnswerFromId('JwWggjAKtOkA', payload);

//  returns {
//     "type": "text",
//     "text": "Laura",
//     "field": {
//       "id": "JwWggjAKtOkA",
//       "type": "short_text"
//     }
//   }

getAnswerValue

Given an answer object returns the corresponding response value.

import { getAnswerValue } from 'typeform-payload-helpers';

const answer = {
  type: 'text',
  text: 'Laura',
  field: {
    id: 'JwWggjAKtOkA',
    type: 'short_text'
  }
};

getAnswerValue(answer);
// 'Laura'

About the payload parameter

Notice that all payload parameters have the same structure as an object received from a Webhooks post. You can see an example on the platform's documentation.

If you have a form definition stored in other format pass it inside an object.

import { getIdFromRef } from 'typeform-payload-helpers';
import customFormDefinition from './custom-form-definition.js'

// pass the object with the 'definition' property name
getIdFromRef('some_custom_ref', { definition: customFormDefinition });

Contributing

This repository use yarn for managind the dependencies.

  • Clone or fork this repository.
  • Run yarn to install the dependencies.
  • Apply your fixes or features.

Update or add tests if necessary.

  • Run yarn test to make sure there's no regression.
  • Submit a PR 🎉

-You can do yarn test --watch when working on TDD mode.

TODO

  • Add linting and precommit hooks.