UNPKG

2.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const constants_1 = require("../../constants");
4const shared_utils_1 = require("../../utils/shared.utils");
5/**
6 * Decorator that marks a class as a Nest controller that can receive inbound
7 * requests and produce responses.
8 *
9 * An HTTP Controller responds to inbound HTTP Requests and produces HTTP Responses.
10 * It defines a class that provides the context for one or more related route
11 * handlers that correspond to HTTP request methods and associated routes
12 * for example `GET /api/profile`, `POST /user/resume`
13 *
14 * A Microservice Controller responds to requests as well as events, running over
15 * a variety of transports [(read more here)](https://docs.nestjs.com/microservices/basics).
16 * It defines a class that provides a context for one or more message or event
17 * handlers.
18 *
19 * @param prefixOrOptions a `route path prefix` or a `ControllerOptions` object.
20 * A `route path prefix` is pre-pended to the path specified in any request decorator
21 * in the class. `ControllerOptions` is an options configuration object specifying:
22 * - `scope` - symbol that determines the lifetime of a Controller instance.
23 * [See Scope](https://docs.nestjs.com/fundamentals/injection-scopes#usage) for
24 * more details.
25 * - `prefix` - string that defines a `route path prefix`. The prefix
26 * is pre-pended to the path specified in any request decorator in the class.
27 *
28 * @see [Routing](https://docs.nestjs.com/controllers#routing)
29 * @see [Controllers](https://docs.nestjs.com/controllers)
30 * @see [Microservices](https://docs.nestjs.com/microservices/basics#request-response)
31 * @see [Scope](https://docs.nestjs.com/fundamentals/injection-scopes#usage)
32 *
33 * @publicApi
34 */
35function Controller(prefixOrOptions) {
36 const defaultPath = '/';
37 const [path, host, scopeOptions] = shared_utils_1.isUndefined(prefixOrOptions)
38 ? [defaultPath, undefined, undefined]
39 : shared_utils_1.isString(prefixOrOptions)
40 ? [prefixOrOptions, undefined, undefined]
41 : [
42 prefixOrOptions.path || defaultPath,
43 prefixOrOptions.host,
44 { scope: prefixOrOptions.scope },
45 ];
46 return (target) => {
47 Reflect.defineMetadata(constants_1.PATH_METADATA, path, target);
48 Reflect.defineMetadata(constants_1.HOST_METADATA, host, target);
49 Reflect.defineMetadata(constants_1.SCOPE_OPTIONS_METADATA, scopeOptions, target);
50 };
51}
52exports.Controller = Controller;