feat: add typeKey option to preserve resource type in deserialized objects#258
Open
SBoudrias wants to merge 1 commit into
Open
feat: add typeKey option to preserve resource type in deserialized objects#258SBoudrias wants to merge 1 commit into
SBoudrias wants to merge 1 commit into
Conversation
…jects
This adds a `typeKey` option that allows preserving the JSON:API resource
type in deserialized objects. This is useful when you need to know the
original type of a resource after deserialization, especially when dealing
with polymorphic relationships.
When deserializing:
- If `typeKey` is specified, the resource type is included in the output
using the specified key name
- Works for both the main resource and relationships (included or not)
When serializing:
- The `typeKey` attribute is automatically removed from the output
- Validates that `idKey` and `typeKey` are different
- Validates that the `typeKey` value matches the specified type (if present)
Example:
```ts
deserialize(response, { typeKey: 'type' })
// { type: 'users', id: '1', firstName: 'Joe' }
serialize(entity, 'users', { typeKey: 'type' })
// Strips 'type' from attributes
```
|
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days. |
Author
|
I can't remove the label myself. Would really like to see this get in. Let me know if you want me to change anything. That being said, I understand it's not been modified in 6 months. Hoping the project isn't dead. |
|
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days. |
Author
|
Still would like to get this in 🥹 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a
typeKeyoption that allows preserving the JSON:API resource type in deserialized objects. This is useful when you need to know the original type of a resource after deserialization, especially when dealing with polymorphic relationships.Deserialization
When
typeKeyis specified, the resource type is included in the output using the specified key name:This works for:
includedarray (returns{ id, [typeKey]: type })Serialization
When serializing with
typeKey:typeKeyattribute is automatically removed from the output (so it doesn't appear inattributes)idKeyandtypeKeyare differenttypeKeyvalue matches the specified type (if present in entity)Test plan
typeKeytypeKey