From 194bee25afea39f5977788da4ae2232195953344 Mon Sep 17 00:00:00 2001 From: RM3 Date: Mon, 13 Jan 2025 17:23:58 -0300 Subject: [PATCH] fix: added missing params to getEventMessages() dont_include_hashed_keys, entity_updated_after --- packages/sdk/src/getEntities.ts | 36 +++++++++-------- packages/sdk/src/getEventMessages.ts | 50 +++++++++++------------- packages/sdk/src/index.ts | 34 ++++++++++------ packages/sdk/src/subscribeEntityQuery.ts | 26 ++++++------ packages/sdk/src/subscribeEventQuery.ts | 33 +++++++--------- packages/sdk/src/types.ts | 4 ++ 6 files changed, 92 insertions(+), 91 deletions(-) diff --git a/packages/sdk/src/getEntities.ts b/packages/sdk/src/getEntities.ts index 2b772231..e1a92a07 100644 --- a/packages/sdk/src/getEntities.ts +++ b/packages/sdk/src/getEntities.ts @@ -2,7 +2,7 @@ import * as torii from "@dojoengine/torii-client"; import { convertQueryToClause } from "./convertQuerytoClause"; import { parseEntities } from "./parseEntities"; -import { QueryType, SchemaType, StandardizedQueryResult } from "./types"; +import { GetParams, SchemaType, StandardizedQueryResult } from "./types"; /** * Fetches entities from the Torii client based on the provided query. @@ -26,20 +26,22 @@ import { QueryType, SchemaType, StandardizedQueryResult } from "./types"; * } * }, 100, 0, { logging: true }); */ -export async function getEntities( - client: torii.ToriiClient, - query: QueryType, - schema: T, - callback: (response: { - data?: StandardizedQueryResult; - error?: Error; - }) => void, - orderBy: torii.OrderBy[] = [], - entityModels: string[] = [], - limit: number = 100, // Default limit - offset: number = 0, // Default offset - options?: { logging?: boolean } // Logging option -): Promise> { +export async function getEntities({ + client, + schema, + query, + callback, + orderBy = [], + entityModels = [], + limit = 100, // Default limit + offset = 0, // Default offset + options = { logging: false }, // Logging option + dontIncludeHashedKeys = false, + entityUpdatedAfter = 0, +}: GetParams & { + client: torii.ToriiClient; + schema: T; +}): Promise> { const clause = convertQueryToClause(query, schema); let cursor = offset; @@ -53,8 +55,8 @@ export async function getEntities( order_by: orderBy, entity_models: entityModels, clause, - dont_include_hashed_keys: false, - entity_updated_after: 0, + dont_include_hashed_keys: dontIncludeHashedKeys, + entity_updated_after: entityUpdatedAfter, }; try { diff --git a/packages/sdk/src/getEventMessages.ts b/packages/sdk/src/getEventMessages.ts index 046b5f67..83f1869f 100644 --- a/packages/sdk/src/getEventMessages.ts +++ b/packages/sdk/src/getEventMessages.ts @@ -2,12 +2,7 @@ import * as torii from "@dojoengine/torii-client"; import { convertQueryToClause } from "./convertQuerytoClause"; import { parseEntities } from "./parseEntities"; -import { - ParsedEntity, - QueryType, - SchemaType, - StandardizedQueryResult, -} from "./types"; +import { GetParams, SchemaType, StandardizedQueryResult } from "./types"; import { parseHistoricalEvents } from "./parseHistoricalEvents"; /** @@ -32,23 +27,24 @@ import { parseHistoricalEvents } from "./parseHistoricalEvents"; * } * }, 100, 0, { logging: true }); */ -export async function getEventMessages( - client: torii.ToriiClient, - query: QueryType, - schema: T, - callback: (response: { - data?: StandardizedQueryResult | StandardizedQueryResult[]; - error?: Error; - }) => void, - orderBy: torii.OrderBy[] = [], - entityModels: string[] = [], - limit: number = 100, // Default limit - offset: number = 0, // Default offset - options?: { logging?: boolean }, // Logging option - historical?: boolean -): Promise | StandardizedQueryResult[]> { +export async function getEventMessages({ + client, + schema, + query, + callback, + orderBy = [], + entityModels = [], + limit = 100, // Default limit + offset = 0, // Default offset + options = { logging: false }, // Logging option + historical = true, + dontIncludeHashedKeys = true, + entityUpdatedAfter = 0, +}: GetParams & { + client: torii.ToriiClient; + schema: T; +}): Promise | StandardizedQueryResult[]> { const clause = convertQueryToClause(query, schema); - const isHistorical = !!historical; let cursor = offset; let continueFetching = true; @@ -61,14 +57,14 @@ export async function getEventMessages( order_by: orderBy, entity_models: entityModels, clause, - dont_include_hashed_keys: true, - entity_updated_after: 0, + dont_include_hashed_keys: dontIncludeHashedKeys, + entity_updated_after: entityUpdatedAfter, }; try { const entities = await client.getEventMessages( toriiQuery, - isHistorical + historical ); if (options?.logging) { @@ -77,7 +73,7 @@ export async function getEventMessages( Object.assign(allEntities, entities); - const parsedEntities = isHistorical + const parsedEntities = historical ? parseHistoricalEvents(allEntities, options) : parseEntities(allEntities, options); @@ -101,7 +97,7 @@ export async function getEventMessages( console.log("All fetched entities:", allEntities); } - return isHistorical + return historical ? parseHistoricalEvents(allEntities, options) : parseEntities(allEntities, options); } diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 1305db15..95e0a0b1 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -46,7 +46,7 @@ export async function init( * @returns {Promise} - A promise that resolves when the subscription is set up. */ subscribeEntityQuery: ({ query, callback, options }) => - subscribeEntityQuery(client, query, schema, callback, options), + subscribeEntityQuery({ client, schema, query, callback, options }), /** * Subscribes to event queries. * @@ -54,14 +54,14 @@ export async function init( * @returns {Promise} - A promise that resolves when the subscription is set up. */ subscribeEventQuery: ({ query, callback, options, historical }) => - subscribeEventQuery( + subscribeEventQuery({ client, - query, schema, + query, callback, options, - historical - ), + historical, + }), /** * Fetches entities based on the provided query. * @@ -76,18 +76,22 @@ export async function init( limit, offset, options, + dontIncludeHashedKeys, + entityUpdatedAfter, }) => - getEntities( + getEntities({ client, - query, schema, + query, callback, orderBy, entityModels, limit, offset, - options - ), + options, + dontIncludeHashedKeys, + entityUpdatedAfter, + }), /** * Fetches event messages based on the provided query. * @@ -103,19 +107,23 @@ export async function init( offset, options, historical, + dontIncludeHashedKeys, + entityUpdatedAfter, }) => - getEventMessages( + getEventMessages({ client, - query, schema, + query, callback, orderBy, entityModels, limit, offset, options, - historical - ), + historical, + dontIncludeHashedKeys, + entityUpdatedAfter, + }), /** * Generates typed data for any user-defined message. diff --git a/packages/sdk/src/subscribeEntityQuery.ts b/packages/sdk/src/subscribeEntityQuery.ts index c09488df..64fa6a0c 100644 --- a/packages/sdk/src/subscribeEntityQuery.ts +++ b/packages/sdk/src/subscribeEntityQuery.ts @@ -2,11 +2,7 @@ import * as torii from "@dojoengine/torii-client"; import { convertQueryToEntityKeyClauses } from "./convertQueryToEntityKeyClauses"; import { parseEntities } from "./parseEntities"; -import { - SchemaType, - StandardizedQueryResult, - SubscriptionQueryType, -} from "./types"; +import { SchemaType, StandardizedQueryResult, SubscribeParams } from "./types"; /** * Subscribes to entity updates based on the provided query and invokes the callback with the updated data. @@ -28,16 +24,16 @@ import { * } * }, { logging: true }); */ -export async function subscribeEntityQuery( - client: torii.ToriiClient, - query: SubscriptionQueryType, - schema: T, - callback?: (response: { - data?: StandardizedQueryResult; - error?: Error; - }) => void, - options?: { logging?: boolean } -): Promise { +export async function subscribeEntityQuery({ + client, + schema, + query, + callback, + options = { logging: false }, +}: SubscribeParams & { + client: torii.ToriiClient; + schema: T; +}): Promise { if (options?.logging) { console.log("Query:", query); console.log( diff --git a/packages/sdk/src/subscribeEventQuery.ts b/packages/sdk/src/subscribeEventQuery.ts index e353baf2..d1baf276 100644 --- a/packages/sdk/src/subscribeEventQuery.ts +++ b/packages/sdk/src/subscribeEventQuery.ts @@ -2,11 +2,7 @@ import * as torii from "@dojoengine/torii-client"; import { convertQueryToEntityKeyClauses } from "./convertQueryToEntityKeyClauses"; import { parseEntities } from "./parseEntities"; -import { - SchemaType, - StandardizedQueryResult, - SubscriptionQueryType, -} from "./types"; +import { SchemaType, StandardizedQueryResult, SubscribeParams } from "./types"; import { parseHistoricalEvents } from "./parseHistoricalEvents"; /** @@ -29,26 +25,25 @@ import { parseHistoricalEvents } from "./parseHistoricalEvents"; * } * }, { logging: true }); */ -export async function subscribeEventQuery( - client: torii.ToriiClient, - query: SubscriptionQueryType, - schema: T, - callback?: (response: { - data?: StandardizedQueryResult | StandardizedQueryResult[]; - error?: Error; - }) => void, - options?: { logging?: boolean }, - historical?: boolean -): Promise { - const isHistorical = !!historical; +export async function subscribeEventQuery({ + client, + schema, + query, + callback, + options = { logging: false }, + historical = true, +}: SubscribeParams & { + client: torii.ToriiClient; + schema: T; +}): Promise { return client.onEventMessageUpdated( convertQueryToEntityKeyClauses(query, schema), - isHistorical, + historical, (entityId: string, entityData: any) => { try { if (callback) { const data = { [entityId]: entityData }; - const parsedData = isHistorical + const parsedData = historical ? parseHistoricalEvents(data, options) : parseEntities(data, options); if (options?.logging) { diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 93531b4e..6d0bc431 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -416,4 +416,8 @@ export interface GetParams { options?: SDKFunctionOptions; // historical events historical?: boolean; + // hashed keys on events + dontIncludeHashedKeys?: boolean; + // entity updated after + entityUpdatedAfter?: number; }