Skip to content

RicoSuter/SigSpec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

SigSpec for SignalR Core

Experimental specification and code generator for SignalR Core.

Run SigSpec.Console to see a demo spec and the generated TypeScript code.

Please let me know what you think here.

Based on NJsonSchema (see also: NSwag).

Original idea: RicoSuter/NSwag#691

Sample

Hub:

public class ChatHub : Hub<IChatClient>
{
    public Task Send(string message)
    {
        if (message == string.Empty)
        {
            return Clients.All.Welcome();
        }

        return Clients.All.Send(message);
    }

    public Task Foo(Bar bar)
    {
        return Task.CompletedTask;
    }
}

public class Bar
{
    [JsonProperty("firstName")]
    public string FirstName { get; set; }

    [JsonProperty("lastName")]
    public string LastName { get; set; }
}

public interface IChatClient
{
    Task Welcome();

    Task Send(string message);
}

Generated spec:

{
  "hubs": {
    "chat": {
      "name": "Chat",
      "description": "",
      "operations": {
        "Send": {
          "description": "",
          "parameters": {
            "message": {
              "type": [
                "null",
                "string"
              ],
              "description": ""
            }
          }
        },
        "Foo": {
          "description": "",
          "parameters": {
            "bar": {
              "description": "",
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "$ref": "#/definitions/Bar"
                }
              ]
            }
          }
        }
      },
      "callbacks": {
        "Welcome": {
          "description": "",
          "parameters": {}
        },
        "Send": {
          "description": "",
          "parameters": {
            "message": {
              "type": [
                "null",
                "string"
              ],
              "description": ""
            }
          }
        }
      }
    }
  },
  "definitions": {
    "Bar": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "firstName": {
          "type": [
            "null",
            "string"
          ]
        },
        "lastName": {
          "type": [
            "null",
            "string"
          ]
        }
      }
    }
  }
}

Generated TypeScript code:

export class ChatHub {
    ChatHub(private connection: any) {
    }

    send(message: string) {
        this.connection.invoke('Send', message);
    }

    foo(bar: Bar) {
        this.connection.invoke('Foo', bar);
    }

    registerCallbacks(implementation: IChatHubCallbacks) {
        this.connection.on('Welcome', () => implementation.welcome());
        this.connection.on('Send', (message) => implementation.send(message));
    }
}

export interface IChatHubCallbacks {
    welcome();
    send(message: string);
}

export interface Bar {
    firstName: string;
    lastName: string;
}

About

Specification and code generator for SignalR Core.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published