Skip to content

Commit 464c2bb

Browse files
Merge pull request nestjs#2688 from BrunnerLivio/feature/api-docs
docs() add API documentation
2 parents a9aaf86 + 85f3897 commit 464c2bb

83 files changed

Lines changed: 2071 additions & 143 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/common/PACKAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The common package comes with decorators such as `@Controller()`, `@Injectable` and so on.

packages/common/cache/cache.module.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,40 @@ import {
88
CacheOptionsFactory,
99
} from './interfaces/cache-module.interface';
1010

11+
/**
12+
* Module that provides Nest cache-manager.
13+
*
14+
* @see [Caching](https://docs.nestjs.com/techniques/caching)
15+
*
16+
* @publicApi
17+
*/
1118
@Module({
1219
providers: [createCacheManager()],
1320
exports: [CACHE_MANAGER],
1421
})
1522
export class CacheModule {
23+
/**
24+
* Configure the cache manager statically.
25+
*
26+
* @param options options to configure the cache manager
27+
*
28+
* @see [Customize caching](https://docs.nestjs.com/techniques/caching#customize-caching)
29+
*/
1630
static register(options: CacheModuleOptions = {}): DynamicModule {
1731
return {
1832
module: CacheModule,
1933
providers: [{ provide: CACHE_MODULE_OPTIONS, useValue: options }],
2034
};
2135
}
2236

37+
/**
38+
* Configure the cache manager dynamically.
39+
*
40+
* @param options method for dynamically supplying cache manager configuration
41+
* options
42+
*
43+
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration)
44+
*/
2345
static registerAsync(options: CacheModuleAsyncOptions): DynamicModule {
2446
return {
2547
module: CacheModule,

packages/common/cache/cache.providers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { CACHE_MANAGER, CACHE_MODULE_OPTIONS } from './cache.constants';
44
import { defaultCacheOptions } from './default-options';
55
import { CacheManagerOptions } from './interfaces/cache-manager.interface';
66

7+
/**
8+
* Creates a CacheManager Provider.
9+
*
10+
* @publicApi
11+
*/
712
export function createCacheManager(): Provider {
813
return {
914
provide: CACHE_MANAGER,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { SetMetadata } from '../../decorators';
22
import { CACHE_KEY_METADATA } from '../cache.constants';
33

4+
/**
5+
* Decorator that sets the caching key used to store/retrieve cached items for
6+
* Web sockets or Microservice based apps.
7+
*
8+
* For example:
9+
* `@CacheKey('events')`
10+
*
11+
* @param key string naming the field to be used as a cache key
12+
*
13+
* @see [Caching](https://docs.nestjs.com/techniques/caching)
14+
*
15+
* @publicApi
16+
*/
417
export const CacheKey = (key: string) => SetMetadata(CACHE_KEY_METADATA, key);

packages/common/cache/interfaces/cache-manager.interface.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,70 @@ export interface LiteralObject {
22
[key: string]: any;
33
}
44

5+
/**
6+
* Interface defining a cache store. Implement this interface to create a custom
7+
* cache store.
8+
*
9+
* @publicApi
10+
*/
511
export interface CacheStore {
12+
/**
13+
* Create a key/value pair in the cache.
14+
*
15+
* @param key cache key
16+
* @param value cache value
17+
*/
618
set<T>(key: string, value: T): Promise<void> | void;
19+
/**
20+
* Retrieve a key/value pair from the cache.
21+
*
22+
* @param key cache key
23+
*/
724
get<T>(key: string): Promise<void> | void;
25+
/**
26+
* Destroy a key/value pair from the cache.
27+
*
28+
* @param key cache key
29+
*/
830
del(key: string): void | Promise<void>;
931
}
1032

33+
/**
34+
* Interface defining a factory to create a cache store.
35+
*
36+
* @publicApi
37+
*/
1138
export interface CacheStoreFactory {
39+
/**
40+
* Return a configured cache store.
41+
*
42+
* @param args Cache manager options received from `CacheModule.register()`
43+
* or `CacheModule.registerAcync()`
44+
*/
1245
create(args: LiteralObject): CacheStore;
1346
}
1447

48+
/**
49+
* Interface defining Cache Manager configuration options.
50+
*
51+
* @publicApi
52+
*/
1553
export interface CacheManagerOptions {
54+
/**
55+
* Cache storage manager. Default is `'memory'` (in-memory store). See
56+
* [Different stores](https://docs.nestjs.com/techniques/caching#different-stores)
57+
* for more info.
58+
*/
1659
store?: string | CacheStoreFactory;
60+
/**
61+
* Time to live - amount of time in seconds that a response is cached before it
62+
* is deleted. Subsequent request will call through the route handler and refresh
63+
* the cache. Defaults to 5 seconds.
64+
*/
1765
ttl?: number;
66+
/**
67+
* Maximum number of responses to store in the cache. Defaults to 100.
68+
*/
1869
max?: number;
1970
isCacheableValue?: (value: any) => boolean;
2071
}

packages/common/cache/interfaces/cache-module.interface.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,47 @@ export interface CacheModuleOptions extends CacheManagerOptions {
55
[key: string]: any;
66
}
77

8+
/**
9+
* Interface describing a `CacheOptionsFactory`. Providers supplying configuration
10+
* options for the Cache module must implement this interface.
11+
*
12+
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration)
13+
*
14+
* @publicApi
15+
*/
816
export interface CacheOptionsFactory {
917
createCacheOptions(): Promise<CacheModuleOptions> | CacheModuleOptions;
1018
}
1119

20+
/**
21+
* Options for dynamically configuring the Cache module.
22+
*
23+
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration)
24+
*
25+
* @publicApi
26+
*/
1227
export interface CacheModuleAsyncOptions
1328
extends Pick<ModuleMetadata, 'imports'> {
29+
/**
30+
* Injection token resolving to an existing provider. The provider must implement
31+
* the `CacheOptionsFactory` interface.
32+
*/
1433
useExisting?: Type<CacheOptionsFactory>;
34+
/**
35+
* Injection token resolving to a class that will be instantiated as a provider.
36+
* The class must implement the `CacheOptionsFactory` interface.
37+
*/
1538
useClass?: Type<CacheOptionsFactory>;
39+
/**
40+
* Function returning options (or a Promise resolving to options) to configure the
41+
* cache module.
42+
*/
1643
useFactory?: (
1744
...args: any[]
1845
) => Promise<CacheModuleOptions> | CacheModuleOptions;
46+
/**
47+
* Dependencies that a Factory may inject.
48+
*/
1949
inject?: any[];
2050
extraProviders?: Provider[];
2151
}

packages/common/decorators/core/bind.decorator.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
2-
* Binds parameter decorators to the method
3-
* Useful when the language doesn't provide a 'Parameter Decorators' feature (vanilla JavaScript)
4-
* @param {} ...decorators
2+
* Decorator that binds *parameter decorators* to the method that follows.
3+
*
4+
* Useful when the language doesn't provide a 'Parameter Decorator' feature
5+
* (i.e., vanilla JavaScript).
6+
*
7+
* @param decorators one or more parameter decorators (e.g., `Req()`)
8+
*
9+
* @publicApi
510
*/
611
export function Bind(...decorators: any[]): MethodDecorator {
712
return <T>(

packages/common/decorators/core/catch.decorator.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@ import { FILTER_CATCH_EXCEPTIONS } from '../../constants';
22
import { Type } from '../../interfaces';
33

44
/**
5-
* Defines an exception filter. Takes set of exception types as arguments which have to be caught by this filter.
6-
* The class should implement the `ExceptionFilter` interface.
5+
* Decorator that marks a class as a Nest exception filter. An exception filter
6+
* handles exceptions thrown by or not handled by your application code.
7+
*
8+
* The decorated class must implement the `ExceptionFilter` interface.
9+
*
10+
* @param exceptions one or more exception *types* specifying
11+
* the exceptions to be caught and handled by this filter.
12+
*
13+
* @see [Exception Filters](https://docs.nestjs.com/exception-filters)
14+
*
15+
* @usageNotes
16+
* Exception filters are applied using the `@UseFilters()` decorator, or (globally)
17+
* with `app.useGlobalFilters()`.
18+
*
19+
* @publicApi
720
*/
821
export function Catch(...exceptions: Type<any>[]): ClassDecorator {
922
return (target: object) => {

packages/common/decorators/core/controller.decorator.ts

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,139 @@ import { PATH_METADATA, SCOPE_OPTIONS_METADATA } from '../../constants';
22
import { isString, isUndefined } from '../../utils/shared.utils';
33
import { ScopeOptions } from './../../interfaces/scope-options.interface';
44

5+
/**
6+
* Interface defining options that can be passed to `@Controller()` decorator
7+
*
8+
* @publicApi
9+
*/
510
export interface ControllerOptions extends ScopeOptions {
11+
/**
12+
* Specifies an optional `route path prefix`. The prefix is pre-pended to the
13+
* path specified in any request decorator in the class.
14+
*
15+
* @see [Routing](https://docs.nestjs.com/controllers#routing)
16+
*/
617
path?: string;
718
}
819

20+
export function Controller();
21+
export function Controller(prefix: string);
22+
export function Controller(options: ControllerOptions);
23+
/**
24+
* Decorator that marks a class as a Nest controller that can receive inbound
25+
* requests and produce responses.
26+
*
27+
* An HTTP Controller responds to inbound HTTP Requests and produces HTTP Responses.
28+
* It defines a class that provides the context for one or more related route
29+
* handlers that correspond to HTTP request methods and associated routes
30+
* for example `GET /api/profile`, `POST /user/resume`.
31+
*
32+
* A Microservice Controller responds to requests as well as events, running over
33+
* a variety of transports [(read more here)](https://docs.nestjs.com/microservices/basics).
34+
* It defines a class that provides a context for one or more message or event
35+
* handlers.
36+
*
37+
* @see [Controllers](https://docs.nestjs.com/controllers)
38+
* @see [Microservices](https://docs.nestjs.com/microservices/basics#request-response)
39+
*
40+
* @publicApi
41+
*/
42+
export function Controller();
43+
44+
/**
45+
* Decorator that marks a class as a Nest controller that can receive inbound
46+
* requests and produce responses.
47+
*
48+
* An HTTP Controller responds to inbound HTTP Requests and produces HTTP Responses.
49+
* It defines a class that provides the context for one or more related route
50+
* handlers that correspond to HTTP request methods and associated routes
51+
* for example `GET /api/profile`, `POST /user/resume`.
52+
*
53+
* A Microservice Controller responds to requests as well as events, running over
54+
* a variety of transports [(read more here)](https://docs.nestjs.com/microservices/basics).
55+
* It defines a class that provides a context for one or more message or event
56+
* handlers.
57+
*
58+
* @param {string} prefix string that defines a `route path prefix`. The prefix
59+
* is pre-pended to the path specified in any request decorator in the class.
60+
*
61+
* @see [Routing](https://docs.nestjs.com/controllers#routing)
62+
* @see [Controllers](https://docs.nestjs.com/controllers)
63+
* @see [Microservices](https://docs.nestjs.com/microservices/basics#request-response)
64+
*
65+
* @publicApi
66+
*/
67+
export function Controller(prefix: string);
68+
69+
/**
70+
* Decorator that marks a class as a Nest controller that can receive inbound
71+
* requests and produce responses.
72+
*
73+
* An HTTP Controller responds to inbound HTTP Requests and produces HTTP Responses.
74+
* It defines a class that provides the context for one or more related route
75+
* handlers that correspond to HTTP request methods and associated routes
76+
* for example `GET /api/profile`, `POST /user/resume`.
77+
*
78+
* A Microservice Controller responds to requests as well as events, running over
79+
* a variety of transports [(read more here)](https://docs.nestjs.com/microservices/basics).
80+
* It defines a class that provides a context for one or more message or event
81+
* handlers.
82+
*
83+
* @param {object} options configuration object specifying:
84+
*
85+
* - `scope` - symbol that determines the lifetime of a Controller instance.
86+
* [See Scope](https://docs.nestjs.com/fundamentals/injection-scopes#usage) for
87+
* more details.
88+
* - `prefix` - string that defines a `route path prefix`. The prefix
89+
* is pre-pended to the path specified in any request decorator in the class.
90+
*
91+
* @see [Routing](https://docs.nestjs.com/controllers#routing)
92+
* @see [Controllers](https://docs.nestjs.com/controllers)
93+
* @see [Microservices](https://docs.nestjs.com/microservices/basics#request-response)
94+
*
95+
* @publicApi
96+
*/
97+
export function Controller(options: ControllerOptions);
98+
999
/**
10-
* Defines the controller. Controller can inject dependencies through constructor.
11-
* Those dependencies have to belong to the same module.
100+
* Decorator that marks a class as a Nest controller that can receive inbound
101+
* requests and produce responses.
102+
*
103+
* An HTTP Controller responds to inbound HTTP Requests and produces HTTP Responses.
104+
* It defines a class that provides the context for one or more related route
105+
* handlers that correspond to HTTP request methods and associated routes
106+
* for example `GET /api/profile`, `POST /user/resume`
107+
*
108+
* A Microservice Controller responds to requests as well as events, running over
109+
* a variety of transports [(read more here)](https://docs.nestjs.com/microservices/basics).
110+
* It defines a class that provides a context for one or more message or event
111+
* handlers.
112+
*
113+
* @param prefixOrOptions a `route path prefix` or a `ControllerOptions` object.
114+
* A `route path prefix` is pre-pended to the path specified in any request decorator
115+
* in the class. `ControllerOptions` is an options configuration object specifying:
116+
* - `scope` - symbol that determines the lifetime of a Controller instance.
117+
* [See Scope](https://docs.nestjs.com/fundamentals/injection-scopes#usage) for
118+
* more details.
119+
* - `prefix` - string that defines a `route path prefix`. The prefix
120+
* is pre-pended to the path specified in any request decorator in the class.
121+
*
122+
* @see [Routing](https://docs.nestjs.com/controllers#routing)
123+
* @see [Controllers](https://docs.nestjs.com/controllers)
124+
* @see [Microservices](https://docs.nestjs.com/microservices/basics#request-response)
125+
* @see [Scope](https://docs.nestjs.com/fundamentals/injection-scopes#usage)
126+
*
127+
* @publicApi
12128
*/
13-
export function Controller(): ClassDecorator;
14-
export function Controller(prefix: string): ClassDecorator;
15-
export function Controller(options: ControllerOptions): ClassDecorator;
16129
export function Controller(
17130
prefixOrOptions?: string | ControllerOptions,
18131
): ClassDecorator {
19132
const defaultPath = '/';
20133
const [path, scopeOptions] = isUndefined(prefixOrOptions)
21134
? [defaultPath, undefined]
22135
: isString(prefixOrOptions)
23-
? [prefixOrOptions, undefined]
24-
: [prefixOrOptions.path || defaultPath, { scope: prefixOrOptions.scope }];
136+
? [prefixOrOptions, undefined]
137+
: [prefixOrOptions.path || defaultPath, { scope: prefixOrOptions.scope }];
25138

26139
return (target: object) => {
27140
Reflect.defineMetadata(PATH_METADATA, path, target);

0 commit comments

Comments
 (0)