Kavita/API/Extensions/HttpExtensions.cs
Joe Milazzo dec65b9262
More Polish (#4336)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
2026-01-10 09:06:52 -08:00

37 lines
1.3 KiB
C#

using System.Text.Json;
using API.Helpers;
using Microsoft.AspNetCore.Http;
namespace API.Extensions;
#nullable enable
public static class HttpExtensions
{
private static readonly JsonSerializerOptions Options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
/// <summary>
/// Adds pagination headers - Use with <see cref="PagedList{T}"/>
/// </summary>
/// <param name="response"></param>
/// <param name="currentPage"></param>
/// <param name="itemsPerPage"></param>
/// <param name="totalItems"></param>
/// <param name="totalPages"></param>
public static void AddPaginationHeader(this HttpResponse response, int currentPage,
int itemsPerPage, int totalItems, int totalPages)
{
var paginationHeader = new PaginationHeader(currentPage, itemsPerPage, totalItems, totalPages);
response.Headers.Append("Pagination", JsonSerializer.Serialize(paginationHeader, Options));
response.Headers.Append("Access-Control-Expose-Headers", "Pagination");
}
public static void AddPaginationHeader<T>(this HttpResponse response, PagedList<T> pagedList)
{
response.AddPaginationHeader(pagedList.CurrentPage, pagedList.PageSize, pagedList.TotalCount, pagedList.TotalPages);
}
}