Description
The use case:
I use this library for a REST API wrapper where I map the returned JSON responses to my classes. I am struggling with just one specific case.
The REST Api returns a cardName
, but unfortunately no cardId
which could be used to retrieve the cardName
. However I can resolve the cardId
by a given cardName and I highly prefer to store cardIds instead of the cardNames in my database.
The code:
function cardResolver(cardNameOrId: string | number): number {
// If we serialize a previously deserialized object we will already get the cardId instead of a name
if (typeof cardNameOrId === 'number') {
return <number>cardNameOrId;
}
return CardHelper.getCardByName(cardNameOrId).id;
}
@Exclude()
export class BaseCard {
@Transform(cardResolver)
@Expose({ name: 'name' })
public id: number;
}
The problem:
This requirement is a challenge though, because now the library is supposed to handle two different schemas. One is the REST Api which returns the cardName and the other is my database which returns the cardId.
While my @Transform
function is not an issue (it can handle both json inputs and recognizes the difference using the type), I need some sort of alias
, since the input json can be either:
{ cardName: 'Goblin Barrel' }
or
{ cardId: 260000013 }
My question:
Can I achieve this with the given library features or does it make sense to add an alias feature?