diff --git a/__tests__/ain.test.ts b/__tests__/ain.test.ts index b2d337a..5de20f8 100644 --- a/__tests__/ain.test.ts +++ b/__tests__/ain.test.ts @@ -42,7 +42,7 @@ describe('ain-js', function() { it('should set provider', async function() { ain.setProvider(test_node_2); - expect(await ain.net.getNetworkId()).toBe('Testnet'); + expect(await ain.net.getNetworkId()).toBe(0); expect(await ain.net.isListening()).toMatchSnapshot(); expect(await ain.net.getPeerCount()).toBeGreaterThan(0); expect(await ain.net.isSyncing()).toBe(false); @@ -612,8 +612,7 @@ describe('ain-js', function() { value: { ".function": { '0xFUNCTION_HASH': { - service_name: "functions.ainetwork.ai", - event_listener: "https://events.ainetwork.ai/trigger", + function_url: "https://events.ainetwork.ai/trigger", function_id: '0xFUNCTION_HASH', function_type: "REST" } @@ -666,18 +665,6 @@ describe('ain-js', function() { }); }); - it('deleteValue', function(done) { - ain.db.ref(allowed_path).deleteValue() - .then(res => { - expect(res.result.code).toBe(0); - done(); - }) - .catch((error) => { - console.log("deleteValue error:",error); - done(); - }); - }); - it('getValue', async function() { expect(await ain.db.ref(allowed_path).getValue()).toMatchSnapshot(); }); @@ -712,6 +699,28 @@ describe('ain-js', function() { )).toMatchSnapshot(); }); + it('get with options', async function() { + expect(await ain.db.ref().getValue(allowed_path, { is_final: true })).toMatchSnapshot(); + expect(await ain.db.ref().getValue(allowed_path, { is_global: true })).toMatchSnapshot(); + expect(await ain.db.ref().getValue(allowed_path, { is_shallow: true })).toMatchSnapshot(); + expect(await ain.db.ref().getValue(allowed_path, { include_proof: true })).toMatchSnapshot(); + expect(await ain.db.ref().getValue(allowed_path, { include_tree_info: true })).toMatchSnapshot(); + const getWithVersion = await ain.db.ref().getValue(allowed_path, { include_version: true }); + expect(getWithVersion['#version']).not.toBeNull(); + }); + + it('deleteValue', function(done) { + ain.db.ref(allowed_path).deleteValue() + .then(res => { + expect(res.result.code).toBe(0); + done(); + }) + .catch((error) => { + console.log("deleteValue error:",error); + done(); + }); + }); + it('evalRule: true', function(done) { ain.db.ref(allowed_path).evalRule({ ref: '/can/write', value: 'hi' }) .then(res => { diff --git a/package.json b/package.json index 669309e..94170d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ainblockchain/ain-js", - "version": "1.1.8", + "version": "1.1.9", "description": "", "main": "lib/ain.js", "scripts": { diff --git a/src/ain-db/ref.ts b/src/ain-db/ref.ts index c7729f5..8bddff4 100755 --- a/src/ain-db/ref.ts +++ b/src/ain-db/ref.ts @@ -11,7 +11,8 @@ import { SetMultiTransactionInput, EvalRuleInput, EvalOwnerInput, - MatchInput + MatchInput, + GetOptions, } from '../types'; import Ain from '../ain'; import { PushId } from './push-id'; @@ -76,8 +77,8 @@ export default class Reference { * Returns the value at the path. * @param path */ - getValue(path?: string): Promise { - const req = Reference.buildGetRequest('GET_VALUE', Reference.extendPath(this.path, path)); + getValue(path?: string, options?: GetOptions): Promise { + const req = Reference.buildGetRequest('GET_VALUE', Reference.extendPath(this.path, path), options); return this._ain.provider.send('ain_get', req); } @@ -85,8 +86,8 @@ export default class Reference { * Returns the rule at the path. * @param path */ - getRule(path?: string): Promise { - const req = Reference.buildGetRequest('GET_RULE', Reference.extendPath(this.path, path)); + getRule(path?: string, options?: GetOptions): Promise { + const req = Reference.buildGetRequest('GET_RULE', Reference.extendPath(this.path, path), options); return this._ain.provider.send('ain_get', req); } @@ -94,8 +95,8 @@ export default class Reference { * Returns the owner config at the path. * @param path */ - getOwner(path?: string): Promise { - const req = Reference.buildGetRequest('GET_OWNER', Reference.extendPath(this.path, path)); + getOwner(path?: string, options?: GetOptions): Promise { + const req = Reference.buildGetRequest('GET_OWNER', Reference.extendPath(this.path, path), options); return this._ain.provider.send('ain_get', req); } @@ -103,8 +104,8 @@ export default class Reference { * Returns the function config at the path. * @param path */ - getFunction(path?: string): Promise { - const req = Reference.buildGetRequest('GET_FUNCTION', Reference.extendPath(this.path, path)); + getFunction(path?: string, options?: GetOptions): Promise { + const req = Reference.buildGetRequest('GET_FUNCTION', Reference.extendPath(this.path, path), options); return this._ain.provider.send('ain_get', req); } @@ -363,8 +364,12 @@ export default class Reference { * @param type * @param ref */ - static buildGetRequest(type: GetOperationType, ref: string) { - return { type, ref: Reference.sanitizeRef(ref) }; + static buildGetRequest(type: GetOperationType, ref: string, options?: GetOptions) { + const request = { type, ref: Reference.sanitizeRef(ref) }; + if (options) { + Object.assign(request, options); + } + return request; } /** diff --git a/src/types.ts b/src/types.ts index 95ad3b6..afa56b9 100755 --- a/src/types.ts +++ b/src/types.ts @@ -58,6 +58,15 @@ export type GetOperationType = "GET_VALUE" | "GET_RULE" | "GET_OWNER" | "GET_FUN export type OwnerPermission = "branch_owner" | "write_function" | "write_owner" | "write_rule"; +export type GetOptions = { + is_global?: boolean, + is_final?: boolean, + is_shallow?: boolean, + include_version?: boolean, + include_tree_info?: boolean, + include_proof?: boolean, +} + export interface SetOperation { type: SetOperationType; ref: string; @@ -70,10 +79,9 @@ export interface SetMultiOperation { op_list: SetOperation[]; } -export interface GetOperation { +export interface GetOperation extends GetOptions { type: GetOperationType; ref?: string; - is_global?: boolean; } export interface GetMultiOperation {