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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<htmx-config
include-antiforgery-token="true"
methods-that-use-url-params="[HttpVerb.Get, HttpVerb.Delete]"
default-swap-style="HtmxSwap.InnerHtml">
<response-handling code="204" swap="false"/>
<response-handling code="422" swap="true"/>
Expand Down
52 changes: 52 additions & 0 deletions src/Ramstack.HtmxToolkit/HttpVerb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Ramstack.HtmxToolkit;

/// <summary>
/// Defines the HTTP methods that can be used in htmx configuration.
/// </summary>
public enum HttpVerb
{
/// <summary>
/// The GET method requests a representation of the specified resource.
/// </summary>
Get,

/// <summary>
/// The HEAD method asks for a response identical to a GET request, but without the response body.
/// </summary>
Head,

/// <summary>
/// The POST method submits an entity to the specified resource.
/// </summary>
Post,

/// <summary>
/// The PUT method replaces all current representations of the target resource with the request payload.
/// </summary>
Put,

/// <summary>
/// The DELETE method deletes the specified resource.
/// </summary>
Delete,

/// <summary>
/// The CONNECT method establishes a tunnel to the server identified by the target resource.
/// </summary>
Connect,

/// <summary>
/// The OPTIONS method describes the communication options for the target resource.
/// </summary>
Options,

/// <summary>
/// The TRACE method performs a message loop-back test along the path to the target resource.
/// </summary>
Trace,

/// <summary>
/// The PATCH method applies partial modifications to a resource.
/// </summary>
Patch
}
23 changes: 23 additions & 0 deletions src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ public static string GetSwapValue(this HtmxSwap value)
};
}

/// <summary>
/// Converts a <see cref="HttpVerb"/> value to its corresponding lowercase HTTP method string.
/// </summary>
/// <param name="value">The <see cref="HttpVerb"/> value.</param>
/// <returns>
/// The lowercase string representation, such as "get", "post", "delete", etc.
/// </returns>
public static string GetHttpVerbValue(this HttpVerb value)
{
return value switch
{
HttpVerb.Get => "get",
HttpVerb.Head => "head",
HttpVerb.Post => "post",
HttpVerb.Put => "put",
HttpVerb.Delete => "delete",
HttpVerb.Connect => "connect",
HttpVerb.Options => "options",
HttpVerb.Trace => "trace",
_ => "patch"
};
}

/// <summary>
/// Parses a string into a <see cref="HtmxSwap"/> value.
/// </summary>
Expand Down
89 changes: 89 additions & 0 deletions src/Ramstack.HtmxToolkit/Internal/HttpVerbArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections;
using System.Runtime.CompilerServices;

namespace Ramstack.HtmxToolkit.Internal;

/// <summary>
/// A lightweight wrapper over an array of <see cref="HttpVerb"/> values
/// that implements <see cref="IEnumerable{T}"/> to lazily produce lowercase
/// HTTP method strings without allocating an intermediate <see cref="string"/> array.
/// </summary>
internal readonly struct HttpVerbArray : IEnumerable<string>
{
/// <summary>
/// Gets the underlying array of <see cref="HttpVerb"/> values.
/// </summary>
public HttpVerb[]? Values { get; }

/// <summary>
/// Initializes a new instance of the <see cref="HttpVerbArray"/> struct.
/// </summary>
/// <param name="values">The array of <see cref="HttpVerb"/> values, or <see langword="null" />.</param>
public HttpVerbArray(HttpVerb[]? values) =>
Values = values;

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// An enumerator that can be used to iterate through the collection.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() =>
new(Values);

/// <inheritdoc />
IEnumerator<string> IEnumerable<string>.GetEnumerator() =>
GetEnumerator();

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();

/// <summary>
/// Represents an enumerator that lazily converts <see cref="HttpVerb"/> values
/// to lowercase string representations.
/// </summary>
public struct Enumerator : IEnumerator<string>
{
private readonly HttpVerb[] _verbs;
private int _index;

/// <inheritdoc />
public string Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _verbs[_index].GetHttpVerbValue();
}

/// <summary>
/// Initializes a new instance of the <see cref="Enumerator"/> structure.
/// </summary>
/// <param name="verbs">The array of <see cref="HttpVerb"/> values, or <see langword="null" />.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Enumerator(HttpVerb[]? verbs)
{
_verbs = verbs ?? [];
_index = -1;
}

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
_index++;
return (uint)_index < (uint)_verbs.Length;
}

object IEnumerator.Current => Current;

/// <inheritdoc />
public void Reset() =>
_index = -1;

/// <inheritdoc />
public void Dispose()
{
}
}
}
16 changes: 5 additions & 11 deletions src/Ramstack.HtmxToolkit/TagHelpers/HtmxConfigTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,12 @@ public bool? GlobalViewTransitions
/// Gets or sets a list of HTTP methods that use URL parameters.
/// Defaults to <c>["get"]</c> in HTMX 1.x and <c>["get", "delete"]</c> in HTMX 2.x.
/// </summary>
/// <remarks>
/// Supported in HTMX 1.x and 2.x.
/// <para>
/// Allowed values: <c>"get"</c>, <c>"head"</c>, <c>"post"</c>, <c>"put"</c>,
/// <c>"delete"</c>, <c>"connect"</c>, <c>"options"</c>, <c>"trace"</c>, <c>"patch"</c>.
/// </para>
/// </remarks>
/// <remarks>Supported in HTMX 1.x and 2.x.</remarks>
[HtmlAttributeName("methods-that-use-url-params")]
public string[]? MethodsThatUseUrlParams
public HttpVerb[]? MethodsThatUseUrlParams
{
get => _config.MethodsThatUseUrlParams;
set => _config.MethodsThatUseUrlParams = value;
get => _config.MethodsThatUseUrlParams.GetValueOrDefault().Values;
set => _config.MethodsThatUseUrlParams = new HttpVerbArray(value);
}

/// <summary>
Expand Down Expand Up @@ -566,7 +560,7 @@ internal sealed class HtmxConfigData
public bool? DefaultFocusScroll { get; set; }
public bool? GetCacheBusterParam { get; set; }
public bool? GlobalViewTransitions { get; set; }
public string[]? MethodsThatUseUrlParams { get; set; }
public HttpVerbArray? MethodsThatUseUrlParams { get; set; }
public bool? SelfRequestsOnly { get; set; }
public bool? IgnoreTitle { get; set; }
public bool? ScrollIntoViewOnBoost { get; set; }
Expand Down
Loading