tanb / codable

Codable base class for TypeScript
2 stars 2 forks source link

@EncodingKeys decorator for exclude some property. #2

Open tanb opened 5 years ago

tanb commented 5 years ago

If it's difference between API response and request body format,

When decoding time, image will be Image class instance, but I don't want to encode it. I just want to send the image's id as image value in a request body.

Then EncodingKeys decorator works in just encoding time for property key mapping.

@EncodingKeys({
  id: "id",
  username: "username",
  imageId: "image"
})
class Profile {
  id: number;
  username: string;
  @CodableType(Image)
  image: Image;
  get imageId(): {
    return this.image.id;
  };
}

This will be follows after encode.

{
  "id": 1,
  "username": "appleseed",
  "image": 42
}
tanb commented 5 years ago

Will be implemented after #1 released.