Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This library has following features.
- Logger
- Result
- some utils
- NestJS Decorators

## 🚀 Getting Started

Expand Down Expand Up @@ -91,6 +92,37 @@ const result = await toResultAsync(promise);
console.log(result.isFailure()); // true
```

### NestJS Decorators

#### TypedApiParam

The TypedApiParam decorator is a type-safe wrapper around NestJS Swagger's @ApiParam decorator. It allows you to specify route parameters with type checking based on your DTO classes.

```typescript
import { Controller, Get } from '@nestjs/common';
import { TypedApiParam } from '@plugoinc/common';

class UserDto {
id: string;
name: string;
}

@Controller('users')
class UserController {
@TypedApiParam<UserDto>({
name: 'id',
type: 'string',
description: 'User ID',
})
@Get(':id')
getUser(@Param() params: UserDto) {
// Implementation
}
}
```

By using TypedApiParam, you get type checking for the `name` property, ensuring it matches a property from your DTO class.

## 🪪 License

MIT
Expand Down
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './api-paginated-response.decorator';
export * from './typed-api-param.decorator';
50 changes: 50 additions & 0 deletions src/decorators/typed-api-param.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Controller, Get } from '@nestjs/common';
import { DECORATORS } from '@nestjs/swagger/dist/constants';
import { ApiProperty } from '@nestjs/swagger';
import { TypedApiParam } from './typed-api-param.decorator';

describe('TypedApiParam', () => {
describe('classのmethodに設定された場合', () => {
class UserDto {
@ApiProperty({ required: true })
id: string;

@ApiProperty({ required: true })
name: string;
}

@Controller('tests')
class TestUserController {
@TypedApiParam<UserDto>({
name: 'id',
type: 'string',
description: 'User ID',
})
@Get(':id')
public get(): UserDto {
return { id: '1', name: 'test' };
}
}

it('メソッドにmetadataが設定されている', () => {
const controller = new TestUserController();
expect(
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.get),
).toBeTruthy();

const metadata = Reflect.getMetadata(
DECORATORS.API_PARAMETERS,
controller.get,
);
expect(metadata).toEqual([
{
name: 'id',
type: 'string',
description: 'User ID',
in: 'path',
required: true,
},
]);
});
});
});
14 changes: 14 additions & 0 deletions src/decorators/typed-api-param.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiParam, ApiParamOptions } from '@nestjs/swagger';

/**
* nestjs swaggerの @ApiParam をラッピングしたdecorator
* GenericsでDTOクラスを受け取り、そのkeyをApiParamのnameに型づけする
*
* @param options Swagger parameter options with typed name property
* @returns Decorator function
*/
export const TypedApiParam = <T extends object>(
options: ApiParamOptions & { name: keyof T },
) => {
return ApiParam({ ...options });
};