Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added missing params to getEventMessages() #373

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions packages/sdk/src/getEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,20 +26,22 @@ import { QueryType, SchemaType, StandardizedQueryResult } from "./types";
* }
* }, 100, 0, { logging: true });
*/
export async function getEntities<T extends SchemaType>(
client: torii.ToriiClient,
query: QueryType<T>,
schema: T,
callback: (response: {
data?: StandardizedQueryResult<T>;
error?: Error;
}) => void,
orderBy: torii.OrderBy[] = [],
entityModels: string[] = [],
limit: number = 100, // Default limit
offset: number = 0, // Default offset
options?: { logging?: boolean } // Logging option
): Promise<StandardizedQueryResult<T>> {
export async function getEntities<T extends SchemaType>({
client,
schema,
query,
callback,
orderBy = [],
entityModels = [],
limit = 100, // Default limit
offset = 0, // Default offset
options = { logging: false }, // Logging option
dontIncludeHashedKeys = false,
entityUpdatedAfter = 0,
}: GetParams<T> & {
client: torii.ToriiClient;
schema: T;
}): Promise<StandardizedQueryResult<T>> {
const clause = convertQueryToClause(query, schema);

let cursor = offset;
Expand All @@ -53,8 +55,8 @@ export async function getEntities<T extends SchemaType>(
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 {
Expand Down
50 changes: 23 additions & 27 deletions packages/sdk/src/getEventMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -32,23 +27,24 @@ import { parseHistoricalEvents } from "./parseHistoricalEvents";
* }
* }, 100, 0, { logging: true });
*/
export async function getEventMessages<T extends SchemaType>(
client: torii.ToriiClient,
query: QueryType<T>,
schema: T,
callback: (response: {
data?: StandardizedQueryResult<T> | StandardizedQueryResult<T>[];
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<T> | StandardizedQueryResult<T>[]> {
export async function getEventMessages<T extends SchemaType>({
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<T> & {
client: torii.ToriiClient;
schema: T;
}): Promise<StandardizedQueryResult<T> | StandardizedQueryResult<T>[]> {
const clause = convertQueryToClause(query, schema);
const isHistorical = !!historical;

let cursor = offset;
let continueFetching = true;
Expand All @@ -61,14 +57,14 @@ export async function getEventMessages<T extends SchemaType>(
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) {
Expand All @@ -77,7 +73,7 @@ export async function getEventMessages<T extends SchemaType>(

Object.assign(allEntities, entities);

const parsedEntities = isHistorical
const parsedEntities = historical
? parseHistoricalEvents<T>(allEntities, options)
: parseEntities<T>(allEntities, options);

Expand All @@ -101,7 +97,7 @@ export async function getEventMessages<T extends SchemaType>(
console.log("All fetched entities:", allEntities);
}

return isHistorical
return historical
? parseHistoricalEvents<T>(allEntities, options)
: parseEntities<T>(allEntities, options);
}
34 changes: 21 additions & 13 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ export async function init<T extends SchemaType>(
* @returns {Promise<void>} - 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.
*
* @param {SubscribeParams<T>} params - Parameters object
* @returns {Promise<void>} - 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.
*
Expand All @@ -76,18 +76,22 @@ export async function init<T extends SchemaType>(
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.
*
Expand All @@ -103,19 +107,23 @@ export async function init<T extends SchemaType>(
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.
Expand Down
26 changes: 11 additions & 15 deletions packages/sdk/src/subscribeEntityQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -28,16 +24,16 @@ import {
* }
* }, { logging: true });
*/
export async function subscribeEntityQuery<T extends SchemaType>(
client: torii.ToriiClient,
query: SubscriptionQueryType<T>,
schema: T,
callback?: (response: {
data?: StandardizedQueryResult<T>;
error?: Error;
}) => void,
options?: { logging?: boolean }
): Promise<torii.Subscription> {
export async function subscribeEntityQuery<T extends SchemaType>({
client,
schema,
query,
callback,
options = { logging: false },
}: SubscribeParams<T> & {
client: torii.ToriiClient;
schema: T;
}): Promise<torii.Subscription> {
if (options?.logging) {
console.log("Query:", query);
console.log(
Expand Down
33 changes: 14 additions & 19 deletions packages/sdk/src/subscribeEventQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -29,26 +25,25 @@ import { parseHistoricalEvents } from "./parseHistoricalEvents";
* }
* }, { logging: true });
*/
export async function subscribeEventQuery<T extends SchemaType>(
client: torii.ToriiClient,
query: SubscriptionQueryType<T>,
schema: T,
callback?: (response: {
data?: StandardizedQueryResult<T> | StandardizedQueryResult<T>[];
error?: Error;
}) => void,
options?: { logging?: boolean },
historical?: boolean
): Promise<torii.Subscription> {
const isHistorical = !!historical;
export async function subscribeEventQuery<T extends SchemaType>({
client,
schema,
query,
callback,
options = { logging: false },
historical = true,
}: SubscribeParams<T> & {
client: torii.ToriiClient;
schema: T;
}): Promise<torii.Subscription> {
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<T>(data, options)
: parseEntities<T>(data, options);
if (options?.logging) {
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,8 @@ export interface GetParams<T extends SchemaType> {
options?: SDKFunctionOptions;
// historical events
historical?: boolean;
// hashed keys on events
dontIncludeHashedKeys?: boolean;
// entity updated after
entityUpdatedAfter?: number;
}
Loading