Skip to content

Latest commit

 

History

History
65 lines (42 loc) · 1.39 KB

helpers.md

File metadata and controls

65 lines (42 loc) · 1.39 KB

Helpers

Home > Extension > Helpers

Intro

A couple of helpers ship with the library to make coding hooks a little easier.

Helpers generally take a payload of unknown origin, check its type, and process accordingly.

Usage

All helpers take a value, and return a value:

import { helpers } from 'axios-actions'

const input = 1
const output = helpers.double(input)

console.log(output)
// 2

Built-in helpers

process helper

The process helper exists to iterate over array or single object responses.

The following example converts response data values to uppercase, no matter whether it's an object or array:

import { helpers } from 'axios-actions'

function onResponse (res) {
  helpers.process(res.data => item => {
    Object
      .keys(item)
      .forEach(key => {
        item[key] = String(item).toUpperCase()
      })
  })
}

toJSON helper

The json helper exists to convert values to JSON, whether a POJO or class with toJSON() method.

The following example converts request data to JSON, whether it's an object or class instance:

import { helpers } from 'axios-actions'

function onRequest(data) {
  return helpers.toJSON(data)
}

Next steps