anttiviljami / react-openapi-client

Consume OpenAPI-enabled APIs with React Hooks
MIT License
57 stars 10 forks source link

useOperation: Cannot read property 'apply' of undefined #10

Open MV10 opened 3 years ago

MV10 commented 3 years ago

We have a simple React app, brand new, doesn't do anything but attempt to invoke a single API and dump the contents of the return data, as a first attempt to use this package. I own the API project too, and I can see the request which returns the OpenAPI doc (and I know it's valid, I've used .NET and Java codegen to call it). I'm not sure where or how to troubleshoot this error message.

After the OpenAPI doc is returned, the call to useOperation fails with:

TypeError: Cannot read property 'apply' of undefined
        at useOperation.js:63
        at step (useOperation.js:33)
        at Object.next (useOperation.js:14)
        at fulfilled (useOperation.js:5)

The OpenAPI JSON is v3.0.1 and it currently has just a single entry in paths:

"paths":
  "/techsupport/CacheSystem/GetNavData": {
    "get": {
(usual stuff omitted: tags array, response 200, content list, etc.)
  }

This is on a locked-down corporate machine (no Internet access etc) but the code is so simple I'll just retype it here. It's Root instead of App because the real React apps are part of a single-spa parent app:

const Root = (props) => (
  <OpenAPIProvider definition="https://localhost:5050/openapi/v1/btpapi.json">
    <DumpNavData userData={props.userData} />
  </OpenAPIProvider>
);

const DumpNavData = (props) => {
  const { loading, data, error } = useOperation('/techsupport/CacheSystem/GetNavData', props.userData.viewEntitlement);

  if (loading) {
    return <div>Loading</div>;
  }

  if (error ) {
    console.log(error);
    return <div>Error</div>;
  }

  const stringData = JSON.stringify(data);

  return <div>{stringData}</div>;
};

export default Root;
MV10 commented 3 years ago

This is the error message when the schema is missing explicit operationId values, so it's probably related to this: https://github.com/anttiviljami/openapi-client-axios/issues/19

It seems to me that a better error message is important, so I'm leaving this open for the moment.

That also leads to another question -- can this package be used to invoke APIs by path, as I originally attempted to do in the code I posted above? Our real API is gigantic, it would be unreasonably difficult to generate guaranteed-unique (but useful) operationId values across the entire API surface.