Add api.registerTypes (#670)
This commit is contained in:
@@ -2,24 +2,20 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '../types';
|
||||
import { Constructor, RegistryTypes } from '../types';
|
||||
|
||||
import { isFunction, isString } from '@polkadot/util';
|
||||
|
||||
import { createClass } from './createType';
|
||||
|
||||
type ConstructorObj = {
|
||||
[name: string]: Constructor | string | { [name: string]: string }
|
||||
};
|
||||
|
||||
export class TypeRegistry {
|
||||
static readonly defaultRegistry: TypeRegistry = new TypeRegistry();
|
||||
|
||||
private _registry: Map<string, Constructor> = new Map();
|
||||
|
||||
register (type: Constructor | ConstructorObj): void;
|
||||
register (type: Constructor | RegistryTypes): void;
|
||||
register (name: string, type: Constructor): void;
|
||||
register (arg1: string | Constructor | ConstructorObj, arg2?: Constructor): void {
|
||||
register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void {
|
||||
if (isString(arg1)) {
|
||||
const name = arg1;
|
||||
const type = arg2!;
|
||||
@@ -35,7 +31,7 @@ export class TypeRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
private registerObject (obj: ConstructorObj) {
|
||||
private registerObject (obj: RegistryTypes) {
|
||||
Object.entries(obj).forEach(([name, type]) => {
|
||||
if (isString(type)) {
|
||||
this._registry.set(name, createClass(type));
|
||||
|
||||
@@ -67,3 +67,7 @@ export type Constructor<T = Codec> = { new(value?: any): T };
|
||||
export type ConstructorDef<T = Codec> = { [index: string]: Constructor<T> };
|
||||
|
||||
export type TypeDef = { [index: string]: Codec };
|
||||
|
||||
export type RegistryTypes = {
|
||||
[name: string]: Constructor | string | { [name: string]: string }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user