Data transformers

Sophie Andriamahenintsoa
2 min readMay 25, 2021

Schemas of the flow overview.

Why we need to transform data ?

  • To have uniformed data structure
  • If we immediately use data from external (API) on our view component, every time API changes, we must modify on our component.

So to facilitate maintenance and debug, we just modify data from transformers.

What is transformer responsibility?

To convert an external data into an internal and vice versa

When we should have a transformer ?

A transformer is a kind of bridge between two layers. Each layer has its own structure to represent data.

transformer cycle layer
  • data API called to internal
  • before sending API data

What is rule regarding our transformer ?

  • We must have general transformer interface witch define our future transformers method, with 2 generics types (source and return types) as this example:
export interface TransformerInterface<T, U> {
transform(source: T): U;
}

- Your services transformers will implement this interface to keep the rules that one transform method by Transformer service.

Example:

@Injectable()
export class UserTransformerAPIService implements TransformerInterface<UserTypeUI, UserTypeApi> {
transform(source: UserTypeUI): UserTypeApi {
return {
firstName: source.firstName,
lastName: source.lastName,
email: source.email + source.domain,
role {
roleName: source.role,
isAllowedToPublish: source.isPublic
}
}
}
}
  • On 1st line, we add on TransformerInterface the 2 types we’ll use (source and return type)
  • On 2nd line, we define transform method

--

--

Sophie Andriamahenintsoa

Front-end dev, focused on UI/UX(+responsive design, accessibility), JS/TS and their frameworks, animations