-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: resolve conflicts, tiny updates
- Loading branch information
Showing
18 changed files
with
523 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
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,45 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { DECORATORS } from '../constants'; | ||
|
||
/** | ||
* Set the default getter for the given type to the decorated method | ||
* | ||
* This is to be used in conjunction with `ApiProperty({link: () => Type})` to generate link objects | ||
* in the swagger schema | ||
* | ||
* ```typescript | ||
* @Controller('users') | ||
* class UserController { | ||
* @Get(':userId') | ||
* @ApiDefaultGetter(UserGet, 'userId') | ||
* getUser(@Param('userId') userId: string) { | ||
* // ... | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param type The type for which the decorated function is the default getter | ||
* @param parameter Name of the parameter in the route of the getter which corresponds to the id of the type | ||
* | ||
* @see [Swagger link objects](https://swagger.io/docs/specification/links/) | ||
*/ | ||
export function ApiDefaultGetter( | ||
type: Type<unknown> | Function, | ||
parameter: string | ||
): MethodDecorator { | ||
return ( | ||
prototype: object, | ||
key: string | symbol, | ||
descriptor: PropertyDescriptor | ||
) => { | ||
if (type.prototype) { | ||
Reflect.defineMetadata( | ||
DECORATORS.API_DEFAULT_GETTER, | ||
{ getter: descriptor.value, parameter, prototype }, | ||
type.prototype | ||
); | ||
} | ||
|
||
return descriptor; | ||
}; | ||
} |
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,76 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { DECORATORS } from '../constants'; | ||
|
||
export interface ApiLinkOptions { | ||
from: Type<unknown> | Function; | ||
/** | ||
* Field in the type `from` which is used as a parameter in the decorated route | ||
* | ||
* @default 'id' | ||
*/ | ||
fromField?: string; | ||
/** | ||
* Name of the parameter in the decorated route | ||
*/ | ||
routeParam: string; | ||
} | ||
|
||
/** | ||
* Defines this route as a link between two types | ||
* | ||
* Typically used when the link between the types is not present in the `from` type, | ||
* eg with the following | ||
* | ||
* ```typescript | ||
* class User { | ||
* @ApiProperty() | ||
* id: string | ||
* | ||
* // no field documentIds: string[] | ||
* } | ||
* | ||
* class Document { | ||
* @ApiProperty() | ||
* id: string | ||
* } | ||
* | ||
* @Controller() | ||
* class UserController { | ||
* @Get(':userId/documents') | ||
* @ApiLink({from: User, fromField: 'id', routeParam: 'userId'}) | ||
* getDocuments(@Param('userId') userId: string)): Promise<Documents[]> | ||
* } | ||
* ``` | ||
* | ||
* @param type The type for which the decorated function is the default getter | ||
* @param parameter Name of the parameter in the route of the getter which corresponds to the id of the type | ||
* | ||
* @see [Swagger link objects](https://swagger.io/docs/specification/links/) | ||
*/ | ||
export function ApiLink({ | ||
from, | ||
fromField = 'id', | ||
routeParam | ||
}: ApiLinkOptions): MethodDecorator { | ||
return ( | ||
controllerPrototype: object, | ||
key: string | symbol, | ||
descriptor: PropertyDescriptor | ||
) => { | ||
const { prototype } = from; | ||
if (prototype) { | ||
const links = Reflect.getMetadata(DECORATORS.API_LINK, prototype) ?? []; | ||
|
||
links.push({ | ||
method: descriptor.value, | ||
prototype: controllerPrototype, | ||
field: fromField, | ||
parameter: routeParam | ||
}); | ||
|
||
Reflect.defineMetadata(DECORATORS.API_LINK, links, prototype); | ||
} | ||
|
||
return descriptor; | ||
}; | ||
} |
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
Oops, something went wrong.