Skip to content

Commit

Permalink
feat: Body is not returned when statuscode is 204 or EmptyBody() requ…
Browse files Browse the repository at this point in the history
…ested through HtmxResponse object
  • Loading branch information
egil committed Apr 20, 2024
1 parent c9dd6e4 commit c020977
Show file tree
Hide file tree
Showing 28 changed files with 422 additions and 440 deletions.
3 changes: 1 addition & 2 deletions src/Htmxor/Antiforgery/HtmxAntiforgeryMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Htmxor.Configuration;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Http;

namespace Htmxor.Antiforgery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;

namespace Htmxor.Configuration;
namespace Htmxor.Components;

/// <summary>
/// This component will render a meta tag with the serialized <see cref="HtmxConfig"/> object,
/// enabling customization of Htmx.
/// </summary>
/// <remarks>
/// Configure the <see cref="HtmxConfig"/> via the
/// <see cref="HtmxorApplicationBuilderExtensions.AddHtmx(IRazorComponentsBuilder, Action{Htmxor.Configuration.HtmxConfig}?)"/>
/// <see cref="HtmxorApplicationBuilderExtensions.AddHtmx(IRazorComponentsBuilder, Action{Htmxor.HtmxConfig}?)"/>
/// method.
/// </remarks>
public class HtmxConfigHeadOutlet : IComponent
Expand All @@ -21,7 +21,7 @@ public class HtmxConfigHeadOutlet : IComponent
/// <inheritdoc/>
public void Attach(RenderHandle renderHandle)
{
var json = JsonSerializer.Serialize(Config, HtmxConfigJsonSerializerContext.Default.HtmxConfig);
var json = JsonSerializer.Serialize(Config, HtmxJsonSerializerContext.Default.HtmxConfig);
renderHandle.Render(builder =>
{
builder.AddMarkupContent(0, @$"<meta name=""htmx-config"" content='{json}'>");
Expand Down
3 changes: 2 additions & 1 deletion src/Htmxor/Components/HtmxPartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Task SetParametersAsync(ParameterView parameters)
return Task.CompletedTask;
}

internal bool IsMatchingRequest(HtmxContext htmxContext) => true;
internal bool IsMatchingRequest(HtmxContext htmxContext)
=> true;
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Htmxor;
using Htmxor.Antiforgery;
using Htmxor.Builder;
using Htmxor.Configuration;
using Htmxor.DependencyInjection;
using Htmxor.Http;
using Htmxor.Rendering;
Expand Down
3 changes: 3 additions & 0 deletions src/Htmxor/EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Htmxor;

/// <summary>
/// Custom Htmxor Blazor event handlers.
/// </summary>
[EventHandler("onget", typeof(HtmxContextEventArgs), enableStopPropagation: false, enablePreventDefault: false)]
[EventHandler("onpost", typeof(HtmxContextEventArgs), enableStopPropagation: false, enablePreventDefault: false)]
[EventHandler("onput", typeof(HtmxContextEventArgs), enableStopPropagation: false, enablePreventDefault: false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
using Htmxor.Antiforgery;
using Htmxor.Configuration.Serialization;

namespace Htmxor.Configuration;
namespace Htmxor;

/// <summary>
/// Htmx configuration options.
/// </summary>
public record class HtmxConfig
{
/// <summary>
/// Default <see cref="JsonSerializerOptions"/> used with <see cref="HtmxConfig"/>.
/// </summary>
public readonly static JsonTypeInfo<HtmxConfig> JsonTypeInfo = HtmxConfigJsonSerializerContext.Default.HtmxConfig;

/// <summary>
/// Defaults to <see langword="true" /> if this property is null. really only useful for testing
/// </summary>
Expand Down Expand Up @@ -217,6 +212,27 @@ public record class HtmxConfig
[JsonPropertyName("scrollIntoViewOnBoost")]
public bool? ScrollIntoViewOnBoost { get; set; }

//// V2 feature
//[JsonPropertyName("responseHandling")]
//public IList<ResponseHandlingRule>? ResponseHandling { get; set; }

[JsonInclude, JsonPropertyName("antiforgery")]
internal HtmxAntiforgeryOptions? Antiforgery { get; init; }
}

//public record class ResponseHandlingRule
//{
// public required string Code { get; set; }
//
// public bool Swap { get; set; }
//
// public bool? Error { get; set; }
//
// public bool? IgnoreTitle { get; set; }
//
// public string? Select { get; set; }
//
// public string? Target { get; set; }
//
// public string? SwapOverride { get; set; }
//}
2 changes: 0 additions & 2 deletions src/Htmxor/HtmxContextEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ public HtmxContextEventArgs(HtmxContext context)
Response = context.Response;
this.context = context;
}

public void NoContentResponse() => context.NoContentResponse();
}
63 changes: 63 additions & 0 deletions src/Htmxor/Http/AjaxContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Htmxor.Http;

/// <summary>
/// Represents a location event with various properties.
/// </summary>
public class AjaxContext
{
/// <summary>
/// Gets or sets the source element of the request.
/// </summary>
[JsonPropertyName("source")]
public string? Source { get; set; }

/// <summary>
/// Gets or sets an event that "triggered" the request.
/// </summary>
[JsonPropertyName("event")]
public string? Event { get; set; }

/// <summary>
/// Gets or sets a callback that will handle the response HTML.
/// </summary>
[JsonPropertyName("handler")]
public string? Handler { get; set; }

/// <summary>
/// Gets or sets the target to swap the response into.
/// </summary>
[JsonPropertyName("target")]
public string? Target { get; set; }

/// <summary>
/// Gets or sets how the response will be swapped in relative to the target.
/// </summary>
[JsonPropertyName("swap")]
public SwapStyle? Swap { get; set; }

/// <summary>
/// Gets or sets values to submit with the request.
/// </summary>
[JsonPropertyName("values")]
public string? Values { get; set; }

/// <summary>
/// Gets or sets headers to submit with the request.
/// </summary>
[JsonPropertyName("headers")]
public string? Headers { get; set; }

/// <summary>
/// Gets or sets allows you to select the content you want swapped from a response.
/// </summary>
[JsonPropertyName("select")]
public string? Select { get; set; }
}

7 changes: 1 addition & 6 deletions src/Htmxor/Http/HtmxContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ namespace Htmxor.Http;

public class HtmxContext
{
internal bool NoContentResponseRequested { get; private set; }

public HtmxRequest Request { get; }

public HtmxResponse Response { get; }

public HtmxContext(HttpContext context)
{
Request = new HtmxRequest(context);
Response = new HtmxResponse();
Response = new HtmxResponse(context);
}

public void NoContentResponse()
=> NoContentResponseRequested = true;
}
Loading

0 comments on commit c020977

Please sign in to comment.