axios-date-transformer
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

axios-date-transformer

Maintainability Test Coverage codecov Build on Main License Last Commit dependencies

An Axios transformer for seamlessly converting ISO 8601 formatted date strings with millisecond precision to JavaScript Date objects. Simplify handling of Date objects in JSON responses with this lightweight utility.

Installation

npm install axios-date-transformer

or

yarn add axios-date-transformer

Usage

Creating a new axios instance

import { createAxiosDateTransformer } from 'axios-date-transformer';

// Create an Axios instance with the date transformer
const axiosInstance = createAxiosDateTransformer({
    baseURL: 'https://example.org',
});

// Use axiosInstance for your requests
axiosInstance
    .get('/api/data')
    .then(response => {
        // Date strings in the response data are automatically converted to Date objects
        console.log(response.data);
    })
    .catch(error => {
        console.error(error);
    });

Adding the transformer to an already existing instance of axios

import { addAxiosDateTransformer } from 'axios-date-transformer';

// Create an Axios instance with the date transformer
const axiosInstance = axios.create({
    baseURL: 'https://example.org',
});
const axiosWithTransformer = addAxiosDateTransformer(axiosInstance);

// Use axiosInstance for your requests
axiosWithTransformer
    .get('/api/data')
    .then(response => {
        // Date strings in the response data are automatically converted to Date objects
        console.log(response.data);
    })
    .catch(error => {
        console.error(error);
    });

Contributing

If you find a bug or have an enhancement suggestion, feel free to open an issue or submit a pull request. Contributions are welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Package Sidebar

Install

npm i axios-date-transformer

Weekly Downloads

51

Version

1.0.3

License

MIT

Unpacked Size

10.4 kB

Total Files

6

Last publish

Collaborators

  • angelxmoreno