( operationId: string, )
| 3 | import { UnknownOperationMethod, OpenAPIClientAxios, AxiosResponse, AxiosError } from 'openapi-client-axios'; |
| 4 | |
| 5 | export function useOperationMethod( |
| 6 | operationId: string, |
| 7 | ): [ |
| 8 | UnknownOperationMethod, |
| 9 | { loading: boolean; error?: Error; data?: any; response: AxiosResponse; api: OpenAPIClientAxios }, |
| 10 | ] { |
| 11 | const { api } = useContext(OpenAPIContext); |
| 12 | |
| 13 | const [loading, setLoading] = useState(false); |
| 14 | const [error, setError] = useState<AxiosError>(undefined); |
| 15 | const [data, setData] = useState<any>(undefined); |
| 16 | const [response, setResponse] = useState<AxiosResponse>(undefined); |
| 17 | |
| 18 | const operationMethod: UnknownOperationMethod = useCallback( |
| 19 | async (...params) => { |
| 20 | setLoading(true); |
| 21 | const client = await api.getClient(); |
| 22 | let res: AxiosResponse; |
| 23 | try { |
| 24 | res = await client[operationId](...params); |
| 25 | setResponse(res); |
| 26 | setData(res.data); |
| 27 | } catch (err) { |
| 28 | setError(err as AxiosError); |
| 29 | } |
| 30 | setLoading(false); |
| 31 | return res; |
| 32 | }, |
| 33 | [setLoading, setData, setError], |
| 34 | ); |
| 35 | |
| 36 | return [operationMethod, { loading, error, data, response, api }]; |
| 37 | } |
no outgoing calls
no test coverage detected