Skip to content

@EncodingKeys decorator for exclude some property. #2

@tanb

Description

@tanb

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

  • Response Body
{
  "id": 1,
  "username": "appleseed",
  "image": {
    "id": 42,
    "url": "https://example.com/image.jpg"
  }
}
  • Request Body
{
  "id": 1,
  "username": "appleseed",
  "image": 42
}

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ImprovementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions