From 4ec38f8c475af694f4b809fa7601fed748aecb08 Mon Sep 17 00:00:00 2001 From: Spring Chiu Date: Thu, 28 Dec 2023 00:49:03 +0800 Subject: [PATCH] fix: fix reconstructor undefine error --- .../test-status-deserialize-class.ava.js | 2 +- examples/src/status-deserialize-class.js | 2 +- .../lib/collections/lookup-map.d.ts | 4 +- .../near-sdk-js/lib/collections/lookup-map.js | 45 ++-------------- .../near-sdk-js/lib/collections/subtype.d.ts | 5 -- .../near-sdk-js/lib/collections/subtype.js | 15 +++--- packages/near-sdk-js/lib/utils.js | 10 ++-- .../near-sdk-js/src/collections/lookup-map.ts | 51 +++---------------- .../near-sdk-js/src/collections/subtype.ts | 16 +++--- packages/near-sdk-js/src/utils.ts | 6 +-- 10 files changed, 34 insertions(+), 122 deletions(-) diff --git a/examples/__tests__/test-status-deserialize-class.ava.js b/examples/__tests__/test-status-deserialize-class.ava.js index 0d249f998..f52d92d24 100644 --- a/examples/__tests__/test-status-deserialize-class.ava.js +++ b/examples/__tests__/test-status-deserialize-class.ava.js @@ -74,7 +74,7 @@ test("Ali push_message and get_messages", async (t) => { ); }); -test.only("Ali set_nested_efficient_recordes then get_nested_efficient_recordes text", async (t) => { +test("Ali set_nested_efficient_recordes then get_nested_efficient_recordes text", async (t) => { const { ali, bob, statusMessage } = t.context.accounts; await ali.call(statusMessage, "set_nested_efficient_recordes", { id: "1", message: "hello" }, { gas: 35_000_000_000_000n }); await bob.call(statusMessage, "set_nested_efficient_recordes", { id: "1", message: "hello" }, { gas: 35_000_000_000_000n }); diff --git a/examples/src/status-deserialize-class.js b/examples/src/status-deserialize-class.js index 270cd1b8c..389b4c7ad 100644 --- a/examples/src/status-deserialize-class.js +++ b/examples/src/status-deserialize-class.js @@ -193,7 +193,7 @@ export class StatusDeserializeClass { const lookupNestVec = this.lookup_nest_vec.get(account_id, { defaultValue: new Vector("ei_" + account_id + "_"), }); - lookupNestVec.push(message); + lookupNestVec.push(message); // this.lookup_nest_vec.set(account_id, lookupNestVec); this.unordered_set.set(account_id); diff --git a/packages/near-sdk-js/lib/collections/lookup-map.d.ts b/packages/near-sdk-js/lib/collections/lookup-map.d.ts index fff2958ec..36be477af 100644 --- a/packages/near-sdk-js/lib/collections/lookup-map.d.ts +++ b/packages/near-sdk-js/lib/collections/lookup-map.d.ts @@ -1,8 +1,9 @@ import { GetOptions } from "../types/collections"; +import { SubType } from "./subtype"; /** * A lookup map that stores data in NEAR storage. */ -export declare class LookupMap { +export declare class LookupMap extends SubType { readonly keyPrefix: string; /** * @param keyPrefix - The byte prefix to use when storing elements inside this collection. @@ -14,7 +15,6 @@ export declare class LookupMap { * @param key - The value for which to check the presence. */ containsKey(key: string): boolean; - subtype(): any; /** * Get the data stored at the provided key. * diff --git a/packages/near-sdk-js/lib/collections/lookup-map.js b/packages/near-sdk-js/lib/collections/lookup-map.js index 5ffa5f96b..0a5548eb1 100644 --- a/packages/near-sdk-js/lib/collections/lookup-map.js +++ b/packages/near-sdk-js/lib/collections/lookup-map.js @@ -1,18 +1,15 @@ import * as near from "../api"; import { getValueWithOptions, serializeValueWithOptions, encode, } from "../utils"; -import { UnorderedMap } from "./unordered-map"; -import { LookupSet } from "./lookup-set"; -import { UnorderedSet } from "./unordered-set"; -import { Vector } from "./vector"; -import { LOOKUP_MAP_SCHE, LOOKUP_SET_SCHE, UNORDERED_MAP_SCHE, UNORDERED_SET_SCHE, VECTOR_SCHE, } from "./subtype"; +import { SubType } from "./subtype"; /** * A lookup map that stores data in NEAR storage. */ -export class LookupMap { +export class LookupMap extends SubType { /** * @param keyPrefix - The byte prefix to use when storing elements inside this collection. */ constructor(keyPrefix) { + super(); this.keyPrefix = keyPrefix; } /** @@ -24,9 +21,6 @@ export class LookupMap { const storageKey = this.keyPrefix + key; return near.storageHasKey(storageKey); } - /* eslint-disable @typescript-eslint/no-explicit-any */ - /* eslint-disable @typescript-eslint/no-empty-function */ - subtype() { } /** * Get the data stored at the provided key. * @@ -39,38 +33,7 @@ export class LookupMap { if (options == undefined) { options = {}; } - if (options.reconstructor == undefined && this.subtype() != undefined) { - // eslint-disable-next-line no-prototype-builtins - if (this.subtype().hasOwnProperty(UNORDERED_MAP_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = UnorderedMap.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } - else if (this.subtype().hasOwnProperty(LOOKUP_MAP_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = LookupMap.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } - else if (this.subtype().hasOwnProperty(LOOKUP_SET_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = LookupSet.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } - else if (this.subtype().hasOwnProperty(UNORDERED_SET_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = UnorderedSet.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } - else if (this.subtype().hasOwnProperty(VECTOR_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = Vector.reconstruct; - } - } + options = this.set_reconstructor(options); return getValueWithOptions(this.subtype(), value, options); } /** diff --git a/packages/near-sdk-js/lib/collections/subtype.d.ts b/packages/near-sdk-js/lib/collections/subtype.d.ts index 7f279c45f..b43bf1cff 100644 --- a/packages/near-sdk-js/lib/collections/subtype.d.ts +++ b/packages/near-sdk-js/lib/collections/subtype.d.ts @@ -1,9 +1,4 @@ import { GetOptions } from "../types/collections"; -export declare const LOOKUP_MAP_SCHE = "lookup_map"; -export declare const LOOKUP_SET_SCHE = "lookup_set"; -export declare const UNORDERED_MAP_SCHE = "unordered_map"; -export declare const UNORDERED_SET_SCHE = "unordered_set"; -export declare const VECTOR_SCHE = "vector"; export declare abstract class SubType { subtype(): any; set_reconstructor(options?: Omit, "serializer">): Omit, "serializer">; diff --git a/packages/near-sdk-js/lib/collections/subtype.js b/packages/near-sdk-js/lib/collections/subtype.js index 28667e0b5..af87b68c3 100644 --- a/packages/near-sdk-js/lib/collections/subtype.js +++ b/packages/near-sdk-js/lib/collections/subtype.js @@ -1,8 +1,3 @@ -export const LOOKUP_MAP_SCHE = "lookup_map"; -export const LOOKUP_SET_SCHE = "lookup_set"; -export const UNORDERED_MAP_SCHE = "unordered_map"; -export const UNORDERED_SET_SCHE = "unordered_set"; -export const VECTOR_SCHE = "vector"; export class SubType { /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-empty-function */ @@ -11,12 +6,16 @@ export class SubType { if (options == undefined) { options = {}; } - // eslint-disable-next-line no-prototype-builtins - if (options.reconstructor == undefined && this.subtype() != undefined && this.subtype().hasOwnProperty("collection")) { + const subtype = this.subtype(); + if (options.reconstructor == undefined && + subtype != undefined && + // eslint-disable-next-line no-prototype-builtins + subtype.hasOwnProperty("collection") && + typeof this.subtype().collection.reconstructor === "function") { // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - options.reconstructor = this.subtype()["reconstructor"]; + options.reconstructor = this.subtype().collection.reconstructor; } return options; } diff --git a/packages/near-sdk-js/lib/utils.js b/packages/near-sdk-js/lib/utils.js index d734297b5..45e34e83f 100644 --- a/packages/near-sdk-js/lib/utils.js +++ b/packages/near-sdk-js/lib/utils.js @@ -1,5 +1,4 @@ import { cloneDeep } from "lodash-es"; -import * as near from "./api"; // make PromiseIndex a nominal typing var PromiseIndexBrand; (function (PromiseIndexBrand) { @@ -49,11 +48,13 @@ export function getValueWithOptions(subDatatype, value, options = { return options?.defaultValue ?? null; } if (options?.reconstructor) { - near.log(deserialized); // example: // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} const collection = options.reconstructor(deserialized); - // eslint-disable-next-line no-prototype-builtins - if (subDatatype !== undefined && subDatatype.hasOwnProperty("collection") && subDatatype["collection"].hasOwnProperty("value")) { + if (subDatatype !== undefined && + // eslint-disable-next-line no-prototype-builtins + subDatatype.hasOwnProperty("collection") && + // eslint-disable-next-line no-prototype-builtins + subDatatype["collection"].hasOwnProperty("value")) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore collection.subtype = function () { @@ -187,7 +188,6 @@ export function decodeObj2class(class_instance, obj) { // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} return subtype_value; }; - // eslint-disable-next-line no-prototype-builtins } else { // normal case with nested Class, such as field is truck: Truck, diff --git a/packages/near-sdk-js/src/collections/lookup-map.ts b/packages/near-sdk-js/src/collections/lookup-map.ts index 0e9b7c825..4bb653e32 100644 --- a/packages/near-sdk-js/src/collections/lookup-map.ts +++ b/packages/near-sdk-js/src/collections/lookup-map.ts @@ -5,26 +5,18 @@ import { serializeValueWithOptions, encode, } from "../utils"; -import { UnorderedMap } from "./unordered-map"; -import { LookupSet } from "./lookup-set"; -import { UnorderedSet } from "./unordered-set"; -import { Vector } from "./vector"; -import { - LOOKUP_MAP_SCHE, - LOOKUP_SET_SCHE, - UNORDERED_MAP_SCHE, - UNORDERED_SET_SCHE, - VECTOR_SCHE, -} from "./subtype"; +import { SubType } from "./subtype"; /** * A lookup map that stores data in NEAR storage. */ -export class LookupMap { +export class LookupMap extends SubType { /** * @param keyPrefix - The byte prefix to use when storing elements inside this collection. */ - constructor(readonly keyPrefix: string) {} + constructor(readonly keyPrefix: string) { + super(); + } /** * Checks whether the collection contains the value. @@ -36,10 +28,6 @@ export class LookupMap { return near.storageHasKey(storageKey); } - /* eslint-disable @typescript-eslint/no-explicit-any */ - /* eslint-disable @typescript-eslint/no-empty-function */ - subtype(): any {} - /** * Get the data stored at the provided key. * @@ -55,34 +43,7 @@ export class LookupMap { if (options == undefined) { options = {}; } - if (options.reconstructor == undefined && this.subtype() != undefined) { - // eslint-disable-next-line no-prototype-builtins - if (this.subtype().hasOwnProperty(UNORDERED_MAP_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = UnorderedMap.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } else if (this.subtype().hasOwnProperty(LOOKUP_MAP_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = LookupMap.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } else if (this.subtype().hasOwnProperty(LOOKUP_SET_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = LookupSet.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } else if (this.subtype().hasOwnProperty(UNORDERED_SET_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = UnorderedSet.reconstruct; - // eslint-disable-next-line no-prototype-builtins - } else if (this.subtype().hasOwnProperty(VECTOR_SCHE)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - options.reconstructor = Vector.reconstruct; - } - } + options = this.set_reconstructor(options); return getValueWithOptions(this.subtype(), value, options); } diff --git a/packages/near-sdk-js/src/collections/subtype.ts b/packages/near-sdk-js/src/collections/subtype.ts index 9b95228d1..1b1801624 100644 --- a/packages/near-sdk-js/src/collections/subtype.ts +++ b/packages/near-sdk-js/src/collections/subtype.ts @@ -1,11 +1,5 @@ import { GetOptions } from "../types/collections"; -export const LOOKUP_MAP_SCHE = "lookup_map"; -export const LOOKUP_SET_SCHE = "lookup_set"; -export const UNORDERED_MAP_SCHE = "unordered_map"; -export const UNORDERED_SET_SCHE = "unordered_set"; -export const VECTOR_SCHE = "vector"; - export abstract class SubType { /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-empty-function */ @@ -17,16 +11,18 @@ export abstract class SubType { if (options == undefined) { options = {}; } - // eslint-disable-next-line no-prototype-builtins + const subtype = this.subtype(); if ( options.reconstructor == undefined && - this.subtype() != undefined && - this.subtype().hasOwnProperty("collection") + subtype != undefined && + // eslint-disable-next-line no-prototype-builtins + subtype.hasOwnProperty("collection") && + typeof this.subtype().collection.reconstructor === "function" ) { // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - options.reconstructor = this.subtype()["reconstructor"]; + options.reconstructor = this.subtype().collection.reconstructor; } return options; } diff --git a/packages/near-sdk-js/src/utils.ts b/packages/near-sdk-js/src/utils.ts index 07b367e85..f611e0d21 100644 --- a/packages/near-sdk-js/src/utils.ts +++ b/packages/near-sdk-js/src/utils.ts @@ -1,6 +1,5 @@ import { GetOptions } from "./types/collections"; import { cloneDeep } from "lodash-es"; -import * as near from "./api"; export interface Env { uint8array_to_latin1_string(a: Uint8Array): string; @@ -90,13 +89,13 @@ export function getValueWithOptions( } if (options?.reconstructor) { - near.log(deserialized); // example: // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} const collection = options.reconstructor(deserialized); - // eslint-disable-next-line no-prototype-builtins if ( subDatatype !== undefined && + // eslint-disable-next-line no-prototype-builtins subDatatype.hasOwnProperty("collection") && + // eslint-disable-next-line no-prototype-builtins subDatatype["collection"].hasOwnProperty("value") ) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -256,7 +255,6 @@ export function decodeObj2class(class_instance, obj) { // example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}} return subtype_value; }; - // eslint-disable-next-line no-prototype-builtins } else { // normal case with nested Class, such as field is truck: Truck, class_instance[key] = decodeObj2class(class_instance[key], obj[key]);