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

simple-axios-case-converter

v1.0.5

Published

[![npm](https://img.shields.io/npm/v/simple-axios-case-converter.svg)](https://www.npmjs.com/package/simple-axios-case-converter) [![test](https://github.com/yutak23/simple-axios-case-converter/actions/workflows/test.yaml/badge.svg)](https://github.com/yu

Downloads

262

Readme

simple-axios-case-converter

npm test style

Axios request/response object key case converts snake_case/camelCase.

Problem 😕

In many cases, the API's naming convention differs from JavaScript/TypeScript's(ex API is snake_case, JavaScript/TypeScript is camelCase), which means developers need to convert the case of object key.

Solution 😄

This library makes it easy for developers to convert case.

Features:

  • Converts axios request data params object keys into snake_case
  • Converts axios response data object keys into camelCase

With this library, you will be free from converting from camelCase to snake_case when making a request, and from converting from snake_case to camelCase in a response.

Installation

npm

$ npm install simple-axios-case-converter

yarn

$ yarn add simple-axios-case-converter

Usage

TypeScript

import axios, { AxiosResponse } from 'axios';
import axiosCaseConverter from 'simple-axios-case-converter';

interface User {
	id: number;
	firstName: string;
	lastName: string;
	fullName: string;
}

// setting simple axios case converter
axiosCaseConverter(axios);

const res: AxiosResponse<User> = await axios.get('https://example.com/api/v1/me');
// you need not convert response `data` to camelCase (typeof res.data is `User`)
console.log(res.data);

// you need not convert request `params` to snake_case
axios.get('https://example.com/api/v1/todos', { params: { onlyComplete: true } });

// you need not convert request `data` to snake_case
axios.post('https://example.com/api/v1/address', {
	countryCode: 'JP',
	postalCode: '123-4567',
	prefecture: 'Tokyo',
	city: 'Shibuya'
});

// you need not convert request `data` to snake_case
axios.patch('https://example.com/api/v1/address/123', { postalCode: '123-4567' });

// you need not convert request `data` to snake_case
axios.delete('https://example.com/api/v1/address/123', { data: { addressId: 123 } });

JavaScript

ES Module

import axios from 'axios';
import axiosCaseConverter from 'simple-axios-case-converter';

// setting simple axios case converter
axiosCaseConverter(axios);

const res = await axios.get('https://example.com/api/v1/me');
// you need not convert response `data` to camelCase
console.log(res.data);

// you need not convert request `params` to snake_case
axios.get('https://example.com/api/v1/todos', { params: { onlyComplete: true } });

// you need not convert request `data` to snake_case
axios.post('https://example.com/api/v1/address', {
	countryCode: 'JP',
	postalCode: '123-4567',
	prefecture: 'Tokyo',
	city: 'Shibuya'
});

// you need not convert request `data` to snake_case
axios.patch('https://example.com/api/v1/address/123', { postalCode: '123-4567' });

// you need not convert request `data` to snake_case
axios.delete('https://example.com/api/v1/address/123', { data: { addressId: 123 } });

CommonJS

Note that you should be require('...').default.

const axios = require('axios');
const axiosCaseConverter = require('simple-axios-case-converter').default;

// setting simple axios case converter
axiosCaseConverter(axios);

// after, the implementation is the same as the ES Module implementation

API

axiosCaseConverter: (axios, options) -> { requestInterceptorId: number, responseInterceptorId: number }

options

Optional
Type: object

requestExcludeKeys

Optional
Type: Array<string | RegExp>

Exclude keys from being snake-cased at request params and data.

responseExcludeKeys

Optional
Type: Array<string | RegExp>

Exclude keys from being camle-cased at response data.

Note

What is different from axios-case-converter

There is a library, axios-case-converter, which provides similar functionality, but the differences from that library are as follows.

  1. Simple
    Various options can be specified for axios-case-converter, but that is also the flip side of complexity.
    This library is very simple and simply converts request(data, params) and response(data) cases to sanke_case/camelCase.

  2. No side effects
    The axios-case-converter overrides axios.defaults, which may cause unexpected side effects.
    This library is simpler and safer because it uses the interceptor and does not rewrite the default config.

  3. Easily remove the case conversion feature
    Since axios-case-converter overwrites axios.defaults (axios instance default config), if you want to remove axios-case-converter functionality, you need to update axios.defaults again and will have a hard time.
    Since this library uses interceptor, you can easily disable the functionality of this library by eject as follows.

    const { requestInterceptorId, responseInterceptorId } = axiosCaseConverter(axios);
    axios.interceptors.request.eject(requestInterceptorId);
    axios.interceptors.response.eject(responseInterceptorId);

License

MIT licensed