Skip to content

Commit d57d204

Browse files
ArcadeModeCopilot
andauthored
Improved sample project (#93)
#86 --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: ArcadeMode <5969155+ArcadeMode@users.noreply.github.com>
1 parent 36361fb commit d57d204

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+426
-639
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<ItemGroup>
4+
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="10.0.3" />
45
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
56
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0" />
67
<PackageVersion Include="coverlet.collector" Version="6.0.4" />

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,6 @@ Table 2. TypeShim support for .NET-JS interop types
334334

335335
*<sub>For `[TSExport]` classes</sub>
336336

337-
## Run the sample
338-
339-
To build and run the project:
340-
```
341-
cd Sample/TypeShim.Sample.Client && npm install && npm run build && cd ../TypeShim.Sample.Server && dotnet run
342-
```
343-
The app should be available on [http://localhost:5012](http://localhost:5012)
344-
345337
## <a name="installing"></a>Installing
346338

347339
To use TypeShim all you have to do is install it directly into your `Microsoft.NET.Sdk.WebAssembly`-powered project. Check the [configuration](#configuration) section for configuration you might want to adjust to your project.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ temp/
9797
# Preserve root lock
9898
!package-lock.json
9999
# If individual workspace package-locks accidentally get created (npm -w install inside):
100-
@typeshim/*/package-lock.json
100+
@client/*/package-lock.json
101101

102102
# -----------------------------
103103
# Misc bundler artifacts
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TypeShim.Sample;
1+
namespace Client.Library;
22

33
public class PeopleDto
44
{

sample/TypeShim.Sample/TypeShim.Sample.csproj renamed to sample/Library/Library.csproj

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,35 @@
55
<OverrideHtmlAssetPlaceholders>true</OverrideHtmlAssetPlaceholders>
66
<Nullable>enable</Nullable>
77

8-
<WasmFingerprintAssets>true</WasmFingerprintAssets>
8+
<WasmBundlerFriendlyBootConfig>true</WasmBundlerFriendlyBootConfig>
9+
<WasmFingerprintAssets>false</WasmFingerprintAssets>
910
<CompressionEnabled>false</CompressionEnabled>
11+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
12+
<WasmEnableHotReload>false</WasmEnableHotReload>
1013
</PropertyGroup>
1114

1215
<ItemGroup>
1316
<StaticWebAssetFingerprintPattern Include="JS" Pattern="*.js" Expression="#[.{fingerprint}]!" />
1417
</ItemGroup>
1518

1619
<PropertyGroup>
20+
<OutputPath>./bin/</OutputPath>
1721
<!--<TypeShim_GeneratedDir>../../../TypeShim</TypeShim_GeneratedDir>-->
18-
<TypeShim_TypeScriptOutputDirectory>../../../../TypeShim.Sample.Client/@typeshim/wasm-exports</TypeShim_TypeScriptOutputDirectory>
19-
<TypeShim_MSBuildMessagePriority>High</TypeShim_MSBuildMessagePriority>
22+
<!--<TypeShim_TypeScriptOutputDirectory>wwwroot</TypeShim_TypeScriptOutputDirectory>-->
23+
<!--<TypeShim_MSBuildMessagePriority>High</TypeShim_MSBuildMessagePriority>-->
2024
</PropertyGroup>
2125

2226
<ItemGroup>
2327
<PackageReference Include="Microsoft.Extensions.Hosting" />
2428
<PackageReference Include="TypeShim" />
25-
26-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" />
27-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" />
2829
</ItemGroup>
30+
31+
<PropertyGroup>
32+
<EnableDefaultContentItems>false</EnableDefaultContentItems>
33+
</PropertyGroup>
2934
<ItemGroup>
30-
<Folder Include="wwwroot\" />
35+
<Content Include="wwwroot\**\*">
36+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37+
</Content>
3138
</ItemGroup>
3239
</Project>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
22
using System.Threading.Tasks;
3+
using TypeShim;
34

4-
namespace TypeShim.Sample;
5+
namespace Client.Library;
56

67
[TSExport]
78
public class People()

sample/Library/PeopleApiClient.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Net.Http.Json;
6+
using System.Text.Json.Serialization;
7+
using System.Threading.Tasks;
8+
9+
namespace Client.Library;
10+
11+
public class PeopleApiClient(HttpClient httpClient)
12+
{
13+
public async Task<IEnumerable<Person>> GetAllPeopleAsync()
14+
{
15+
PeopleDto? dto = await httpClient.GetFromJsonAsync("/people/all", typeof(PeopleDto), PersonDtoSerializerContext.Default) as PeopleDto;
16+
return dto?.People?.Select(dto => dto.ToPerson()) ?? [];
17+
}
18+
}
19+
20+
[JsonSourceGenerationOptions(
21+
GenerationMode = JsonSourceGenerationMode.Serialization | JsonSourceGenerationMode.Metadata,
22+
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
23+
[JsonSerializable(typeof(PeopleDto))]
24+
[JsonSerializable(typeof(PersonDto))]
25+
[JsonSerializable(typeof(PersonDto[]))]
26+
[JsonSerializable(typeof(DogDto))]
27+
[JsonSerializable(typeof(DogDto[]))]
28+
internal partial class PersonDtoSerializerContext : JsonSerializerContext { }

sample/Library/PeopleApp.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using System;
5+
using System.Net.Http;
6+
using TypeShim;
7+
8+
namespace Client.Library;
9+
10+
[TSExport]
11+
public class PeopleAppOptions
12+
{
13+
public required string BaseAddress { get; init; }
14+
}
15+
16+
[TSExport]
17+
public class PeopleApp
18+
{
19+
private readonly IHost _host;
20+
21+
public PeopleApp(PeopleAppOptions options)
22+
{
23+
// we dont -need- a servicecollection for this demo but its here to show you can use anything on the .net side
24+
_host = new HostBuilder().ConfigureServices(services =>
25+
{
26+
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(options.BaseAddress) });
27+
services.AddSingleton<PeopleApiClient>();
28+
services.AddSingleton<PeopleProvider>(sp => new PeopleProvider(sp.GetRequiredService<PeopleApiClient>()));
29+
}).Build();
30+
Console.WriteLine($".NET {nameof(PeopleApp)} Constructor completed");
31+
}
32+
33+
public PeopleProvider GetPeopleProvider()
34+
{
35+
return _host.Services.GetRequiredService<PeopleProvider>();
36+
}
37+
}
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Threading.Tasks;
55
using TypeShim;
66

7-
namespace TypeShim.Sample;
7+
namespace Client.Library;
88

99
[TSExport]
1010
public class PeopleProvider
@@ -17,18 +17,10 @@ internal PeopleProvider(PeopleApiClient apiClient)
1717
_apiClient = apiClient;
1818
}
1919

20-
public Person[]? PeopleCache => AllPeople;
21-
public Task<TimeoutUnit?>? DelayTask { get; set; } = null;
22-
2320
public async Task<People> FetchPeopleAsync()
2421
{
2522
try
2623
{
27-
if (DelayTask != null)
28-
{
29-
await Task.Delay((await DelayTask)?.Timeout ?? 0);
30-
}
31-
3224
if (AllPeople == null)
3325
{
3426
AllPeople = [.. await _apiClient.GetAllPeopleAsync()];
@@ -46,11 +38,4 @@ public async Task<People> FetchPeopleAsync()
4638
throw; // hand over to js
4739
}
4840
}
49-
}
50-
51-
52-
[TSExport]
53-
public class TimeoutUnit
54-
{
55-
public int Timeout { get; set; } = 0;
5641
}

sample/Library/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using System;
5+
using System.Net.Http;
6+
using System.Runtime.InteropServices.JavaScript;
7+
8+
namespace Client.Library;
9+
10+
public partial class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
Console.WriteLine(".NET Main method entered.");
15+
16+
// You can put any startup logic here like any other .NET application
17+
// alternatively you could expose a class that embodies your app and treat the .NET code as a library.
18+
// For this demo we'll go with the latter, PeopleApp will be constructed from the JS side.
19+
Console.WriteLine($"{nameof(PeopleApp)} will be constructed from the JS side in this demo.");
20+
}
21+
}

0 commit comments

Comments
 (0)