I have defined a DTO
and configured it as follows to use common properties in multiple places.
<code>// base-brand.dto.ts
import { ApiProperty } from '@nestjs/swagger';
export class BaseBrandDto {
@ApiProperty({
type: Number,
required: true,
example: 1,
})
readonly id: number;
@ApiProperty({
type: String,
required: true,
example: 'Default',
})
readonly name: string;
}
</code>
<code>// base-brand.dto.ts
import { ApiProperty } from '@nestjs/swagger';
export class BaseBrandDto {
@ApiProperty({
type: Number,
required: true,
example: 1,
})
readonly id: number;
@ApiProperty({
type: String,
required: true,
example: 'Default',
})
readonly name: string;
}
</code>
// base-brand.dto.ts
import { ApiProperty } from '@nestjs/swagger';
export class BaseBrandDto {
@ApiProperty({
type: Number,
required: true,
example: 1,
})
readonly id: number;
@ApiProperty({
type: String,
required: true,
example: 'Default',
})
readonly name: string;
}
<code>// game-brand.dto.ts
import { ApiProperty, PickType } from '@nestjs/swagger';
import { BaseBrandDto } from './base/base-brand.dto';
export class GameBrandDto extends PickType(BaseBrandDto, ['id', 'name'] as const) {
@ApiProperty({
example: 'EA',
})
readonly name: string;
}
</code>
<code>// game-brand.dto.ts
import { ApiProperty, PickType } from '@nestjs/swagger';
import { BaseBrandDto } from './base/base-brand.dto';
export class GameBrandDto extends PickType(BaseBrandDto, ['id', 'name'] as const) {
@ApiProperty({
example: 'EA',
})
readonly name: string;
}
</code>
// game-brand.dto.ts
import { ApiProperty, PickType } from '@nestjs/swagger';
import { BaseBrandDto } from './base/base-brand.dto';
export class GameBrandDto extends PickType(BaseBrandDto, ['id', 'name'] as const) {
@ApiProperty({
example: 'EA',
})
readonly name: string;
}
<code>// find-all-game-brands.dto.ts
import { ApiProperty, PickType } from '@nestjs/swagger';
import { ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { GameBrandDto } from './game-brand.dto.ts';
class GameBrandListsDto {
@ValidateNested({ each : true })
@Type(() => GameBrandDto)
@ApiProperty({
type: [GameBrandDto],
required: true,
examples: [GameBrandDto],
})
readonly lists: GameBrandDto[];
}
export class FindAllGameBrandsDto extends PickType(BaseResponseDto, ['result', 'message'] as a const) {
@ValidatedNested({ each : true })
@Type(() => GameBrandListsDto)
@ApiProperty({
type: GameBrandListsDto,
required: true,
example: GameBrandListsDto,
})
readonly brand: GameBrandListsDto;
}
</code>
<code>// find-all-game-brands.dto.ts
import { ApiProperty, PickType } from '@nestjs/swagger';
import { ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { GameBrandDto } from './game-brand.dto.ts';
class GameBrandListsDto {
@ValidateNested({ each : true })
@Type(() => GameBrandDto)
@ApiProperty({
type: [GameBrandDto],
required: true,
examples: [GameBrandDto],
})
readonly lists: GameBrandDto[];
}
export class FindAllGameBrandsDto extends PickType(BaseResponseDto, ['result', 'message'] as a const) {
@ValidatedNested({ each : true })
@Type(() => GameBrandListsDto)
@ApiProperty({
type: GameBrandListsDto,
required: true,
example: GameBrandListsDto,
})
readonly brand: GameBrandListsDto;
}
</code>
// find-all-game-brands.dto.ts
import { ApiProperty, PickType } from '@nestjs/swagger';
import { ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { GameBrandDto } from './game-brand.dto.ts';
class GameBrandListsDto {
@ValidateNested({ each : true })
@Type(() => GameBrandDto)
@ApiProperty({
type: [GameBrandDto],
required: true,
examples: [GameBrandDto],
})
readonly lists: GameBrandDto[];
}
export class FindAllGameBrandsDto extends PickType(BaseResponseDto, ['result', 'message'] as a const) {
@ValidatedNested({ each : true })
@Type(() => GameBrandListsDto)
@ApiProperty({
type: GameBrandListsDto,
required: true,
example: GameBrandListsDto,
})
readonly brand: GameBrandListsDto;
}
This form doesn’t present any issues in the NestJS CLI
. However, accessing the Swagger documentation causes a console error
Uncaught ReferenceError: _swagger is not defined”
It suggests that there is an issue with init
, showing the following error line.
<code>“GameBrandListsDto”: {
“type”: “object”,
“properties”: {
“lists”: {
“examples”: [
class GameBrandDto extends (0, _swagger.PickType)(_basebranddto.BaseBrandDto, [
'id',
'name'
])
</code>
<code>“GameBrandListsDto”: {
“type”: “object”,
“properties”: {
“lists”: {
“examples”: [
class GameBrandDto extends (0, _swagger.PickType)(_basebranddto.BaseBrandDto, [
'id',
'name'
])
</code>
“GameBrandListsDto”: {
“type”: “object”,
“properties”: {
“lists”: {
“examples”: [
class GameBrandDto extends (0, _swagger.PickType)(_basebranddto.BaseBrandDto, [
'id',
'name'
])
What seems to be the problem?