Weffe/axios-api-versioning

Find another way to expose apiVersion and versioningStrategy inside AxiosRequestConfig

Weffe opened this issue · 2 comments

Weffe commented

Currently, we are modifying the global AxiosRequestConfig to add in apiVersion and versioningStrategy as optional properties. This is needed so TypeScript users can pass in those options on a request without getting any issues.

It would be better to find a different way to provide the same type safety without modifying the global AxiosRequestConfig.

What I can think of is creating our own type AxiosRequestConfigWithVersioning and exposing it. Then, recreate the axios interface with get, post, etc, to accept the AxiosRequestConfigWithVersioning instead of AxiosRequestConfig.

This also tightens things up in terms of type safety because as soon as you import this module then it automatically modifies AxiosRequestConfig. If a TypeScript user creates an axios instance that does not have versioning, they will still get the option to define apiVersion and versioningStrategy in the AxiosRequestConfig which could be confusing.

Reference:

/**
* modify the global axios type of AxiosRequestConfig
* to add "apiVersion" & "versioningStrategy" to config object
*/
declare module "axios" {
interface AxiosRequestConfig {
apiVersion?: string;
versioningStrategy?: string
}
}

Currently, we are modifying the global AxiosRequestConfig to add in apiVersion and versioningStrategy as optional properties. This is needed so TypeScript users can pass in those options on a request without getting any issues.

It would be better to find a different way to provide the same type safety without modifying the global AxiosRequestConfig.

What I can think of is creating our own type AxiosRequestConfigWithVersioning and exposing it. Then, recreate the axios interface with get, post, etc, to accept the AxiosRequestConfigWithVersioning instead of AxiosRequestConfig.

This also tightens things up in terms of type safety because as soon as you import this module then it automatically modifies AxiosRequestConfig. If a TypeScript user creates an axios instance that does not have versioning, they will still get the option to define apiVersion and versioningStrategy in the AxiosRequestConfig which could be confusing.

Reference:

/**
* modify the global axios type of AxiosRequestConfig
* to add "apiVersion" & "versioningStrategy" to config object
*/
declare module "axios" {
interface AxiosRequestConfig {
apiVersion?: string;
versioningStrategy?: string
}
}

if i create typing defination, it will produce this error

image

image

Weffe commented

Hey @alanhg I actually have resolved this issue so you wouldn't need to add in the type definition yourself. This library already has built-in types since v2.0.0 release and should just work fine in your application. You should only notice the customized types when you are passing in options to axios requests as seen here (https://weffe.github.io/axios-api-versioning/#/guides/overriding-api-config). Or when accessing the properties from a fulfilled axios request like so:

import axios from 'axios';
import { withVersioning, VersioningStrategy } from 'axios-api-versioning';
import { AxiosResponseWithVersioning } from 'axios-api-versioning/dist/lib/types/axios';

const client = withVersioning(axios, {
    apiVersion: '1',
    versioningStrategy: VersioningStrategy.MediaType
});

async function foo() {
    // cast the normal AxiosResponse to AxiosResponseWithVersioning to get proper type hinting
    const res = await client.get('http://example.com') as AxiosResponseWithVersioning;
    
    // should get TS auto-complete + type hinting on res.config
    // where apiVersion and versioningStrategy exists 
    res.config.apiVersion
    res.config.versioningStrategy 
}