@pooflabs/core 0.0.41 → 0.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,6 +39,15 @@ export interface User {
39
39
  address: string;
40
40
  provider: AuthProvider;
41
41
  }
42
+
43
+ export interface GetManyResult {
44
+ path: string;
45
+ data: any | null;
46
+ error?: {
47
+ code: 'NOT_FOUND' | 'UNAUTHORIZED' | 'INVALID_PATH';
48
+ message: string;
49
+ };
50
+ }
42
51
  ```
43
52
 
44
53
  ### Core Operations
@@ -52,6 +61,7 @@ function getConfig(): Promise<ClientConfig>;
52
61
 
53
62
  // Data operations
54
63
  function get(path: string): Promise<any>;
64
+ function getMany(paths: string[], options?: { bypassCache?: boolean }): Promise<GetManyResult[]>;
55
65
  function set(path: string, data: any, options?: SetOptions): Promise<any>;
56
66
  function setMany(paths: { [key: string]: any }, options?: SetOptions): Promise<any>;
57
67
  function setFile(path: string, file: File, metadata?: any): Promise<any>;
@@ -113,6 +113,20 @@ export declare function count(path: string, opts?: CountOptions): Promise<Aggreg
113
113
  */
114
114
  export declare function aggregate(path: string, operation: AggregateOperation, opts?: AggregateOptions): Promise<AggregateResult>;
115
115
  export declare function get(path: string, opts?: GetOptions): Promise<any>;
116
+ export type GetManyResult = {
117
+ path: string;
118
+ data: any | null;
119
+ error?: {
120
+ code: 'NOT_FOUND' | 'UNAUTHORIZED' | 'INVALID_PATH';
121
+ message: string;
122
+ };
123
+ };
124
+ export declare function getMany(paths: string[], opts?: {
125
+ bypassCache?: boolean;
126
+ _overrides?: {
127
+ headers?: Record<string, string>;
128
+ };
129
+ }): Promise<GetManyResult[]>;
116
130
  export type RunExpressionOptions = {
117
131
  returnType?: 'Bool' | 'String' | 'Int' | 'UInt';
118
132
  _overrides?: RequestOverrides;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { init } from './client/config';
2
2
  export { getConfig, ClientConfig } from './client/config';
3
- export { get, set, setMany, setFile, getFiles, runQuery, runQueryMany, runExpression, runExpressionMany, signMessage, signTransaction, signAndSubmitTransaction, count, aggregate, RequestOverrides, SetOptions, GetOptions, RunQueryOptions, CountOptions, AggregateOptions, AggregateOperation, AggregateResult, RunExpressionOptions, RunExpressionResult, InsufficientBalanceError } from './client/operations';
3
+ export { get, getMany, set, setMany, setFile, getFiles, runQuery, runQueryMany, runExpression, runExpressionMany, signMessage, signTransaction, signAndSubmitTransaction, count, aggregate, RequestOverrides, SetOptions, GetOptions, RunQueryOptions, CountOptions, AggregateOptions, AggregateOperation, AggregateResult, RunExpressionOptions, RunExpressionResult, InsufficientBalanceError, GetManyResult } from './client/operations';
4
4
  export { subscribe, closeAllSubscriptions, clearCache, getCachedData, reconnectWithNewAuth } from './client/subscription';
5
5
  export * from './types';
6
6
  export { getIdToken } from './utils/utils';
package/dist/index.js CHANGED
@@ -3748,6 +3748,90 @@ function cleanupExpiredCache() {
3748
3748
  });
3749
3749
  lastCacheCleanup = now;
3750
3750
  }
3751
+ async function getMany(paths, opts = {}) {
3752
+ if (paths.length === 0) {
3753
+ return [];
3754
+ }
3755
+ if (paths.length > 30) {
3756
+ throw new Error('Cannot fetch more than 30 documents at once');
3757
+ }
3758
+ const normalizedPaths = [];
3759
+ for (const path of paths) {
3760
+ let normalizedPath = path.startsWith("/") ? path.slice(1) : path;
3761
+ if (normalizedPath.endsWith("*") && normalizedPath.length > 1) {
3762
+ normalizedPath = normalizedPath.slice(0, -1);
3763
+ }
3764
+ if (!normalizedPath || normalizedPath.length === 0) {
3765
+ throw new Error(`Invalid path provided: ${path}`);
3766
+ }
3767
+ const pathIsDocument = normalizedPath.split("/").length % 2 === 0;
3768
+ if (!pathIsDocument) {
3769
+ throw new Error(`Path must point to a document (even number of segments): ${path}`);
3770
+ }
3771
+ normalizedPaths.push(normalizedPath);
3772
+ }
3773
+ const now = Date.now();
3774
+ const results = new Array(paths.length);
3775
+ const uncachedIndices = [];
3776
+ const uncachedPaths = [];
3777
+ for (let i = 0; i < normalizedPaths.length; i++) {
3778
+ const normalizedPath = normalizedPaths[i];
3779
+ const cacheKey = `${normalizedPath}:`;
3780
+ if (!opts.bypassCache && getCache[cacheKey] && now < getCache[cacheKey].expiresAt) {
3781
+ results[i] = { path: normalizedPath, data: getCache[cacheKey].data };
3782
+ }
3783
+ else {
3784
+ uncachedIndices.push(i);
3785
+ uncachedPaths.push(normalizedPath);
3786
+ }
3787
+ }
3788
+ if (uncachedPaths.length > 0) {
3789
+ try {
3790
+ const response = await makeApiRequest('POST', 'items/batch', { paths: uncachedPaths }, opts._overrides);
3791
+ const serverResults = response.data.results;
3792
+ const serverResultsMap = new Map();
3793
+ for (const result of serverResults) {
3794
+ serverResultsMap.set(result.path, result);
3795
+ }
3796
+ for (let i = 0; i < uncachedIndices.length; i++) {
3797
+ const originalIndex = uncachedIndices[i];
3798
+ const normalizedPath = uncachedPaths[i];
3799
+ const serverResult = serverResultsMap.get(normalizedPath);
3800
+ if (serverResult) {
3801
+ results[originalIndex] = serverResult;
3802
+ if (!serverResult.error && !opts.bypassCache) {
3803
+ const cacheKey = `${normalizedPath}:`;
3804
+ getCache[cacheKey] = {
3805
+ data: serverResult.data,
3806
+ expiresAt: now + GET_CACHE_TTL
3807
+ };
3808
+ }
3809
+ }
3810
+ else {
3811
+ results[originalIndex] = {
3812
+ path: normalizedPath,
3813
+ data: null,
3814
+ error: { code: 'NOT_FOUND', message: `No result returned for path ${normalizedPath}` }
3815
+ };
3816
+ }
3817
+ }
3818
+ if (now - lastCacheCleanup > 5000) {
3819
+ cleanupExpiredCache();
3820
+ lastCacheCleanup = now;
3821
+ }
3822
+ }
3823
+ catch (error) {
3824
+ for (const originalIndex of uncachedIndices) {
3825
+ results[originalIndex] = {
3826
+ path: normalizedPaths[originalIndex],
3827
+ data: null,
3828
+ error: { code: 'NOT_FOUND', message: error instanceof Error ? error.message : 'Unknown error' }
3829
+ };
3830
+ }
3831
+ }
3832
+ }
3833
+ return results;
3834
+ }
3751
3835
  async function runQuery(absolutePath, queryName, queryArgs, opts) {
3752
3836
  const result = await runQueryMany([{ absolutePath, queryName, queryArgs }], opts);
3753
3837
  return result[0];
@@ -5007,6 +5091,7 @@ exports.getCachedData = getCachedData;
5007
5091
  exports.getConfig = getConfig;
5008
5092
  exports.getFiles = getFiles;
5009
5093
  exports.getIdToken = getIdToken;
5094
+ exports.getMany = getMany;
5010
5095
  exports.init = init;
5011
5096
  exports.reconnectWithNewAuth = reconnectWithNewAuth;
5012
5097
  exports.refreshSession = refreshSession;