toJSON returns the correct type (#1038)

* Introduce AnyJson

* Other small fixes
This commit is contained in:
Amaury Martiny
2019-06-19 21:11:32 +02:00
committed by Jaco Greeff
parent fb357dc455
commit 7b461e7f21
24 changed files with 45 additions and 37 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ export class StorageFunctionModifier extends Enum {
return this.toNumber() === 0;
}
toJSON (): any {
toJSON (): string {
// This looks prettier in the generated JSON
return this.toString();
}
+2 -2
View File
@@ -5,7 +5,7 @@
import { u8aConcat, u8aToHex } from '@polkadot/util';
import Compact from './Compact';
import { Codec } from '../types';
import { AnyJsonArray, Codec } from '../types';
import { compareArray } from './utils';
/**
@@ -64,7 +64,7 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> im
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): AnyJsonArray {
return this.map((entry) =>
entry.toJSON()
);
+2
View File
@@ -110,6 +110,8 @@ export default abstract class AbstractInt extends BN implements Codec {
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
// FIXME this return type should by string | number, but BN's return type
// is string.
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
return this._isHexJson || (super.bitLength() > 52)
? this.toHex()
+1 -1
View File
@@ -6,7 +6,7 @@
* @name Base
* @description A type extends the Base class, when it holds a value
*/
export default class Base<T = any> {
export default class Base<T> {
protected raw: T;
constructor (value?: any) {
+1 -1
View File
@@ -115,7 +115,7 @@ export default class Compact extends Base<UInt | Moment> implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string | number {
return this.raw.toJSON();
}
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec, Constructor } from '../types';
import { AnyJson, Codec, Constructor } from '../types';
import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util';
@@ -205,7 +205,7 @@ export default class Enum extends Base<Codec> implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): AnyJson {
return this._isBasic
? this._index
: { [this.type]: this.raw.toJSON() };
+3 -3
View File
@@ -5,7 +5,7 @@
import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import Base from './Base';
import { Codec, Constructor } from '../types';
import { AnyJson, Codec, Constructor } from '../types';
import Null from '../primitive/Null';
/**
@@ -111,7 +111,7 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): AnyJson {
return this.raw.toJSON();
}
@@ -163,7 +163,7 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
* @description Returns the value that the Option represents (if available) or defaultValue if none
* @param defaultValue The value to return if the option isNone
*/
unwrapOr <O> (defaultValue: O): T | O {
unwrapOr<O> (defaultValue: O): T | O {
return this.isSome
? this.unwrap()
: defaultValue;
+1 -1
View File
@@ -134,7 +134,7 @@ export default class CodecSet extends Set<string> implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string[] {
return this.strings;
}
+4 -2
View File
@@ -4,7 +4,7 @@
import { hexToU8a, isHex, isObject, isU8a, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util';
import { Codec, Constructor, ConstructorDef } from '../types';
import { AnyJsonObject, Codec, Constructor, ConstructorDef } from '../types';
import { compareMap, decodeU8a } from './utils';
/**
@@ -218,7 +218,9 @@ export default class Struct<
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): AnyJsonObject | string {
// FIXME the return type string is only used by Extrinsic (extends Struct),
// but its toJSON is the hex value
return [...this.keys()].reduce((json, key) => {
const jsonKey = this._jsonMap.get(key) || key;
const value = this.get(key);
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec } from '../types';
import { AnyJsonObject, Codec } from '../types';
import { isUndefined } from '@polkadot/util';
@@ -71,7 +71,7 @@ export default class StructAny extends Map<string, any> implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): AnyJsonObject {
return [...this.entries()].reduce((json, [key, value]) => {
json[key] = value;
+1 -1
View File
@@ -84,7 +84,7 @@ export default class U8a extends Uint8Array implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string {
return this.toHex();
}
+1 -1
View File
@@ -50,7 +50,7 @@ export default class AccountId extends U8aFixed {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string {
return this.toString();
}
+1 -1
View File
@@ -98,7 +98,7 @@ export default class AccountIndex extends U32 {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string {
return this.toString();
}
+1 -1
View File
@@ -109,7 +109,7 @@ export default class Address extends Base<AccountId | AccountIndex> implements C
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string {
return this.raw.toJSON();
}
+1 -1
View File
@@ -64,7 +64,7 @@ export default class Bool extends Boolean implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): boolean {
return this.valueOf();
}
+2
View File
@@ -95,6 +95,8 @@ export default class Moment extends Date implements Codec {
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
// FIXME Return type should be number, but conflicts with Date.toJSON()
// which returns string
return this.toNumber();
}
+1 -1
View File
@@ -42,7 +42,7 @@ export default class Null implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): null {
return null;
}
@@ -50,7 +50,7 @@ export default class StorageHasher extends Enum {
return this.toNumber() === 4;
}
toJSON (): any {
toJSON (): string {
// This looks prettier in the generated JSON
return this.toString();
}
+1 -4
View File
@@ -17,9 +17,6 @@ import Compact from '../codec/Compact';
*/
// TODO
// - Strings should probably be trimmed (docs do come through with extra padding)
// - Potentially we want a "TypeString" extension to this. Basically something that
// wraps the `Balance`, `T::AccountId`, etc. The reasoning - with a "TypeString"
// we can nicely strip types down like "T::AcountId" -> "AccountId"
export default class Text extends String implements Codec {
constructor (value: Text | string | AnyU8a | { toString: () => string } = '') {
super(
@@ -89,7 +86,7 @@ export default class Text extends String implements Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string {
return this.toString();
}
+3 -3
View File
@@ -95,7 +95,7 @@ export class ConsensusEngineId extends U32 {
* @description Override the default toString to return a 4-byte string
*/
toString (): string {
return ConsensusEngineId.idToString(this);
return ConsensusEngineId.idToString(this as BN);
}
}
@@ -372,7 +372,7 @@ export class DigestItem extends Enum {
/**
* @description Converts the Object to JSON, typically used for RPC transfers. For logs, we overrides to produce the hex version (sligning with substrate gives in actual JSON responses)
*/
toJSON (): any {
toJSON (): string {
return this.toHex();
}
@@ -423,4 +423,4 @@ export default class Digest extends Struct {
* @description
* A [[Header]] Digest
*/
export class DigestOf extends Digest {}
export class DigestOf extends Digest { }
+1 -1
View File
@@ -192,7 +192,7 @@ export default class Extrinsic extends Struct implements IExtrinsic {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
toJSON (): string {
return this.toHex();
}
+6 -3
View File
@@ -15,6 +15,7 @@ export type IKeyringPair = {
sign: (data: Uint8Array) => Uint8Array;
};
interface CodecArgArray extends Array<CodecArg> { }
export type CodecArg = Codec | BN | Boolean | String | Uint8Array | boolean | number | string | undefined | CodecArgArray | CodecArgObject;
export type Callback<T> = (result: T) => void | Promise<void>;
@@ -23,8 +24,6 @@ interface CodecArgObject {
[index: string]: CodecArg;
}
interface CodecArgArray extends Array<CodecArg> { }
export type AnyFunction = (...args: any[]) => any;
export type AnyNumber = BN | Uint8Array | number | string;
@@ -33,6 +32,10 @@ export type AnyString = string | String;
export type AnyU8a = Uint8Array | Array<number> | string;
export type AnyJsonObject = { [key: string]: AnyJson };
export interface AnyJsonArray extends Array<AnyJson> { }
export type AnyJson = string | number | boolean | null | undefined | AnyJsonObject | AnyJsonArray;
/**
* @name Codec
* @description
@@ -64,7 +67,7 @@ export interface Codec {
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any;
toJSON (): AnyJson;
/**
* @description Returns the base runtime type name for this instance