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

0.0.1 • Public • Published

JSON22-Axios

Axios interceptor providing support to JSON22 data format in your applications.

Features

  • Ready to use Axios interceptor
  • Parse JSON22 body content
  • Serialize data to JSON22
  • Support for global interceptor as well as request level transformation
  • Both CJS/ESM modules support

Installation

npm install json22-axios

Add interceptor at your client setup

import axios from 'axios';
import { Json22RequestInterceptor } from 'json22-axios';

axios.interceptors.request.use(Json22RequestInterceptor());

For old-fashioned javascript

const axios = require('axios');
const { Json22RequestInterceptor } = require('json22-axios');

axios.interceptors.request.use(Json22RequestInterceptor());

Options

Both stringify and parse methods of JSON22 accepts options. You may be interested to define such options at global level as well as with isolated client instance.

Json22RequestInterceptor accepts the next options structure

interface Json22AxiosOptions {
    json22ParseOptions?: Json22ParseOptions;
    json22StringifyOptions?: Json22StringifyOptions;
}

See also Json22ParseOptions and Json22StringifyOptions at JSON22 API description

Define global level options

import axios from 'axios';
import { Json22RequestInterceptor } from 'json22-axios';
import { TypedModel } from './models/typed-model.js';

axios.interceptors.request.use(Json22RequestInterceptor({
    json22ParseOptions: { context: { TypedModel } },
}));

Define isolated client options

import axios from 'axios';
import { Json22RequestInterceptor } from 'json22-axios';
import { TypedModel } from './models/typed-model.js';

const client = axios.create();
client.interceptors.request.use(Json22RequestInterceptor({
    json22ParseOptions: { context: { TypedModel } },
}));

Request level data transformation

In same rare cases you might be interested to set up data transformation for a specific query. This case you shall not use the interceptor. Instead, you'll have to use data transformers functions. Data transformers do not accept options, so you'll need to define it on query configuration at json22Options.

import axios from 'axios';
import { transformJson22StringToData, transformDataToJson22String } from 'json22-axios';
import { TypedModel } from './models/typed-model.js';

export async function postData(data) {
    const resp = await axios.request({
        method: 'POST',
        baseURL: 'https://example.com',
        url: '/api/data',
        transformResponse: transformJson22StringToData,
        transformRequest: transformDataToJson22String,
        json22Options: { json22ParseOptions: { context: { TypedModel } } },
        data
    });
    return resp.data;
}   

Note: json22Options configuration field is not defined by axios. That is the reason we do not recommend to use json22 data transformers directly. Please, use interceptor instead.

Package Sidebar

Install

npm i json22-axios

Weekly Downloads

3

Version

0.0.1

License

MIT

Unpacked Size

12.4 kB

Total Files

6

Last publish

Collaborators

  • dancecoder