A simple set of libraries that enables you to parse, interpret, serialize and build simple queries for fields that could be used in building flexible REST endpoints (but not limited to API):
https://my-app.com/animals?fields=id,photoUrl,guardian{name}&filters=id(gt:5)
Add a dependency using the NuGet package manager (console):
Install-Package PartialResponseRequest.AspNetCore.ResponsePruner.System.Text.Json
Use services.AddPartialResponse()
to add a custom json output formatter, that will search for ?fields=...
query string that would be used to prune the response for the client.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddPartialResponse();
}
Add a dependency using the NuGet package manager (console):
Install-Package PartialResponseRequest.Fields
Allows you to parse fields query for processing/interpreting and reacting based on it, like building a custom optimized SQL query:
var parser = new FieldsQueryParser();
IEnumerable<FieldToken> fields = parser.Parse("id,name,photoUrl");
var interpreter = new FieldsQueryInterpreter(fields);
// Use the interpreter to build queries
if (interpreter.Includes("id"))
{
// include id field
}
View the documentation here
Build the project
build.ps1 --target=Build [--configuration=Release]
Run tests
build.ps1 --target=Test [--outputDirectory=./output]
Run a test coverage report
build.ps1 --target=Report [--testResultsDirectory=./test-results/]
Create nuget packages
build.ps1 --target=Pack [--outputDirectory=./output] [--configuration=Release] [--package-version=1.0.0.0]