using System.Text.Json; using System.Text.Json.Serialization; using Asp.Versioning; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Umbraco.Cms.Api.Common.DependencyInjection; using Umbraco.Cms.Api.Delivery.Accessors; using Umbraco.Cms.Api.Delivery.Configuration; using Umbraco.Cms.Api.Delivery.Handlers; using Umbraco.Cms.Api.Delivery.Json; using Umbraco.Cms.Api.Delivery.Rendering; using Umbraco.Cms.Api.Delivery.Routing; using Umbraco.Cms.Api.Delivery.Security; using Umbraco.Cms.Api.Delivery.Services; using Umbraco.Cms.Core; using Umbraco.Cms.Core.DeliveryApi; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Infrastructure.Security; namespace Umbraco.Extensions; public static class UmbracoBuilderExtensions { public static IUmbracoBuilder AddDeliveryApi(this IUmbracoBuilder builder) { builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(provider => { HttpContext? httpContext = provider.GetRequiredService().HttpContext; ApiVersion? apiVersion = httpContext?.GetRequestedApiVersion(); if (apiVersion is null) { return provider.GetRequiredService(); } // V1 of the Delivery API uses a different expansion strategy than V2+ return apiVersion.MajorVersion == 1 ? provider.GetRequiredService() : provider.GetRequiredService(); }); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.ConfigureOptions(); builder.AddUmbracoApiOpenApiUI(); builder .Services .AddControllers() .AddJsonOptions(Constants.JsonOptionsNames.DeliveryApi, options => { // all Delivery API specific JSON options go here options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.TypeInfoResolver = new DeliveryApiJsonTypeResolver(); options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); builder.Services.AddAuthentication(); builder.AddUmbracoOpenIddict(); builder.AddNotificationAsyncHandler(); builder.AddNotificationAsyncHandler(); builder.AddNotificationAsyncHandler(); builder.AddNotificationAsyncHandler(); builder.AddNotificationAsyncHandler(); // FIXME: remove this when Delivery API V1 is removed builder.Services.AddSingleton(); return builder; } }