-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
657 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
import applyCaseMiddleware from 'axios-case-converter'; | ||
import { camelCase } from 'camel-case'; | ||
|
||
// conforms basePath in swagger.json | ||
export const BASE_PATH = '/api/assisted-install'; | ||
|
||
// Prevent axios converter to change object keys from '4.7-fc2' to '4_7Fc2' | ||
const axiosCaseConverterOptions = { | ||
caseFunctions: { | ||
camel: (input: string) => | ||
camelCase(input, { | ||
stripRegexp: /[^A-Z0-9.-]+/gi, | ||
}), | ||
}, | ||
}; | ||
|
||
const getDefaultClient = (withoutConverter = false) => { | ||
const client = axios.create(); | ||
client.interceptors.request.use((cfg) => ({ | ||
...cfg, | ||
url: `${process.env.AIUI_APP_API_ROOT || ''}${cfg.url || ''}`, | ||
})); | ||
if (withoutConverter) { | ||
return client; | ||
} else { | ||
return applyCaseMiddleware(client, axiosCaseConverterOptions); | ||
} | ||
}; | ||
|
||
let client: AxiosInstance = getDefaultClient(); | ||
let clientWithoutConverter: AxiosInstance = getDefaultClient(true); | ||
let ocmClient: AxiosInstance | null; | ||
let isInOcm = false; | ||
|
||
const aiInterceptor = (client: AxiosInstance) => { | ||
client.interceptors.request.use((cfg) => ({ | ||
...cfg, | ||
url: `${BASE_PATH}${cfg.url || ''}`, | ||
})); | ||
return client; | ||
}; | ||
|
||
const getOcmClient = () => ocmClient; | ||
|
||
export const setAuthInterceptor = (authInterceptor: (client: AxiosInstance) => AxiosInstance) => { | ||
ocmClient = authInterceptor(axios.create()); | ||
isInOcm = true; | ||
client = applyCaseMiddleware( | ||
aiInterceptor(authInterceptor(axios.create())), | ||
axiosCaseConverterOptions, | ||
); | ||
clientWithoutConverter = aiInterceptor(authInterceptor(axios.create())); | ||
}; | ||
|
||
export { client, getOcmClient, isInOcm, clientWithoutConverter }; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { AxiosError } from 'axios'; | ||
import { hasProp, isNonNullObject } from '../types/typescriptExtensions'; | ||
|
||
// Implementation from Axios.isAxiosError v0.29.2, which is not available in OCM's version 0.17.x | ||
export function isAxiosError<T = unknown, D = unknown>( | ||
payload: unknown, | ||
): payload is AxiosError<T, D> { | ||
return ( | ||
isNonNullObject(payload) && hasProp(payload, 'isAxiosError') && payload.isAxiosError === true | ||
); | ||
} |
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 * from './axiosClient'; | ||
export * from './types'; | ||
export * from './utils'; |
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
Oops, something went wrong.