mathematic-inc / ts-japi

A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification
Apache License 2.0
203 stars 16 forks source link

[FR] Transform db values to custom values #14

Closed happypath123 closed 4 years ago

happypath123 commented 4 years ago

Is your feature request related to a problem? *

Maybe I am missing this in the documentation. It seems like there's no such a feature for customizing original json object attributes.

For example:

const user1 = { first_name: 'first', last_name: 'last', age: 20 }

My desired output for this is:

{
    "data": {
        "id": "2",
        "type": "users",
        "attributes": {
            "full_name": "first last",
            "age": 20
        },
}

rather than

{
    "data": {
        "id": "2",
        "type": "users",
        "attributes": {
            "first_name": "first",
           "last_name": "last",
            "age": 20
        },
}

Forgive me if this feature already exists but I just missed it in the doc 😅

jrandolf commented 4 years ago

Check out the projection option: https://mu-io.github.io/ts-japi/interfaces/serializeroptions.html

happypath123 commented 4 years ago

I think the projection option is more for hiding an attribute? Not customizing an attribute?

Would be great if we could have something like the transform option in jsonapi-serializer? https://github.com/SeyZ/jsonapi-serializer/blob/c5b6ea5d9e9dac7eb161b480b242b02ce34bd4e3/test/serializer.js#L2540-L2544 🙏🙏

jrandolf commented 4 years ago

Ah, I see what you mean. Misread your example!

So, the main problem with this idea is, fundamentally speaking, a serializer should just serialize data. Nothing more. Any preprocessing of data should happen before serialization.

At our work, we promote using specialized types for transformation. For example, in your example, we would use the following types:

type User = {
 first_name: string;
 last_name: string;
 age: number;
}
type UserPayload = {
 full_name: string;
 age: number;
}

In this way, we have built a proper DTO type that can be used consistently in other operations (one offs should never be a reason to not define your types!) that have to do with User. All that is required now is the transformation function (user: User) => UserPayload which can be placed before serialization.

happypath123 commented 4 years ago

a serializer should just serialize data. Nothing more.

Agree! Thanks for the suggestion!👍