Hi Richard,
This is actually possible, what you can do is to extend the response from the Content Delivery API.
Here is an example on how to do it.
Mostly the code is boilerplate and is needed to extend the response but the magic happens where I have fetched the parent content and added a custom extra object to the response.
public class MyDeliveryApiJsonTypeResolver : DeliveryApiJsonTypeResolver
{
protected override Type[] GetDerivedTypes(JsonTypeInfo jsonTypeInfo)
=> jsonTypeInfo.Type == typeof(IApiContentResponse)
? new[] { typeof(MyApiContentResponse), typeof(ApiContentResponse) }
: base.GetDerivedTypes(jsonTypeInfo);
}
public class MyApiContentResponseComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.AddSingleton<IApiContentResponseBuilder, MyApiContentResponseBuilder>();
builder
.Services
.AddControllers()
.AddJsonOptions(
Constants.JsonOptionsNames.DeliveryApi,
options => options.JsonSerializerOptions.TypeInfoResolver = new MyDeliveryApiJsonTypeResolver());
}
}
public class MyApiContentResponseBuilder : ApiContentResponseBuilder
{
private readonly IHttpContextAccessor _httpContextAccessor;
public MyApiContentResponseBuilder(
IApiContentNameProvider apiContentNameProvider,
IApiContentRouteBuilder apiContentRouteBuilder,
IOutputExpansionStrategyAccessor outputExpansionStrategyAccessor,
IHttpContextAccessor httpContextAccessor)
: base(apiContentNameProvider, apiContentRouteBuilder, outputExpansionStrategyAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override IApiContentResponse Create(
IPublishedContent content,
string name,
IApiContentRoute route,
IDictionary<string, object?> properties)
{
var parentCustomData = new
{
ParentId = content.Parent?.Key,
ParentName = content.Parent?.Name,
ParentType = content.Parent?.ContentType.Alias
};
IDictionary<string, IApiContentRoute> cultures = GetCultures(content);
return new MyApiContentResponse(
content.Key,
name,
content.ContentType.Alias,
content.CreateDate,
content.UpdateDate,
route,
properties,
cultures,
parentCustomData);
}
}
public class MyApiContentResponse : ApiContentResponse
{
public object ExtraData { get; }
public MyApiContentResponse(
Guid id,
string name,
string contentType,
DateTime createDate,
DateTime updateDate,
IApiContentRoute route,
IDictionary<string, object?> properties,
IDictionary<string, IApiContentRoute> cultures,
object extraData)
: base(id, name, contentType, createDate, updateDate, route, properties, cultures)
=> ExtraData = extraData;
}
Let me know if you need more help.