JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 76438
  • Score
    100M100P100Q159580F
  • License MIT

Axios transformer/interceptor that converts snake_case/camelCase

Package Exports

  • axios-case-converter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (axios-case-converter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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

Usage

You can fully use camelCase.

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

(async () => {
  const client = applyConverters(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 = applyConverters(axios.create(), options);

preservedKeys: string[]

Provide the keys that need to be excluded from being transformed.

const options = {
  preservedKeys: ["_preserve_this_key"]
};

Check the tests for more info

Attention

FormData compatibility

If you use FormData on Internet Explorer or Safari, 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.'
];

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 '_iteratortypeof Symbol === "function"?Symbol.iterator:"@@iterator"') · Issue #15902 · facebook/react-native