Skip to content

Commit 5e8b5be

Browse files
committed
Merge branch 'main' into custom-json-serializer
# Conflicts: # InertiaCore/Response.cs # InertiaCore/ResponseFactory.cs # README.md
2 parents 4a6f291 + 81aa149 commit 5e8b5be

27 files changed

Lines changed: 834 additions & 166 deletions

.github/workflows/dotnet.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ name: .NET
55

66
on:
77
push:
8-
branches: [ "main" ]
8+
branches:
9+
- "main"
10+
- "v1"
911
pull_request:
10-
branches: [ "main" ]
12+
branches:
13+
- "main"
14+
- "v1"
1115

1216
jobs:
1317
build:
@@ -19,7 +23,11 @@ jobs:
1923
- name: Setup .NET
2024
uses: actions/setup-dotnet@v3
2125
with:
22-
dotnet-version: 9.0.x
26+
dotnet-version: |
27+
6.0.x
28+
7.0.x
29+
8.0.x
30+
9.0.x
2331
- name: Restore dependencies
2432
run: dotnet restore
2533
- name: Build

InertiaCore/Extensions/Configure.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ public static IApplicationBuilder UseInertia(this IApplicationBuilder app)
1919
Inertia.UseFactory(factory);
2020

2121
var viteBuilder = app.ApplicationServices.GetService<IViteBuilder>();
22-
if (viteBuilder != null) Vite.UseBuilder(viteBuilder);
22+
if (viteBuilder != null)
23+
{
24+
Vite.UseBuilder(viteBuilder);
25+
Inertia.Version(Vite.GetManifestHash);
26+
}
2327

2428
app.Use(async (context, next) =>
2529
{
2630
if (context.IsInertiaRequest()
2731
&& context.Request.Method == "GET"
28-
&& context.Request.Headers["X-Inertia-Version"] != Inertia.GetVersion())
32+
&& context.Request.Headers[InertiaHeader.Version] != Inertia.GetVersion())
2933
{
3034
await OnVersionChange(context, app);
3135
return;
@@ -80,7 +84,7 @@ private static async Task OnVersionChange(HttpContext context, IApplicationBuild
8084

8185
if (tempData.Any()) tempData.Keep();
8286

83-
context.Response.Headers.Override("X-Inertia-Location", context.RequestedUri());
87+
context.Response.Headers.Override(InertiaHeader.Location, context.RequestedUri());
8488
context.Response.StatusCode = (int)HttpStatusCode.Conflict;
8589

8690
await context.Response.CompleteAsync();
Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
11
using System.Text.Json;
2+
using InertiaCore.Utils;
23
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Http.Extensions;
45
using Microsoft.AspNetCore.Mvc;
6+
using System.Text;
57

68
namespace InertiaCore.Extensions;
79

810
internal static class InertiaExtensions
911
{
10-
internal static IEnumerable<string> Only(this object obj, IEnumerable<string> only) =>
11-
obj.GetType().GetProperties().Select(c => c.Name)
12-
.Intersect(only, StringComparer.OrdinalIgnoreCase).ToList();
13-
14-
internal static List<string> GetPartialData(this ActionContext context) =>
15-
context.HttpContext.Request.Headers["X-Inertia-Partial-Data"]
16-
.FirstOrDefault()?.Split(",")
17-
.Where(s => !string.IsNullOrEmpty(s))
18-
.ToList() ?? new List<string>();
19-
2012
internal static bool IsInertiaPartialComponent(this ActionContext context, string component) =>
21-
context.HttpContext.Request.Headers["X-Inertia-Partial-Component"] == component;
13+
context.HttpContext.Request.Headers[InertiaHeader.PartialComponent] == component;
2214

2315
internal static string RequestedUri(this HttpContext context) =>
2416
Uri.UnescapeDataString(context.Request.GetEncodedPathAndQuery());
2517

2618
internal static string RequestedUri(this ActionContext context) => context.HttpContext.RequestedUri();
2719

2820
internal static bool IsInertiaRequest(this HttpContext context) =>
29-
bool.TryParse(context.Request.Headers["X-Inertia"], out _);
21+
bool.TryParse(context.Request.Headers[InertiaHeader.Inertia], out _);
3022

3123
internal static bool IsInertiaRequest(this ActionContext context) => context.HttpContext.IsInertiaRequest();
3224

3325
internal static string ToCamelCase(this string s) => JsonNamingPolicy.CamelCase.ConvertName(s);
3426

3527
internal static bool Override<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
3628
{
37-
if (dictionary.ContainsKey(key))
38-
{
39-
dictionary[key] = value;
40-
return true;
41-
}
42-
43-
dictionary.Add(key, value);
44-
return false;
29+
if (dictionary.TryAdd(key, value)) return false;
30+
dictionary[key] = value;
31+
32+
return true;
33+
}
34+
35+
internal static Task<object?> ResolveAsync(this Func<object?> func)
36+
{
37+
var rt = func.Method.ReturnType;
38+
39+
if (!rt.IsGenericType || rt.GetGenericTypeDefinition() != typeof(Task<>))
40+
return Task.Run(func.Invoke);
41+
42+
var task = func.DynamicInvoke() as Task;
43+
return task!.ResolveResult();
44+
}
45+
46+
internal static async Task<object?> ResolveResult(this Task task)
47+
{
48+
await task.ConfigureAwait(false);
49+
var result = task.GetType().GetProperty("Result");
50+
51+
return result?.GetValue(task);
52+
}
53+
54+
internal static string MD5(this string s)
55+
{
56+
using var md5 = System.Security.Cryptography.MD5.Create();
57+
var inputBytes = Encoding.UTF8.GetBytes(s);
58+
var hashBytes = md5.ComputeHash(inputBytes);
59+
60+
var sb = new StringBuilder();
61+
foreach (var t in hashBytes)
62+
sb.Append(t.ToString("x2"));
63+
64+
return sb.ToString();
4565
}
4666
}

InertiaCore/Inertia.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using InertiaCore.Props;
23
using InertiaCore.Utils;
34
using Microsoft.AspNetCore.Html;
45

@@ -18,7 +19,9 @@ public static class Inertia
1819

1920
public static Task<IHtmlContent> Html(dynamic model) => _factory.Html(model);
2021

21-
public static void Version(object? version) => _factory.Version(version);
22+
public static void Version(string? version) => _factory.Version(version);
23+
24+
public static void Version(Func<string?> version) => _factory.Version(version);
2225

2326
public static string? GetVersion() => _factory.GetVersion();
2427

@@ -28,5 +31,13 @@ public static class Inertia
2831

2932
public static void Share(IDictionary<string, object?> data) => _factory.Share(data);
3033

34+
public static AlwaysProp Always(string value) => _factory.Always(value);
35+
36+
public static AlwaysProp Always(Func<string> callback) => _factory.Always(callback);
37+
38+
public static AlwaysProp Always(Func<Task<object?>> callback) => _factory.Always(callback);
39+
3140
public static LazyProp Lazy(Func<object?> callback) => _factory.Lazy(callback);
41+
42+
public static LazyProp Lazy(Func<Task<object?>> callback) => _factory.Lazy(callback);
3243
}

InertiaCore/Models/InertiaOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public class InertiaOptions
66

77
public bool SsrEnabled { get; set; } = false;
88
public string SsrUrl { get; set; } = "http://127.0.0.1:13714/render";
9+
public bool EncryptHistory { get; set; } = false;
910
}

InertiaCore/Models/Page.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ internal class Page
66
public string Component { get; set; } = default!;
77
public string? Version { get; set; }
88
public string Url { get; set; } = default!;
9+
public bool EncryptHistory { get; set; } = false;
10+
public bool ClearHistory { get; set; } = false;
911
}

InertiaCore/Props/AlwaysProp.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace InertiaCore.Props;
2+
3+
public class AlwaysProp : InvokableProp
4+
{
5+
internal AlwaysProp(object? value) : base(value)
6+
{
7+
}
8+
9+
internal AlwaysProp(Func<object?> value) : base(value)
10+
{
11+
}
12+
13+
internal AlwaysProp(Func<Task<object?>> value) : base(value)
14+
{
15+
}
16+
}

InertiaCore/Props/InvokableProp.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using InertiaCore.Extensions;
2+
3+
namespace InertiaCore.Props;
4+
5+
public class InvokableProp
6+
{
7+
private readonly object? _value;
8+
9+
protected InvokableProp(object? value) => _value = value;
10+
11+
internal Task<object?> Invoke()
12+
{
13+
return _value switch
14+
{
15+
Func<object?> f => f.ResolveAsync(),
16+
Task t => t.ResolveResult(),
17+
InvokableProp p => p.Invoke(),
18+
_ => Task.FromResult(_value)
19+
};
20+
}
21+
}

InertiaCore/Props/LazyProp.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace InertiaCore.Props;
2+
3+
public class LazyProp : InvokableProp
4+
{
5+
internal LazyProp(Func<object?> value) : base(value)
6+
{
7+
}
8+
9+
internal LazyProp(Func<Task<object?>> value) : base(value)
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)