Skip to content

Commit 796c96c

Browse files
committed
Added feature: History encryption
1 parent 2f606cc commit 796c96c

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

InertiaNetCore/Inertia.cs

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static class Inertia
3535
public static void Share(InertiaProps data) => _factory.Share(data);
3636

3737
public static void Flash(string key, string? value) => _factory.Flash(key, value);
38+
39+
public static void EnableEncryptHistory(bool enable = true) => _factory.EnableEncryptHistory(enable);
40+
public static void ClearHistory() => _factory.ClearHistory();
3841

3942
public static LazyProp<T> Lazy<T>(Func<T?> callback) => _factory.Lazy(callback);
4043
public static LazyProp<T> Lazy<T>(Func<Task<T?>> callback) => _factory.Lazy(callback);

InertiaNetCore/Models/InertiaOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class InertiaOptions
1313

1414
public Action<SessionOptions> ConfigureSession { get; set; } = _ => { };
1515

16+
public bool EncryptHistory { get; set; }
17+
1618
private static JsonSerializerOptions DefaultJsonSerializerOptions { get; } = new()
1719
{
1820
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,

InertiaNetCore/Models/InertiaPage.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public readonly record struct InertiaPage
88
public required string Component { get; init; }
99
public required string? Version { get; init; }
1010
public required string Url { get; init; }
11+
public required bool EncryptHistory { get; init; }
12+
public required bool ClearHistory { get; init; }
1113
}

InertiaNetCore/Response.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
namespace InertiaNetCore;
1111

12-
public class Response(string component, InertiaProps props, string? version, InertiaOptions options)
12+
public class Response(string component, InertiaProps props, string? version, InertiaOptions options, bool? encryptHistory, bool clearHistory)
1313
: IActionResult
1414
{
1515
private IDictionary<string, object>? _viewData;
16+
private readonly InertiaOptions _options = options;
1617

1718
public async Task ExecuteResultAsync(ActionContext context)
1819
{
@@ -23,7 +24,9 @@ public async Task ExecuteResultAsync(ActionContext context)
2324
Url = context.HttpContext.RequestedUri(),
2425
Props = await GetFinalProps(context),
2526
DeferredProps = GetDeferredProps(context),
26-
MergeProps = GetMergeProps(context)
27+
MergeProps = GetMergeProps(context),
28+
ClearHistory = clearHistory,
29+
EncryptHistory = encryptHistory ?? options.EncryptHistory
2730
};
2831

2932
if (!context.HttpContext.IsInertiaRequest())
@@ -39,15 +42,15 @@ public async Task ExecuteResultAsync(ActionContext context)
3942
viewData[key] = value;
4043
}
4144

42-
await new ViewResult { ViewName = options.RootView, ViewData = viewData }.ExecuteResultAsync(context);
45+
await new ViewResult { ViewName = _options.RootView, ViewData = viewData }.ExecuteResultAsync(context);
4346
}
4447
else
4548
{
4649
context.HttpContext.Response.Headers.Append("X-Inertia", "true");
4750
context.HttpContext.Response.Headers.Append("Vary", "Accept");
4851
context.HttpContext.Response.StatusCode = 200;
4952

50-
var jsonResult = new JsonResult(page, options.JsonSerializerOptions);
53+
var jsonResult = new JsonResult(page, _options.JsonSerializerOptions);
5154
await jsonResult.ExecuteResultAsync(context);
5255
}
5356
}
@@ -89,7 +92,7 @@ private Dictionary<string, List<string>> GetDeferredProps(ActionContext context)
8992
}
9093

9194
// apply json serialization options to dictionary keys before grouping them
92-
var jsonOptions = options.JsonSerializerOptions as JsonSerializerOptions;
95+
var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions;
9396
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions);
9497

9598
return tmp!
@@ -113,7 +116,7 @@ private List<string> GetMergeProps(ActionContext context)
113116
}
114117

115118
// apply json serialization options to dictionary keys before grouping them
116-
var jsonOptions = options.JsonSerializerOptions as JsonSerializerOptions;
119+
var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions;
117120
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions);
118121

119122
return tmp!.Select(prop => prop.Key).ToList();

InertiaNetCore/ResponseFactory.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ namespace InertiaNetCore;
1414
internal class ResponseFactory(IHttpContextAccessor contextAccessor, SsrGateway ssrGateway, IOptions<InertiaOptions> options)
1515
{
1616
private object? _version;
17+
private bool? _encryptHistory;
18+
private bool _clearHistory;
1719

1820
public Response Render(string component, InertiaProps? props = default)
1921
{
2022
props ??= [];
2123

22-
return new Response(component, props, GetVersion(), options.Value);
24+
return new Response(component, props, GetVersion(), options.Value, _encryptHistory, _clearHistory);
2325
}
2426

2527
public async Task<IHtmlContent> Head(dynamic model)
@@ -111,6 +113,16 @@ public void Flash(string key, string? value)
111113
context.Features.Set(flash);
112114
}
113115

116+
public void EnableEncryptHistory(bool enable = true)
117+
{
118+
_encryptHistory = enable;
119+
}
120+
121+
public void ClearHistory()
122+
{
123+
_clearHistory = true;
124+
}
125+
114126
public LazyProp<T> Lazy<T>(Func<T?> callback) => new(callback);
115127
public LazyProp<T> Lazy<T>(Func<Task<T?>> callback) => new(callback);
116128
public DeferredProp<T> Defer<T>(Func<T?> callback, string? group) => new(callback, group);

0 commit comments

Comments
 (0)