MCPcopy Create free account
hub / github.com/anttiviljami/react-openapi-client / useOperationMethod

Function useOperationMethod

src/useOperationMethod.tsx:5–37  ·  view source on GitHub ↗
(
  operationId: string,
)

Source from the content-addressed store, hash-verified

3import { UnknownOperationMethod, OpenAPIClientAxios, AxiosResponse, AxiosError } from 'openapi-client-axios';
4
5export 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}

Callers 2

PetDetailsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected