-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose axios types at the top-level
- Loading branch information
Weffe
committed
Feb 11, 2020
1 parent
957bc24
commit 879718f
Showing
11 changed files
with
192 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { IVersioningConfig, PickPartial } from './types'; | ||
|
||
export const defaultWithVersioningConfig: PickPartial<IVersioningConfig, "apiVersion" | "versioningStrategy"> = { | ||
export const defaultWithVersioningConfig: PickPartial<IVersioningConfig, 'apiVersion' | 'versioningStrategy'> = { | ||
mediaTypeKeyName: 'v', | ||
queryStringKeyName: 'api-version', | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { withVersioning } from './axios-api-versioning'; | ||
export { VersioningStrategy, IWithVersioningConfig, MediaTypeFormatterFn } from './types'; | ||
export * from './types/axios'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
export { | ||
VersioningStrategy, | ||
MediaTypeFormatterFn, | ||
IVersioningConfig, | ||
IWithVersioningConfig, | ||
PickPartial, | ||
} from './types' | ||
export { VersioningStrategy, MediaTypeFormatterFn, IVersioningConfig, IWithVersioningConfig, PickPartial } from './types'; | ||
|
||
export * from './axios'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,79 @@ | ||
import axios from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
import * as status from "http-status-codes"; | ||
import { withVersioning } from "../axios-api-versioning"; | ||
import { IWithVersioningConfig, VersioningStrategy } from "../types"; | ||
import { AxiosInstanceWithVersioning } from "../types/axios"; | ||
|
||
const test_url = "http://localhost:3000"; | ||
const MOCK_RES = "hello_world"; | ||
import axios, { AxiosInstance } from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import * as status from 'http-status-codes'; | ||
import { withVersioning } from '../src/axios-api-versioning'; | ||
import { IWithVersioningConfig, VersioningStrategy } from '../src/types'; | ||
import { AxiosInstanceWithVersioning } from '../src/types/axios'; | ||
|
||
const test_url = 'http://localhost:3000'; | ||
const MOCK_RES = 'hello_world'; | ||
let mock: MockAdapter; | ||
let instance: AxiosInstanceWithVersioning; | ||
|
||
describe('Testing correct response config of "QueryString" strategy', () => { | ||
let versioningConfig: IWithVersioningConfig = { | ||
apiVersion: "1.0", | ||
versioningStrategy: VersioningStrategy.QueryString | ||
}; | ||
let versioningConfig: IWithVersioningConfig = { | ||
apiVersion: '1.0', | ||
versioningStrategy: VersioningStrategy.QueryString, | ||
}; | ||
|
||
beforeAll(() => { | ||
instance = withVersioning(axios, versioningConfig); | ||
mock = new MockAdapter(instance); | ||
}); | ||
beforeAll(() => { | ||
instance = withVersioning(axios, versioningConfig); | ||
mock = new MockAdapter(instance as AxiosInstance); | ||
}); | ||
|
||
test('it should have the "apiVersion" as a query param in the response config', async () => { | ||
mock.onGet(test_url).reply(status.OK, MOCK_RES); | ||
test('it should have the "apiVersion" as a query param in the response config', async () => { | ||
mock.onGet(test_url).reply(status.OK, MOCK_RES); | ||
|
||
const res = await instance.get(test_url); | ||
const { params } = res.config; | ||
const res = await instance.get(test_url); | ||
const { params } = res.config; | ||
|
||
expect(params).toHaveProperty("api-version"); | ||
expect(params["api-version"]).toBe(versioningConfig.apiVersion); | ||
}); | ||
expect(params).toHaveProperty('api-version'); | ||
expect(params['api-version']).toBe(versioningConfig.apiVersion); | ||
}); | ||
}); | ||
|
||
describe('Testing correct response config of "MediaType" strategy', () => { | ||
let versioningConfig: IWithVersioningConfig = { | ||
apiVersion: "1.0", | ||
versioningStrategy: VersioningStrategy.MediaType | ||
}; | ||
let versioningConfig: IWithVersioningConfig = { | ||
apiVersion: '1.0', | ||
versioningStrategy: VersioningStrategy.MediaType, | ||
}; | ||
|
||
beforeAll(() => { | ||
instance = withVersioning(axios, versioningConfig); | ||
mock = new MockAdapter(instance); | ||
}); | ||
beforeAll(() => { | ||
instance = withVersioning(axios, versioningConfig); | ||
mock = new MockAdapter(instance as AxiosInstance); | ||
}); | ||
|
||
test('it should have the "apiVersion" as an accept-param in the Accept header', async () => { | ||
mock.onGet(test_url).reply(status.OK, MOCK_RES); | ||
test('it should have the "apiVersion" as an accept-param in the Accept header', async () => { | ||
mock.onGet(test_url).reply(status.OK, MOCK_RES); | ||
|
||
const res = await instance.get(test_url); | ||
const { headers } = res.config; | ||
const res = await instance.get(test_url); | ||
const { headers } = res.config; | ||
|
||
expect(headers).toHaveProperty("Accept"); | ||
expect(headers["Accept"]).toMatch(`v=${versioningConfig.apiVersion}`); | ||
}); | ||
expect(headers).toHaveProperty('Accept'); | ||
expect(headers['Accept']).toMatch(`v=${versioningConfig.apiVersion}`); | ||
}); | ||
}); | ||
|
||
describe('Testing correct response config of "UrlPath" strategy', () => { | ||
let versioningConfig: IWithVersioningConfig = { | ||
apiVersion: "1", | ||
versioningStrategy: VersioningStrategy.UrlPath | ||
}; | ||
let versioningConfig: IWithVersioningConfig = { | ||
apiVersion: '1', | ||
versioningStrategy: VersioningStrategy.UrlPath, | ||
}; | ||
|
||
const blank_test_url = test_url + "/v{apiVersion}"; | ||
const versioned_test_url = test_url + `/v${versioningConfig.apiVersion}`; | ||
const blank_test_url = test_url + '/v{apiVersion}'; | ||
const versioned_test_url = test_url + `/v${versioningConfig.apiVersion}`; | ||
|
||
beforeAll(() => { | ||
instance = withVersioning(axios, versioningConfig); | ||
mock = new MockAdapter(instance); | ||
}); | ||
beforeAll(() => { | ||
instance = withVersioning(axios, versioningConfig); | ||
mock = new MockAdapter(instance as AxiosInstance); | ||
}); | ||
|
||
test('it should have the "apiVersion" as a url param in the url', async () => { | ||
mock.onGet(versioned_test_url).reply(status.OK, MOCK_RES); | ||
test('it should have the "apiVersion" as a url param in the url', async () => { | ||
mock.onGet(versioned_test_url).reply(status.OK, MOCK_RES); | ||
|
||
const res = await instance.get(blank_test_url); | ||
const { url } = res.config; | ||
const res = await instance.get(blank_test_url); | ||
const { url } = res.config; | ||
|
||
expect(url).toBe(versioned_test_url); | ||
}); | ||
expect(url).toBe(versioned_test_url); | ||
}); | ||
}); |
Oops, something went wrong.