Skip to content
On this page

Serializers

Serializer functions are the last step of the HTTP Request-Response cycle. In this section, we will provide all information about serializers.

  • You will learn
  • What is a serializer?
  • How to define a serializer for a model?
  • How to define a serializer by handler type?
  • How to define a global serializer?

Getting started

A serializer function is just a simple function that takes two arguments (data and request) and returns the record item.

Let's check the following example;

ts
const mySerializer = (data: any, request: Request) => {
  // do something
  return data;
};

As you can see, the function is not an async function. Axe API doesn't allow to use async function for a serializer function.

The data variable means the record item by model. For example, if you are defining a model-based serializer for the User model, Axe API sends you a record from the users table via the data parameter.

The request variable means the pure HTTP request object that is generated by Express.js.

You can change the data context by using a serializer function.

For example; you can add a computed field for each item. Let's assume that the user model has two fields on the database table; name and surname. You can create a new field that is called fullname by using a serializer function.

json
{
  "id": 1,
  "name": "John",
  "surname": "Locke"
}
ts
import { Request } from "express";

export default (item: any, request: Request) => {
  return {
    ...item,
    fullname: `${item.name} ${item.surname}`,
  };
};
json
{
  "id": 1,
  "name": "John",
  "surname": "Locke",
  "fullname": "John Locke"
}

Model-based serializer

You can define a specific serializer for a specific model like the following example;

ts
import { Request } from "express";

export default (item: any, request: Request) => {
  return {
    ...item,
    fullname: `${item.name} ${item.surname}`,
  };
};
json
{
  "id": 1,
  "name": "John",
  "surname": "Locke",
  "fullname": "John Locke"
}

This serializer is used for all user responses. For example in user pagination, fetching a user, and updating a user.

Also, all relational queries use *model-based serialization**. This means that your user responses will have the same response structures in all requests.

Handler-based serializer

In the version configuration file, you can define a handler-based serializer. Those kinds of serializers are used for all handler types like all paginations, etc.

You can define a handler-based serializer function, like the following example.

ts
import { Request } from "express";
import { IVersionConfig, HandlerTypes } from "axe-api";

const simpleSerializer = (data: any, request: Request) => {
  data.signed = true;
  return data;
};

const config: IVersionConfig = {
  serializers: [
    {
      handler: [HandlerTypes.PAGINATE],
      serializer: [simpleSerializer],
    },
  ],
};

export default config;

In the example above, we defined the simpleSerializer for all PAGINATE handlers. All PAGINATE handlers will be using the serializer function.

Global serializer

You can define a version-based serializer function in the version config file. That kind of serializer function works in every HTTP request that returns a response.

ts
import { Request } from "express";
import { IVersionConfig } from "axe-api";

const simpleSerializer = (data: any, request: Request) => {
  data.signed = true;
  return data;
};

const config: IVersionConfig = {
  serializers: [simpleSerializer],
};

export default config;

In the example above, we defined a global serializer function. This means that function will be triggered by all model items.

Next steps

In this section, we have talked about serializers and shown some examples.

In the next chapter, we are going to talk about configurations.

Released under the MIT License.