-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in the skelton for the api handler.
- This is a wrapper around Axios for get/post commands. - Created a series of hooks to handle how we process errors and success. - Added this to the container - Created a context wrapper.
- Loading branch information
Showing
26 changed files
with
615 additions
and
327 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createContext } from 'react'; | ||
|
||
import AxiosDefinition from './definitions/axios-definition'; | ||
|
||
export const ApiHandlerContext = createContext<AxiosDefinition | null>(null); |
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 { ModularContainerDefinition } from 'configuration/deffinitions/modular-container-definition'; | ||
|
||
import Axios from './axios'; | ||
import AxiosDefinition from './definitions/axios-definition'; | ||
import CoreContainerDefinition from '../service-container/deffinitions/core-container-definition'; | ||
|
||
export const axiosServiceContainer: ModularContainerDefinition = ( | ||
container: CoreContainerDefinition | ||
) => { | ||
container.register<AxiosDefinition>('ApiHandler', new 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import axios, { AxiosRequestConfig } from 'axios'; | ||
|
||
import AxiosDefinition from './definitions/axios-definition'; | ||
|
||
export default class Axios implements AxiosDefinition { | ||
async get<T>(url: string, config?: AxiosRequestConfig): Promise<T> { | ||
const response = await axios.get<T>(url, config); | ||
return response.data; | ||
} | ||
|
||
async post<T, D>( | ||
url: string, | ||
data: D, | ||
config?: AxiosRequestConfig | ||
): Promise<T> { | ||
const response = await axios.post<T>(url, data, config); | ||
return response.data; | ||
} | ||
} |
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,19 @@ | ||
import React, { ReactNode } from 'react'; | ||
|
||
import { ApiHandlerContext } from '../api-handler-context'; | ||
import ApiHandlerProviderProps from './types/api-handler-provider-props'; | ||
import AxiosDefinition from '../definitions/axios-definition'; | ||
|
||
import { serviceContainer } from 'service-container/core-container'; | ||
|
||
export const ApiHandlerProvider = ( | ||
props: ApiHandlerProviderProps | ||
): ReactNode => { | ||
const apiHandler = serviceContainer().fetch<AxiosDefinition>('ApiHandler'); | ||
|
||
return ( | ||
<ApiHandlerContext.Provider value={apiHandler}> | ||
{props.children} | ||
</ApiHandlerContext.Provider> | ||
); | ||
}; |
5 changes: 5 additions & 0 deletions
5
resources/js/axios/components/types/api-handler-provider-props.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
export default interface ApiHandlerProviderProps { | ||
children: React.ReactNode; | ||
} |
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,18 @@ | ||
export default interface AxiosDefinition { | ||
/** | ||
* Handles get requests. | ||
* | ||
* @param url | ||
* @param config | ||
*/ | ||
get<T, C>(url: string, config?: Record<string, C>): Promise<T>; | ||
|
||
/** | ||
* handles post requests. | ||
* | ||
* @param url | ||
* @param data | ||
* @param config | ||
*/ | ||
post<T, C, D>(url: string, data: D, config?: Record<string, C>): Promise<T>; | ||
} |
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,3 @@ | ||
export default interface AxiosErrorMessageDefinition { | ||
errorMessage: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
resources/js/axios/definitions/use-axios-error-message-handler-definition.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface AxiosErrorMessageDefinition { | ||
onError: (message: string) => void; | ||
} |
3 changes: 3 additions & 0 deletions
3
resources/js/axios/definitions/use-axios-success-definition.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface UseAxiosSuccessDefinition<T> { | ||
data: T | null; | ||
} |
3 changes: 3 additions & 0 deletions
3
resources/js/axios/definitions/use-axios-success-handler-definition.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface UseAxiosSuccessHandlerDefinition<T> { | ||
onSuccess: (data: T) => void; | ||
} |
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,4 @@ | ||
export enum AxiosEventTypes { | ||
SUCCESS = 'success', | ||
ERROR = 'error', | ||
} |
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,14 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { ApiHandlerContext } from '../api-handler-context'; | ||
import AxiosDefinition from '../definitions/axios-definition'; | ||
|
||
export const useApiHandler = (): AxiosDefinition => { | ||
const context = useContext(ApiHandlerContext); | ||
|
||
if (!context) { | ||
throw new Error('useApiHandler must be used within an ApiHandlerContext'); | ||
} | ||
|
||
return context; | ||
}; |
26 changes: 26 additions & 0 deletions
26
resources/js/axios/hooks/use-axios-error-message-handler.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEventSystem } from 'event-system/hooks/use-event-system'; | ||
|
||
import AxiosErrorMessageDefinition from '../definitions/use-axios-error-message-handler-definition'; | ||
import { AxiosEventTypes } from '../event-types/axios-events-types'; | ||
|
||
export const useAxiosErrorMessageHandler = (): AxiosErrorMessageDefinition => { | ||
const eventSystem = useEventSystem(); | ||
|
||
const manageAxiosErrorEmitter = eventSystem.isEventRegistered( | ||
AxiosEventTypes.ERROR | ||
) | ||
? eventSystem.getEventEmitter<{ [key: string]: string }>( | ||
AxiosEventTypes.ERROR | ||
) | ||
: eventSystem.registerEvent<{ [key: string]: string }>( | ||
AxiosEventTypes.ERROR | ||
); | ||
|
||
const onError = (message: string) => { | ||
manageAxiosErrorEmitter.emit(AxiosEventTypes.ERROR, message); | ||
}; | ||
|
||
return { | ||
onError, | ||
}; | ||
}; |
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,33 @@ | ||
import { useEventSystem } from 'event-system/hooks/use-event-system'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import AxiosErrorMessageDefinition from '../definitions/use-axios-error-definition'; | ||
import { AxiosEventTypes } from '../event-types/axios-events-types'; | ||
import UseAxiosErrorState from '../types/use-axios-error-state'; | ||
|
||
export const useAxiosError = (): AxiosErrorMessageDefinition => { | ||
const eventSystem = useEventSystem(); | ||
|
||
const [errorMessage, setErrorMessage] = | ||
useState<UseAxiosErrorState['errorMessage']>(''); | ||
|
||
const manageAxiosErrorEmitter = eventSystem.getEventEmitter<{ | ||
[key: string]: string; | ||
}>(AxiosEventTypes.ERROR); | ||
|
||
useEffect(() => { | ||
const updateMessage = (message: string) => { | ||
setErrorMessage(message); | ||
}; | ||
|
||
manageAxiosErrorEmitter.on(AxiosEventTypes.ERROR, updateMessage); | ||
|
||
return () => { | ||
manageAxiosErrorEmitter.off(AxiosEventTypes.ERROR, updateMessage); | ||
}; | ||
}, [manageAxiosErrorEmitter]); | ||
|
||
return { | ||
errorMessage, | ||
}; | ||
}; |
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,24 @@ | ||
import { useEventSystem } from 'event-system/hooks/use-event-system'; | ||
|
||
import UseAxiosSuccessHandlerDefinition from '../definitions/use-axios-success-handler-definition'; | ||
import { AxiosEventTypes } from '../event-types/axios-events-types'; | ||
|
||
export const useAxiosSuccessHandler = < | ||
T, | ||
>(): UseAxiosSuccessHandlerDefinition<T> => { | ||
const eventSystem = useEventSystem(); | ||
|
||
const manageAxiosSuccessEmitter = eventSystem.isEventRegistered( | ||
AxiosEventTypes.SUCCESS | ||
) | ||
? eventSystem.getEventEmitter<{ [key: string]: T }>(AxiosEventTypes.SUCCESS) | ||
: eventSystem.registerEvent<{ [key: string]: T }>(AxiosEventTypes.SUCCESS); | ||
|
||
const onSuccess = (data: T): void => { | ||
manageAxiosSuccessEmitter.emit(AxiosEventTypes.SUCCESS, data); | ||
}; | ||
|
||
return { | ||
onSuccess, | ||
}; | ||
}; |
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,32 @@ | ||
import { useEventSystem } from 'event-system/hooks/use-event-system'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import UseAxiosSuccessDefinition from '../definitions/use-axios-success-definition'; | ||
import { AxiosEventTypes } from '../event-types/axios-events-types'; | ||
import UseAxiosSuccessState from '../types/use-axios-success-state'; | ||
|
||
export const useAxiosError = <T>(): UseAxiosSuccessDefinition<T> => { | ||
const eventSystem = useEventSystem(); | ||
|
||
const [data, setData] = useState<UseAxiosSuccessState<T>['data']>(null); | ||
|
||
const manageAxiosErrorEmitter = eventSystem.getEventEmitter<{ | ||
[key: string]: T; | ||
}>(AxiosEventTypes.SUCCESS); | ||
|
||
useEffect(() => { | ||
const updateData = (data: T) => { | ||
setData(data); | ||
}; | ||
|
||
manageAxiosErrorEmitter.on(AxiosEventTypes.SUCCESS, updateData); | ||
|
||
return () => { | ||
manageAxiosErrorEmitter.off(AxiosEventTypes.SUCCESS, updateData); | ||
}; | ||
}, [manageAxiosErrorEmitter]); | ||
|
||
return { | ||
data, | ||
}; | ||
}; |
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,3 @@ | ||
export default interface UseAxiosErrorState { | ||
errorMessage: string; | ||
} |
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,3 @@ | ||
export default interface UseAxiosSuccessState<T> { | ||
data: T | null; | ||
} |
2 changes: 1 addition & 1 deletion
2
...finitions/modular-container-deffintion.ts → ...finitions/modular-container-definition.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import CoreContainerDefinition from '../../service-container/deffinitions/core-container-definition'; | ||
|
||
export type ModularContainerDeffintion = ( | ||
export type ModularContainerDefinition = ( | ||
container: CoreContainerDefinition | ||
) => void; |
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,10 +1,12 @@ | ||
import { eventServiceContainer } from 'event-system/event-service-container'; | ||
|
||
import { ModularContainerDeffintion } from './deffinitions/modular-container-deffintion'; | ||
import { ModularContainerDefinition } from './deffinitions/modular-container-definition'; | ||
import { axiosServiceContainer } from '../axios/axios-service-container'; | ||
|
||
/** | ||
* Register service containers here. | ||
*/ | ||
export const serviceContainers: ModularContainerDeffintion[] = [ | ||
export const serviceContainers: ModularContainerDefinition[] = [ | ||
eventServiceContainer, | ||
axiosServiceContainer, | ||
]; |
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.