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

axios-case-converter

v1.1.1

Published

Axios transformer/interceptor that converts snake_case/camelCase

Downloads

327,075

Readme

axios-case-converter

npm version Build Status Coverage Status

Axios transformer/interceptor that converts snake_case/camelCase

  • Converts outgoing data params object keys into snake_case
  • Converts incoming data object keys into camelCase
  • Converts outgoing headers object keys into Header-Case
  • Converts incoming headers object keys into camelCase

Installing

NPM

npm install axios-case-converter

[!IMPORTANT]

Axios is a peer dependency of axios-case-converter and must be installed separately.

npm install axios

CDN

<script src="https://unpkg.com/axios-case-converter@latest/dist/axios-case-converter.min.js"></script>

It is strongly recommended that you replace latest with a fixed version.

Usage

You can fully use camelCase in your JavaScript codes.

import applyCaseMiddleware from 'axios-case-converter';
import axios from 'axios';

(async () => {
  const client = applyCaseMiddleware(axios.create());
  const { data } = await client.post(
    'https://example.com/api/endpoint',
    {
      targetId: 1
    },
    {
      params: { userId: 1 },
      headers: { userAgent: 'Mozilla' }
    }
  );

  console.log(data.actionResult.users[0].screenName);
})();

Options

const client = applyCaseMiddleware(axios.create(), options);

preservedKeys: string[] | Function

Disable transformation when the string matched or satisfied the condition.

const options = {
  preservedKeys: ['preserve_this_key_1', 'preserve_this_key_2']
};
const options = {
  preservedKeys: (input) => {
    return ['preserve_this_key_1', 'preserve_this_key_2'].includes(input);
  }
};

ignoreHeaders: boolean

Disable HTTP headers transformation.

const options = {
  ignoreHeaders: true
};

ignoreParams: boolean

Disable HTTP URL parameters transformation.

const options = {
  ignoreParams: true
};

caseFunctions: { snake?: Function, camel?: Function, header?: Function }

Override built-in change-case functions.

const options = {
  caseFunctions: {
    camel: (input, options) => {
      return (input.charAt(0).toLowerCase() + input.slice(1)).replace(/[-_](.)/g, (match, group1) => group1.toUpperCase());
    }
  }
};

caseOptions: { stripRegexp?: RegExp }

By default, { stripRegexp: /[^A-Z0-9[\]]+/gi } is used as default change-case function options. This preserves [] chars in object keys. If you wish keeping original change-case behavior, override the options.

const options = {
  caseOptions: {
    stripRegexp: /[^A-Z0-9]+/gi
  }
};

caseMiddleware: { requestTransformer?: Function, responseTransformer?: Function, requestInterceptor?: Function }

Totally override axios-case-converter behaviors.

const options = {
  caseMiddleware: {
    requestInterceptor: (config) => {
      // Disable query string transformation
      return config;
    }
  }
};
Check the tests for more info

Attention

[!WARNING]

Object compatibility

If you run on Internet Explorer, you need polyfill for Object.prorotypte.entries().

[!WARNING]

FormData compatibility

If you use FormData on Internet Explorer, you need polyfill of FormData.prototype.entries().

If you use FormData on React Native, please ignore the following warnings after confirming that polyfill is impossible.

// RN >= 0.52
import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings([
  'Be careful that FormData cannot be transformed on React Native.'
]);

// RN < 0.52
console.ignoredYellowBox = [
  'Be careful that FormData cannot be transformed on React Native.'
];

[!WARNING]

Symbol compatibility

If you use React Native for Android development, you should use Symbol polyfill from core-js to avoid bugs with iterators:

  1. Create polyfill.js in root directory with code:
global.Symbol = require('core-js/es6/symbol');
require('core-js/fn/symbol/iterator');
  1. Include polyfill.js in entry point of your app (e.g. app.js):
import { Platform } from 'react-native';

// ...

if (Platform.OS === 'android') {
  require('./polyfill.js');
}

cf. undefined is not a function(evaluating '_iterator[typeof Symbol === "function"?Symbol.iterator:"@@iterator"]()') · Issue #15902 · facebook/react-native