From a00888613526d6bf9239d7ebac54a3ccf48ce558 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 12 Aug 2026 00:49:10 +0500 Subject: [PATCH] Replace raw strings with HttpVerb enum --- .../Pages/Shared/_Layout.cshtml | 1 + src/Ramstack.HtmxToolkit/HttpVerb.cs | 52 +++++++++++ .../Internal/EnumHelper.cs | 23 +++++ .../Internal/HttpVerbArray.cs | 89 +++++++++++++++++++ .../TagHelpers/HtmxConfigTagHelper.cs | 16 ++-- 5 files changed, 170 insertions(+), 11 deletions(-) create mode 100644 src/Ramstack.HtmxToolkit/HttpVerb.cs create mode 100644 src/Ramstack.HtmxToolkit/Internal/HttpVerbArray.cs diff --git a/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml b/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml index fb0b84e..d77b166 100644 --- a/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml +++ b/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml @@ -8,6 +8,7 @@ diff --git a/src/Ramstack.HtmxToolkit/HttpVerb.cs b/src/Ramstack.HtmxToolkit/HttpVerb.cs new file mode 100644 index 0000000..4e68ca0 --- /dev/null +++ b/src/Ramstack.HtmxToolkit/HttpVerb.cs @@ -0,0 +1,52 @@ +namespace Ramstack.HtmxToolkit; + +/// +/// Defines the HTTP methods that can be used in htmx configuration. +/// +public enum HttpVerb +{ + /// + /// The GET method requests a representation of the specified resource. + /// + Get, + + /// + /// The HEAD method asks for a response identical to a GET request, but without the response body. + /// + Head, + + /// + /// The POST method submits an entity to the specified resource. + /// + Post, + + /// + /// The PUT method replaces all current representations of the target resource with the request payload. + /// + Put, + + /// + /// The DELETE method deletes the specified resource. + /// + Delete, + + /// + /// The CONNECT method establishes a tunnel to the server identified by the target resource. + /// + Connect, + + /// + /// The OPTIONS method describes the communication options for the target resource. + /// + Options, + + /// + /// The TRACE method performs a message loop-back test along the path to the target resource. + /// + Trace, + + /// + /// The PATCH method applies partial modifications to a resource. + /// + Patch +} diff --git a/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs b/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs index 10de98e..f0983d4 100644 --- a/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs +++ b/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs @@ -67,6 +67,29 @@ public static string GetSwapValue(this HtmxSwap value) }; } + /// + /// Converts a value to its corresponding lowercase HTTP method string. + /// + /// The value. + /// + /// The lowercase string representation, such as "get", "post", "delete", etc. + /// + 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" + }; + } + /// /// Parses a string into a value. /// diff --git a/src/Ramstack.HtmxToolkit/Internal/HttpVerbArray.cs b/src/Ramstack.HtmxToolkit/Internal/HttpVerbArray.cs new file mode 100644 index 0000000..fbec4a0 --- /dev/null +++ b/src/Ramstack.HtmxToolkit/Internal/HttpVerbArray.cs @@ -0,0 +1,89 @@ +using System.Collections; +using System.Runtime.CompilerServices; + +namespace Ramstack.HtmxToolkit.Internal; + +/// +/// A lightweight wrapper over an array of values +/// that implements to lazily produce lowercase +/// HTTP method strings without allocating an intermediate array. +/// +internal readonly struct HttpVerbArray : IEnumerable +{ + /// + /// Gets the underlying array of values. + /// + public HttpVerb[]? Values { get; } + + /// + /// Initializes a new instance of the struct. + /// + /// The array of values, or . + public HttpVerbArray(HttpVerb[]? values) => + Values = values; + + /// + /// Returns an enumerator that iterates through the collection. + /// + /// + /// An enumerator that can be used to iterate through the collection. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Enumerator GetEnumerator() => + new(Values); + + /// + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + /// + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + /// + /// Represents an enumerator that lazily converts values + /// to lowercase string representations. + /// + public struct Enumerator : IEnumerator + { + private readonly HttpVerb[] _verbs; + private int _index; + + /// + public string Current + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _verbs[_index].GetHttpVerbValue(); + } + + /// + /// Initializes a new instance of the structure. + /// + /// The array of values, or . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal Enumerator(HttpVerb[]? verbs) + { + _verbs = verbs ?? []; + _index = -1; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool MoveNext() + { + _index++; + return (uint)_index < (uint)_verbs.Length; + } + + object IEnumerator.Current => Current; + + /// + public void Reset() => + _index = -1; + + /// + public void Dispose() + { + } + } +} diff --git a/src/Ramstack.HtmxToolkit/TagHelpers/HtmxConfigTagHelper.cs b/src/Ramstack.HtmxToolkit/TagHelpers/HtmxConfigTagHelper.cs index 56bcfcf..763834f 100644 --- a/src/Ramstack.HtmxToolkit/TagHelpers/HtmxConfigTagHelper.cs +++ b/src/Ramstack.HtmxToolkit/TagHelpers/HtmxConfigTagHelper.cs @@ -369,18 +369,12 @@ public bool? GlobalViewTransitions /// Gets or sets a list of HTTP methods that use URL parameters. /// Defaults to ["get"] in HTMX 1.x and ["get", "delete"] in HTMX 2.x. /// - /// - /// Supported in HTMX 1.x and 2.x. - /// - /// Allowed values: "get", "head", "post", "put", - /// "delete", "connect", "options", "trace", "patch". - /// - /// + /// Supported in HTMX 1.x and 2.x. [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); } /// @@ -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; }