Skip to content

Commit 48a5844

Browse files
authored
Merge pull request #80 from rameel/typed-values
config: use typed values for swap and request options
2 parents 563ade7 + 5478616 commit 48a5844

14 files changed

Lines changed: 372 additions & 48 deletions

File tree

‎samples/Ramstack.HtmxToolkit.Demo/Pages/Examples/RequestOptions.cshtml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313

1414
<section class="demo-card">
1515
<div class="demo-card__header">
16-
<h2><code>hx-request-timeout</code></h2>
17-
<p>The toolkit emits an HTMX 4 <code>hx-config</code> timeout for this report preview.</p>
16+
<h2><code>hx-request-*</code></h2>
17+
<p>The toolkit emits an HTMX 4 <code>hx-config</code> with a timeout, cache mode, and redirect mode for this report preview.</p>
1818
</div>
1919

2020
<div class="demo-actions">
2121
<button hx-get
2222
hx-page="/Examples/RequestOptions"
2323
hx-page-handler="Preview"
2424
hx-request-timeout="2000"
25+
hx-request-cache="@HtmxRequestCache.NoCache"
26+
hx-request-redirect="@HtmxRequestRedirect.Error"
2527
hx-target="#preview-result">
2628
Generate report preview
2729
</button>

‎src/Ramstack.HtmxToolkit/Configuration/HtmxV1Config.cs‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,6 @@ public bool? UseTemplateFragments
182182
set => SetField(ref field, value);
183183
}
184184

185-
/// <summary>
186-
/// Gets or sets the WebSocket reconnection delay strategy.
187-
/// The HTMX default is <c>full-jitter</c>.
188-
/// </summary>
189-
public string? WsReconnectDelay
190-
{
191-
get;
192-
set => SetField(ref field, value);
193-
}
194-
195185
/// <summary>
196186
/// Gets or sets the type of binary data received over WebSocket connections.
197187
/// The HTMX default is <see cref="HtmxBinaryType.Blob" />.

‎src/Ramstack.HtmxToolkit/Configuration/HtmxV2Config.cs‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,6 @@ public string[]? AttributesToSettle
182182
set => SetField(ref field, value);
183183
}
184184

185-
/// <summary>
186-
/// Gets or sets the WebSocket reconnection delay strategy.
187-
/// The HTMX default is <c>full-jitter</c>.
188-
/// </summary>
189-
/// <remarks>Requires the HTMX WebSocket extension.</remarks>
190-
public string? WsReconnectDelay
191-
{
192-
get;
193-
set => SetField(ref field, value);
194-
}
195-
196185
/// <summary>
197186
/// Gets or sets the type of binary data received over WebSocket connections.
198187
/// The HTMX default is <see cref="HtmxBinaryType.Blob" />.

‎src/Ramstack.HtmxToolkit/Configuration/HtmxV4Config.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,13 @@ public string? InlineScriptNonce
121121
}
122122

123123
/// <summary>
124-
/// Gets or sets a comma-separated list of extensions that HTMX is allowed to load.
125-
/// The HTMX default is an empty string.
124+
/// Gets or sets a comma-separated list of extensions that HTMX is allowed to register.
125+
/// The HTMX default is an empty string, which allows all extensions.
126126
/// </summary>
127+
/// <remarks>
128+
/// HTMX compares extension names exactly. Whitespace is allowed only after a comma;
129+
/// any other whitespace becomes part of a name, so <c>"sse ,ws"</c> does not allow <c>sse</c>.
130+
/// </remarks>
127131
public string? Extensions
128132
{
129133
get;

‎src/Ramstack.HtmxToolkit/Configuration/ResponseHandlingConfig.cs‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using System.Text.Json.Serialization;
2+
3+
using Ramstack.HtmxToolkit.Internal;
4+
15
namespace Ramstack.HtmxToolkit.Configuration;
26

37
/// <summary>
@@ -37,7 +41,23 @@ public sealed class ResponseHandlingConfig
3741
public string? Target { get; set; }
3842

3943
/// <summary>
40-
/// Gets or sets an alternative swap mechanism for the response.
44+
/// Gets or sets an alternative swap style for the response.
45+
/// </summary>
46+
/// <remarks>
47+
/// Reads the style from <see cref="SwapOverrideExpression" />, or returns <see langword="null" />
48+
/// if the style is not recognized. Assigning a style replaces the complete expression,
49+
/// including any modifiers. Assigning <see langword="null" /> clears the override.
50+
/// </remarks>
51+
[JsonIgnore]
52+
public HtmxSwap? SwapOverride
53+
{
54+
get => EnumHelper.ParseHtmxSwap(SwapOverrideExpression);
55+
set => SwapOverrideExpression = value.GetSwapValue();
56+
}
57+
58+
/// <summary>
59+
/// Gets or sets the complete swap override expression, including any swap modifiers.
4160
/// </summary>
42-
public string? SwapOverride { get; set; }
61+
[JsonPropertyName("swapOverride")]
62+
public string? SwapOverrideExpression { get; set; }
4363
}

‎src/Ramstack.HtmxToolkit/HtmxLocationOptions.cs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Ramstack.HtmxToolkit.Internal;
2+
13
namespace Ramstack.HtmxToolkit;
24

35
/// <summary>
@@ -20,8 +22,25 @@ public sealed class HtmxLocationOptions
2022
/// <summary>
2123
/// Gets or sets how the response will be swapped relative to the target element.
2224
/// </summary>
25+
/// <remarks>
26+
/// <para>Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.</para>
27+
/// <para>
28+
/// Reads the style from <see cref="SwapExpression" />, or returns <see langword="null" />
29+
/// if the style is not recognized. Assigning a style replaces the complete expression,
30+
/// including any modifiers. Assigning <see langword="null" /> clears the swap.
31+
/// </para>
32+
/// </remarks>
33+
public HtmxSwap? Swap
34+
{
35+
get => EnumHelper.ParseHtmxSwap(SwapExpression);
36+
set => SwapExpression = value.GetSwapValue();
37+
}
38+
39+
/// <summary>
40+
/// Gets or sets the complete swap expression, including any swap modifiers.
41+
/// </summary>
2342
/// <remarks>Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.</remarks>
24-
public HtmxSwap? Swap { get; set; }
43+
public string? SwapExpression { get; set; }
2544

2645
/// <summary>
2746
/// Gets or sets the form field values to submit with the request.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Ramstack.HtmxToolkit;
2+
3+
/// <summary>
4+
/// Specifies the Fetch cache mode for an HTMX request.
5+
/// </summary>
6+
/// <remarks>Supported only in HTMX 4.x.</remarks>
7+
public enum HtmxRequestCache
8+
{
9+
/// <summary>
10+
/// Uses the browser's normal HTTP cache behavior.
11+
/// </summary>
12+
Default,
13+
14+
/// <summary>
15+
/// Bypasses the cache and does not store the response in it.
16+
/// </summary>
17+
NoStore,
18+
19+
/// <summary>
20+
/// Bypasses the cache lookup and updates the cache with the response.
21+
/// </summary>
22+
Reload,
23+
24+
/// <summary>
25+
/// Revalidates a cached response before using it.
26+
/// </summary>
27+
NoCache,
28+
29+
/// <summary>
30+
/// Uses a cached response even if stale, or fetches and caches a response on a cache miss.
31+
/// </summary>
32+
ForceCache,
33+
34+
/// <summary>
35+
/// Uses a cached response even if stale, and fails on a cache miss.
36+
/// </summary>
37+
/// <remarks>
38+
/// Requires the same-origin Fetch mode, which is the HTMX default.
39+
/// The request fails if <see cref="Configuration.HtmxV4Config.Mode" /> specifies another mode.
40+
/// </remarks>
41+
OnlyIfCached
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace Ramstack.HtmxToolkit;
2+
3+
/// <summary>
4+
/// Specifies the Fetch redirect mode for an HTMX request.
5+
/// </summary>
6+
/// <remarks>Supported only in HTMX 4.x.</remarks>
7+
public enum HtmxRequestRedirect
8+
{
9+
/// <summary>
10+
/// Follows redirects automatically.
11+
/// </summary>
12+
Follow,
13+
14+
/// <summary>
15+
/// Fails the request when a redirect is encountered.
16+
/// </summary>
17+
/// <remarks>
18+
/// HTMX raises the <c>htmx:error</c> event and does not swap the response.
19+
/// </remarks>
20+
Error,
21+
22+
/// <summary>
23+
/// Returns an opaque redirect response instead of following the redirect.
24+
/// </summary>
25+
/// <remarks>
26+
/// The response has status 0, and its headers and body are not accessible.
27+
/// HTMX cannot read <c>HX-*</c> response headers, does not treat the response as an error,
28+
/// and swaps the empty body into the target.
29+
/// </remarks>
30+
Manual
31+
}

‎src/Ramstack.HtmxToolkit/HtmxResponse.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ public HtmxResponse Reselect(string value) =>
197197
SetHeader(this, HtmxResponseHeaderNames.Reselect, value);
198198

199199
/// <summary>
200-
/// Adds a client-side event to the response header selected by <paramref name="trigger" />.
200+
/// Adds a client-side event to the response header selected by <paramref name="timing" />.
201201
/// </summary>
202202
/// <param name="eventName">The event name to trigger.</param>
203-
/// <param name="trigger">The event timing. Defaults to <see cref="HtmxTriggerTiming.Receive" />.</param>
203+
/// <param name="timing">The event timing. Defaults to <see cref="HtmxTriggerTiming.Receive" />.</param>
204204
/// <returns>
205205
/// The current <see cref="HtmxResponse" /> instance.
206206
/// </returns>
@@ -209,8 +209,8 @@ public HtmxResponse Reselect(string value) =>
209209
/// <c>HX-Trigger</c> and runs when the request completes (after the swap whenever one is performed).
210210
/// See <see href="https://github.com/bigskysoftware/htmx/pull/3900">PR #3900</see>.
211211
/// </remarks>
212-
public HtmxResponse TriggerEvent(string eventName, HtmxTriggerTiming trigger = HtmxTriggerTiming.Receive) =>
213-
AddToPendingEvent(this, eventName, "{}", trigger);
212+
public HtmxResponse TriggerEvent(string eventName, HtmxTriggerTiming timing = HtmxTriggerTiming.Receive) =>
213+
AddToPendingEvent(this, eventName, "{}", timing);
214214

215215
/// <summary>
216216
/// Adds a client-side event and its detail to the response header selected by

‎src/Ramstack.HtmxToolkit/Serialization/HtmxLocationOptionsJsonView.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ internal readonly struct HtmxLocationOptionsJsonView(string path, HtmxLocationOp
2525
public string? Target => options.Target;
2626

2727
/// <summary>
28-
/// Gets the serialized swap style.
28+
/// Gets the swap expression.
2929
/// </summary>
30-
[JsonConverter(typeof(HtmxSwapJsonConverter))]
31-
public HtmxSwap? Swap => options.Swap;
30+
public string? Swap => options.SwapExpression;
3231

3332
/// <summary>
3433
/// Gets the form field values.

0 commit comments

Comments
 (0)