Skip to content

Commit d796c84

Browse files
committed
commit json-rpc changes
1 parent c1efc4f commit d796c84

File tree

3 files changed

+155
-34
lines changed

3 files changed

+155
-34
lines changed

extensions/Sisk.JsonRPC/Documentation/JsonRpcHtmlExport.cs

Lines changed: 146 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,122 @@ namespace Sisk.JsonRPC.Documentation;
1717
/// </summary>
1818
public class JsonRpcHtmlExport : IJsonRpcDocumentationExporter {
1919

20+
/// <summary>
21+
/// Gets or sets an boolean indicating if an summary should be exported in the HTML.
22+
/// </summary>
23+
public bool ExportSummary { get; set; } = true;
24+
25+
/// <summary>
26+
/// Gets or sets an optional object to append to the header of the
27+
/// exported HTML.
28+
/// </summary>
29+
public object? Header { get; set; }
30+
2031
/// <summary>
2132
/// Gets or sets the CSS styles used in the HTML export.
2233
/// </summary>
2334
public string? Style { get; set; } = """
24-
* { box-sizing: border-box; }
25-
html, body { margin: 0; background-color: #f4f9ff; font-family: Arial }
35+
* { box-sizing: border-box; }
2636
p, li { line-height: 1.6 }
27-
li + li { margin-top: 1em; }
28-
.monospaced { font-family: monospace; }
37+
38+
html, body {
39+
margin: 0;
40+
background-color: white;
41+
font-size: 16px;
42+
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"
43+
}
2944
3045
main {
3146
background: white;
32-
max-width: 800px;
47+
max-width: 900px;
48+
width: 90vw;
3349
margin: 30px auto 0 auto;
3450
padding: 20px 40px;
3551
border-radius: 14px;
36-
border: 1px solid #cbd3da;
52+
border: 1px solid #d1d9e0;
53+
}
54+
55+
h1, h2, h3 {
56+
margin-top: 2.5rem;
57+
margin-bottom: 1rem;
58+
font-weight: 600;
59+
line-height: 1.25;
60+
}
61+
62+
h1, h2 {
63+
padding-bottom: .3em;
64+
border-bottom: 2px solid #d1d9e0b3;
3765
}
3866
39-
h1 {
40-
padding-bottom: .25em;
41-
border-bottom: 2px solid #aacae7;
42-
font-size: 1.8em;
43-
font-weight: medium;
67+
h1 {
68+
font-size: 2em;
4469
}
4570
4671
h2 {
47-
padding: 1em 0 .25em 0;
48-
border-bottom: 1px solid #aacae7;
49-
font-size: 1.4em;
50-
font-weight: normal;
72+
font-size: 1.5em;
73+
}
74+
75+
h1 a,
76+
h2 a {
77+
opacity: 0;
78+
color: #000;
79+
text-decoration: none !important;
80+
user-select: none;
81+
}
82+
83+
h1:hover a,
84+
h2:hover a {
85+
opacity: 0.3;
86+
}
87+
88+
h1 a:hover,
89+
h2 a:hover {
90+
opacity: 1;
91+
}
92+
93+
.paramlist {
94+
padding-left: 0;
95+
list-style-type: none;
96+
}
97+
98+
.paramtitle {
99+
display: flex;
100+
gap: 15px;
101+
margin-bottom: 5px;
102+
}
103+
104+
.paramtitle b {
105+
padding: .2em .4em;
106+
margin: 0;
107+
font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace;
108+
font-size: 14px;
109+
font-weight: 600;
110+
white-space: break-spaces;
111+
background-color: #eff1f3;
112+
border-radius: 6px;
113+
}
114+
115+
.paramlist li + li {
116+
margin-top: 1em;
117+
border-top: 1px solid #d8dee4;
118+
padding-top: 1.25em;
119+
}
120+
121+
.muted {
122+
color: #656d76;
123+
}
124+
125+
.at {
126+
color: #9a6700;
51127
}
52128
53-
.details > span {
54-
display: inline-block;
55-
background-color: #e3e8ed;
56-
padding: 1px 6px;
57-
font-size: 0.9em;
58-
font-weight: bold;
129+
a {
130+
color: #0969da;
131+
text-decoration: none;
59132
}
60133
61-
.details > span:not(:first-child) {
62-
margin-left: 5px;
134+
a:hover {
135+
text-decoration: underline;
63136
}
64137
""";
65138

@@ -70,6 +143,10 @@ public class JsonRpcHtmlExport : IJsonRpcDocumentationExporter {
70143
protected string EncodeDocumentationHtml ( JsonRpcDocumentation documentation ) {
71144
HtmlElement html = new HtmlElement ( "html" );
72145

146+
string GetMethodIdName ( string name ) {
147+
return new string ( name.Where ( char.IsLetterOrDigit ).ToArray () );
148+
}
149+
73150
html += new HtmlElement ( "head", head => {
74151
head += new HtmlElement ( "title", "JSON-RPC 2.0 Application Documentation" );
75152
if (this.Style != null) {
@@ -83,24 +160,62 @@ protected string EncodeDocumentationHtml ( JsonRpcDocumentation documentation )
83160
.GroupBy ( g => g.Category );
84161

85162
body += new HtmlElement ( "main", main => {
163+
164+
if (this.Header is not null) {
165+
main += this.Header;
166+
}
167+
168+
if (this.ExportSummary) {
169+
main += new HtmlElement ( "h1", "Summary" );
170+
171+
foreach (var category in methodsGrouped) {
172+
main += new HtmlElement ( "p", (category.Key ?? "Methods") + ":" );
173+
main += new HtmlElement ( "ul", ul => {
174+
foreach (var method in documentation.Methods) {
175+
ul += new HtmlElement ( "li", li => {
176+
li += new HtmlElement ( "a", method.MethodName )
177+
.WithAttribute ( "href", $"#{GetMethodIdName ( method.MethodName )}" );
178+
if (!string.IsNullOrEmpty ( method.Description )) {
179+
li += new HtmlElement ( "span", $" - {method.Description}" )
180+
.WithClass ( "muted" );
181+
}
182+
} );
183+
}
184+
} );
185+
}
186+
}
187+
86188
foreach (var category in methodsGrouped) {
87189
main += new HtmlElement ( "h1", category.Key ?? "Methods" );
88190
foreach (var method in category) {
89191
main += new HtmlElement ( "section", section => {
90-
section += new HtmlElement ( "h2", method.MethodName );
192+
193+
section.Id = GetMethodIdName ( method.MethodName );
194+
195+
section += new HtmlElement ( "h2", h2 => {
196+
h2 += method.MethodName;
197+
h2 += new HtmlElement ( "a", "🔗" )
198+
.WithAttribute ( "href", $"#{section.Id}" );
199+
} );
91200
section += new HtmlElement ( "p", method.Description ?? "" );
92201
section += new HtmlElement ( "p", $"Returns: {method.ReturnType.FullName}" );
93202
section += new HtmlElement ( "ul", ulParams => {
203+
ulParams.ClassList.Add ( "paramlist" );
94204
foreach (var param in method.Parameters) {
95205
ulParams += new HtmlElement ( "li", li => {
96-
li += new HtmlElement ( "b", param.ParameterName )
97-
.WithClass ( "monospaced" );
98-
li += new HtmlElement ( "div", param.Description ?? "(no description)" );
99-
li += new HtmlElement ( "div", paramDetails => {
100-
paramDetails.WithClass ( "details" );
101-
paramDetails += new HtmlElement ( "span", param.IsOptional ? "Optional" : "Required" );
102-
paramDetails += new HtmlElement ( "span", param.ParameterType.FullName );
206+
li += new HtmlElement ( "div", div => {
207+
div.ClassList.Add ( "paramtitle" );
208+
209+
div += new HtmlElement ( "b", param.ParameterName );
210+
211+
div += new HtmlElement ( "span", param.ParameterType.FullName )
212+
.WithClass ( "muted" );
213+
214+
if (!param.IsOptional)
215+
div += new HtmlElement ( "span", "Required" ).WithClass ( "at" );
216+
103217
} );
218+
li += new HtmlElement ( "div", param.Description ?? "" );
104219
} );
105220
}
106221
} );

extensions/Sisk.JsonRPC/JsonRpcTransportLayer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ JsonRpcResponse HandleRpcRequest ( JsonRpcRequest request ) {
205205

206206
var jsonParameter = jsonValueObject [ paramName ];
207207

208+
if (jsonParameter.IsNull && !param.IsOptional && Nullable.GetUnderlyingType ( param.ParameterType ) == null) {
209+
response = new JsonRpcResponse ( null,
210+
new JsonRpcError ( JsonErrorCode.InvalidParams, $"Parameter \"{paramName}\" is required.", JsonValue.Null ), request.Id );
211+
return response;
212+
}
213+
208214
methodInvokationParameters [ i ] = jsonParameter.MaybeNull ()?.Get ( param.ParameterType );
209215
}
210216
}

extensions/Sisk.JsonRPC/Sisk.JsonRPC.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
<PackageTags>http-server,http,web framework</PackageTags>
2525
<RepositoryType>git</RepositoryType>
2626

27-
<AssemblyVersion>1.3.1</AssemblyVersion>
28-
<FileVersion>1.3.1</FileVersion>
29-
<Version>1.3.1-beta1</Version>
27+
<AssemblyVersion>1.3.2</AssemblyVersion>
28+
<FileVersion>1.3.2</FileVersion>
29+
<Version>1.3.2</Version>
3030

3131
<NeutralLanguage>en</NeutralLanguage>
3232
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>

0 commit comments

Comments
 (0)