1.1.8 • Published 9 months ago

@iamnnort/nestjs-serializer v1.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Info

Serialization module for NestJS - Efficient - Flexible - Streamlined

Installation

yarn install @iamnnort/nestjs-serializer

Usage

// types.ts

export enum Scopes {
  BASE = 'BASE',
  FULL = 'FULL',
}

// model.ts

import { SerializeField } from '@iamnnort/nestjs-serializer';
import { Scopes } from './types';

export class Model {
  @SerializeField([{ scopes: [Scopes.BASE] }])
  id: number;

  constructor(dto: { id: number }) {
    this.id = dto.id;
  }
}

// city.ts

import { SerializeField } from '@iamnnort/nestjs-serializer';
import { Model } from './model';
import { Scopes } from './types';

export class City extends Model {
  @SerializeField([{ scopes: [Scopes.FULL] }])
  name: string;

  constructor(dto: { id: number; name: string }) {
    super(dto);

    this.name = dto.name;
  }
}

// user.ts

import { SerializeField, SerializeRelation } from '@iamnnort/nestjs-serializer';
import { City } from './city';
import { Model } from './model';
import { Scopes } from './types';

export class User extends Model {
  @SerializeField([{ scopes: [Scopes.FULL] }])
  name: string;

  @SerializeRelation([{ scopes: [Scopes.FULL], relationScopes: [Scopes.BASE, Scopes.FULL] }])
  city: City;

  constructor(dto: { id: number; name: string; city: City }) {
    super(dto);

    this.name = dto.name;
    this.city = dto.city;
  }
}

// controller.ts

import { Controller, Get, Post, UseInterceptors } from '@nestjs/common';
import { SerializerIdInterceptor, SerializerInterceptor } from '@iamnnort/nestjs-serializer';
import { User } from './user';
import { Scopes } from './types';
import { City } from './city';

@Controller()
export class AppController {
  @Get()
  @UseInterceptors(SerializerInterceptor({ scopes: [Scopes.BASE] }))
  search() {
    const city = new City({
      id: 1,
      name: 'London',
    });

    const user = new User({
      id: 1,
      name: 'John',
      city,
    });

    return [user];
  }

  @Get(':id')
  @UseInterceptors(SerializerInterceptor({ scopes: [Scopes.BASE, Scopes.FULL] }))
  get() {
    const city = new City({
      id: 1,
      name: 'London',
    });

    const user = new User({
      id: 1,
      name: 'John',
      city,
    });

    return user;
  }

  @Post()
  @UseInterceptors(SerializerIdInterceptor)
  create() {
    const city = new City({
      id: 1,
      name: 'London',
    });

    const user = new User({
      id: 1,
      name: 'John',
      city,
    });

    return user;
  }
}

// module.ts

import { Module } from '@nestjs/common';
import { LoggerModule } from '@iamnnort/nestjs-logger';
import { SerializerModule } from '@iamnnort/nestjs-serializer';
import { Model } from './model';
import { AppController } from './controller';

@Module({
  imports: [
    LoggerModule,
    SerializerModule.register({
      globalEntityNames: [Model.name],
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

// index.ts

import { NestFactory } from '@nestjs/core';
import { LoggerService } from '@iamnnort/nestjs-logger';
import { AppModule } from './module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    bufferLogs: true,
  });

  app.useLogger(new LoggerService());

  await app.listen(3000);
}

bootstrap();

Output

[System] Application is starting...
[System] Application started.
[System] [Request] GET /
[System] [Response] GET / 200 OK [{ id: 1 }]
[System] [Request] GET /1
[System] [Response] GET /1 200 OK [{ id: 1, name: "John", city: { id: 1, name: "London" } }]
[System] [Request] POST /1
[System] [Response] POST /1 201 Created { id: 1 }

License

This project is licensed under the MIT license. See the LICENSE file for more info.

1.1.8

9 months ago

1.1.7

9 months ago

1.1.6

9 months ago

1.1.5

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.4

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago