toJSON returns the correct type (#1038)
* Introduce AnyJson * Other small fixes
This commit is contained in:
committed by
Jaco Greeff
parent
fb357dc455
commit
7b461e7f21
@@ -3,6 +3,7 @@
|
|||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { AccountId, Header, u64 } from '@polkadot/types';
|
import { AccountId, Header, u64 } from '@polkadot/types';
|
||||||
|
import { AnyJsonObject } from '@polkadot/types/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name HeaderExtended
|
* @name HeaderExtended
|
||||||
@@ -65,9 +66,9 @@ export default class HeaderExtended extends Header {
|
|||||||
/**
|
/**
|
||||||
* @description Creates the JSON representation
|
* @description Creates the JSON representation
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): AnyJsonObject {
|
||||||
return {
|
return {
|
||||||
...super.toJSON(),
|
...super.toJSON() as AnyJsonObject,
|
||||||
author: this.author
|
author: this.author
|
||||||
? this.author.toJSON()
|
? this.author.toJSON()
|
||||||
: undefined
|
: undefined
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import { ReferendumInfo, ReferendumIndex } from '@polkadot/types';
|
import { ReferendumInfo, ReferendumIndex } from '@polkadot/types';
|
||||||
|
import { AnyJsonObject } from '@polkadot/types/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name ReferendumInfoExtended
|
* @name ReferendumInfoExtended
|
||||||
@@ -31,9 +32,9 @@ export default class ReferendumInfoExtended extends ReferendumInfo {
|
|||||||
/**
|
/**
|
||||||
* @description Creates the JSON representation
|
* @description Creates the JSON representation
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): AnyJsonObject {
|
||||||
return {
|
return {
|
||||||
...super.toJSON(),
|
...super.toJSON() as AnyJsonObject,
|
||||||
index: this.index.toJSON()
|
index: this.index.toJSON()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class StorageFunctionModifier extends Enum {
|
|||||||
return this.toNumber() === 0;
|
return this.toNumber() === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
// This looks prettier in the generated JSON
|
// This looks prettier in the generated JSON
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import { u8aConcat, u8aToHex } from '@polkadot/util';
|
import { u8aConcat, u8aToHex } from '@polkadot/util';
|
||||||
|
|
||||||
import Compact from './Compact';
|
import Compact from './Compact';
|
||||||
import { Codec } from '../types';
|
import { AnyJsonArray, Codec } from '../types';
|
||||||
import { compareArray } from './utils';
|
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
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): AnyJsonArray {
|
||||||
return this.map((entry) =>
|
return this.map((entry) =>
|
||||||
entry.toJSON()
|
entry.toJSON()
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -110,6 +110,8 @@ export default abstract class AbstractInt extends BN implements Codec {
|
|||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
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
|
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
|
||||||
return this._isHexJson || (super.bitLength() > 52)
|
return this._isHexJson || (super.bitLength() > 52)
|
||||||
? this.toHex()
|
? this.toHex()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
* @name Base
|
* @name Base
|
||||||
* @description A type extends the Base class, when it holds a value
|
* @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;
|
protected raw: T;
|
||||||
|
|
||||||
constructor (value?: any) {
|
constructor (value?: any) {
|
||||||
|
|||||||
@@ -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
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string | number {
|
||||||
return this.raw.toJSON();
|
return this.raw.toJSON();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// 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';
|
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
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): AnyJson {
|
||||||
return this._isBasic
|
return this._isBasic
|
||||||
? this._index
|
? this._index
|
||||||
: { [this.type]: this.raw.toJSON() };
|
: { [this.type]: this.raw.toJSON() };
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
||||||
|
|
||||||
import Base from './Base';
|
import Base from './Base';
|
||||||
import { Codec, Constructor } from '../types';
|
import { AnyJson, Codec, Constructor } from '../types';
|
||||||
import Null from '../primitive/Null';
|
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
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): AnyJson {
|
||||||
return this.raw.toJSON();
|
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
|
* @description Returns the value that the Option represents (if available) or defaultValue if none
|
||||||
* @param defaultValue The value to return if the option isNone
|
* @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
|
return this.isSome
|
||||||
? this.unwrap()
|
? this.unwrap()
|
||||||
: defaultValue;
|
: defaultValue;
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ export default class CodecSet extends Set<string> implements Codec {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string[] {
|
||||||
return this.strings;
|
return this.strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import { hexToU8a, isHex, isObject, isU8a, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util';
|
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';
|
import { compareMap, decodeU8a } from './utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -218,7 +218,9 @@ export default class Struct<
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @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) => {
|
return [...this.keys()].reduce((json, key) => {
|
||||||
const jsonKey = this._jsonMap.get(key) || key;
|
const jsonKey = this._jsonMap.get(key) || key;
|
||||||
const value = this.get(key);
|
const value = this.get(key);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// 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';
|
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
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): AnyJsonObject {
|
||||||
return [...this.entries()].reduce((json, [key, value]) => {
|
return [...this.entries()].reduce((json, [key, value]) => {
|
||||||
json[key] = value;
|
json[key] = value;
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ export default class U8a extends Uint8Array implements Codec {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
return this.toHex();
|
return this.toHex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export default class AccountId extends U8aFixed {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ export default class AccountIndex extends U32 {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
return this.raw.toJSON();
|
return this.raw.toJSON();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ export default class Bool extends Boolean implements Codec {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): boolean {
|
||||||
return this.valueOf();
|
return this.valueOf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,8 @@ export default class Moment extends Date implements Codec {
|
|||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): any {
|
||||||
|
// FIXME Return type should be number, but conflicts with Date.toJSON()
|
||||||
|
// which returns string
|
||||||
return this.toNumber();
|
return this.toNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export default class Null implements Codec {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): null {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export default class StorageHasher extends Enum {
|
|||||||
return this.toNumber() === 4;
|
return this.toNumber() === 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
// This looks prettier in the generated JSON
|
// This looks prettier in the generated JSON
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ import Compact from '../codec/Compact';
|
|||||||
*/
|
*/
|
||||||
// TODO
|
// TODO
|
||||||
// - Strings should probably be trimmed (docs do come through with extra padding)
|
// - 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 {
|
export default class Text extends String implements Codec {
|
||||||
constructor (value: Text | string | AnyU8a | { toString: () => string } = '') {
|
constructor (value: Text | string | AnyU8a | { toString: () => string } = '') {
|
||||||
super(
|
super(
|
||||||
@@ -89,7 +86,7 @@ export default class Text extends String implements Codec {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ export class ConsensusEngineId extends U32 {
|
|||||||
* @description Override the default toString to return a 4-byte string
|
* @description Override the default toString to return a 4-byte string
|
||||||
*/
|
*/
|
||||||
toString (): 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)
|
* @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();
|
return this.toHex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,4 +423,4 @@ export default class Digest extends Struct {
|
|||||||
* @description
|
* @description
|
||||||
* A [[Header]] Digest
|
* A [[Header]] Digest
|
||||||
*/
|
*/
|
||||||
export class DigestOf extends Digest {}
|
export class DigestOf extends Digest { }
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ export default class Extrinsic extends Struct implements IExtrinsic {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||||
*/
|
*/
|
||||||
toJSON (): any {
|
toJSON (): string {
|
||||||
return this.toHex();
|
return this.toHex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export type IKeyringPair = {
|
|||||||
sign: (data: Uint8Array) => Uint8Array;
|
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 CodecArg = Codec | BN | Boolean | String | Uint8Array | boolean | number | string | undefined | CodecArgArray | CodecArgObject;
|
||||||
|
|
||||||
export type Callback<T> = (result: T) => void | Promise<void>;
|
export type Callback<T> = (result: T) => void | Promise<void>;
|
||||||
@@ -23,8 +24,6 @@ interface CodecArgObject {
|
|||||||
[index: string]: CodecArg;
|
[index: string]: CodecArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CodecArgArray extends Array<CodecArg> { }
|
|
||||||
|
|
||||||
export type AnyFunction = (...args: any[]) => any;
|
export type AnyFunction = (...args: any[]) => any;
|
||||||
|
|
||||||
export type AnyNumber = BN | Uint8Array | number | string;
|
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 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
|
* @name Codec
|
||||||
* @description
|
* @description
|
||||||
@@ -64,7 +67,7 @@ export interface Codec {
|
|||||||
/**
|
/**
|
||||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
* @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
|
* @description Returns the base runtime type name for this instance
|
||||||
|
|||||||
Reference in New Issue
Block a user