Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/articles/configuration-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ builder.Services.AddHtmxToolkit(options =>
{
config.History = HtmxHistoryMode.Enabled;
config.DefaultTimeout = 10_000;
config.Mode = HtmxFetchMode.SameOrigin;
config.Mode = HtmxRequestMode.SameOrigin;
config.Transitions = true;
config.NoSwap = ["204", "304", "4xx", "5xx"];
});
Expand Down Expand Up @@ -49,6 +49,7 @@ no `HX-*` headers or `hx-headers` values, and no antiforgery token sent in a hea
and `[HtmxRequest]` does not match. A cross-origin response is opaque, so HTMX cannot read its headers
and swaps an empty body into the target.

The mode applies to all requests: HTMX ignores a `mode` set on an element with `hx-config`.
Changing this setting does not configure CORS on the ASP.NET Core server.

## History behavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ namespace Ramstack.HtmxToolkit.Configuration;
/// Specifies the request mode used by HTMX.
/// </summary>
/// <remarks>
/// <para>In HTMX 4.x, this is passed as the <c>mode</c> option of the Fetch API.</para>
/// <para>
/// In HTMX 4.x, this is passed as the <c>mode</c> option of the Fetch API. The mode applies to all requests:
/// HTMX ignores a <c>mode</c> set on an element, so it is configured only with <see cref="HtmxV4Config.Mode" />.
/// </para>
/// <para>
/// HTMX 1.9.x and 2.x use <c>XMLHttpRequest</c> instead of <c>fetch</c>.
/// Use <see cref="HtmxV1Config.SelfRequestsOnly" /> or <see cref="HtmxV2Config.SelfRequestsOnly" />
/// to restrict requests to the current origin.
/// </para>
/// </remarks>
public enum HtmxFetchMode
public enum HtmxRequestMode
{
/// <summary>
/// Allows requests only to the current origin.
Expand Down
6 changes: 3 additions & 3 deletions src/Ramstack.HtmxToolkit/Configuration/HtmxV4Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ public int? DefaultTimeout

/// <summary>
/// Gets or sets the request mode passed to the Fetch API.
/// The HTMX default is <see cref="HtmxFetchMode.SameOrigin" />.
/// The HTMX default is <see cref="HtmxRequestMode.SameOrigin" />.
/// </summary>
[JsonConverter(typeof(HtmxFetchModeJsonConverter))]
public HtmxFetchMode? Mode
[JsonConverter(typeof(HtmxRequestModeJsonConverter))]
public HtmxRequestMode? Mode
{
get;
set => SetField(ref field, value);
Expand Down
28 changes: 14 additions & 14 deletions src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,43 +136,43 @@ or HtmxHttpVerb.Trace
}

/// <summary>
/// Converts a <see cref="HtmxFetchMode" /> value
/// Converts a <see cref="HtmxRequestMode" /> value
/// to the corresponding Fetch API request mode string.
/// </summary>
/// <param name="value">The <see cref="HtmxFetchMode" /> value.</param>
/// <param name="value">The <see cref="HtmxRequestMode" /> value.</param>
/// <returns>
/// <c>same-origin</c>, <c>cors</c>, or <c>no-cors</c>.
/// </returns>
public static string GetFetchModeValue(this HtmxFetchMode value)
public static string GetRequestModeValue(this HtmxRequestMode value)
{
Debug.Assert(value
is HtmxFetchMode.SameOrigin
or HtmxFetchMode.Cors
or HtmxFetchMode.NoCors);
is HtmxRequestMode.SameOrigin
or HtmxRequestMode.Cors
or HtmxRequestMode.NoCors);

return value switch
{
HtmxFetchMode.SameOrigin => "same-origin",
HtmxFetchMode.Cors => "cors",
HtmxRequestMode.SameOrigin => "same-origin",
HtmxRequestMode.Cors => "cors",
_ => "no-cors"
};
}

/// <summary>
/// Parses a string into a <see cref="HtmxFetchMode" /> value.
/// Parses a string into a <see cref="HtmxRequestMode" /> value.
/// </summary>
/// <param name="expression">The string to parse.</param>
/// <returns>
/// The parsed <see cref="HtmxFetchMode" /> value if successful;
/// The parsed <see cref="HtmxRequestMode" /> value if successful;
/// otherwise, <see langword="null" />.
/// </returns>
public static HtmxFetchMode? ParseHtmxFetchMode(string? expression)
public static HtmxRequestMode? ParseHtmxRequestMode(string? expression)
{
return expression switch
{
"same-origin" => HtmxFetchMode.SameOrigin,
"cors" => HtmxFetchMode.Cors,
"no-cors" => HtmxFetchMode.NoCors,
"same-origin" => HtmxRequestMode.SameOrigin,
"cors" => HtmxRequestMode.Cors,
"no-cors" => HtmxRequestMode.NoCors,
_ => null
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
namespace Ramstack.HtmxToolkit.Serialization;

/// <summary>
/// Represents a <see cref="JsonConverter{T}" /> for nullable <see cref="HtmxFetchMode" /> values.
/// Represents a <see cref="JsonConverter{T}" /> for nullable <see cref="HtmxRequestMode" /> values.
/// </summary>
internal sealed class HtmxFetchModeJsonConverter : JsonConverter<HtmxFetchMode?>
internal sealed class HtmxRequestModeJsonConverter : JsonConverter<HtmxRequestMode?>
{
/// <inheritdoc />
public override HtmxFetchMode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
public override HtmxRequestMode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
throw new NotSupportedException();

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, HtmxFetchMode? value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, HtmxRequestMode? value, JsonSerializerOptions options)
{
// NOTE: value is never null here: null-valued properties are omitted
// by JsonIgnoreCondition.WhenWritingNull before this converter is invoked.
writer.WriteStringValue(value.GetValueOrDefault().GetFetchModeValue());
writer.WriteStringValue(value.GetValueOrDefault().GetRequestModeValue());
}
}
10 changes: 5 additions & 5 deletions tests/Ramstack.HtmxToolkit.Tests/Internal/EnumHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public void GetWsBinaryTypeValue_ReturnsExpectedString(HtmxBinaryType value, str
public void GetScrollBehaviorValue_ReturnsExpectedString(HtmxScrollBehavior value, string expected) =>
Assert.That(value.GetScrollBehaviorValue(), Is.EqualTo(expected));

[TestCase(HtmxFetchMode.SameOrigin, "same-origin")]
[TestCase(HtmxFetchMode.Cors, "cors")]
[TestCase(HtmxFetchMode.NoCors, "no-cors")]
public void GetFetchModeValue_ReturnsExpectedString(HtmxFetchMode value, string expected) =>
Assert.That(value.GetFetchModeValue(), Is.EqualTo(expected));
[TestCase(HtmxRequestMode.SameOrigin, "same-origin")]
[TestCase(HtmxRequestMode.Cors, "cors")]
[TestCase(HtmxRequestMode.NoCors, "no-cors")]
public void GetRequestModeValue_ReturnsExpectedString(HtmxRequestMode value, string expected) =>
Assert.That(value.GetRequestModeValue(), Is.EqualTo(expected));
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public async Task ProcessAsync_SerializesOnlyHtmx4Options()
htmx.Extensions = "sse, ws";
htmx.ImplicitInheritance = false;
htmx.DefaultTimeout = 5000;
htmx.Mode = HtmxFetchMode.NoCors;
htmx.Mode = HtmxRequestMode.NoCors;
htmx.DefaultFocusScroll = true;
htmx.Transitions = true;
htmx.MorphIgnore = ["data-htmx-powered"];
Expand Down
Loading