{"id":15696048,"url":"https://github.com/nolleh/serialize-interceptor","last_synced_at":"2025-05-07T08:33:28.904Z","repository":{"id":57356690,"uuid":"467791245","full_name":"nolleh/serialize-interceptor","owner":"nolleh","description":"Nestjs interceptor for dto serialize. (snake -\u003e camel to input dto, camel -\u003e snake to returned json)","archived":false,"fork":false,"pushed_at":"2024-05-30T15:09:58.000Z","size":215,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-30T10:18:56.902Z","etag":null,"topics":["interceptor","marshalling","middleware","nestjs","nodejs","npm-package","npm-packages","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nolleh.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-09T05:34:40.000Z","updated_at":"2025-01-20T06:24:52.000Z","dependencies_parsed_at":"2024-05-30T17:17:37.917Z","dependency_job_id":"d9d4454c-c788-4451-bd94-28388c5576c3","html_url":"https://github.com/nolleh/serialize-interceptor","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nolleh%2Fserialize-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nolleh%2Fserialize-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nolleh%2Fserialize-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nolleh%2Fserialize-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nolleh","download_url":"https://codeload.github.com/nolleh/serialize-interceptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252305223,"owners_count":21726621,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interceptor","marshalling","middleware","nestjs","nodejs","npm-package","npm-packages","typescript"],"created_at":"2024-10-03T19:06:48.706Z","updated_at":"2025-05-07T08:33:28.887Z","avatar_url":"https://github.com/nolleh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://app.travis-ci.com/nolleh/serialize-interceptor.svg?branch=master)](https://app.travis-ci.com/nolleh/serialize-interceptor)\n[![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)\n[![npm version](https://badge.fury.io/js/serialize-interceptor.svg)](https://badge.fury.io/js/serialize-interceptor)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n## Overview\n\nSerializeInterceptor\n\nIntercepts request/response data and deserializes/serializes to DTO format.\n\n1. For requests: converts snake_case to camelCase (you can use camelCase in your DTO while accepting snake_case JSON input)\n2. For responses: converts camelCase to snake_case (you can use camelCase in your DTO while sending snake_case JSON to clients)\n\nIn summary:\n\n- JSON layer: snake_case\n- Model layer: camelCase\n\nThis conversion works for nested objects as well.\n\n## Example\n\nWhen a client sends the following data:\n\n```json\n{\n  \"name\": \"nolleh\",\n  \"email\": \"nolleh7707@gmail.com\",\n  \"some_snake_data\": \"hello world\",\n  \"live\": {\n    \"country\": \"South Korea\",\n    \"city\": \"Seongnam\",\n    \"some_snake_data\": \"hello world2\"\n  }\n}\n```\n\nYou can access it in your code as:\n\n```typescript\nclass LiveDto {\n  country,\n  city,\n  someSnakeData\n}\n\nclass MyDto {\n  name,\n  email,\n  someSnakeData,\n  live,\n}\n```\n\n## Usage\n\nAdd this to your main code.\nYou can find the complete example at:\n[](\"https://github.com/nolleh/serialize-interceptor/test/app.ts\")\n\n```typescript\n\nimport { SerializeInterceptor } from 'serialize-interceptor';\nimport { NestFactory } from '@nestjs/core';\n...\nconst app = await NestFactory.create(AppModule);\n/** use our interceptor **/\napp.useGlobalInterceptors(new SerializeInterceptor);\n\n// @since 1.1.5\n// if you want to customize serializer, then put your strategy.\n// const strategy: Strategy = {\n//   in: DEFAULT_STRATEGY.in,\n//   out: (v) =\u003e {\n//     // return 'test-swallow up!';\n//     return snakeToCamel(v)\n//   },\n// };\n// app.useGlobalInterceptors(new SerializeInterceptor(strategy));\n```\n\nOR in module\n\n```typescript\n@Module({\n  controllers: [AppController],\n  providers: [\n    {\n      provide: APP_INTERCEPTOR,\n      useClass: SerializeInterceptor,\n    },\n\n    /**  @since 1.1.5\n    // if you want to customize serializer, then put your strategy.\n    {\n      provide: Strategy,\n      useFactory: () =\u003e ({\n        in: DEFAULT_STRATEGY.in,\n        out: (v) =\u003e {\n          // return 'test-swallow up!';\n          // your custom func. the default for 'out' is.. snakeToCamel.\n          return snakeToCamel(v);\n        },\n      }),\n    },\n    **/\n})\n\nexport class AppModule {}\n```\n\n## Custom Serializer (Strategy)\n\nYou can define your own serialization strategy as shown in the snippets above.\n\nSerializeInterceptor provides classes to help you define your own strategy:\n\n```typescript\n/** Since the regenerated value's fields differ from the original,\n * it's challenging to declare the return type.\n * The input type is also not meaningful.\n *\n * in: request layer (default: snakeToCamel),\n * out: response layer (default: camelToSnake).\n *\n * i.e. const DEFAULT_STRATEGY: Strategy = { in: snakeToCamel, out: camelToSnake };\n */\nexport class Strategy {\n  in: (value: any) =\u003e any;\n  out: (value: any) =\u003e any;\n}\nexport const DEFAULT_STRATEGY: Strategy = {\n  in: snakeToCamel,\n  out: camelToSnake,\n};\n```\n\nAs shown above, implement the `Strategy` class with in/out functions,\nand provide it as a constructor (either through injection or by creating a new instance).\nThe interceptor will then work according to your definition.\n\n🤔 Do you need a specific strategy that you'd like to see provided by this library?  \nLet me know!\n\nAvailable strategies:\n\n| Name          | Description    | Remark (side effect)                                   | Default                    |\n| ------------- | -------------- | ------------------------------------------------------ | -------------------------- |\n| snakeToCamel  | snake -\u003e camel | dash(-, kebab) also converted to camel                 | default for in (request)   |\n| camelToSnake  | camel -\u003e snake | starting with capital (pascal) also converted to snake | default for out (response) |\n| kebabToCamel  | kebab -\u003e camel | No side effects                                        |                            |\n| camelToKebab  | camel -\u003e kebab | No side effects                                        |                            |\n| camelTosnake2 | camel ↔ snake | without kebab side effects                             |                            |\n| snakeToCamel2 | snake -\u003e camel | without kebab side effects                             |                            |\n\n⚠️ The default snakeToCamel / camelToSnake has side effects that also convert kebab-case and PascalCase.  \nWhile these side effects can be useful in many cases, they might not be desirable in all situations.\n\n## Dependencies\n\nNestJS\n\n\u003e Designed for NestJS interceptor.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnolleh%2Fserialize-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnolleh%2Fserialize-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnolleh%2Fserialize-interceptor/lists"}