Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1dc8b6b
added header constants
adrum Dec 14, 2024
3a33449
refactor resolve props
adrum Dec 14, 2024
c456148
added always prop
adrum Dec 14, 2024
7d0d3bf
fix some compile time warnings
adrum Dec 14, 2024
f9ad84b
added always prop test
adrum Dec 15, 2024
77473d6
added async task wrapper
adrum Dec 15, 2024
f64ca76
.net 8
adrum Dec 15, 2024
51fe030
restore header keys
adrum Dec 21, 2024
62a0864
added .net 9
adrum Dec 21, 2024
97b7c67
Merge branch 'fix/warnings' into feature/optional-prop
adrum Dec 21, 2024
2ab0765
added ignore first load
adrum Dec 21, 2024
bf08e03
add optional prop
adrum Dec 21, 2024
3e30450
added optional test
adrum Dec 21, 2024
3cacdaa
update version in actions
adrum Dec 21, 2024
1e18696
dont resolve Lazy and Optional Props until they are invoked
adrum Dec 21, 2024
bd5b6c1
Merge branch 'main' into fork/adrum/feature/optional-prop
kapi2289 Jan 6, 2025
66312a3
Merge branch 'main' into fork/adrum/feature/optional-prop
kapi2289 Jan 6, 2025
cf2c45e
Minor fixes and changes
kapi2289 Jan 6, 2025
42396a7
Add missing Inertia static methods
kapi2289 Jan 6, 2025
6b383a3
Merge branch 'main' into feature/optional-prop
adrum Jan 11, 2025
ea4592e
make optional prop invokable
adrum Jan 11, 2025
ad8c476
fix test sdks?
adrum Jan 11, 2025
a6a9fe0
Merge branch 'v1' into feature/optional-prop
adrum Feb 8, 2025
2ae0bff
revert formatting
adrum Feb 8, 2025
0e9353c
one more formatting fix
adrum Feb 8, 2025
702cdf8
Merge branch 'main' of ssh://github.com/kapi2289/InertiaCore into fea…
adrum Mar 2, 2025
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
4 changes: 4 additions & 0 deletions InertiaCore/Inertia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public static class Inertia
public static LazyProp Lazy(Func<object?> callback) => _factory.Lazy(callback);

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

public static OptionalProp Optional(Func<object?> callback) => _factory.Optional(callback);

public static OptionalProp Optional(Func<Task<object?>> callback) => _factory.Optional(callback);
}
4 changes: 3 additions & 1 deletion InertiaCore/Props/LazyProp.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using InertiaCore.Utils;

namespace InertiaCore.Props;

public class LazyProp : InvokableProp
public class LazyProp : InvokableProp, IIgnoresFirstLoad
{
internal LazyProp(Func<object?> value) : base(value)
{
Expand Down
14 changes: 14 additions & 0 deletions InertiaCore/Props/OptionalProp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using InertiaCore.Utils;

namespace InertiaCore.Props;

public class OptionalProp : InvokableProp, IIgnoresFirstLoad
{
internal OptionalProp(Func<object?> value) : base(value)
{
}

internal OptionalProp(Func<Task<object?>> value) : base(value)
{
}
}
2 changes: 1 addition & 1 deletion InertiaCore/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected internal async Task ProcessResponse()

if (!isPartial)
return props
.Where(kv => kv.Value is not LazyProp)
.Where(kv => kv.Value is not IIgnoresFirstLoad)
.ToDictionary(kv => kv.Key, kv => kv.Value);

props = props.ToDictionary(kv => kv.Key, kv => kv.Value);
Expand Down
4 changes: 4 additions & 0 deletions InertiaCore/ResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal interface IResponseFactory
public AlwaysProp Always(Func<Task<object?>> callback);
public LazyProp Lazy(Func<object?> callback);
public LazyProp Lazy(Func<Task<object?>> callback);
public OptionalProp Optional(Func<object?> callback);
public OptionalProp Optional(Func<Task<object?>> callback);
}

internal class ResponseFactory : IResponseFactory
Expand Down Expand Up @@ -144,4 +146,6 @@ public void Share(IDictionary<string, object?> data)
public AlwaysProp Always(object? value) => new(value);
public AlwaysProp Always(Func<object?> callback) => new(callback);
public AlwaysProp Always(Func<Task<object?>> callback) => new(callback);
public OptionalProp Optional(Func<object?> callback) => new(callback);
public OptionalProp Optional(Func<Task<object?>> callback) => new(callback);
}
5 changes: 5 additions & 0 deletions InertiaCore/Utils/IIgnoresFirstLoad.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace InertiaCore.Utils;

public interface IIgnoresFirstLoad
{
}
139 changes: 139 additions & 0 deletions InertiaCoreTests/UnitTestOptionalData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using InertiaCore.Models;
using Microsoft.AspNetCore.Http;

namespace InertiaCoreTests;

public partial class Tests
{
[Test]
[Description("Test if the optional data is fetched properly.")]
public async Task TestOptionalData()
{
var response = _factory.Render("Test/Page", new
{
Test = "Test",
TestFunc = new Func<string>(() => "Func"),
TestOptional = _factory.Optional(() =>
{
Assert.Fail();
return "Optional";
})
});

var context = PrepareContext();

response.SetContext(context);
await response.ProcessResponse();

var page = response.GetJson().Value as Page;

Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "testFunc", "Func" },
{ "errors", new Dictionary<string, string>(0) }
}));
}

[Test]
[Description("Test if the optional data is fetched properly with specified partial props.")]
public async Task TestOptionalPartialData()
{
var response = _factory.Render("Test/Page", new
{
TestFunc = new Func<string>(() => "Func"),
TestOptional = _factory.Optional(() => "Optional")
});

var headers = new HeaderDictionary
{
{ "X-Inertia-Partial-Data", "testFunc,testOptional" },
{ "X-Inertia-Partial-Component", "Test/Page" }
};

var context = PrepareContext(headers);

response.SetContext(context);
await response.ProcessResponse();

var page = response.GetJson().Value as Page;

Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "testFunc", "Func" },
{ "testOptional", "Optional" },
{ "errors", new Dictionary<string, string>(0) }
}));
}


[Test]
[Description("Test if the optional async data is fetched properly.")]
public async Task TestOptionalAsyncData()
{
var testFunction = new Func<Task<object?>>(async () =>
{
Assert.Fail();
await Task.Delay(100);
return "Optional Async";
});

var response = _factory.Render("Test/Page", new
{
Test = "Test",
TestFunc = new Func<string>(() => "Func"),
TestOptional = _factory.Optional(testFunction)
});

var context = PrepareContext();

response.SetContext(context);
await response.ProcessResponse();

var page = response.GetJson().Value as Page;

Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "testFunc", "Func" },
{ "errors", new Dictionary<string, string>(0) }
}));
}

[Test]
[Description("Test if the optional async data is fetched properly with specified partial props.")]
public async Task TestOptionalAsyncPartialData()
{
var testFunction = new Func<Task<string>>(async () =>
{
await Task.Delay(100);
return "Optional Async";
});

var response = _factory.Render("Test/Page", new
{
TestFunc = new Func<string>(() => "Func"),
TestOptional = _factory.Optional(async () => await testFunction())
});

var headers = new HeaderDictionary
{
{ "X-Inertia-Partial-Data", "testFunc,testOptional" },
{ "X-Inertia-Partial-Component", "Test/Page" }
};

var context = PrepareContext(headers);

response.SetContext(context);
await response.ProcessResponse();

var page = response.GetJson().Value as Page;

Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "testFunc", "Func" },
{ "testOptional", "Optional Async" },
{ "errors", new Dictionary<string, string>(0) }
}));
}
}