You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

serialize-interceptor

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serialize-interceptor

[![Build Status](https://app.travis-ci.com/nolleh/serialize-interceptor.svg?branch=master)](https://app.travis-ci.com/nolleh/serialize-interceptor) [![Coverage Status](https://github.com/nolleh/serialize-interceptor/raw/gh-pages/badges/coverage-jest%20cov

1.1.9
latest
Source
npmnpm
Version published
Weekly downloads
284
-34.56%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Coverage Status npm version License: MIT

Overview

SerializeInterceptor

Intercepts request/response data and deserializes/serializes to DTO format.

  • For requests: converts snake_case to camelCase (you can use camelCase in your DTO while accepting snake_case JSON input)
  • For responses: converts camelCase to snake_case (you can use camelCase in your DTO while sending snake_case JSON to clients)

In summary:

  • JSON layer: snake_case
  • Model layer: camelCase

This conversion works for nested objects as well.

Example

When a client sends the following data:

{
  "name": "nolleh",
  "email": "nolleh7707@gmail.com",
  "some_snake_data": "hello world",
  "live": {
    "country": "South Korea",
    "city": "Seongnam",
    "some_snake_data": "hello world2"
  }
}

You can access it in your code as:

class LiveDto {
  country,
  city,
  someSnakeData
}

class MyDto {
  name,
  email,
  someSnakeData,
  live,
}

Usage

Add this to your main code. You can find the complete example at:


import { SerializeInterceptor } from 'serialize-interceptor';
import { NestFactory } from '@nestjs/core';
...
const app = await NestFactory.create(AppModule);
/** use our interceptor **/
app.useGlobalInterceptors(new SerializeInterceptor);

// @since 1.1.5
// if you want to customize serializer, then put your strategy.
// const strategy: Strategy = {
//   in: DEFAULT_STRATEGY.in,
//   out: (v) => {
//     // return 'test-swallow up!';
//     return snakeToCamel(v)
//   },
// };
// app.useGlobalInterceptors(new SerializeInterceptor(strategy));

OR in module

@Module({
  controllers: [AppController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: SerializeInterceptor,
    },

    /**  @since 1.1.5
    // if you want to customize serializer, then put your strategy.
    {
      provide: Strategy,
      useFactory: () => ({
        in: DEFAULT_STRATEGY.in,
        out: (v) => {
          // return 'test-swallow up!';
          // your custom func. the default for 'out' is.. snakeToCamel.
          return snakeToCamel(v);
        },
      }),
    },
    **/
})

export class AppModule {}

Custom Serializer (Strategy)

You can define your own serialization strategy as shown in the snippets above.

SerializeInterceptor provides classes to help you define your own strategy:

/** Since the regenerated value's fields differ from the original,
 * it's challenging to declare the return type.
 * The input type is also not meaningful.
 *
 * in: request layer (default: snakeToCamel),
 * out: response layer (default: camelToSnake).
 *
 * i.e. const DEFAULT_STRATEGY: Strategy = { in: snakeToCamel, out: camelToSnake };
 */
export class Strategy {
  in: (value: any) => any;
  out: (value: any) => any;
}
export const DEFAULT_STRATEGY: Strategy = {
  in: snakeToCamel,
  out: camelToSnake,
};

As shown above, implement the Strategy class with in/out functions, and provide it as a constructor (either through injection or by creating a new instance). The interceptor will then work according to your definition.

🤔 Do you need a specific strategy that you'd like to see provided by this library?
Let me know!

Available strategies:

NameDescriptionRemark (side effect)Default
snakeToCamelsnake -> cameldash(-, kebab) also converted to cameldefault for in (request)
camelToSnakecamel -> snakestarting with capital (pascal) also converted to snakedefault for out (response)
kebabToCamelkebab -> camelNo side effects
camelToKebabcamel -> kebabNo side effects
camelTosnake2camel ↔ snakewithout kebab side effects
snakeToCamel2snake -> camelwithout kebab side effects

⚠️ The default snakeToCamel / camelToSnake has side effects that also convert kebab-case and PascalCase.
While these side effects can be useful in many cases, they might not be desirable in all situations.

Dependencies

NestJS

Designed for NestJS interceptor.

Keywords

nestjs

FAQs

Package last updated on 03 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts