Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nolleh/serialize-interceptor
nestjs interceptor for dto serialize. (snake -> camel to input dto, camel -> snake to returned json)
https://github.com/nolleh/serialize-interceptor
interceptor marshalling middleware nestjs nodejs npm-package npm-packages typescript
Last synced: 2 months ago
JSON representation
nestjs interceptor for dto serialize. (snake -> camel to input dto, camel -> snake to returned json)
- Host: GitHub
- URL: https://github.com/nolleh/serialize-interceptor
- Owner: nolleh
- License: mit
- Created: 2022-03-09T05:34:40.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-30T15:09:58.000Z (7 months ago)
- Last Synced: 2024-10-24T08:49:30.408Z (2 months ago)
- Topics: interceptor, marshalling, middleware, nestjs, nodejs, npm-package, npm-packages, typescript
- Language: TypeScript
- Homepage:
- Size: 210 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- License: LICENSE
Awesome Lists containing this project
README
[![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%20coverage.svg?raw=true)](https://nolleh.github.io/serialize-interceptor/badges/coverage-jest%20coverage.svg?raw=true)
[![npm version](https://badge.fury.io/js/serialize-interceptor.svg)](https://badge.fury.io/js/serialize-interceptor)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)## Overview
SerializeInterceptor
intercept request/response, and deserialize/serialize to dto data.
1. for request, snake -> camel. (you can retrieve dto using camel, for snake cases json input)
2. for response, camel -> snake. (you can send dto using camel, and client retrieve snake case json input)in short, json layer: snake
model layer: camelIt also works to nested object.
## example
when client send below data,
```json
{
"name": "nolleh",
"email": "[email protected]",
"some_snake_data": "hello world"
"live": {
"country": "South Korea",
"city": "Seongnam",
"some_snake_data": "hello world2"
}
}
```you can retrieve as (in code)
```typescript
class LiveDto {
country,
city,
someSnakeData
}class MyDto {
name,
email,
someSnakeData,
live,
}```
## Usage
in your main code, put this.
you can check this code from
[]("https://github.com/nolleh/serialize-interceptor/test/app.ts")```typescript
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
```typescript
@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 {}
```## Customed Serializer (Strategy)
you can put your serialize strategy as you wish, that briefly shown in above snippet.
serializeInterceptor provides classes to help definition your own class.
```typescript
/** because the regenerated value's field is differ from original,
* it is hard to declare 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 you can see, implementing class `Strategy` that contains in/out function,
and put it as constructor (by injecting or creating new one),
then the interceptor will work as you defined.🤔 is there A strategy that one you want to be provided by this lib?
let me know!for now :
| Name | Desc | Remark (side effect) | Default |
| ------------ | -------------- | ------------------------------------------------------ | -------------------------- |
| snakeToCamel | snake -> camel | dash(-, kebab) also converted to camel | default for in (request) |
| camelToSnake | camel -> snake | starting with capital (pascal) also converted to snake | default for out (response) |
| kebabToCamel | kebab -> camel | X | |
| camelTokebab | camel -> kebab | X | |⚠️ the default snakeToCamel / camelToSnake has side effect that also converting kebab, pascal.
considering it's usage, couldn't simply say the side effect is disadvantage.
but to handle diversity of usage case, there will be soon added additional strategy that doesn't have the effect.## Dependencies
nestjs
> designed for nestjs interceptor.