diff --git a/package.json b/package.json index 1251580..eaf6a7c 100644 --- a/package.json +++ b/package.json @@ -22,13 +22,18 @@ "@angular/platform-browser": "^5.2.9", "@angular/platform-browser-dynamic": "^5.2.9", "@angular/router": "^5.2.0", + "@loafer/core": "^0.0.1", + "@loafer/decorator": "^0.0.1", + "@loafer/ng-logger": "^0.0.1", + "@loafer/ng-rest": "^0.0.1", + "@loafer/ng-rpc": "^0.0.1", "@ngrx/core": "^1.2.0", "@ngrx/effects": "^5.2.0", "@ngrx/entity": "^5.2.0", "@ngrx/router-store": "^5.2.0", "@ngrx/store": "^5.2.0", "@ngrx/store-devtools": "^5.2.0", - "@overflow/commons-typescript": "^0.0.1", + "@overflow/commons-typescript": "^0.0.3", "angular-l10n": "^4.1.5", "angularx-qrcode": "^1.1.0", "chart.js": "^2.7.2", diff --git a/src/@loafer/core/error.ts b/src/@loafer/core/error.ts deleted file mode 100644 index e1e727b..0000000 --- a/src/@loafer/core/error.ts +++ /dev/null @@ -1,6 +0,0 @@ -export class IllegalArgumentError extends Error { - public constructor(message?: string) { - super(message); - Object.setPrototypeOf(this, new.target.prototype); - } -} diff --git a/src/@loafer/core/index.ts b/src/@loafer/core/index.ts deleted file mode 100644 index b38ebc9..0000000 --- a/src/@loafer/core/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './type'; diff --git a/src/@loafer/core/reflect/AccessibleObject.ts b/src/@loafer/core/reflect/AccessibleObject.ts deleted file mode 100644 index 5e31fab..0000000 --- a/src/@loafer/core/reflect/AccessibleObject.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { - Type, -} from '../type'; - -import { - TypeUtil, -} from '../util/TypeUtil'; - -import { AnnotatedElement } from './AnnotatedElement'; -import { Annotation } from './Annotation'; - -export abstract class AccessibleObject implements AnnotatedElement { - private _annotations: Map, Annotation>; - - protected constructor() { - this._annotations = new Map(); - } - - public _addAnnotation(annotation: AnnotationType): void { - this._annotations.set(TypeUtil.getType(annotation), annotation); - } - - public isAnnotationPresent(annotationClass: Type): boolean { - return null !== this.getAnnotation(annotationClass); - } - - public getOwnAnnotation(annotationClass: Type): AnnotationType | undefined { - return this._annotations.get(annotationClass); - } - - public getOwnAnnotations(): Map, Annotation> { - return this._annotations; - } - - public getOwnAnnotationsByType(annotationClass: Type) - : AnnotationType[] | undefined { - if (0 === this._annotations.size) { - return undefined; - } - const results: AnnotationType[] = []; - for (const classType of Array.from(this._annotations.keys())) { - if (classType === annotationClass) { - results.push(this._annotations.get(classType)); - } - } - if (0 === results.length) { - return undefined; - } - return results; - } - - public getAnnotation(annotationClass: Type): AnnotationType | undefined { - return this.getOwnAnnotation(annotationClass); - } - - public getAnnotations(): Map, Annotation> { - return this.getOwnAnnotations(); - } - - public getAnnotationsByType(annotationClass: Type) - : AnnotationType[] | undefined { - return this.getOwnAnnotationsByType(annotationClass); - } -} diff --git a/src/@loafer/core/reflect/AnnotatedElement.ts b/src/@loafer/core/reflect/AnnotatedElement.ts deleted file mode 100644 index 3cd1663..0000000 --- a/src/@loafer/core/reflect/AnnotatedElement.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { - Type, -} from '../type'; - -import { Annotation } from './Annotation'; - -export interface AnnotatedElement { - _addAnnotation(annotation: AnnotationType): void; - - isAnnotationPresent(annotationClass: Type): boolean; - getOwnAnnotation(annotationClass: Type): AnnotationType | undefined; - getOwnAnnotations(): Map, Annotation>; - getOwnAnnotationsByType(annotationClass: Type): AnnotationType[] | undefined; - getAnnotation(annotationClass: Type): AnnotationType | undefined; - getAnnotations(): Map, Annotation>; - getAnnotationsByType(annotationClass: Type): AnnotationType[] | undefined; -} diff --git a/src/@loafer/core/reflect/Annotation.ts b/src/@loafer/core/reflect/Annotation.ts deleted file mode 100644 index 7155b2d..0000000 --- a/src/@loafer/core/reflect/Annotation.ts +++ /dev/null @@ -1,7 +0,0 @@ -export abstract class Annotation { - public readonly attribute: Attribute; - - public constructor(attribute?: Attribute) { - this.attribute = attribute; - } -} diff --git a/src/@loafer/core/reflect/Class.ts b/src/@loafer/core/reflect/Class.ts deleted file mode 100644 index 4e4636d..0000000 --- a/src/@loafer/core/reflect/Class.ts +++ /dev/null @@ -1,228 +0,0 @@ -import { - Type, - PropertyKeyType, -} from '../type'; - -import { - TypeUtil, -} from '../util/TypeUtil'; - -import { AccessibleObject } from './AccessibleObject'; -import { Annotation } from './Annotation'; -import { SystemClassRegistry } from './ClassRegistry'; -import { Constructor } from './Constructor'; -import { Field } from './Field'; -import { Method } from './Method'; -import { Metadata } from './Metadata'; - - - -export class Class extends AccessibleObject { - private _type: Type; - private _constructor: Constructor; - private _fields: Map; - private _methods: Map; - - /** - * forClass - */ - public static forType(type: Type): Class | undefined { - return SystemClassRegistry.get(type); - } - - /** - * _defineClass - */ - public static _defineClass(type: Type): Class { - let clazz: Class = Class.forType(type); - if (undefined === clazz) { - clazz = new Class(type); - SystemClassRegistry.set(type, clazz); - } - - if (null === clazz._constructor) { - const parameterTypes = Metadata.getOwnParamTypes(type); - if (undefined !== parameterTypes) { - clazz._constructor = new Constructor(clazz, parameterTypes); - } - } - - return clazz; - } - - private constructor(type: Type) { - super(); - this._type = type; - this._fields = new Map(); - this._methods = new Map(); - } - - /** - * _defineField - */ - public _defineConstructor(parameterTypes: any[]): Constructor { - let cons: Constructor = this._constructor; - if (undefined === cons) { - cons = new Constructor(this, parameterTypes); - this._constructor = cons; - } - return cons; - } - - /** - * _defineField - */ - public _defineField(propertyKey: PropertyKeyType, propertyType: any): Field { - let field: Field = this._fields.get(propertyKey); - if (undefined === field) { - field = new Field(this, propertyKey, propertyType); - this._fields.set(propertyKey, field); - } - return field; - } - - /** - * _defineMethod - */ - public _defineMethod(propertyKey: PropertyKeyType, parameterTypes: any[], returnType: any): Method { - let method: Method = this._methods.get(propertyKey); - if (undefined === method) { - method = new Method(this, propertyKey, parameterTypes, returnType); - this._methods.set(propertyKey, method); - } - return method; - } - - public getType(): Type { - return this._type; - } - - public getConstructor(): Constructor { - if (undefined === this._constructor) { - this._constructor = new Constructor(this, undefined); - } - return this._constructor; - } - - public getOwnField(propertyKey: PropertyKeyType): Field | undefined { - return this._fields.get(propertyKey); - } - - public getOwnFields(): Map { - return this._fields; - } - - public getField(propertyKey: PropertyKeyType): Field | undefined { - const fields = this.getFields(); - - return fields.get(propertyKey); - } - - public getFields(): Map { - const fields: Map = new Map(); - - const types = TypeUtil.ancestorsOf(this._type); - if (null === types || 0 === types.length) { - return fields; - } - for (let i = 0; i < types.length; i++) { - const tType = types[i]; - const tClazz = Class.forType(tType); - if (undefined === tClazz) { - continue; - } - - tClazz.getOwnFields().forEach((value: Field, key: PropertyKeyType, map: Map): void => { - fields.set(key, value); - }); - } - - return fields; - } - - public getOwnMethod(propertyKey: PropertyKeyType): Method | undefined { - return this._methods.get(propertyKey); - } - - public getOwnMethods(): Map { - return this._methods; - } - - public getMethod(propertyKey: PropertyKeyType): Method | undefined { - const methods = this.getMethods(); - - return methods.get(propertyKey); - } - - public getMethods(): Map { - const methods: Map = new Map(); - - const types = TypeUtil.ancestorsOf(this._type); - if (null === types || 0 === types.length) { - return methods; - } - for (let i = 0; i < types.length; i++) { - const tClazzType = types[i]; - const tClazz = Class.forType(tClazzType); - if (undefined === tClazz) { - continue; - } - - tClazz.getOwnMethods().forEach((value: Method, key: PropertyKeyType, map: Map): void => { - methods.set(key, value); - }); - } - - return methods; - } - - public getAnnotation(annotationClass: Type): AnnotationType | undefined { - const annotations = this.getAnnotations(); - - return annotations.get(annotationClass); - } - - public getAnnotations(): Map, Annotation> { - const annotations: Map, Annotation> = new Map(); - - const types = TypeUtil.ancestorsOf(this._type); - if (null === types || 0 === types.length) { - return annotations; - } - for (let i = 0; i < types.length; i++) { - const tClazzType = types[i]; - const tClazz = Class.forType(tClazzType); - if (undefined === tClazz) { - continue; - } - - tClazz.getOwnAnnotations().forEach((value: Annotation, key: Type, map: Map, Annotation>): void => { - annotations.set(key, value); - }); - } - - return annotations; - } - - public getAnnotationsByType(annotationClass: Type) - : AnnotationType[] | undefined { - const annotations = this.getAnnotations(); - if (0 === annotations.size) { - return undefined; - } - const results: AnnotationType[] = []; - for (const classType of Array.from(annotations.keys())) { - if (classType === annotationClass) { - results.push(annotations.get(classType)); - } - } - if (0 === results.length) { - return undefined; - } - return results; - } - - public getName(): string { - return this._type.name; - } -} diff --git a/src/@loafer/core/reflect/ClassRegistry.ts b/src/@loafer/core/reflect/ClassRegistry.ts deleted file mode 100644 index b559da1..0000000 --- a/src/@loafer/core/reflect/ClassRegistry.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Type } from '../type'; -import { Registry } from '../util/Registry'; - -import { Class } from './Class'; - -export class ClassRegistry extends Registry, Class> { - public constructor(parent?: ClassRegistry) { - super(parent); - } -} - -export const SystemClassRegistry = new ClassRegistry(); diff --git a/src/@loafer/core/reflect/Constructor.ts b/src/@loafer/core/reflect/Constructor.ts deleted file mode 100644 index f68b523..0000000 --- a/src/@loafer/core/reflect/Constructor.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { - PropertyKeyType, -} from '../type'; - -import { - TypeUtil, -} from '../util/TypeUtil'; - -import { Class } from './Class'; -import { Executable } from './Executable'; - -export class Constructor extends Executable { - private _rawConstructor: Function; - - public constructor(declaringClazz: Class, parameterTypes?: any[]) { - super(declaringClazz, CONSTRUCTOR_NAME, parameterTypes); - this._rawConstructor = TypeUtil.getPrototype(declaringClazz.getType())[CONSTRUCTOR_NAME]; - } - - public newInstance(...args: any[]): any { - const ctor = this.getDeclaringClass().getType(); - return new (ctor.bind.apply(ctor, [null].concat(args)))(); - } -} - -const CONSTRUCTOR_NAME = 'constructor'; diff --git a/src/@loafer/core/reflect/Executable.ts b/src/@loafer/core/reflect/Executable.ts deleted file mode 100644 index 8f4cc01..0000000 --- a/src/@loafer/core/reflect/Executable.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { - PropertyKeyType, -} from '../type'; - -import { - TypeUtil, -} from '../util/TypeUtil'; - -import { AccessibleObject } from './AccessibleObject'; -import { Class } from './Class'; -import { Member } from './Member'; -import { Parameter } from './Parameter'; - -export abstract class Executable extends AccessibleObject implements Member { - private _clazz: Class; - private _name: PropertyKeyType; - private _parameters: Parameter[]; - - protected constructor(declaringClazz: Class, name: PropertyKeyType, parameterTypes?: any[]) { - super(); - - this._clazz = declaringClazz; - this._name = name; - - if (undefined === parameterTypes) { - return; - } - - const parameterNames = TypeUtil.getParameterNames(declaringClazz.getType(), name); - this._parameters = []; - - for (let i = 0; i < parameterTypes.length; i++) { - const parameterType = parameterTypes[i]; - const parameterName = parameterNames[i]; - const parameter: Parameter = new Parameter(this, parameterType, parameterName, i); - this._parameters.push(parameter); - } - } - - public getDeclaringClass(): Class { - return this._clazz; - } - - public getName(): PropertyKeyType { - return this._name; - } - - /** - * getParameterCount - */ - public getParameterCount(): number { - if (null === this._parameters) { - return 0; - } - return this._parameters.length; - } - /** - * getParameters - */ - public getParameters(): Parameter[] | undefined { - return this._parameters; - } - /** - * getParameter - */ - public getParameter(index: number): Parameter | undefined { - if (null === this._parameters) { - return undefined; - } - if (0 > index || this._parameters.length <= index) { - return undefined; - } - return this._parameters[index]; - } -} diff --git a/src/@loafer/core/reflect/Field.ts b/src/@loafer/core/reflect/Field.ts deleted file mode 100644 index 9e0cbbe..0000000 --- a/src/@loafer/core/reflect/Field.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { - PropertyKeyType, -} from '../type'; - -import { AccessibleObject } from './AccessibleObject'; -import { Class } from './Class'; -import { Member } from './Member'; - -export class Field extends AccessibleObject implements Member { - private _clazz: Class; - private _name: PropertyKeyType; - private _type: any; - - public constructor(declaringClazz: Class, name: PropertyKeyType, fieldType: any) { - super(); - this._clazz = declaringClazz; - this._name = name; - this._type = fieldType; - } - - public getDeclaringClass(): Class { - return this._clazz; - } - - public getName(): PropertyKeyType { - return this._name; - } - - public getType(): any { - return this._type; - } - -} diff --git a/src/@loafer/core/reflect/Member.ts b/src/@loafer/core/reflect/Member.ts deleted file mode 100644 index f3b045f..0000000 --- a/src/@loafer/core/reflect/Member.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { - PropertyKeyType, -} from '../type'; - -import { Class } from './Class'; - -export interface Member { - getDeclaringClass(): Class; - getName(): PropertyKeyType; -} diff --git a/src/@loafer/core/reflect/Metadata.ts b/src/@loafer/core/reflect/Metadata.ts deleted file mode 100644 index caab825..0000000 --- a/src/@loafer/core/reflect/Metadata.ts +++ /dev/null @@ -1,521 +0,0 @@ -import { - MetadataKeyType, - PropertyKeyType, -} from '../type'; - -import { TypeUtil } from '../util/TypeUtil'; - - -export class Metadata { - /** - * Gets the metadata value for the provided metadata key on the target object or its prototype chain. - * @param key A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Metadata.get("custom:annotation", Example); - * - * // property (on constructor) - * result = Metadata.get("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Metadata.get("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Metadata.get("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Metadata.get("custom:annotation", Example.prototype, "method"); - * ``` - * - */ - public static get(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): any { - return Reflect.getMetadata(key, TypeUtil.getType(target), propertyKey); - } - - /** - * Gets the metadata value for the provided metadata key on the target object or its prototype chain. - * @param key A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Metadata.getOwn("custom:annotation", Example); - * - * // property (on constructor) - * result = Metadata.getOwn("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Metadata.getOwn("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Metadata.getOwn("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Metadata.getOwn("custom:annotation", Example.prototype, "method"); - * ``` - * - */ - public static getOwn(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): any { - return Reflect.getOwnMetadata(key, TypeUtil.getType(target), propertyKey); - } - - /** - * Gets the metadata value for the provided metadata DESIGN_TYPE on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.getType(Example); - * - * // property (on constructor) - * result = Metadata.getType(Example, "staticProperty"); - * - * // method (on constructor) - * result = Metadata.getType(Example, "staticMethod"); - * ``` - * - */ - public static getType(target: any, propertyKey?: PropertyKeyType): any { - return Reflect.getMetadata(DESIGN_TYPE, target, propertyKey); - } - - /** - * Gets the metadata value for the provided metadata DESIGN_TYPE on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.getOwnType(Example); - * - * // property (on constructor) - * result = Metadata.getOwnType(Example, "staticProperty"); - * - * // method (on constructor) - * result = Metadata.getOwnType(Example, "staticMethod"); - * ``` - * - */ - public static getOwnType(target: any, propertyKey?: PropertyKeyType): any { - return Reflect.getMetadata(DESIGN_TYPE, target, propertyKey); - } - - /** - * Gets the metadata value for the provided metadata DESIGN_RETURN_TYPE on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.getReturnType(Example); - * - * // property (on constructor) - * result = Metadata.getReturnType(Example, "staticProperty"); - * - * // method (on constructor) - * result = Metadata.getReturnType(Example, "staticMethod"); - * ``` - * - */ - public static getReturnType(target: any, propertyKey?: PropertyKeyType): any { - return Reflect.getMetadata(DESIGN_RETURN_TYPE, target, propertyKey); - } - - /** - * Gets the metadata value for the provided metadata DESIGN_RETURN_TYPE on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.getOwnReturnType(Example); - * - * // property (on constructor) - * result = Metadata.getOwnReturnType(Example, "staticProperty"); - * - * // method (on constructor) - * result = Metadata.getOwnReturnType(Example, "staticMethod"); - * ``` - * - */ - public static getOwnReturnType(target: any, propertyKey?: PropertyKeyType): any { - return Reflect.getOwnMetadata(DESIGN_RETURN_TYPE, target, propertyKey); - } - - /** - * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. - * @param key A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Metadata.has("custom:annotation", Example); - * - * // property (on constructor) - * result = Metadata.has("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Metadata.has("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Metadata.has("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Metadata.has("custom:annotation", Example.prototype, "method"); - * ``` - * - */ - public static has(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean { - return Reflect.hasMetadata(key, TypeUtil.getType(target), propertyKey); - } - - /** - * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. - * @param key A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Metadata.has("custom:annotation", Example); - * - * // property (on constructor) - * result = Metadata.hasOwn("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Metadata.hasOwn("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Metadata.hasOwn("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Metadata.hasOwn("custom:annotation", Example.prototype, "method"); - * ``` - * - */ - public static hasOwn(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean { - return Reflect.hasOwnMetadata(key, TypeUtil.getType(target), propertyKey); - } - - /** - * Deletes the metadata entry from the target object with the provided key. - * @param key A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns `true` if the metadata entry was found and deleted; otherwise, false. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Metadata.delete("custom:annotation", Example); - * - * // property (on constructor) - * result = Metadata.delete("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Metadata.delete("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Metadata.delete("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Metadata.delete("custom:annotation", Example.prototype, "method"); - * ``` - * - */ - public static delete(key: MetadataKeyType, target: any, propertyKey?: PropertyKeyType): boolean { - return Reflect.deleteMetadata(key, TypeUtil.getType(target), propertyKey); - } - - /** - * Set the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @param value A value that contains attached metadata. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.setParamTypes(Example, undefined, [Object]); - * - * // property (on constructor) - * result = Metadata.setParamTypes(Example, "staticProperty", [Object]); - * - * // property (on prototype) - * result = Metadata.setParamTypes(Example.prototype, "property", [Object]); - * - * // method (on constructor) - * result = Metadata.setParamTypes(Example, "staticMethod", [Object]); - * - * // method (on prototype) - * result = Metadata.setParamTypes(Example.prototype, "method", [Object]); - * ``` - * - */ - public static setParamTypes(target: any, propertyKey: PropertyKeyType, value: any): void { - return this.set(DESIGN_PARAM_TYPES, value, target.prototype, propertyKey); - } - - /** - * Get all metadata for a metadataKey. - * @param metadataKey - */ - public static getTargetsFromPropertyKey = (metadataKey: MetadataKeyType): any[] => - PROPERTIES.has(metadataKey) ? PROPERTIES.get(metadataKey) || [] : [] - - /** - * Define a unique metadata entry on the target. - * @param key A key used to store and retrieve metadata. - * @param value A value that contains attached metadata. - * @param target The target object on which to define metadata. - * @param propertyKey The property key for the target. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * Reflect.defineMetadata("custom:annotation", options, Example); - * - * // property (on constructor) - * Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty"); - * - * // property (on prototype) - * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property"); - * - * // method (on constructor) - * Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod"); - * - * // method (on prototype) - * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method"); - * - * // decorator factory as metadata-producing annotation. - * function MyAnnotation(options): PropertyDecorator { - * return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key); - * } - * ``` - * - */ - public static set(key: MetadataKeyType, value: any, target: any, propertyKey?: PropertyKeyType): void { - - const targets: any[] = PROPERTIES.has(key) ? PROPERTIES.get(key) || [] : []; - const classConstructor = TypeUtil.getType(target); - - if (targets.indexOf(classConstructor) === -1) { - targets.push(classConstructor); - PROPERTIES.set(key, targets); - } - - Reflect.defineMetadata(key, value, TypeUtil.getType(target), propertyKey); - } - - /** - * Gets the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.getParamTypes(Example); - * - * // property (on constructor) - * result = Metadata.getParamTypes(Example, "staticProperty"); - * - * // method (on constructor) - * result = Metadata.getParamTypes(Example, "staticMethod"); - * ``` - * - */ - public static getParamTypes(target: any, propertyKey?: PropertyKeyType): any[] { - return Reflect.getMetadata(DESIGN_PARAM_TYPES, target, propertyKey); - } - - /** - * Gets the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * ```typescript - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // on contructor - * result = Metadata.getParamTypes(Example); - * - * // property (on constructor) - * result = Metadata.getParamTypes(Example, "staticProperty"); - * - * // method (on constructor) - * result = Metadata.getParamTypes(Example, "staticMethod"); - * ``` - * - */ - public static getOwnParamTypes(target: any, propertyKey?: PropertyKeyType): any[] { - return Reflect.getOwnMetadata(DESIGN_PARAM_TYPES, target, propertyKey); - } -} - - -/** - * Metadata key - * @private - * @type {string} - */ -const DESIGN_PARAM_TYPES = 'design:paramtypes'; -/** - * Metadata key - * @private - * @type {string} - */ -const DESIGN_TYPE = 'design:type'; -/** - * Metadata key - * @private - * @type {string} - */ -const DESIGN_RETURN_TYPE = 'design:returntype'; -/** - * Properties collections - * @private - * @type {string} - */ -const PROPERTIES: Map = new Map(); diff --git a/src/@loafer/core/reflect/Method.ts b/src/@loafer/core/reflect/Method.ts deleted file mode 100644 index 82997c6..0000000 --- a/src/@loafer/core/reflect/Method.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { - PropertyKeyType, -} from '../type'; - -import { - TypeUtil, -} from '../util/TypeUtil'; - -import { Class } from './Class'; -import { Executable } from './Executable'; - -export class Method extends Executable { - private _returnType: any; - private _rawMethod: Function; - - public constructor(declaringClazz: Class, name: PropertyKeyType, parameterTypes: any[], returnType: any) { - super(declaringClazz, name, parameterTypes); - this._returnType = returnType; - this._rawMethod = TypeUtil.getPrototype(declaringClazz.getType())[name]; - } - - public getReturnType(): any { - return this._returnType; - } - - public invoke(instance: Object, ...args: any[]): any { - return this._rawMethod.apply(instance, args); - } -} diff --git a/src/@loafer/core/reflect/Parameter.ts b/src/@loafer/core/reflect/Parameter.ts deleted file mode 100644 index 19d515a..0000000 --- a/src/@loafer/core/reflect/Parameter.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { AccessibleObject } from './AccessibleObject'; -import { Executable } from './Executable'; - -export class Parameter extends AccessibleObject { - private _executable: Executable; - private _type: any; - private _index: number; - private _name: string; - - public constructor(executable: Executable, parameterType: any, name: string, index: number) { - super(); - this._executable = executable; - this._type = parameterType; - this._name = name; - this._index = index; - } - - public getDeclaringExecutable(): Executable { - return this._executable; - } - - public getType(): any { - return this._type; - } - - public getName(): string { - return this._name; - } - - public getIndex(): number { - return this._index; - } -} diff --git a/src/@loafer/core/reflect/index.ts b/src/@loafer/core/reflect/index.ts deleted file mode 100644 index 0dbbe0f..0000000 --- a/src/@loafer/core/reflect/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -export * from './AccessibleObject'; -export * from './AnnotatedElement'; -export * from './Annotation'; -export * from './Class'; -export * from './Constructor'; - -export * from './Executable'; -export * from './Field'; -export * from './Member'; -export * from './Metadata'; -export * from './Method'; -export * from './Parameter'; diff --git a/src/@loafer/core/type.ts b/src/@loafer/core/type.ts deleted file mode 100644 index e1683d4..0000000 --- a/src/@loafer/core/type.ts +++ /dev/null @@ -1,16 +0,0 @@ -export declare const Type: FunctionConstructor; -export declare function isType(v: any): v is Type; -export interface Type extends Function { - new (...args: any[]): T; -} - -export declare type IdentityType = T | symbol; -export declare type PropertyKeyType = IdentityType; -export declare type MetadataKeyType = IdentityType; - -export enum PrimitiveType { - ANY = 'any', - STRING = 'string', - NUMBER = 'number', - BOOLEAN = 'boolean', -} diff --git a/src/@loafer/core/util/AnnotationUtil.ts b/src/@loafer/core/util/AnnotationUtil.ts deleted file mode 100644 index c070cb3..0000000 --- a/src/@loafer/core/util/AnnotationUtil.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Class, Annotation } from '../reflect'; -import { Type } from '../type'; - -export abstract class AnnotationUtils { - public static hasAnnotation(type: Type, annotationClass: Type): boolean { - const annotation = AnnotationUtils.getAnnotation(type, annotationClass); - if (undefined !== annotation) { - return true; - } - return false; - } - - public static getAnnotation(type: Type, annotationClass: Type): T | undefined { - const clazz = Class.forType(type); - if (undefined === clazz) { - return undefined; - } - - const annotations = clazz.getAnnotations(); - if (0 === annotations.size) { - return undefined; - } - - for (const annonClassType of Array.from(annotations.keys())) { - if (annonClassType === annotationClass) { - return annotations.get(annonClassType); - } - const annotation = AnnotationUtils.getAnnotation(annonClassType, annotationClass); - if (undefined !== annotation) { - return annotation; - } - } - - return undefined; - } - - - -} diff --git a/src/@loafer/core/util/Registry.ts b/src/@loafer/core/util/Registry.ts deleted file mode 100644 index 1e0428d..0000000 --- a/src/@loafer/core/util/Registry.ts +++ /dev/null @@ -1,66 +0,0 @@ -export abstract class Registry { - private _parent: Registry; - private _map: Map; - - protected constructor(parent?: Registry) { - this._parent = parent; - this._map = new Map(); - } - - public get parent(): Registry | undefined { - return this._parent; - } - - public get size(): number { - return this._map.size; - } - - public get(key: K): V | undefined { - let v = this._map.get(key); - if (undefined === v && undefined !== this._parent) { - v = this._parent.get(key); - } - - return v; - } - - public has(key: K): boolean { - let exist = this._map.has(key); - if (!exist && undefined !== this._parent) { - exist = this._parent.has(key); - } - return exist; - } - - public set(key: K, value: V): void { - this._map.set(key, value); - } - - public entries(): IterableIterator<[K, V]> { - return this._map.entries(); - } - - public keys(): IterableIterator { - return this._map.keys(); - } - - public values(): IterableIterator { - return this._map.values(); - } - - public clear(): void { - this._map.clear(); - } - - public delete(key: K): boolean { - let result = this._map.delete(key); - if (!result && undefined !== this._parent) { - result = this._parent.delete(key); - } - return result; - } - - public forEach(callback: (vlaue: V, key: K, map: Map) => void, thisArg?: any): void { - this._map.forEach(callback, thisArg); - } -} diff --git a/src/@loafer/core/util/TypeUtil.ts b/src/@loafer/core/util/TypeUtil.ts deleted file mode 100644 index 2161fd4..0000000 --- a/src/@loafer/core/util/TypeUtil.ts +++ /dev/null @@ -1,407 +0,0 @@ -import { - Type, - PrimitiveType, - PropertyKeyType, -} from '../type'; - - -export class TypeUtil { - /** - * Get the provide constructor. - * @param target - */ - public static getContructor(target: any): Type { - return typeof target === 'function' ? target : target.constructor; - } - - /** - * Get the provide constructor if target is an instance. - * @param target - * @returns {*} - */ - public static getType(target: any): Type { - return target.prototype ? target : target.constructor; - } - - /** - * Get the provide prototype if target is an instance. - * @param target - * @returns {*} - */ - public static getPrototype(target: any): Object { - return typeof target === 'function' ? target.prototype : target; - } - - /** - * - * @param target - * @returns {symbol} - */ - public static getTypeOrSymbol(target: any): any { - return typeof target === 'symbol' ? target : TypeUtil.getType(target); - } - - /** - * Return true if the given obj is a primitive. - * @param target - * @returns {boolean} - */ - public static isPrimitiveOrPrimitiveType(target: any): boolean { - return TypeUtil.isString(target) - || TypeUtil.isNumber(target) - || TypeUtil.isBoolean(target); - } - - /** - * - * @param target - * @returns {PrimitiveType} - */ - public static primitiveOf(target: any): PrimitiveType { - if (TypeUtil.isString(target)) { - return PrimitiveType.STRING; - } - if (TypeUtil.isNumber(target)) { - return PrimitiveType.NUMBER; - } - if (TypeUtil.isBoolean(target)) { - return PrimitiveType.BOOLEAN; - } - return PrimitiveType.ANY; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isString(target: any): boolean { - return typeof target === 'string' || target instanceof String || target === String; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isNumber(target: any): boolean { - return typeof target === 'number' || target instanceof Number || target === Number; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isBoolean(target: any): boolean { - return typeof target === 'boolean' || target instanceof Boolean || target === Boolean; - } - - /** - * - * @param target - * @returns {Boolean} - */ - public static isArray(target: any): boolean { - return Array.isArray(target); - } - - /** - * Return true if the clazz is an array. - * @param target - * @returns {boolean} - */ - public static isArrayOrArrayType(target: any): boolean { - if (target === Array) { - return true; - } - return TypeUtil.isArray(target); - } - - /** - * Return true if the target. - * @param target - * @returns {boolean} - */ - public static isCollection(target: any): boolean { - return TypeUtil.isArrayOrArrayType(target) - || target === Map - || target instanceof Map - || target === Set - || target instanceof Set - || target === WeakMap - || target instanceof WeakMap - || target === WeakSet - || target instanceof WeakSet; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isDate(target: any): boolean { - return target === Date || target instanceof Date; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isMethod(target: any, propertyKey: PropertyKeyType): boolean { - if (typeof(target[propertyKey]) === undefined) { - return false; - } - return typeof target[propertyKey] === 'function'; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isObject(target: any): boolean { - return target === Object; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isType(target: any): boolean { - return !TypeUtil.isPrimitiveOrPrimitiveType(target) - && !TypeUtil.isObject(target) - && !TypeUtil.isDate(target) - && target !== undefined - && !TypeUtil.isPromise(target); - } - - /** - * Return true if the value is an empty string, null or undefined. - * @param value - * @returns {boolean} - */ - public static isEmpty(value: any): boolean { - return value === '' || value === null || value === undefined; - } - - /** - * Get object name - */ - public static nameOf(obj: any): string { - switch (typeof obj) { - default: - return '' + obj; - case 'symbol': - return TypeUtil.nameOfSymbol(obj); - case 'function': - return TypeUtil.nameOfType(obj); - } - } - - /** - * Get the provide name. - * @param target - */ - public static nameOfType(target: any): string { - return typeof target === 'function' - ? target.name - : target.constructor.name; - } - /** - * Get symbol name. - * @param sym - */ - public static nameOfSymbol(sym: symbol): string { - return sym.toString().replace('Symbol(', '').replace(')', ''); - } - /** - * - * @param out - * @param obj - * @param {{[p: string]: (collection: any[], value: any) => any}} reducers - * @returns {any} - */ - public static deepExtends(out: any, obj: any, reducers: { [key: string]: (collection: any[], value: any) => any } = {}): any { - - if (obj === undefined || obj === null) { - return obj; - } - - if (TypeUtil.isPrimitiveOrPrimitiveType(obj) || typeof obj === 'symbol' || typeof obj === 'function') { - return obj; - } - - if (TypeUtil.isArrayOrArrayType(obj)) { - out = out || []; - } else { - out = out || {}; - } - - const defaultReducer = reducers.default ? reducers.default : (collection: any[], value: any) => { - collection.push(value); - return collection; - }; - const set = (key: string | number, value: any) => { - if (TypeUtil.isArrayOrArrayType(obj)) { - out.push(value); - } else { - out[key] = value; - } - }; - - Object.keys(obj).forEach(key => { - let value = obj[key]; - - if (value === undefined || value === null) { - return; - } - - if (value === '' && out[key] !== '') { - return; - } - - if (TypeUtil.isPrimitiveOrPrimitiveType(value) || typeof value === 'function') { - set(key, value); - return; - } - - if (TypeUtil.isArrayOrArrayType(value)) { - - value = value.map((v: any) => TypeUtil.deepExtends(undefined, v)); - - set(key, [] - .concat(out[key] || [], value) - .reduce((collection: any[], v: any) => - reducers[key] ? reducers[key](collection, v) : defaultReducer(collection, v), - [])); - return; - } - - // Object - if (TypeUtil.isArrayOrArrayType(obj)) { - set(key, TypeUtil.deepExtends(undefined, value, reducers)); - } else { - set(key, TypeUtil.deepExtends(out[key], value, reducers)); - } - }); - - if (TypeUtil.isArrayOrArrayType(out)) { - out.reduce((collection: any[], value: any) => defaultReducer(collection, value), []); - } - - return out; - } - - /** - * - * @param target - * @returns {boolean} - */ - public static isPromise(target: any): boolean { - return target === Promise || target instanceof Promise; - } - - /** - * - * @param target - * @returns {any} - */ - public static getInheritedType(target: Type): Type { - return Object.getPrototypeOf(target); - } - - /** - * - * @param target - * @param {PropertyKeyType} propertyKey - * @returns {PropertyDescriptor} - */ - public static descriptorOf(target: any, propertyKey: PropertyKeyType): PropertyDescriptor { - return Object.getOwnPropertyDescriptor(target && target.prototype || target, propertyKey); - } - - /** - * - * @param target - * @param {PropertyKeyType} propertyKey - * @returns {string[]} - */ - public static getParameterNames(target: any, propertyKey: PropertyKeyType): string[] { - const rawType = TypeUtil.getPrototype(target); - const fn: Function = rawType[propertyKey]; - - const code = fn.toString() - .replace(COMMENTS, '') - .replace(FAT_ARROWS, '') - .replace(DEFAULT_PARAMS, ''); - - const result = code.slice(code.indexOf('(') + 1, code.indexOf(')')).match(/([^\s,]+)/g); - - return result === null - ? [] - : result; - } - - /** - * - * @param target - * @returns {Array} - */ - public static ancestorsOf(target: Type): Type[] { - const classes: Type[] = []; - - let currentTarget = TypeUtil.getType(target); - - while (TypeUtil.nameOf(currentTarget) !== '') { - classes.unshift(currentTarget); - currentTarget = TypeUtil.getInheritedType(currentTarget); - } - - return classes; - } - - /** - * - * @param target - * @param {string} name - * @param {Function} callback - */ - public static applyBefore(target: any, name: string, callback: Function): void { - const original = target[name]; - target[name] = function (...args: any[]): any { - callback(...args); - return original.apply(this, args); - }; - } - - /** - * - * @param {Promise} promise - * @param {number} time - * @returns {Promise} - */ - public static promiseTimeout(promise: Promise, time: number = 1000): Promise<{ ok: boolean, response: any }> { - const timeout = (p: Promise, t: number) => new Promise((resolve) => { - p.then((response) => { - resolve(); - return response; - }); - setTimeout(() => resolve({ok: false}), t); - }); - - promise = promise.then((response) => ({ok: true, response})); - - return Promise.race([ - promise, - timeout(promise, time), - ]); - } - -} - -const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; -const DEFAULT_PARAMS = /=[^,]+/mg; -const FAT_ARROWS = /=>.*$/mg; diff --git a/src/@loafer/decorator/Decorator.ts b/src/@loafer/decorator/Decorator.ts deleted file mode 100644 index 2fb4d7f..0000000 --- a/src/@loafer/decorator/Decorator.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { - Type, - PropertyKeyType, -} from '@loafer/core'; - -import { - Annotation, -} from '@loafer/core/reflect'; - -export interface Decorator { - classDecorator?: (target: TFunction) => TFunction | void; - propertyDecorator?: (target: Object, propertyKey: PropertyKeyType) => void; - methodDecorator?: (target: Object, propertyKey: PropertyKeyType, - descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor | void; - parameterDecorator?: (target: Object, propertyKey: PropertyKeyType, parameterIndex: number) => void; -} - -export abstract class Decorator extends Annotation { - public constructor(attribute?: Attribute) { - super(attribute); - } -} diff --git a/src/@loafer/decorator/DecoratorFactory.ts b/src/@loafer/decorator/DecoratorFactory.ts deleted file mode 100644 index ea3a21b..0000000 --- a/src/@loafer/decorator/DecoratorFactory.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { - Type, - MetadataKeyType, -} from '@loafer/core'; - -import { - Annotation, - Class, - Constructor, - Field, - Method, - Parameter, - Metadata, -} from '@loafer/core/reflect'; - -import { - TypeUtil, -} from '@loafer/core/util/TypeUtil'; - -import { Decorator } from './Decorator'; -import { DecoratorUtil } from './util'; -import { DecoratorType } from './type'; -import { NotSupportedDecoratorError } from './error'; - - -export class DecoratorFactory { - public static create = (DecoratorClass: Type>) => { - return (attribute: Attribute) => { - const annotation: Decorator = new DecoratorClass(attribute); - const name: string = DecoratorClass.name; - - return (...decoratorArgs: any[]) => { - const decoratorType: DecoratorType = DecoratorUtil.getDecoratorType(decoratorArgs); - - const [target, propertyKey, descriptorOrParameterIndex] = decoratorArgs; - - const clazz: Class = Class._defineClass(TypeUtil.getType(target)); - let field: Field = null; - let method: Method = null; - let parameter: Parameter = null; - let cons: Constructor = null; - - switch (decoratorType) { - case DecoratorType.CLASS: - try { - cons = clazz._defineConstructor(Metadata.getOwnParamTypes(target)); - clazz._addAnnotation(annotation); - return annotation.classDecorator.call(annotation, target); - } catch (e) { - if (e instanceof NotSupportedDecoratorError) { - throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Class.`); - } - throw e; - } - case DecoratorType.PROPERTY: - try { - field = clazz._defineField(propertyKey, Metadata.getOwnType(target, propertyKey)); - field._addAnnotation(annotation); - return annotation.propertyDecorator.call(annotation, target, propertyKey); - } catch (e) { - if (e instanceof NotSupportedDecoratorError) { - throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Property.`); - } - throw e; - } - case DecoratorType.METHOD: - try { - method = clazz._defineMethod(propertyKey, - Metadata.getOwnParamTypes(target, propertyKey), - Metadata.getOwnReturnType(target, propertyKey)); - method._addAnnotation(annotation); - - return annotation.methodDecorator.call(annotation, target, propertyKey, descriptorOrParameterIndex); - } catch (e) { - if (e instanceof NotSupportedDecoratorError) { - throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Method.`); - } - throw e; - } - case DecoratorType.PARAMETER: - try { - if (undefined === propertyKey) { - cons = clazz.getConstructor(); - parameter = cons.getParameter(descriptorOrParameterIndex); - } else { - method = clazz._defineMethod(propertyKey, - Metadata.getOwnParamTypes(target, propertyKey), - Metadata.getOwnReturnType(target, propertyKey)); - parameter = method.getParameter(descriptorOrParameterIndex); - } - parameter._addAnnotation(annotation); - - return annotation.parameterDecorator.call(annotation, target, propertyKey, descriptorOrParameterIndex); - } catch (e) { - if (e instanceof NotSupportedDecoratorError) { - throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Parameter.`); - } - throw e; - } - default: - throw new NotSupportedDecoratorError(`Cannot determine decorator[@${name}] type.`); - } - }; - }; - } - -} diff --git a/src/@loafer/decorator/error.ts b/src/@loafer/decorator/error.ts deleted file mode 100644 index 5aaf4e9..0000000 --- a/src/@loafer/decorator/error.ts +++ /dev/null @@ -1,13 +0,0 @@ -export class NotSupportedDecoratorError extends Error { - public constructor(message?: string) { - super(message); - Object.setPrototypeOf(this, new.target.prototype); - } -} - -export class NotDecoratedClassError extends Error { - public constructor(message?: string) { - super(message); - Object.setPrototypeOf(this, new.target.prototype); - } -} diff --git a/src/@loafer/decorator/index.ts b/src/@loafer/decorator/index.ts deleted file mode 100644 index 1543e2e..0000000 --- a/src/@loafer/decorator/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export * from './Decorator'; -export * from './DecoratorFactory'; -export * from './error'; -export * from './type'; -export * from './util'; diff --git a/src/@loafer/decorator/type.ts b/src/@loafer/decorator/type.ts deleted file mode 100644 index 8781acd..0000000 --- a/src/@loafer/decorator/type.ts +++ /dev/null @@ -1,8 +0,0 @@ -export enum DecoratorType { - CLASS = 'Clazz', - PROPERTY = 'Property', - METHOD = 'Method', - PARAMETER = 'Parameter', -} - -export type DecoratorParametersType = [any, string | symbol, number | PropertyDescriptor]; diff --git a/src/@loafer/decorator/util.ts b/src/@loafer/decorator/util.ts deleted file mode 100644 index 70b34e0..0000000 --- a/src/@loafer/decorator/util.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { - PropertyKeyType, -} from '@loafer/core'; - -import { - TypeUtil, -} from '@loafer/core/util/TypeUtil'; - - -import { - DecoratorType, - DecoratorParametersType, -} from './type'; - -export class DecoratorUtil { - /** - * - * @param {any[]} args - * @returns {DecoratorType} - */ - public static getDecoratorType(args: any[]): DecoratorType { - const [, propertyKey, descriptor] = args; - - if (typeof descriptor === 'number') { - return DecoratorType.PARAMETER; - } - - if (propertyKey && descriptor === undefined || descriptor && (descriptor.get || descriptor.set)) { - return DecoratorType.PROPERTY; - } - return (descriptor && descriptor.value) ? DecoratorType.METHOD : DecoratorType.CLASS; - } - - /** - * - * @param target - * @param {string} propertyKey - * @returns {DecoratorParametersType} - */ - public static decoratorArgs(target: any, propertyKey: PropertyKeyType): DecoratorParametersType { - return [ - target, - propertyKey, - TypeUtil.descriptorOf(target, propertyKey), - ]; - } - -} diff --git a/src/@loafer/ng-logger/core/config.ts b/src/@loafer/ng-logger/core/config.ts deleted file mode 100644 index 0e2c64d..0000000 --- a/src/@loafer/ng-logger/core/config.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { LoggerLevel } from './type'; - -export interface LoggerConfig { - level: LoggerLevel; - serverLogLevel?: LoggerLevel; -} diff --git a/src/@loafer/ng-logger/core/index.ts b/src/@loafer/ng-logger/core/index.ts deleted file mode 100644 index 40a47eb..0000000 --- a/src/@loafer/ng-logger/core/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './config'; -export * from './token'; -export * from './type'; diff --git a/src/@loafer/ng-logger/core/token.ts b/src/@loafer/ng-logger/core/token.ts deleted file mode 100644 index ff2a17c..0000000 --- a/src/@loafer/ng-logger/core/token.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { InjectionToken } from '@angular/core'; - -export const _LOGGER_CONFIG = new InjectionToken('@loafer/ng-logger Internal Logger config'); diff --git a/src/@loafer/ng-logger/core/type.ts b/src/@loafer/ng-logger/core/type.ts deleted file mode 100644 index 72d4473..0000000 --- a/src/@loafer/ng-logger/core/type.ts +++ /dev/null @@ -1,26 +0,0 @@ -export enum LoggerLevel { - TRACE = 0, - DEBUG, - INFO, - LOG, - WARN, - ERROR, - OFF, -} - -export const LoggerLevelName = [ - 'TRACE', - 'DEBUG', - 'INFO', - 'LOG', - 'WARN', - 'ERROR', - 'OFF' -]; - -export interface ServerLoggingParameter { - level: string; - message: string; - addtional?: string; - timestamp: Date; -} diff --git a/src/@loafer/ng-logger/index.ts b/src/@loafer/ng-logger/index.ts deleted file mode 100644 index 4d007cd..0000000 --- a/src/@loafer/ng-logger/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './ng-logger.module'; diff --git a/src/@loafer/ng-logger/ng-logger.module.ts b/src/@loafer/ng-logger/ng-logger.module.ts deleted file mode 100644 index bcde9f1..0000000 --- a/src/@loafer/ng-logger/ng-logger.module.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { - NgModule, - ModuleWithProviders, - Type, - Inject, - InjectionToken, -} from '@angular/core'; - -import { - _LOGGER_CONFIG, - LoggerConfig, -} from './core'; - -import { - SERVICES, LoggerService, -} from './service'; - -export interface LoggerFeatureModuleConfig { - url?: any; -} - -export interface LoggerRootModuleConfig { - config: LoggerConfig; -} - -@NgModule({}) -export class LoggerRootModule { - constructor( - private loggerService: LoggerService, - ) { - } -} - -@NgModule({}) -export class LoggerFeatureModule { - constructor( - private loggerService: LoggerService, - private root: LoggerRootModule, - ) { - } -} - -@NgModule({}) -export class LoggerModule { - static forRoot(config: LoggerRootModuleConfig): ModuleWithProviders { - return { - ngModule: LoggerRootModule, - providers: [ - { - provide: _LOGGER_CONFIG, - useValue: config.config, - }, - SERVICES, - ], - }; - } - - static forFeature(config: LoggerFeatureModuleConfig): ModuleWithProviders { - return { - ngModule: LoggerFeatureModule, - providers: [ - ], - }; - } -} diff --git a/src/@loafer/ng-logger/service/index.ts b/src/@loafer/ng-logger/service/index.ts deleted file mode 100644 index 663327c..0000000 --- a/src/@loafer/ng-logger/service/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './logger.service'; - -import { LoggerService } from './logger.service'; - -export const SERVICES = [ - LoggerService, -]; diff --git a/src/@loafer/ng-logger/service/logger.service.ts b/src/@loafer/ng-logger/service/logger.service.ts deleted file mode 100644 index 768a5de..0000000 --- a/src/@loafer/ng-logger/service/logger.service.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { Injectable, Inject, PLATFORM_ID } from '@angular/core'; -import { isPlatformBrowser } from '@angular/common'; - -import { - LoggerConfig, - LoggerLevel, - LoggerLevelName, - _LOGGER_CONFIG, -} from '../core'; - -export type ConsoleFunc = (message?: any, ...optionalParams: any[]) => void; - -@Injectable() -export class LoggerService { - private _isIE: boolean; - - public constructor( - @Inject(_LOGGER_CONFIG) private readonly config: LoggerConfig, - @Inject(PLATFORM_ID) private readonly platformId, - ) { - this._isIE = isPlatformBrowser(platformId) && - !!(navigator.userAgent.indexOf('MSIE') !== -1 || navigator.userAgent.match(/Trident\//) || navigator.userAgent.match(/Edge\//)); - - } - - public get trace(): ConsoleFunc { - return this.getConsoleMethod(LoggerLevel.TRACE); - } - public get debug(): ConsoleFunc { - return this.getConsoleMethod(LoggerLevel.DEBUG); - } - public get info(): ConsoleFunc { - return this.getConsoleMethod(LoggerLevel.INFO); - } - public get log(): ConsoleFunc { - return this.getConsoleMethod(LoggerLevel.LOG); - } - public get warn(): ConsoleFunc { - return this.getConsoleMethod(LoggerLevel.WARN); - } - public get error(): ConsoleFunc { - return this.getConsoleMethod(LoggerLevel.ERROR); - } - - private _console_log: ConsoleFunc; - - private getConsoleMethod(level: LoggerLevel): ConsoleFunc { - if (level < this.config.level) { - return (message, ...additional: any[]) => {}; - } - - if (this._isIE) { - switch (level) { - case LoggerLevel.WARN: - return console.warn.bind(console, ...this.getLogHeader(level)); - case LoggerLevel.ERROR: - return console.error.bind(console, ...this.getLogHeader(level)); - case LoggerLevel.INFO: - return console.info.bind(console, ...this.getLogHeader(level)); - default: - return console.log.bind(console, ...this.getLogHeader(level)); - } - } else { - return console.log.bind(console, ...this.getLogHeader(level)); - } - } - - private getLogHeader(level: LoggerLevel): any[] { - const params: any[] = []; - params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`); - if (!this._isIE) { - const color = this._getColor(level); - params.push(`color:${color}`); - } - return params; - } - - private _timestamp(): string { - return new Date().toISOString(); - } - - private _log(level: LoggerLevel, message, additional: any[] = []): void { - if (!message) { - return; - } - - // Allow logging on server even if client log level is off - // if (logOnServer) { - // this._logOnServer(level, message, additional); - // } - - // if no message or the log level is less than the environ - if (level < this.config.level) { - return; - } - - try { - message = typeof message === 'string' ? message : JSON.stringify(message, null, 2); - } catch (e) { - additional = [message, ...additional]; - message = 'The provided "message" value could not be parsed with JSON.stringify().'; - } - - // Coloring doesn't work in IE - if (this._isIE) { - return this._logIE(level, message, additional); - } - - const color = this._getColor(level); - - const params: any[] = []; - params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`); - params.push(`color:${color}`); - params.push(message); - params.push(...additional); - - console.log.apply(console, params); - } - - private _logIE(level: LoggerLevel, message: string, additional: any[] = []): void { - const params: any[] = []; - params.push(`${this._timestamp()} [${LoggerLevelName[level]}] `); - params.push(message); - params.push(...additional); - - switch (level) { - case LoggerLevel.WARN: - console.warn.apply(console, params); - break; - case LoggerLevel.ERROR: - console.error.apply(console, params); - break; - case LoggerLevel.INFO: - console.info.apply(console, params); - break; - default: - console.log.apply(console, params); - } - } - - private _getColor(level: LoggerLevel): 'blue' | 'teal' | 'gray' | 'red' | undefined { - switch (level) { - case LoggerLevel.TRACE: - return 'blue'; - case LoggerLevel.DEBUG: - return 'teal'; - case LoggerLevel.INFO: - case LoggerLevel.LOG: - return 'gray'; - case LoggerLevel.WARN: - case LoggerLevel.ERROR: - return 'red'; - case LoggerLevel.OFF: - default: - return; - } - } -} diff --git a/src/@loafer/ng-rest/client/RESTClient.ts b/src/@loafer/ng-rest/client/RESTClient.ts deleted file mode 100644 index 77dd5da..0000000 --- a/src/@loafer/ng-rest/client/RESTClient.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { Injectable, Inject } from '@angular/core'; -import { Location } from '@angular/common'; -import { HttpClient, HttpHeaders, HttpParams, HttpErrorResponse } from '@angular/common/http'; - -import { Observable } from 'rxjs/Observable'; -import 'rxjs/add/operator/do'; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/catch'; -import 'rxjs/add/operator/timeout'; -import 'rxjs/add/observable/throw'; - -import { - RESTError, - RESTClientError, -} from '../protocol'; - -export class RESTClient { - constructor( - private _baseURL: string, - private _httpClient: HttpClient, - ) { - } - - public get httpClient(): HttpClient { - return this._httpClient; - } - - public request(method: string, entry: string, options?: { - body?: any; - headers?: HttpHeaders | { - [header: string]: string | string[]; - }; - observe?: 'body'; - params?: HttpParams | { - [param: string]: string | string[]; - }; - responseType?: 'json'; - reportProgress?: boolean; - withCredentials?: boolean; - }): Observable { - return this._httpClient - .request(method, Location.joinWithSlash(this._baseURL, entry), options) - .map(response => response) - .catch((error: HttpErrorResponse) => { - const restClientError: RESTClientError = { - request: { - method: method, - entry: entry, - options: options, - }, - response: error.error, - }; - - console.error(restClientError); - // const aryMsg = error.error.message.split('|'); - // const resError: RESTError = { - // code: error.error.code, - // message: aryMsg[0], - // data: aryMsg[1] === 'null' ? '' : aryMsg[1], - // }; - return Observable.throw(restClientError); - }); - } -} diff --git a/src/@loafer/ng-rest/client/index.ts b/src/@loafer/ng-rest/client/index.ts deleted file mode 100644 index 74509a3..0000000 --- a/src/@loafer/ng-rest/client/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './RESTClient'; diff --git a/src/@loafer/ng-rest/core/index.ts b/src/@loafer/ng-rest/core/index.ts deleted file mode 100644 index 6b36029..0000000 --- a/src/@loafer/ng-rest/core/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './token'; diff --git a/src/@loafer/ng-rest/core/token.ts b/src/@loafer/ng-rest/core/token.ts deleted file mode 100644 index f869f67..0000000 --- a/src/@loafer/ng-rest/core/token.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { InjectionToken } from '@angular/core'; - -export const _REST_BASE_URL = new InjectionToken('@loafer/ng-rest Internal Base URL'); diff --git a/src/@loafer/ng-rest/index.ts b/src/@loafer/ng-rest/index.ts deleted file mode 100644 index 5f8d09b..0000000 --- a/src/@loafer/ng-rest/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './ng-rest.module'; diff --git a/src/@loafer/ng-rest/ng-rest.module.ts b/src/@loafer/ng-rest/ng-rest.module.ts deleted file mode 100644 index 0da03a8..0000000 --- a/src/@loafer/ng-rest/ng-rest.module.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { - NgModule, - ModuleWithProviders, - Type, - Inject, - InjectionToken, -} from '@angular/core'; - -import { - _REST_BASE_URL, -} from './core'; - -import { - SERVICES, RESTService, -} from './service'; - -export interface RESTFeatureModuleConfig { - url?: any; -} - -export interface RESTRootModuleConfig { - baseURL: string; -} - -@NgModule({}) -export class RESTRootModule { - constructor( - private restService: RESTService, - ) { - } -} - -@NgModule({}) -export class RESTFeatureModule { - constructor( - private restService: RESTService, - private root: RESTRootModule, - ) { - } -} - -@NgModule({}) -export class RESTModule { - static forRoot(config: RESTRootModuleConfig): ModuleWithProviders { - return { - ngModule: RESTRootModule, - providers: [ - { - provide: _REST_BASE_URL, - useValue: config.baseURL, - }, - SERVICES, - ], - }; - } - - static forFeature(config: RESTFeatureModuleConfig): ModuleWithProviders { - return { - ngModule: RESTFeatureModule, - providers: [ - ], - }; - } -} diff --git a/src/@loafer/ng-rest/protocol/RESTError.ts b/src/@loafer/ng-rest/protocol/RESTError.ts deleted file mode 100644 index 4431349..0000000 --- a/src/@loafer/ng-rest/protocol/RESTError.ts +++ /dev/null @@ -1,14 +0,0 @@ -export interface RESTClientError { - request: { - method: string; - entry: string; - options?: any; - }; - response: RESTError; -} - -export interface RESTError { - code: number; - message: string; - data?: any; -} diff --git a/src/@loafer/ng-rest/protocol/index.ts b/src/@loafer/ng-rest/protocol/index.ts deleted file mode 100644 index b025dd3..0000000 --- a/src/@loafer/ng-rest/protocol/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './RESTError'; diff --git a/src/@loafer/ng-rest/service/index.ts b/src/@loafer/ng-rest/service/index.ts deleted file mode 100644 index 20da8fb..0000000 --- a/src/@loafer/ng-rest/service/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './rest.service'; - -import { RESTService } from './rest.service'; - -export const SERVICES = [ - RESTService, -]; diff --git a/src/@loafer/ng-rest/service/rest.service.spec.ts b/src/@loafer/ng-rest/service/rest.service.spec.ts deleted file mode 100644 index cd6b85b..0000000 --- a/src/@loafer/ng-rest/service/rest.service.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { TestBed, inject } from '@angular/core/testing'; - -import { RESTService } from './rest.service'; - -describe('RESTService', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - providers: [RESTService] - }); - }); - - it('should be created', inject([RESTService], (service: RESTService) => { - expect(service).toBeTruthy(); - })); -}); diff --git a/src/@loafer/ng-rest/service/rest.service.ts b/src/@loafer/ng-rest/service/rest.service.ts deleted file mode 100644 index f5f63ec..0000000 --- a/src/@loafer/ng-rest/service/rest.service.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Injectable, Inject } from '@angular/core'; -import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; - -import { Observable } from 'rxjs/Observable'; - -import { Location } from '@angular/common'; - -import 'rxjs/add/operator/do'; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/catch'; -import 'rxjs/add/operator/timeout'; -import 'rxjs/add/observable/throw'; - -import { _REST_BASE_URL } from '../core'; -import { RESTClient } from '../client'; - -@Injectable() -export class RESTService extends RESTClient { - - constructor( - @Inject(_REST_BASE_URL) _baseURL: string, - @Inject(HttpClient) _httpClient: HttpClient, - ) { - super(_baseURL, _httpClient); - } -} diff --git a/src/@loafer/ng-rpc/client/RPCClient.ts b/src/@loafer/ng-rpc/client/RPCClient.ts deleted file mode 100644 index 4f808a8..0000000 --- a/src/@loafer/ng-rpc/client/RPCClient.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { Observable } from 'rxjs/Observable'; -import { Subject } from 'rxjs/Subject'; - -import { RPCClientError } from '../protocol/RPCError'; -import { RPCClientRWC } from './RPCClientRWC'; - -import { - RPCClientCodec, - RPCClientResponseCodec, - RPCClientNotificationCodec, -} from '../protocol/RPCClientCodec'; - -export interface RPCRequestState { - subject: Subject; - request: { - method: string; - params: any[]; - }; -} - -export abstract class RPCClient { - private _requestID: number; - - private _pendingRequestsCount: number; - private _pendingRequests: Map; - - public constructor( - private _codec: RPCClientCodec, - private _rwc: RPCClientRWC, - ) { - this._requestID = 0; - this._pendingRequestsCount = 0; - this._pendingRequests = new Map(); - } - - private getRequestID(): number { - return ++this._requestID; - } - - /** - * connect - */ - public connect(queryString?: string): void { - this._rwc.connect(queryString); - this._rwc.readResponse().subscribe( - (value: Object) => { - this.onMessage(value); - }, - (error: any) => { - console.error(error); - }, - () => { - - } - ); - } - - /** - * close - */ - public disconnect() { - this._rwc.disconnect(); - } - - /** - * notify - */ - public send(method: string, ...args: any[]): void { - this.sendInternal(false, method, args); - } - - /** - * call - */ - public call(method: string, ...args: any[]): Observable { - return this.sendInternal(true, method, args); - } - - /** - * callTimeout - */ - public callTimeout(ms: number, method: string, ...args: any[]): Observable { - - return undefined; - } - - private sendInternal(hasResponse: boolean, method: string, args?: any[]): Observable | undefined { - let id: number; - let resSubject: Subject; - if (hasResponse) { - id = this.getRequestID(); - resSubject = new Subject(); - const reqState: RPCRequestState = { - subject: resSubject, - request: { - method: method, - params: args, - } - }; - this._pendingRequests.set(id, reqState); - this._pendingRequestsCount++; - } - - const req = this._codec.request(method, args, id); - this._rwc.writeRequest(req); - - if (undefined !== resSubject) { - return resSubject.asObservable(); - } - } - - private onMessage(message: Object): void { - const resCodec = this._codec.response(message); - - if (resCodec.isNotification()) { - this.onNotification(resCodec.notification()); - } else { - this.onResponse(resCodec); - } - } - - protected onResponse(resCodec: RPCClientResponseCodec): void { - const id = resCodec.id(); - const result = resCodec.result(); - const error = resCodec.error(); - - const reqState: RPCRequestState = this._pendingRequests.get(id); - - this._pendingRequests.delete(id); - this._pendingRequestsCount--; - - if (undefined !== result) { - reqState.subject.next(result); - } else if (undefined !== error) { - const rpcClientError: RPCClientError = { - request: reqState.request, - response: error, - }; - console.error(rpcClientError); - reqState.subject.error(rpcClientError); - } - } - - protected abstract onNotification(notiCodec: RPCClientNotificationCodec): void; -} diff --git a/src/@loafer/ng-rpc/client/RPCClientRWC.ts b/src/@loafer/ng-rpc/client/RPCClientRWC.ts deleted file mode 100644 index 406d4ee..0000000 --- a/src/@loafer/ng-rpc/client/RPCClientRWC.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { InjectionToken } from '@angular/core'; - -import { Observable } from 'rxjs/Observable'; - -export interface RPCClientRWC { - connect(queryString?: string): void; - readResponse(): Observable; - writeRequest(data: any): void; - disconnect(): void; - connectionStatus(): Observable; -} diff --git a/src/@loafer/ng-rpc/client/index.ts b/src/@loafer/ng-rpc/client/index.ts deleted file mode 100644 index efdf231..0000000 --- a/src/@loafer/ng-rpc/client/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './RPCClient'; -export * from './RPCClientRWC'; diff --git a/src/@loafer/ng-rpc/client/rwc/websocket/RPCClientWebsocketRWC.ts b/src/@loafer/ng-rpc/client/rwc/websocket/RPCClientWebsocketRWC.ts deleted file mode 100644 index f2d0849..0000000 --- a/src/@loafer/ng-rpc/client/rwc/websocket/RPCClientWebsocketRWC.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { Observable } from 'rxjs/Observable'; -import { Subject } from 'rxjs/Subject'; -import { map } from 'rxjs/operator/map'; - -import { - RxWebsocketSubject, - RxWebsocketSubjectConfig, -} from './RxWebsocketSubject'; - -import { RPCClientRWC } from '../../RPCClientRWC'; - -export class RPCClientWebsocketRWC implements RPCClientRWC { - private _wsSocketSubject: RxWebsocketSubject; - private _responseSubject: Subject; - - public constructor( - private _config: RxWebsocketSubjectConfig, - ) { - this._wsSocketSubject = new RxWebsocketSubject(this._config); - } - - public connect(queryString?: string): void { - if (undefined !== queryString) { - this._wsSocketSubject.queryString = queryString; - } - this._wsSocketSubject.connect(); - this._wsSocketSubject.subscribe( - (value: Object) => { - if (undefined !== this._responseSubject) { - this._responseSubject.next(value); - } - }, - (error: any) => { - if (undefined !== this._responseSubject) { - this._responseSubject.error(error); - } - }, - () => { - console.log('sss'); - } - ); - } - - public disconnect(): void { - this._wsSocketSubject.disconnect(); - } - - public connectionStatus(): Observable { - return this._wsSocketSubject.connectionStatus; - } - - public readResponse(): Observable { - if (undefined === this._responseSubject) { - this._responseSubject = new Subject(); - } - return this._responseSubject.asObservable(); - } - - public writeRequest(data: any): void { - this._wsSocketSubject.write(data); - } -} diff --git a/src/@loafer/ng-rpc/client/rwc/websocket/RxWebsocketSubject.ts b/src/@loafer/ng-rpc/client/rwc/websocket/RxWebsocketSubject.ts deleted file mode 100644 index e0d1e39..0000000 --- a/src/@loafer/ng-rpc/client/rwc/websocket/RxWebsocketSubject.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { Observable } from 'rxjs/Observable'; -import { Observer } from 'rxjs/Observer'; -import { Subject } from 'rxjs/Subject'; -import { - WebSocketSubject, - WebSocketSubjectConfig -} from 'rxjs/observable/dom/WebSocketSubject'; - -import 'rxjs/add/operator/distinctUntilChanged'; -import 'rxjs/add/operator/share'; -import 'rxjs/add/operator/takeWhile'; -import 'rxjs/add/observable/interval'; - -export interface RxWebsocketSubjectConfig { - url: string; - protocol?: string | Array; - reconnectInterval?: 5000; - reconnectRetry?: 10; -} - -export class RxWebsocketSubject extends Subject { - private _reconnectionObservable: Observable; - private _wsSubjectConfig: WebSocketSubjectConfig; - private _socket: WebSocketSubject; - private _connectionObserver: Observer; - private _connectionStatus: Observable; - private _queryString: string; - - public constructor(private _config: RxWebsocketSubjectConfig) { - super(); - - this._connectionStatus = new Observable((observer) => { - this._connectionObserver = observer; - }).share().distinctUntilChanged(); - - this._wsSubjectConfig = { - url: _config.url, - protocol: _config.protocol, - closeObserver: { - next: (e: CloseEvent) => { - this._socket = null; - this._connectionObserver.next(false); - } - }, - openObserver: { - next: (e: Event) => { - this._connectionObserver.next(true); - } - }, - }; - - this._connectionStatus.subscribe((isConnected: boolean) => { - if (!this._reconnectionObservable && typeof(isConnected) === 'boolean' && !isConnected) { - this.reconnect(); - } - }); - } - - public set queryString(query: string) { - this._queryString = query; - } - - public get queryString(): string | undefined { - return this._queryString; - } - - public get connectionStatus(): Observable { - return this._connectionStatus; - } - - public connect(): void { - const wsSubjectConfig = Object.assign({}, this._wsSubjectConfig); - if (undefined !== this._queryString) { - wsSubjectConfig.url = wsSubjectConfig.url + '?' + this._queryString; - } - - this._socket = new WebSocketSubject(wsSubjectConfig); - this._socket.subscribe( - (m) => { - this.next(m); - }, - (error: Event) => { - if (!this._socket) { - this.reconnect(); - } - } - ); - } - - public disconnect(): void { - this._socket.complete(); - } - - private reconnect(): void { - this._reconnectionObservable = Observable.interval(this._config.reconnectInterval) - .takeWhile((v, index) => { - return index < this._config.reconnectRetry && !this._socket; - }); - this._reconnectionObservable.subscribe( - () => { - this.connect(); - }, - null, - () => { - this._reconnectionObservable = null; - if (!this._socket) { - this.complete(); - this._connectionObserver.complete(); - } - } - ); - } - - public write(data: any): void { - this._socket.next(data); - } -} diff --git a/src/@loafer/ng-rpc/client/rwc/websocket/index.ts b/src/@loafer/ng-rpc/client/rwc/websocket/index.ts deleted file mode 100644 index 80de9d0..0000000 --- a/src/@loafer/ng-rpc/client/rwc/websocket/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './RPCClientWebsocketRWC'; -export * from './RxWebsocketSubject'; diff --git a/src/@loafer/ng-rpc/core/error.ts b/src/@loafer/ng-rpc/core/error.ts deleted file mode 100644 index f90aac7..0000000 --- a/src/@loafer/ng-rpc/core/error.ts +++ /dev/null @@ -1,6 +0,0 @@ -export class SubscriberParameterError extends Error { - public constructor(message?: string) { - super(message); - Object.setPrototypeOf(this, new.target.prototype); - } -} diff --git a/src/@loafer/ng-rpc/core/index.ts b/src/@loafer/ng-rpc/core/index.ts deleted file mode 100644 index d97df57..0000000 --- a/src/@loafer/ng-rpc/core/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './error'; -export * from './token'; diff --git a/src/@loafer/ng-rpc/core/token.ts b/src/@loafer/ng-rpc/core/token.ts deleted file mode 100644 index fe8bd6e..0000000 --- a/src/@loafer/ng-rpc/core/token.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { InjectionToken } from '@angular/core'; - -export const RPC_CODEC = new InjectionToken('@loafer/ng-rpc RPC codec'); -export const RPC_RWC = new InjectionToken('@loafer/ng-rpc RPC rwc'); - -export const _ROOT_SUBSCRIBERS = new InjectionToken('@loafer/ng-rpc RPC root subscribers'); -export const _FEATURE_SUBSCRIBERS = new InjectionToken('@loafer/ng-rpc RPC feature subscribers'); - diff --git a/src/@loafer/ng-rpc/decorator/index.ts b/src/@loafer/ng-rpc/decorator/index.ts deleted file mode 100644 index f75f0fd..0000000 --- a/src/@loafer/ng-rpc/decorator/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './rpc-subscriber.decorator'; diff --git a/src/@loafer/ng-rpc/decorator/rpc-subscriber.decorator.ts b/src/@loafer/ng-rpc/decorator/rpc-subscriber.decorator.ts deleted file mode 100644 index 8aeadf5..0000000 --- a/src/@loafer/ng-rpc/decorator/rpc-subscriber.decorator.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { - Type, - PropertyKeyType, -} from '@loafer/core'; - -import { - Decorator, - DecoratorFactory, -} from '@loafer/decorator'; - -export interface RPCSubscriberDecoratorAttribute { - method: string; -} - -export class RPCSubscriberDecorator extends Decorator { - public constructor(config: RPCSubscriberDecoratorAttribute) { - super(config); - } - - public methodDecorator = (target: Object, propertyKey: PropertyKeyType, - descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor | void => { - } - -} - -export const RPCSubscriber = DecoratorFactory.create(RPCSubscriberDecorator); diff --git a/src/@loafer/ng-rpc/index.ts b/src/@loafer/ng-rpc/index.ts deleted file mode 100644 index e96ec44..0000000 --- a/src/@loafer/ng-rpc/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './ng-rpc.module'; diff --git a/src/@loafer/ng-rpc/ng-rpc.module.ts b/src/@loafer/ng-rpc/ng-rpc.module.ts deleted file mode 100644 index 34f2139..0000000 --- a/src/@loafer/ng-rpc/ng-rpc.module.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { - NgModule, - ModuleWithProviders, - Type, - Inject, - InjectionToken, -} from '@angular/core'; - -import { - RPC_CODEC, - RPC_RWC, - - _ROOT_SUBSCRIBERS, - _FEATURE_SUBSCRIBERS, -} from './core'; - -import { - RPCClientRWC, -} from './client/RPCClientRWC'; - -import { - RPCClientCodec, -} from './protocol'; - -import { - SERVICES, RPCService, -} from './service'; - -import { - SUBSCRIBERS, RPCSubscribeService, -} from './subscribe'; - -export interface RPCFeatureModuleConfig { - subscribers?: Type[]; -} - -export interface RPCRootModuleConfig { - subscribers?: Type[]; -} - -@NgModule({}) -export class RPCRootModule { - constructor( - private rpcService: RPCService, - private rpcSubscribeService: RPCSubscribeService, - @Inject(_ROOT_SUBSCRIBERS) rootSubscribers: any[], - ) { - rootSubscribers.forEach((subscriber) => { - rpcSubscribeService.addSubscriber(subscriber); - }); - } -} - -@NgModule({}) -export class RPCFeatureModule { - constructor( - private rpcService: RPCService, - private rpcSubscribeService: RPCSubscribeService, - @Inject(_FEATURE_SUBSCRIBERS) featureSubscribersGroups: any[][], - private root: RPCRootModule, - ) { - featureSubscribersGroups.forEach((featureSubscribers) => { - featureSubscribers.forEach((subscriber) => { - rpcSubscribeService.addSubscriber(subscriber); - }); - }); - } -} - -@NgModule({}) -export class RPCModule { - static forRoot(config: RPCRootModuleConfig): ModuleWithProviders { - const subscribers = undefined === config.subscribers ? [] : config.subscribers; - return { - ngModule: RPCRootModule, - providers: [ - subscribers, - { - provide: _ROOT_SUBSCRIBERS, - deps: subscribers, - useFactory: createSourceInstances, - }, - SERVICES, - SUBSCRIBERS, - ], - }; - } - - static forFeature(config: RPCFeatureModuleConfig): ModuleWithProviders { - const subscribers = undefined === config.subscribers ? [] : config.subscribers; - - return { - ngModule: RPCFeatureModule, - providers: [ - subscribers, - { - provide: _FEATURE_SUBSCRIBERS, - multi: true, - deps: subscribers, - useFactory: createSourceInstances, - }, - ], - }; - } -} - -export function createSourceInstances(...instances: any[]) { - return instances; -} diff --git a/src/@loafer/ng-rpc/protocol/RPCClientCodec.ts b/src/@loafer/ng-rpc/protocol/RPCClientCodec.ts deleted file mode 100644 index 94af99e..0000000 --- a/src/@loafer/ng-rpc/protocol/RPCClientCodec.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { RPCError } from './RPCError'; - -export interface RPCClientCodec { - request(method: string, args: any[], id: number): any; - response(res: any): RPCClientResponseCodec; -} - -export interface RPCClientResponseCodec { - id(): number | undefined; - error(): RPCError | undefined; - result(): any | undefined; - - isNotification(): boolean; - notification(): RPCClientNotificationCodec | undefined; -} - -export interface RPCClientNotificationCodec { - method(): string; - params(): any | undefined; -} diff --git a/src/@loafer/ng-rpc/protocol/RPCError.ts b/src/@loafer/ng-rpc/protocol/RPCError.ts deleted file mode 100644 index 91f4aac..0000000 --- a/src/@loafer/ng-rpc/protocol/RPCError.ts +++ /dev/null @@ -1,41 +0,0 @@ -export interface RPCClientError { - request: { - method: string, - params: any[], - }; - response: RPCError; -} - -/** - * Error object representation when a method invocation fails. - */ -export interface RPCError { - /** Indicates the error type that occurred. */ - code: RPCErrorCode; - - /** A short description of the error. */ - message: string; - - /** Additional information about the error */ - data?: any; -}/* - -/** Error codes are same as xml-rpc codes. See http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php */ -export const enum RPCErrorCode { - /** Parse error Invalid JSON was received by the Server. */ - ParseError = -32700, - - /** Invalid Request The JSON sent is not a valid Request object. */ - InvalidRequest = -32600, - - /** The method does not exist / is not available. */ - MethodNotFound = -32601, - - /** Invalid method parameter(s). */ - InvalidParams = - -32602, - - /** Internal JSON-RPC error. */ - InternalError = -32603 - - /** -32000 to -32099: Reserved for implementation-defined Server errors. */ -} diff --git a/src/@loafer/ng-rpc/protocol/index.ts b/src/@loafer/ng-rpc/protocol/index.ts deleted file mode 100644 index a9d3a36..0000000 --- a/src/@loafer/ng-rpc/protocol/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './RPCClientCodec'; -export * from './RPCError'; diff --git a/src/@loafer/ng-rpc/protocol/json/JSONRPCClientCodec.ts b/src/@loafer/ng-rpc/protocol/json/JSONRPCClientCodec.ts deleted file mode 100644 index 27d3980..0000000 --- a/src/@loafer/ng-rpc/protocol/json/JSONRPCClientCodec.ts +++ /dev/null @@ -1,125 +0,0 @@ -import { - RPCClientCodec, - RPCClientResponseCodec, - RPCClientNotificationCodec, -} from '../RPCClientCodec'; - -import { - RPCError, -} from '../RPCError'; - -export interface ClientNotification { - method: string; - params?: string[]; -} - -export interface ClientRequest { - jsonrpc: string; - method: string; - params?: string[]; - id?: number; -} - -export interface ClientResponse { - jsonrpc: string; - result?: any; - error?: RPCError; - id?: number; -} - -export class JSONRPCClientCodec implements RPCClientCodec { - public request(method: string, args: any[], id?: number): any { - const params = convertParamsToStringArray(args); - - const req: ClientRequest = { - jsonrpc: '2.0', - method: method, - params: 0 === params.length ? null : params, - id: id, - }; - return JSON.stringify(req); - } - public response(res: any): RPCClientResponseCodec { - const _res: ClientResponse = { - id: res.id, - jsonrpc: res.jsonrpc, - result: res.result, - error: res.error, - }; - return new JSONRPCClientResponseCodec(_res); - } -} - -function convertParamsToStringArray(args: any[]): string[] | undefined { - const values: string[] = []; - - if (undefined === args || null === args || 0 === args.length) { - return values; - } - - for (let indexI = 0; indexI < args.length; indexI++) { - const arg = args[indexI]; - - switch (typeof arg) { - case 'boolean': - case 'number': // enum - values.push(String(arg)); - break; - case 'string': - values.push(arg); - break; - case 'object': // array, map - values.push(JSON.stringify(arg)); - break; - default: - throw new Error(`Not supported type[${typeof arg}]`); - } - } - - return values; -} - -export class JSONRPCClientResponseCodec implements RPCClientResponseCodec { - public constructor(private _res: ClientResponse) { - } - - public id(): number | undefined { - return this._res.id; - } - public error(): RPCError | undefined { - return this._res.error; - } - public result(): any | undefined { - return this._res.result; - } - - public isNotification(): boolean { - if (undefined !== this.id() || undefined === this.result()) { - return false; - } - return true; - } - - public notification(): RPCClientNotificationCodec | undefined { - if (undefined !== this.id() || undefined === this.result()) { - return undefined; - } - const _noti: ClientNotification = { - method: this._res.result.method, - params: this._res.result.params, - }; - return new JSONRPCClientNotificationCodec(_noti); - } -} - -export class JSONRPCClientNotificationCodec implements RPCClientNotificationCodec { - public constructor(private _noti: ClientNotification) { - } - - public method(): string { - return this._noti.method; - } - public params(): any[] | undefined { - return this._noti.params; - } -} diff --git a/src/@loafer/ng-rpc/protocol/json/index.ts b/src/@loafer/ng-rpc/protocol/json/index.ts deleted file mode 100644 index 2ef50ad..0000000 --- a/src/@loafer/ng-rpc/protocol/json/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './JSONRPCClientCodec'; diff --git a/src/@loafer/ng-rpc/service/index.ts b/src/@loafer/ng-rpc/service/index.ts deleted file mode 100644 index 18154a6..0000000 --- a/src/@loafer/ng-rpc/service/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './rpc.service'; - -import { RPCService } from './rpc.service'; - -export const SERVICES = [ - RPCService, -]; diff --git a/src/@loafer/ng-rpc/service/rpc.service.ts b/src/@loafer/ng-rpc/service/rpc.service.ts deleted file mode 100644 index b263dd6..0000000 --- a/src/@loafer/ng-rpc/service/rpc.service.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Injectable, Inject } from '@angular/core'; -import { Store, select } from '@ngrx/store'; - -import { Observable } from 'rxjs/Observable'; -import { Subject } from 'rxjs/Subject'; - -import { - RPC_CODEC, - RPC_RWC, -} from '../core'; - -import { - RPCClient, - RPCClientRWC, -} from '../client'; - -import { - RPCClientCodec, - RPCClientNotificationCodec, -} from '../protocol'; - -import { - RPCSubscribeService, -} from '../subscribe'; - -@Injectable() -export class RPCService extends RPCClient { - constructor( - @Inject(RPC_CODEC) rpcClientCodec: RPCClientCodec, - @Inject(RPC_RWC) rpcClientRWC: RPCClientRWC, - private rpcSubscribeService: RPCSubscribeService, - ) { - super(rpcClientCodec, rpcClientRWC); - } - - protected onNotification(notiCodec: RPCClientNotificationCodec): void { - this.rpcSubscribeService.notify(notiCodec); - } -} diff --git a/src/@loafer/ng-rpc/subscribe/index.ts b/src/@loafer/ng-rpc/subscribe/index.ts deleted file mode 100644 index d877f8d..0000000 --- a/src/@loafer/ng-rpc/subscribe/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './subscribe.service'; - -import { RPCSubscribeService } from './subscribe.service'; - -export const SUBSCRIBERS = [ - RPCSubscribeService, -]; diff --git a/src/@loafer/ng-rpc/subscribe/subscribe.service.ts b/src/@loafer/ng-rpc/subscribe/subscribe.service.ts deleted file mode 100644 index f1f7426..0000000 --- a/src/@loafer/ng-rpc/subscribe/subscribe.service.ts +++ /dev/null @@ -1,161 +0,0 @@ -import { Injectable, Inject } from '@angular/core'; -import { Store, select } from '@ngrx/store'; - -import { - Type, - PropertyKeyType, -} from '@loafer/core'; - -import { TypeUtil } from '@loafer/core/util/TypeUtil'; - -import { - Class, - Method, - Metadata, -} from '@loafer/core/reflect'; - -import { RPCSubscriberDecorator } from '../decorator'; - -import { - RPCClientNotificationCodec, -} from '../protocol'; -import { SubscriberParameterError } from '../core'; - -export interface SubscriberMethod { - className: PropertyKeyType; - methodName: PropertyKeyType; - parameterTypes: string[]; - - method: Method; - instance: any; -} - -@Injectable() -export class RPCSubscribeService { - private subscriberTypes: Set>; - private subscribers: Set; - private subscriberMethodMap: Map; - - constructor( - ) { - this.subscriberTypes = new Set(); - this.subscribers = new Set(); - this.subscriberMethodMap = new Map(); - } - - public addSubscriber(subscriber: Type): void { - const type = TypeUtil.getType(subscriber); - - if (this.subscriberTypes.has(type)) { - // console.log(`Subscriber[${type.name}] has been added`); - return; - } - - this.subscriberTypes.add(type); - - const clazz = Class.forType(type); - if (undefined === clazz) { - console.log(`Type[${subscriber.name}] is not decorated type`); - return; - } - - const methods = clazz.getMethods(); - methods.forEach((method, propertyKey) => { - const annon = method.getAnnotation(RPCSubscriberDecorator); - if (undefined === annon) { - return; - } - - const subscriberMethodName = annon.attribute.method; - let subscriberMethods: SubscriberMethod[] = this.subscriberMethodMap.get(subscriberMethodName); - if (undefined === subscriberMethods) { - subscriberMethods = []; - this.subscriberMethodMap.set(subscriberMethodName, subscriberMethods); - } - - const paramTypes = this.getParamTypes(method); - - const subscriberMethod: SubscriberMethod = { - className: clazz.getName(), - methodName: method.getName(), - parameterTypes: paramTypes, - method: method, - instance: subscriber, - }; - - subscriberMethods.push(subscriberMethod); - }); - } - - public notify(codec: RPCClientNotificationCodec): void { - const method = codec.method(); - const params = codec.params(); - - const subscriberMethods: SubscriberMethod[] = this.subscriberMethodMap.get(method); - if (undefined === subscriberMethods) { - console.warn(`Subscriber for method[${method}] is not exist`); - return; - } - subscriberMethods.forEach((subscriberMethod) => { - try { - const args = this.converParams(params, subscriberMethod.parameterTypes); - subscriberMethod.method.invoke(subscriberMethod.instance, ...args); - } catch (error) { - console.error(error); - } - }); - } - - private getParamTypes(method: Method): string[] { - if (undefined === method || null === method || 0 === method.getParameterCount()) { - return []; - } - - const parameters = method.getParameters(); - const results: string[] = []; - for (let indexI = 0; indexI < parameters.length; indexI++) { - const paramType = parameters[indexI].getType(); - results.push(paramType.name); - } - return results; - } - - private converParams(params: string[], paramTypes: string[]): any[] { - const results: any[] = []; - - if (undefined === params || null === params || 0 === params.length) { - return results; - } - if (undefined === paramTypes || null === paramTypes || 0 === paramTypes.length) { - return results; - } - if (params.length !== paramTypes.length) { - throw new SubscriberParameterError(`Count is not same from server[${params.length}] and method[${paramTypes.length}]`); - } - for (let indexI = 0; indexI < params.length; indexI++) { - const param = params[indexI]; - const type = paramTypes[indexI]; - switch (type) { - case 'Object': - case 'Array': - case 'Map': - results.push(JSON.parse(param)); - break; - case 'String': - results.push(param); - break; - case 'Number': - results.push(Number(param)); - break; - case 'Boolean': - results.push(Boolean(param)); - break; - case 'Function': - throw new SubscriberParameterError(`Function type [${indexI}] is not allowed`); - default: - throw new SubscriberParameterError(`${type} type parameter[${indexI}] is not allowed`); - } - } - return results; - } -} diff --git a/src/packages/member/component/reset-password/reset-password.component.ts b/src/packages/member/component/reset-password/reset-password.component.ts index 88a75b6..de35f52 100644 --- a/src/packages/member/component/reset-password/reset-password.component.ts +++ b/src/packages/member/component/reset-password/reset-password.component.ts @@ -4,7 +4,7 @@ import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import * as ResetPasswordStore from '../../store/reset-password'; import {select, Store} from '@ngrx/store'; import { ResetPasswordSelector } from '../../store'; -import {RPCClientError} from '../../../../@loafer/ng-rpc/protocol'; +import {RPCClientError} from '@loafer/ng-rpc/protocol'; import {Member} from '@overflow/commons-typescript/model/member/index'; @Component({ diff --git a/src/packages/member/component/settings/totp/config/config-setting.component.ts b/src/packages/member/component/settings/totp/config/config-setting.component.ts index 30db395..003e784 100644 --- a/src/packages/member/component/settings/totp/config/config-setting.component.ts +++ b/src/packages/member/component/settings/totp/config/config-setting.component.ts @@ -14,7 +14,7 @@ import * as TotpStore from 'packages/member/store/totp'; import { AuthSelector } from 'packages/member/store'; import { TotpSelector } from 'packages/member/store'; import { Member } from '@overflow/commons-typescript/model/member'; -import {RPCClientError} from '../../../../../../@loafer/ng-rpc/protocol'; +import {RPCClientError} from '@loafer/ng-rpc/protocol'; @Component({ selector: 'of-config-setting',