Skip to content

Commit

Permalink
sample(@nestjs) add execution context app example
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 27, 2018
1 parent 707b3ef commit eb05d23
Show file tree
Hide file tree
Showing 583 changed files with 17,628 additions and 94 deletions.
25 changes: 25 additions & 0 deletions lib/common/constants.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export declare const metadata: {
MODULES: string;
IMPORTS: string;
COMPONENTS: string;
PROVIDERS: string;
CONTROLLERS: string;
EXPORTS: string;
};
export declare const SHARED_MODULE_METADATA = '__sharedModule__';
export declare const GLOBAL_MODULE_METADATA = '__globalModule__';
export declare const PATH_METADATA = 'path';
export declare const PARAMTYPES_METADATA = 'design:paramtypes';
export declare const SELF_DECLARED_DEPS_METADATA = 'self:paramtypes';
export declare const METHOD_METADATA = 'method';
export declare const ROUTE_ARGS_METADATA = '__routeArguments__';
export declare const CUSTOM_ROUTE_AGRS_METADATA = '__customRouteArgs__';
export declare const EXCEPTION_FILTERS_METADATA = '__exceptionFilters__';
export declare const FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__';
export declare const PIPES_METADATA = '__pipes__';
export declare const GUARDS_METADATA = '__guards__';
export declare const RENDER_METADATA = '__renderTemplate__';
export declare const INTERCEPTORS_METADATA = '__interceptors__';
export declare const HTTP_CODE_METADATA = '__httpCode__';
export declare const GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
export declare const MODULE_PATH = '__module_path__';
27 changes: 27 additions & 0 deletions lib/common/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.metadata = {
MODULES: 'modules',
IMPORTS: 'imports',
COMPONENTS: 'components',
PROVIDERS: 'providers',
CONTROLLERS: 'controllers',
EXPORTS: 'exports',
};
exports.SHARED_MODULE_METADATA = '__sharedModule__';
exports.GLOBAL_MODULE_METADATA = '__globalModule__';
exports.PATH_METADATA = 'path';
exports.PARAMTYPES_METADATA = 'design:paramtypes';
exports.SELF_DECLARED_DEPS_METADATA = 'self:paramtypes';
exports.METHOD_METADATA = 'method';
exports.ROUTE_ARGS_METADATA = '__routeArguments__';
exports.CUSTOM_ROUTE_AGRS_METADATA = '__customRouteArgs__';
exports.EXCEPTION_FILTERS_METADATA = '__exceptionFilters__';
exports.FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__';
exports.PIPES_METADATA = '__pipes__';
exports.GUARDS_METADATA = '__guards__';
exports.RENDER_METADATA = '__renderTemplate__';
exports.INTERCEPTORS_METADATA = '__interceptors__';
exports.HTTP_CODE_METADATA = '__httpCode__';
exports.GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
exports.MODULE_PATH = '__module_path__';
8 changes: 8 additions & 0 deletions lib/common/decorators/core/bind.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Binds parameters decorators to the particular method
* Useful when the language doesn't provide a 'Parameter Decorators' feature (vanilla JavaScript)
* @param {} ...decorators
*/
export declare function Bind(
...decorators: any[]
): (target: object, key: any, descriptor: any) => any;
14 changes: 14 additions & 0 deletions lib/common/decorators/core/bind.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Binds parameters decorators to the particular method
* Useful when the language doesn't provide a 'Parameter Decorators' feature (vanilla JavaScript)
* @param {} ...decorators
*/
function Bind(...decorators) {
return (target, key, descriptor) => {
decorators.forEach((fn, index) => fn(target, key, index));
return descriptor;
};
}
exports.Bind = Bind;
6 changes: 6 additions & 0 deletions lib/common/decorators/core/catch.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'reflect-metadata';
/**
* Defines the Exceptions Filter. Takes set of exception types as an argument which has to be caught by this Filter.
* The class should implement the `ExceptionFilter` interface.
*/
export declare function Catch(...exceptions: any[]): ClassDecorator;
14 changes: 14 additions & 0 deletions lib/common/decorators/core/catch.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const constants_1 = require("../../constants");
/**
* Defines the Exceptions Filter. Takes set of exception types as an argument which has to be caught by this Filter.
* The class should implement the `ExceptionFilter` interface.
*/
function Catch(...exceptions) {
return (target) => {
Reflect.defineMetadata(constants_1.FILTER_CATCH_EXCEPTIONS, exceptions, target);
};
}
exports.Catch = Catch;
27 changes: 27 additions & 0 deletions lib/common/decorators/core/component.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Defines the injectable class. This class can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export declare function Injectable(): ClassDecorator;
/**
* Defines the Component. The component can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export declare function Component(): ClassDecorator;
/**
* Defines the Pipe. The Pipe should implement the `PipeTransform` interface.
*/
export declare function Pipe(): ClassDecorator;
/**
* Defines the Guard. The Guard should implement the `CanActivate` interface.
*/
export declare function Guard(): ClassDecorator;
/**
* Defines the Middleware. The Middleware should implement the `NestMiddleware` interface.
*/
export declare function Middleware(): ClassDecorator;
/**
* Defines the Interceptor. The Interceptor should implement `HttpInterceptor`, `RpcInterceptor` or `WsInterceptor` interface.
*/
export declare function Interceptor(): ClassDecorator;
export declare function mixin(mixinClass: any): any;
60 changes: 60 additions & 0 deletions lib/common/decorators/core/component.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const deprecate = require("deprecate");
/**
* Defines the injectable class. This class can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
function Injectable() {
return (target) => { };
}
exports.Injectable = Injectable;
/**
* Defines the Component. The component can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
function Component() {
deprecate('The @Component() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.');
return (target) => { };
}
exports.Component = Component;
/**
* Defines the Pipe. The Pipe should implement the `PipeTransform` interface.
*/
function Pipe() {
return (target) => { };
}
exports.Pipe = Pipe;
/**
* Defines the Guard. The Guard should implement the `CanActivate` interface.
*/
function Guard() {
deprecate('The @Guard() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.');
return (target) => { };
}
exports.Guard = Guard;
/**
* Defines the Middleware. The Middleware should implement the `NestMiddleware` interface.
*/
function Middleware() {
deprecate('The @Middleware() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.');
return (target) => { };
}
exports.Middleware = Middleware;
/**
* Defines the Interceptor. The Interceptor should implement `HttpInterceptor`, `RpcInterceptor` or `WsInterceptor` interface.
*/
function Interceptor() {
deprecate('The @Interceptor() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.');
return (target) => { };
}
exports.Interceptor = Interceptor;
function mixin(mixinClass) {
this.offset = this.offset ? ++this.offset : Math.random() * 100;
Object.defineProperty(mixinClass, 'name', {
value: JSON.stringify(this.offset),
});
Injectable()(mixinClass);
return mixinClass;
}
exports.mixin = mixin;
6 changes: 6 additions & 0 deletions lib/common/decorators/core/controller.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'reflect-metadata';
/**
* Defines the Controller. The controller can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export declare function Controller(prefix?: string): ClassDecorator;
16 changes: 16 additions & 0 deletions lib/common/decorators/core/controller.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const shared_utils_1 = require("../../utils/shared.utils");
const constants_1 = require("../../constants");
/**
* Defines the Controller. The controller can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
function Controller(prefix) {
const path = shared_utils_1.isUndefined(prefix) ? '/' : prefix;
return (target) => {
Reflect.defineMetadata(constants_1.PATH_METADATA, path, target);
};
}
exports.Controller = Controller;
3 changes: 3 additions & 0 deletions lib/common/decorators/core/dependencies.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'reflect-metadata';
export declare function flatten(arr: any[]): any;
export declare const Dependencies: (...dependencies: any[]) => ClassDecorator;
16 changes: 16 additions & 0 deletions lib/common/decorators/core/dependencies.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const constants_1 = require("../../constants");
function flatten(arr) {
const flat = [].concat(...arr);
return flat.some(Array.isArray) ? flatten(flat) : flat;
}
exports.flatten = flatten;
;
exports.Dependencies = (...dependencies) => {
const flattenDeps = flatten(dependencies);
return (target) => {
Reflect.defineMetadata(constants_1.PARAMTYPES_METADATA, flattenDeps, target);
};
};
15 changes: 15 additions & 0 deletions lib/common/decorators/core/exception-filters.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'reflect-metadata';
import { ExceptionFilter } from '../../index';
/**
* Setups exception filters to the chosen context.
* When the `@UseFilters()` is used on the controller level:
* - Exception Filter will be set up to every handler (every method)
*
* When the `@UseFilters()` is used on the handle level:
* - Exception Filter will be set up only to specified method
*
* @param {ExceptionFilter[]} ...filters (instances)
*/
export declare const UseFilters: (
...filters: ExceptionFilter<any>[]
) => (target: any, key?: any, descriptor?: any) => any;
31 changes: 31 additions & 0 deletions lib/common/decorators/core/exception-filters.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const constants_1 = require("../../constants");
const extend_metadata_util_1 = require("../../utils/extend-metadata.util");
const shared_utils_1 = require("../../utils/shared.utils");
const validate_each_util_1 = require("../../utils/validate-each.util");
const defineFiltersMetadata = (...filters) => {
return (target, key, descriptor) => {
const isFilterValid = (filter) => shared_utils_1.isFunction(filter.catch);
if (descriptor) {
validate_each_util_1.validateEach(target.constructor, filters, isFilterValid, '@UseFilters', 'filter');
extend_metadata_util_1.extendArrayMetadata(constants_1.EXCEPTION_FILTERS_METADATA, filters, descriptor.value);
return descriptor;
}
validate_each_util_1.validateEach(target, filters, isFilterValid, '@UseFilters', 'filter');
extend_metadata_util_1.extendArrayMetadata(constants_1.EXCEPTION_FILTERS_METADATA, filters, target);
return target;
};
};
/**
* Setups exception filters to the chosen context.
* When the `@UseFilters()` is used on the controller level:
* - Exception Filter will be set up to every handler (every method)
*
* When the `@UseFilters()` is used on the handle level:
* - Exception Filter will be set up only to specified method
*
* @param {ExceptionFilter[]} ...filters (instances)
*/
exports.UseFilters = (...filters) => defineFiltersMetadata(...filters);
11 changes: 11 additions & 0 deletions lib/common/decorators/core/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from './controller.decorator';
export * from './component.decorator';
export * from './dependencies.decorator';
export * from './inject.decorator';
export * from './catch.decorator';
export * from './exception-filters.decorator';
export * from './use-pipes.decorator';
export * from './use-guards.decorator';
export * from './reflect-metadata.decorator';
export * from './use-interceptors.decorator';
export * from './bind.decorator';
16 changes: 16 additions & 0 deletions lib/common/decorators/core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./controller.decorator"));
__export(require("./component.decorator"));
__export(require("./dependencies.decorator"));
__export(require("./inject.decorator"));
__export(require("./catch.decorator"));
__export(require("./exception-filters.decorator"));
__export(require("./use-pipes.decorator"));
__export(require("./use-guards.decorator"));
__export(require("./reflect-metadata.decorator"));
__export(require("./use-interceptors.decorator"));
__export(require("./bind.decorator"));
6 changes: 6 additions & 0 deletions lib/common/decorators/core/inject.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'reflect-metadata';
/**
* Injects provider which has to be available in the current injector (module) scope.
* Providers are recognized by types or tokens.
*/
export declare function Inject(token: any): ParameterDecorator;
18 changes: 18 additions & 0 deletions lib/common/decorators/core/inject.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const constants_1 = require("../../constants");
const shared_utils_1 = require("../../utils/shared.utils");
/**
* Injects provider which has to be available in the current injector (module) scope.
* Providers are recognized by types or tokens.
*/
function Inject(token) {
return (target, key, index) => {
const args = Reflect.getMetadata(constants_1.SELF_DECLARED_DEPS_METADATA, target) || [];
const type = shared_utils_1.isFunction(token) ? token.name : token;
args.push({ index, param: type });
Reflect.defineMetadata(constants_1.SELF_DECLARED_DEPS_METADATA, args, target);
};
}
exports.Inject = Inject;
8 changes: 8 additions & 0 deletions lib/common/decorators/core/reflect-metadata.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Assigns the metadata to the class / function under specified `key`.
* This metadata can be reflected using `Reflector` class.
*/
export declare const ReflectMetadata: (
metadataKey: any,
metadataValue: any,
) => (target: object, key?: any, descriptor?: any) => any;
14 changes: 14 additions & 0 deletions lib/common/decorators/core/reflect-metadata.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Assigns the metadata to the class / function under specified `key`.
* This metadata can be reflected using `Reflector` class.
*/
exports.ReflectMetadata = (metadataKey, metadataValue) => (target, key, descriptor) => {
if (descriptor) {
Reflect.defineMetadata(metadataKey, metadataValue, descriptor.value);
return descriptor;
}
Reflect.defineMetadata(metadataKey, metadataValue, target);
return target;
};
13 changes: 13 additions & 0 deletions lib/common/decorators/core/use-guards.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Binds guards to the particular context.
* When the `@UseGuards()` is used on the controller level:
* - Guard will be register to each handler (every method)
*
* When the `@UseGuards()` is used on the handler level:
* - Guard will be registered only to specified method
*
* @param {} ...guards (types)
*/
export declare function UseGuards(
...guards: any[]
): (target: any, key?: any, descriptor?: any) => any;
Loading

0 comments on commit eb05d23

Please sign in to comment.