Socket
Socket
Sign inDemoInstall

@loopback/metadata

Package Overview
Dependencies
3
Maintainers
17
Versions
163
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.4.5 to 0.5.0

dist/src/types.d.ts

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

<a name="0.5.0"></a>
# [0.5.0](https://github.com/strongloop/loopback-next/compare/@loopback/metadata@0.4.5...@loopback/metadata@0.5.0) (2018-04-12)
### Features
* **metadata:** add strongly-typed metadata accessors ([45f9f80](https://github.com/strongloop/loopback-next/commit/45f9f80))
<a name="0.4.5"></a>

@@ -8,0 +19,0 @@ ## [0.4.5](https://github.com/strongloop/loopback-next/compare/@loopback/metadata@0.4.4...@loopback/metadata@0.4.5) (2018-04-11)

23

dist/src/decorator-factory.d.ts

@@ -0,8 +1,3 @@

import { MetadataMap, DecoratorType, MetadataKey } from './types';
/**
* An object mapping keys to corresponding metadata
*/
export interface MetadataMap<T> {
[propertyOrMethodName: string]: T;
}
/**
* Options for a decorator

@@ -27,6 +22,2 @@ */

/**
* Decorator function types
*/
export declare type DecoratorType = ClassDecorator | PropertyDecorator | MethodDecorator | ParameterDecorator;
/**
* Base factory class for decorator functions

@@ -160,3 +151,3 @@ *

*/
protected static _createDecorator<T, M extends T | MetadataMap<T> | MetadataMap<T[]>, D extends DecoratorType>(key: string, spec: T, options?: DecoratorOptions): D;
protected static _createDecorator<T, M extends T | MetadataMap<T> | MetadataMap<T[]>, D extends DecoratorType>(key: MetadataKey<T, D>, spec: T, options?: DecoratorOptions): D;
private static _cloneableTypes;

@@ -178,3 +169,3 @@ static cloneDeep<V>(val: Readonly<V>): V;

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions): ClassDecorator;
static createDecorator<T>(key: MetadataKey<T, ClassDecorator>, spec: T, options?: DecoratorOptions): ClassDecorator;
}

@@ -194,3 +185,3 @@ /**

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions): PropertyDecorator;
static createDecorator<T>(key: MetadataKey<T, PropertyDecorator>, spec: T, options?: DecoratorOptions): PropertyDecorator;
}

@@ -210,3 +201,3 @@ /**

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions): MethodDecorator;
static createDecorator<T>(key: MetadataKey<T, MethodDecorator>, spec: T, options?: DecoratorOptions): MethodDecorator;
}

@@ -227,3 +218,3 @@ /**

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions): ParameterDecorator;
static createDecorator<T>(key: MetadataKey<T, ParameterDecorator>, spec: T, options?: DecoratorOptions): ParameterDecorator;
}

@@ -258,3 +249,3 @@ /**

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions): MethodDecorator;
static createDecorator<T>(key: MetadataKey<T, MethodDecorator>, spec: T, options?: DecoratorOptions): MethodDecorator;
}

@@ -234,3 +234,3 @@ "use strict";

static _createDecorator(key, spec, options) {
const inst = new this(key, spec, options);
const inst = new this(key.toString(), spec, options);
return inst.create();

@@ -237,0 +237,0 @@ }

@@ -0,3 +1,4 @@

export * from './types';
export * from './reflect';
export * from './decorator-factory';
export * from './inspector';

@@ -10,2 +10,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./types"));
__export(require("./reflect"));

@@ -12,0 +13,0 @@ __export(require("./decorator-factory"));

import { NamespacedReflect } from './reflect';
import { MetadataMap } from './decorator-factory';
import { MetadataKey, MetadataMap, DesignTimeMethodMetadata, DecoratorType } from './types';
/**
* Design time metadata for a method.
*
* @example
* ```ts
* class MyController
* {
* myMethod(x: string, y: number, z: MyClass): boolean {
* // ...
* return true;
* }
* }
* ```
*
* The `myMethod` above has design-time metadata as follows:
* ```ts
* {
* type: Function,
* parameterTypes: [String, Number, MyClass],
* returnType: Boolean
* }
* ```
*/
export interface DesignTimeMethodMetadata {
/**
* Type of the method itself. It is `Function`
*/
type: Function;
/**
* An array of parameter types
*/
parameterTypes: Function[];
/**
* Return type
*/
returnType: Function;
}
/**
* Options for inspection

@@ -72,3 +35,3 @@ */

*/
static getClassMetadata<T>(key: string, target: Function, options?: InspectionOptions): T | undefined;
static getClassMetadata<T>(key: MetadataKey<T, ClassDecorator>, target: Function, options?: InspectionOptions): T | undefined;
/**

@@ -81,3 +44,3 @@ * Define metadata for the given target

*/
static defineMetadata<T>(key: string, value: T, target: Object, member?: string | symbol): void;
static defineMetadata<T>(key: MetadataKey<T, DecoratorType>, value: T, target: Object, member?: string | symbol): void;
/**

@@ -90,3 +53,3 @@ * Get the metadata associated with the given key for all methods of the

*/
static getAllMethodMetadata<T>(key: string, target: Object, options?: InspectionOptions): MetadataMap<T> | undefined;
static getAllMethodMetadata<T>(key: MetadataKey<T, MethodDecorator>, target: Object, options?: InspectionOptions): MetadataMap<T> | undefined;
/**

@@ -101,3 +64,3 @@ * Get the metadata associated with the given key for a given method of the

*/
static getMethodMetadata<T>(key: string, target: Object, methodName?: string | symbol, options?: InspectionOptions): T | undefined;
static getMethodMetadata<T>(key: MetadataKey<T, MethodDecorator>, target: Object, methodName?: string | symbol, options?: InspectionOptions): T | undefined;
/**

@@ -110,3 +73,3 @@ * Get the metadata associated with the given key for all properties of the

*/
static getAllPropertyMetadata<T>(key: string, target: Object, options?: InspectionOptions): MetadataMap<T> | undefined;
static getAllPropertyMetadata<T>(key: MetadataKey<T, PropertyDecorator>, target: Object, options?: InspectionOptions): MetadataMap<T> | undefined;
/**

@@ -121,3 +84,3 @@ * Get the metadata associated with the given key for a given property of the

*/
static getPropertyMetadata<T>(key: string, target: Object, propertyName: string | symbol, options?: InspectionOptions): T | undefined;
static getPropertyMetadata<T>(key: MetadataKey<T, PropertyDecorator>, target: Object, propertyName: string | symbol, options?: InspectionOptions): T | undefined;
/**

@@ -132,3 +95,3 @@ * Get the metadata associated with the given key for all parameters of a

*/
static getAllParameterMetadata<T>(key: string, target: Object, methodName?: string | symbol, options?: InspectionOptions): T[] | undefined;
static getAllParameterMetadata<T>(key: MetadataKey<T, ParameterDecorator>, target: Object, methodName?: string | symbol, options?: InspectionOptions): T[] | undefined;
/**

@@ -144,3 +107,3 @@ * Get the metadata associated with the given key for a parameter of a given

*/
static getParameterMetadata<T>(key: string, target: Object, methodName: string | symbol, index: number, options?: InspectionOptions): T | undefined;
static getParameterMetadata<T>(key: MetadataKey<T, ParameterDecorator>, target: Object, methodName: string | symbol, index: number, options?: InspectionOptions): T | undefined;
/**

@@ -147,0 +110,0 @@ * Get TypeScript design time type for a property

@@ -27,4 +27,4 @@ "use strict";

return options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
}

@@ -39,3 +39,3 @@ /**

static defineMetadata(key, value, target, member) {
reflect_1.Reflector.defineMetadata(key, value, target, member);
reflect_1.Reflector.defineMetadata(key.toString(), value, target, member);
}

@@ -51,4 +51,4 @@ /**

return options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
}

@@ -67,4 +67,4 @@ /**

const meta = options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
return meta && meta[methodName];

@@ -81,4 +81,4 @@ }

return options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
}

@@ -96,4 +96,4 @@ /**

const meta = options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
return meta && meta[propertyName];

@@ -113,4 +113,4 @@ }

const meta = options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
return meta && meta[methodName];

@@ -131,4 +131,4 @@ }

const meta = options && options.ownMetadataOnly
? reflect_1.Reflector.getOwnMetadata(key, target)
: reflect_1.Reflector.getMetadata(key, target);
? reflect_1.Reflector.getOwnMetadata(key.toString(), target)
: reflect_1.Reflector.getMetadata(key.toString(), target);
const params = meta && meta[methodName];

@@ -135,0 +135,0 @@ return params && params[index];

{
"name": "@loopback/metadata",
"version": "0.4.5",
"version": "0.5.0",
"description": "LoopBack's metadata utilities for reflection and decoration",

@@ -5,0 +5,0 @@ "engines": {

@@ -415,2 +415,21 @@ # @loopback/metadata

### Use strong-typed metadata access key
You can use MetadataAccessor to provide type checks for metadata access via
keys. For example,
```ts
const CLASS_KEY = MetadataAccessor.create<MyClassMetadata, ClassDecorator>(
'my-class-decorator-key',
);
// Create a class decorator with the key
const myClassDecorator = ClassDecoratorFactory.createDecorator(CLASS_KEY);
// Inspect a class with the key
const myClassMeta = MetadataInspector.getClassMetaData(CLASS_KEY, MyController);
```
Please note MetadataKey can be an instance of MetadataAccessor or a string.
### Inspect design-time metadata of properties/methods

@@ -417,0 +436,0 @@

@@ -9,14 +9,7 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved.

import * as debugModule from 'debug';
import {MetadataMap, DecoratorType, MetadataKey} from './types';
const debug = debugModule('loopback:metadata:decorator');
// tslint:disable:no-any
/**
* An object mapping keys to corresponding metadata
*/
export interface MetadataMap<T> {
[propertyOrMethodName: string]: T;
}
/**
* Options for a decorator

@@ -42,11 +35,2 @@ */

/**
* Decorator function types
*/
export type DecoratorType =
| ClassDecorator
| PropertyDecorator
| MethodDecorator
| ParameterDecorator;
/**
* Base factory class for decorator functions

@@ -316,4 +300,4 @@ *

D extends DecoratorType
>(key: string, spec: T, options?: DecoratorOptions): D {
const inst = new this<T, M, D>(key, spec, options);
>(key: MetadataKey<T, D>, spec: T, options?: DecoratorOptions): D {
const inst = new this<T, M, D>(key.toString(), spec, options);
return inst.create();

@@ -402,3 +386,7 @@ }

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions) {
static createDecorator<T>(
key: MetadataKey<T, ClassDecorator>,
spec: T,
options?: DecoratorOptions,
) {
return super._createDecorator<T, T, ClassDecorator>(key, spec, options);

@@ -459,3 +447,7 @@ }

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions) {
static createDecorator<T>(
key: MetadataKey<T, PropertyDecorator>,
spec: T,
options?: DecoratorOptions,
) {
return super._createDecorator<T, MetadataMap<T>, PropertyDecorator>(

@@ -525,3 +517,7 @@ key,

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions) {
static createDecorator<T>(
key: MetadataKey<T, MethodDecorator>,
spec: T,
options?: DecoratorOptions,
) {
return super._createDecorator<T, MetadataMap<T>, MethodDecorator>(

@@ -618,3 +614,7 @@ key,

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions) {
static createDecorator<T>(
key: MetadataKey<T, ParameterDecorator>,
spec: T,
options?: DecoratorOptions,
) {
return super._createDecorator<T, MetadataMap<T[]>, ParameterDecorator>(

@@ -747,3 +747,7 @@ key,

*/
static createDecorator<T>(key: string, spec: T, options?: DecoratorOptions) {
static createDecorator<T>(
key: MetadataKey<T, MethodDecorator>,
spec: T,
options?: DecoratorOptions,
) {
return super._createDecorator<T, MetadataMap<T[]>, MethodDecorator>(

@@ -750,0 +754,0 @@ key,

@@ -6,4 +6,5 @@ // Copyright IBM Corp. 2017. All Rights Reserved.

export * from './types';
export * from './reflect';
export * from './decorator-factory';
export * from './inspector';

@@ -7,3 +7,8 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved.

import {Reflector, NamespacedReflect} from './reflect';
import {MetadataMap} from './decorator-factory';
import {
MetadataKey,
MetadataMap,
DesignTimeMethodMetadata,
DecoratorType,
} from './types';

@@ -19,40 +24,2 @@ /**

/**
* Design time metadata for a method.
*
* @example
* ```ts
* class MyController
* {
* myMethod(x: string, y: number, z: MyClass): boolean {
* // ...
* return true;
* }
* }
* ```
*
* The `myMethod` above has design-time metadata as follows:
* ```ts
* {
* type: Function,
* parameterTypes: [String, Number, MyClass],
* returnType: Boolean
* }
* ```
*/
export interface DesignTimeMethodMetadata {
/**
* Type of the method itself. It is `Function`
*/
type: Function;
/**
* An array of parameter types
*/
parameterTypes: Function[];
/**
* Return type
*/
returnType: Function;
}
/**
* Options for inspection

@@ -92,3 +59,3 @@ */

static getClassMetadata<T>(
key: string,
key: MetadataKey<T, ClassDecorator>,
target: Function,

@@ -98,4 +65,4 @@ options?: InspectionOptions,

return options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
}

@@ -111,3 +78,3 @@

static defineMetadata<T>(
key: string,
key: MetadataKey<T, DecoratorType>,
value: T,

@@ -117,3 +84,3 @@ target: Object,

) {
Reflector.defineMetadata(key, value, target, member);
Reflector.defineMetadata(key.toString(), value, target, member);
}

@@ -129,3 +96,3 @@

static getAllMethodMetadata<T>(
key: string,
key: MetadataKey<T, MethodDecorator>,
target: Object,

@@ -135,4 +102,4 @@ options?: InspectionOptions,

return options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
}

@@ -150,3 +117,3 @@

static getMethodMetadata<T>(
key: string,
key: MetadataKey<T, MethodDecorator>,
target: Object,

@@ -159,4 +126,4 @@ methodName?: string | symbol,

options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
return meta && meta[methodName];

@@ -173,3 +140,3 @@ }

static getAllPropertyMetadata<T>(
key: string,
key: MetadataKey<T, PropertyDecorator>,
target: Object,

@@ -179,4 +146,4 @@ options?: InspectionOptions,

return options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
}

@@ -194,3 +161,3 @@

static getPropertyMetadata<T>(
key: string,
key: MetadataKey<T, PropertyDecorator>,
target: Object,

@@ -202,4 +169,4 @@ propertyName: string | symbol,

options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
return meta && meta[propertyName];

@@ -218,3 +185,3 @@ }

static getAllParameterMetadata<T>(
key: string,
key: MetadataKey<T, ParameterDecorator>,
target: Object,

@@ -227,4 +194,4 @@ methodName?: string | symbol,

options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
return meta && meta[methodName];

@@ -244,3 +211,3 @@ }

static getParameterMetadata<T>(
key: string,
key: MetadataKey<T, ParameterDecorator>,
target: Object,

@@ -254,4 +221,4 @@ methodName: string | symbol,

options && options.ownMetadataOnly
? Reflector.getOwnMetadata(key, target)
: Reflector.getMetadata(key, target);
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
const params = meta && meta[methodName];

@@ -258,0 +225,0 @@ return params && params[index];

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc