vforteli.TypeScriptHubGenerator 0.1.0

dotnet tool install --global vforteli.TypeScriptHubGenerator --version 0.1.0                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local vforteli.TypeScriptHubGenerator --version 0.1.0                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=vforteli.TypeScriptHubGenerator&version=0.1.0                
nuke :add-package vforteli.TypeScriptHubGenerator --version 0.1.0                

TypeScriptHubGenerator

Create TypeScript clients from SignalR hubs.

For React, a context and hook is created.

Using the tool

# install globally (or locally)
dotnet tool install --global vforteli.TypeScriptHubGenerator

# run the tool
dotnet tshubgen \
    --assembly-path "some/folder/assembly.dll" \
    --output-folder "some/other/folder" \
     --create-react-context

Using the client

React

Using the client requires installing @microsoft/signalr (tested with version 8.0.7)

// Configure hub in eg App.tsx
function App() {
  const hubConnection = new HubConnectionBuilder()
    .withAutomaticReconnect()
    .withUrl("someurl")
    .build();

  return (
    <SomeHubClientContextProvider hubConnection={hubConnection}>
      <>...</>
    </SomeHubClientContextProvider>
  );
}
// Use in some component
export const SomeComponent = () => {
  const someHub = useSomeHubClient();

  // Wrap in callback to be able to remove handler in useEffect
  const handleSomethingHappened = useCallback((message: string | null) => {
    console.debug("hub: " + message);
  }, []);

  // Add handler for a callback. The handler must be removed explicitly or duplicates will be added on re-renders
  useEffect(() => {
    someHub.hub.addSomethingHappenedHandler(handleSomethingHappened);

    return () => {
      someHub.hub.removeSomethingHappenedHandler(handleSomethingHappened);
    };
  }, [handleSomethingHappened, someHub.hub]);

  // Invoke a hub method
  const doSomething = () => {
    someHub.hub.doSomething({ somePayload: "hello from component!" });
  };

  return <>...</>;
};
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
0.1.0 99 11/20/2024
0.1.0-prerelease.17 41 11/20/2024

Initial test release