Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Problem

Currently, when serializing a Paginator, the pagination metadata is always output under the hardcoded key "metadata":

{
  "data": [...],
  "metadata": {  // always hardcoded
  ...
  }
}

This key cannot be changed, which is problematic because:

  • Internal API conventions — teams that have already established a different key (e.g. "meta", "pagination") across their API cannot adopt this library without breaking their existing contract with clients.
  • Backward compatibility — projects migrating to http-transformers from another serialization layer are forced to rename the key in all their API responses, which may break existing clients.

Proposed solution

Add a MetadataKey property to BaseSerializer, mirroring the existing wrap property, with a default value of "metadata".
The Wrappers generic should be extended with a MetadataKey type parameter so that the output type is fully inferred:

class ApiSerializer extends BaseSerializer<{
  Wrap: 'data'
  MetadataKey: 'meta'   
  PaginationMetaData: SimplePaginatorMetaKeys
}> {
  wrap: 'data' = 'data'
  metadataKey: 'meta' = 'meta'
  // ...
}

Happy to open a PR with these changes if the idea sounds good to you !
The transformer pattern is really a good idea ! 🙂

You must be logged in to vote

Replies: 3 comments

Comment options

Agreed. I'm actually having to write an extra function to map the keys just to keep the API consistent for the FE

You must be logged in to vote
0 replies
Comment options

Sure, please send over a PR 👍🏽

You must be logged in to vote
0 replies
Comment options

Hey, I also needed this feature, so I implemented it in this PR

Happy to adjust it if there’s a better approach 👍

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.