Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client-axios): remove axios instance in argument
- Loading branch information
Showing
24 changed files
with
187 additions
and
221 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
samples/react-app-with-react-query/src/api/mutator/custom-instance.ts
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Axios, { AxiosRequestConfig } from 'axios'; | ||
|
||
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' }); | ||
|
||
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => { | ||
return AXIOS_INSTANCE(config).then(({ data }) => data); | ||
}; | ||
|
||
export default customInstance; |
This file was deleted.
Oops, something went wrong.
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,38 +1,3 @@ | ||
import { AxiosError } from 'axios'; | ||
import { useMemo } from 'react'; | ||
import { getApi } from './config'; | ||
import { getAuthHeader } from './headers'; | ||
import { createApiError } from './utilities'; | ||
import { getSwaggerPetstore } from './endpoints/petstoreFromFileSpecWithTransformer'; | ||
|
||
export const useApi = () => { | ||
const token = 'xxxxxxxx'; // get your token | ||
|
||
const headers = useMemo( | ||
() => ({ | ||
...getAuthHeader(token), | ||
}), | ||
[token], | ||
); | ||
|
||
const unauthorizedAndErrorInterceptor = useMemo( | ||
() => ({ | ||
response: { | ||
onRejected(error: AxiosError): Promise<unknown> | void { | ||
//do your check | ||
return Promise.reject(createApiError()); | ||
}, | ||
}, | ||
}), | ||
[], | ||
); | ||
|
||
return useMemo( | ||
() => | ||
getApi({ | ||
headers, | ||
interceptor: unauthorizedAndErrorInterceptor, | ||
baseUrl: '', // use an env or your api url | ||
}), | ||
[headers, unauthorizedAndErrorInterceptor], | ||
); | ||
}; | ||
export const useApi = () => getSwaggerPetstore(); |
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,58 @@ | ||
import React, { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
import { AXIOS_INSTANCE } from './api/mutator/custom-instance'; | ||
type Dispatch = (Auth: string) => void; | ||
|
||
type AuthProviderProps = { children: ReactNode; initialState?: string | null }; | ||
|
||
const AuthContext = createContext<string | null>(null); | ||
const AuthDispatchContext = createContext<Dispatch | null>(null); | ||
|
||
const AuthProvider = ({ children, initialState = null }: AuthProviderProps) => { | ||
// it's a quick demo with useState but you can also have a more complexe state with a useReducer | ||
const [token, setToken] = useState(initialState); | ||
|
||
useEffect(() => { | ||
const interceptorId = AXIOS_INSTANCE.interceptors.request.use((config) => ({ | ||
...config, | ||
headers: token | ||
? { | ||
...config.headers, | ||
Authorization: `Bearer ${token}`, | ||
} | ||
: config.headers, | ||
})); | ||
|
||
return () => { | ||
AXIOS_INSTANCE.interceptors.request.eject(interceptorId); | ||
}; | ||
}, [token]); | ||
|
||
return ( | ||
<AuthContext.Provider value={token}> | ||
<AuthDispatchContext.Provider value={setToken}> | ||
{children} | ||
</AuthDispatchContext.Provider> | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
const useAuth = (): string | null => { | ||
return useContext<string | null>(AuthContext); | ||
}; | ||
|
||
const useAuthDispatch = (): Dispatch => { | ||
const context = useContext<Dispatch | null>(AuthDispatchContext); | ||
|
||
if (context === null) { | ||
throw new Error('useAuthDispatch must be used within a AuthProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
export { AuthProvider, useAuth, useAuthDispatch }; |
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
Oops, something went wrong.