Find another way to expose apiVersion and versioningStrategy inside AxiosRequestConfig
Weffe opened this issue · 2 comments
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:
axios-api-versioning/src/types.ts
Lines 37 to 46 in f680e77
Currently, we are modifying the global
AxiosRequestConfig
to add inapiVersion
andversioningStrategy
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 withget
,post
, etc, to accept theAxiosRequestConfigWithVersioning
instead ofAxiosRequestConfig
.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 anaxios
instance that does not have versioning, they will still get the option to defineapiVersion
andversioningStrategy
in theAxiosRequestConfig
which could be confusing.Reference:
axios-api-versioning/src/types.ts
Lines 37 to 46 in f680e77
if i create typing defination, it will produce this error
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
}