Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: Pagination metadata #2459

Open
JerryNixon opened this issue Nov 15, 2024 · 0 comments
Open

[Enhancement]: Pagination metadata #2459

JerryNixon opened this issue Nov 15, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@JerryNixon
Copy link
Contributor

JerryNixon commented Nov 15, 2024

Proposal

Consider enhancing pagination to include page metadata?

Why?

Many APIs provide additional page information. All of this information is available to us.

Example: https://stapi.co/api/v2/rest/spacecraft/search

Example

{
    "page": {
        "pagingStrategy": "numeric|cursor" // `numeric` means $page, `cursor` means $after,
        "pageNumber": 0, // null when pagingStrategy == cursor
        "pageSize": 50, // reflects the global setting
        "totalPages": 29, // totalElements / pageSize
        "totalElements": 1443, // database count(*) with same filter
        "firstPage": true, // pageNumber == 0
        "lastPage": false // pageNumber == totalPages
    }
}

Configuration

{
  "runtime": {
    "pagination": {
      "max-page-size": 1000000,
      "default-page-size": 100,
      "include-metadata": <boolean; default: false> 
    }
  }
}

Ad hoc request

$page-metadata

Syntax

https://server/api/entity?$page-metadata=true

Rules

$page
present
$after
present
$page-metadata
value
global
setting
Is metadata included in the response payload?
🟩 🟥 🟩 true n/a included
🟥 🟩 🟩 true n/a included
🟥 🟥 🟩 true n/a -
n/a n/a 🟥 false n/a -
🟩 🟩 🟨 missing 🟩 true included
🟩 🟩 🟨 missing 🟩 true included
🟥 🟩 🟨 missing 🟩 true included
🟥 🟥 🟨 missing 🟩 true -
🟩 🟩 🟨 missing 🟥 false -
🟩 🟥 🟨 missing 🟥 false -
🟥 🟩 🟨 missing 🟥 false -
🟥 🟥 🟨 missing 🟥 false -

As English

  1. When the global setting is true:

    • Metadata is not returned if all three ($page, $after, and $page-metadata) are absent.
    • Metadata is returned if any of the following are true:
      • $page is used.
      • $after is used.
      • $page-metadata=true is explicitly used.
  2. When the global setting is false:

    • Metadata is not returned if $page-metadata is not used or is set to false.
    • Metadata is only returned if $page-metadata=true is explicitly used.
  3. Priority:

    • $page-metadata takes precedence when explicitly set, overriding the absence of $page or $after.

As C#

public static bool ShouldIncludeMetadata(
    int? pageValue, 
    string? afterValue, 
    bool? includeMetadata, 
    bool globalSetting = false)
{
    return includeMetadata switch
    {
        true when pageValue.HasValue => true, 
        true when !string.IsNullOrEmpty(afterValue) => true,
        null when pageValue.HasValue => globalSetting,
        null when !string.IsNullOrEmpty(afterValue) => globalSetting,
        _ => false
    };
}
@JerryNixon JerryNixon added the enhancement New feature or request label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant