-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support of ApiParam and ApiQuery for Controller
- Loading branch information
1 parent
d216fd0
commit 3967c4f
Showing
7 changed files
with
306 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
import { DECORATORS } from '../../lib/constants'; | ||
import { ApiParam } from '../../lib/decorators'; | ||
|
||
describe('ApiParam', () => { | ||
describe('class decorator', () => { | ||
@ApiParam({ name: 'testId' }) | ||
@Controller('tests/:testId') | ||
class TestAppController { | ||
@Get() | ||
public get(@Param('testId') testId: string): string { | ||
return testId; | ||
} | ||
|
||
public noAPiMethod(): void { | ||
} | ||
} | ||
|
||
it('should get ApiParam options from api method', () => { | ||
const controller = new TestAppController(); | ||
expect( | ||
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toBeTruthy(); | ||
expect( | ||
Reflect.getMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toEqual([{ in: 'path', name: 'testId', required: true }]); | ||
}); | ||
|
||
it('should not get ApiParam options from non api method', () => { | ||
const controller = new TestAppController(); | ||
expect( | ||
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.noAPiMethod) | ||
).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('method decorator', () => { | ||
@Controller('tests/:testId') | ||
class TestAppController { | ||
@Get() | ||
@ApiParam({ name: 'testId' }) | ||
public get(@Param('testId') testId: string): string { | ||
return testId; | ||
} | ||
} | ||
|
||
it('should get ApiParam options from api method', () => { | ||
const controller = new TestAppController(); | ||
expect( | ||
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toBeTruthy(); | ||
expect( | ||
Reflect.getMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toEqual([{ in: 'path', name: 'testId', required: true }]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Controller, Get, Query } from '@nestjs/common'; | ||
import { DECORATORS } from '../../lib/constants'; | ||
import { ApiQuery } from '../../lib/decorators'; | ||
|
||
describe('ApiQuery', () => { | ||
describe('class decorator', () => { | ||
@ApiQuery({ name: 'testId' }) | ||
@Controller('test') | ||
class TestAppController { | ||
@Get() | ||
public get(@Query('testId') testId: string): string { | ||
return testId; | ||
} | ||
|
||
public noAPiMethod(): void { | ||
} | ||
} | ||
|
||
it('should get ApiQuery options from api method', () => { | ||
const controller = new TestAppController(); | ||
expect( | ||
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toBeTruthy(); | ||
expect( | ||
Reflect.getMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toEqual([{ in: 'query', name: 'testId', required: true }]); | ||
}); | ||
|
||
it('should not get ApiQuery options from non api method', () => { | ||
const controller = new TestAppController(); | ||
expect( | ||
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.noAPiMethod) | ||
).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('method decorator', () => { | ||
@Controller('tests/:testId') | ||
class TestAppController { | ||
@Get() | ||
@ApiQuery({ name: 'testId' }) | ||
public get(@Query('testId') testId: string): string { | ||
return testId; | ||
} | ||
} | ||
|
||
it('should get ApiQuery options from api method', () => { | ||
const controller = new TestAppController(); | ||
expect( | ||
Reflect.hasMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toBeTruthy(); | ||
expect( | ||
Reflect.getMetadata(DECORATORS.API_PARAMETERS, controller.get) | ||
).toEqual([{ in: 'query', name: 'testId', required: true }]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters