Skip to content

Commit bb9a338

Browse files
committed
fix tiny bug for queries with empty data
1 parent bccb836 commit bb9a338

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

Diff for: src/modules/query/query.ts

+63-61
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
import type AlgoStack from "../../index";
2-
import type Cache from "../cache/index";
1+
import type AlgoStack from '../../index';
2+
import type { PromiseResolver } from '../../types';
3+
import type { AddonsKeyMap, AddonsList } from '../addons/types';
4+
import type Cache from '../cache/index';
35
import type {
4-
QueryParams,
5-
Payload,
6-
QueryOptions,
76
FilterFn,
7+
Payload,
88
QueryConfigs,
9+
QueryOptions,
10+
QueryParams,
911
QueryQueue,
1012
RateLimiter,
11-
} from "./types";
12-
import type { AddonsList, AddonsKeyMap } from "../addons/types";
13-
import type { PromiseResolver } from "../../types";
14-
import type { AxiosHeaders, AxiosInstance } from "axios";
15-
import { Buffer } from "buffer";
16-
import { pRateLimit } from "p-ratelimit";
17-
import { utf8ToB64 } from "../../helpers/encoding.js";
18-
import { ApiUrl } from "./enums.js";
19-
import { BaseModule } from "../_baseModule.js";
20-
import camelcaseKeys from "camelcase-keys";
21-
import kebabcaseKeys from "kebabcase-keys";
22-
import axios from "axios";
23-
import merge from "lodash-es/merge.js";
24-
import cloneDeep from "lodash-es/cloneDeep.js";
25-
import Addons from "../addons/addons.js";
26-
import objHash from "object-hash";
27-
import { CacheTable } from "../cache/index.js";
13+
} from './types';
14+
import type { AxiosHeaders, AxiosInstance } from 'axios';
15+
16+
import { Buffer } from 'buffer';
17+
import axios from 'axios';
18+
import camelcaseKeys from 'camelcase-keys';
19+
import kebabcaseKeys from 'kebabcase-keys';
20+
import cloneDeep from 'lodash-es/cloneDeep.js';
21+
import merge from 'lodash-es/merge.js';
22+
import objHash from 'object-hash';
23+
import { pRateLimit } from 'p-ratelimit';
24+
25+
import { BaseModule } from '../_baseModule.js';
26+
import { utf8ToB64 } from '../../helpers/encoding.js';
27+
import Addons from '../addons/addons.js';
28+
import { CacheTable } from '../cache/index.js';
29+
import { ApiUrl } from './enums.js';
2830

2931
/**
3032
* Query class
@@ -52,7 +54,7 @@ export default class Query extends BaseModule {
5254
},
5355
},
5456
this.configs,
55-
configs
57+
configs,
5658
);
5759
this.initRateLimiters();
5860
}
@@ -76,15 +78,15 @@ export default class Query extends BaseModule {
7678
...(this.configs.rateLimiter || {}),
7779
};
7880

79-
this.rateLimiters.set("default", pRateLimit(defaultConfigs));
81+
this.rateLimiters.set('default', pRateLimit(defaultConfigs));
8082
if (!this.configs.rateLimiters) return;
8183
Object.entries(this.configs.rateLimiters).forEach(([key, configs]) => {
8284
this.rateLimiters.set(
8385
key,
8486
pRateLimit({
8587
...defaultConfigs,
8688
...configs,
87-
})
89+
}),
8890
);
8991
});
9092
}
@@ -97,8 +99,8 @@ export default class Query extends BaseModule {
9799
const token = this.stack.configs.apiToken;
98100
if (!token) return params?.headers;
99101
let tokenHeader: string | undefined = undefined;
100-
if (api === ApiUrl.INDEXER) tokenHeader = "X-Indexer-API-Token";
101-
else if (api === ApiUrl.NODE) tokenHeader = "X-Algo-API-Token";
102+
if (api === ApiUrl.INDEXER) tokenHeader = 'X-Indexer-API-Token';
103+
else if (api === ApiUrl.NODE) tokenHeader = 'X-Algo-API-Token';
102104
if (!tokenHeader) return params;
103105
return {
104106
...(params.headers || {}),
@@ -167,7 +169,7 @@ export default class Query extends BaseModule {
167169

168170
data = await this.fetchData(
169171
`${this.stack.configs[base]}${url}`,
170-
reqParams
172+
reqParams,
171173
);
172174
data = camelcaseKeys(data, { deep: true });
173175

@@ -179,7 +181,7 @@ export default class Query extends BaseModule {
179181
i++;
180182
let nextData: Payload = await this.fetchData(
181183
`${this.stack.configs[base]}${url}`,
182-
{ ...reqParams, next: data.nextToken }
184+
{ ...reqParams, next: data.nextToken },
183185
);
184186
delete data.nextToken;
185187
nextData = camelcaseKeys(nextData, { deep: true });
@@ -193,7 +195,7 @@ export default class Query extends BaseModule {
193195
}
194196

195197
// cache result
196-
if (this.cache && cacheTable && !noCache && !data.error) {
198+
if (this.cache && cacheTable && !noCache && !data?.error) {
197199
await this.cache.save(cacheTable, data, { params: reqParams });
198200
}
199201

@@ -236,12 +238,12 @@ export default class Query extends BaseModule {
236238
* ==================================================
237239
*/
238240
private mergeUrlAndParams(url: string, params: Record<string, any>) {
239-
if (!url) return { url: "/", params };
241+
if (!url) return { url: '/', params };
240242
const unusedParams: Record<string, any> = {};
241243
if (params) {
242244
Object.entries(params).forEach(([key, value]) => {
243-
if (url && url.indexOf(":" + key) > -1) {
244-
url = url.replace(":" + key, String(value));
245+
if (url && url.indexOf(':' + key) > -1) {
246+
url = url.replace(':' + key, String(value));
245247
} else {
246248
unusedParams[key] = value;
247249
}
@@ -258,7 +260,7 @@ export default class Query extends BaseModule {
258260
}
259261

260262
private encodeParams(params: QueryParams) {
261-
if (typeof params.notePrefix === "string") {
263+
if (typeof params.notePrefix === 'string') {
262264
params.notePrefix = utf8ToB64(params.notePrefix);
263265
}
264266
return params;
@@ -287,24 +289,24 @@ export default class Query extends BaseModule {
287289
private async fetchData(
288290
url: string,
289291
params: QueryParams = {},
290-
client: AxiosInstance = this.configs.client || axios
292+
client: AxiosInstance = this.configs.client || axios,
291293
) {
292294
try {
293-
if (url.indexOf(":id") > -1) {
295+
if (url.indexOf(':id') > -1) {
294296
return {
295297
error: {
296298
url,
297-
message: "Url is invalid",
299+
message: 'Url is invalid',
298300
},
299301
};
300302
}
301303
params = cloneDeep(params);
302304
const method: string = params.method
303305
? String(params.method).toUpperCase()
304-
: "GET";
306+
: 'GET';
305307
const headers = params.headers as AxiosHeaders;
306308
const data = params?.data || params;
307-
const rateLimiterKey = params.rateLimiter || "default";
309+
const rateLimiterKey = params.rateLimiter || 'default';
308310
if (params.method) delete params.method;
309311
if (params.headers) delete params.headers;
310312
if (params.url) delete params.url;
@@ -318,9 +320,9 @@ export default class Query extends BaseModule {
318320
url,
319321
method,
320322
headers,
321-
params: method === "GET" ? data : undefined,
322-
data: method !== "GET" ? data : undefined,
323-
})
323+
params: method === 'GET' ? data : undefined,
324+
data: method !== 'GET' ? data : undefined,
325+
}),
324326
);
325327
return response.data;
326328
} catch (e: any) {
@@ -337,7 +339,7 @@ export default class Query extends BaseModule {
337339
private async indexerHealth() {
338340
if (!this.initiated) await this.waitForInit();
339341
const response = await this.fetchData(
340-
`${this.stack.configs.indexerUrl}/health`
342+
`${this.stack.configs.indexerUrl}/health`,
341343
);
342344
return camelcaseKeys(response, { deep: true });
343345
}
@@ -355,7 +357,7 @@ export default class Query extends BaseModule {
355357
}
356358
private async indexerAccountTransactions(
357359
accountId: string,
358-
params: QueryParams = {}
360+
params: QueryParams = {},
359361
) {
360362
return await this.query({
361363
endpoint: `/v2/accounts/:id/transactions`,
@@ -368,7 +370,7 @@ export default class Query extends BaseModule {
368370
}
369371
private async indexerAccountAssets(
370372
accountId: string,
371-
params: QueryParams = {}
373+
params: QueryParams = {},
372374
) {
373375
return await this.query({
374376
endpoint: `/v2/accounts/:id/assets`,
@@ -381,7 +383,7 @@ export default class Query extends BaseModule {
381383
}
382384
private async indexerAccountApplications(
383385
accountId: string,
384-
params: QueryParams = {}
386+
params: QueryParams = {},
385387
) {
386388
return await this.query({
387389
endpoint: `/v2/accounts/:id/apps-local-state`,
@@ -406,7 +408,7 @@ export default class Query extends BaseModule {
406408
private async indexerApplicationBox(
407409
appId: number,
408410
boxName: string,
409-
params: QueryParams = {}
411+
params: QueryParams = {},
410412
) {
411413
return await this.query({
412414
endpoint: `/v2/applications/:id/boxes`,
@@ -420,7 +422,7 @@ export default class Query extends BaseModule {
420422
}
421423
private async indexerApplicationBoxes(
422424
appId: number,
423-
params: QueryParams = {}
425+
params: QueryParams = {},
424426
) {
425427
return await this.query({
426428
endpoint: `/v2/applications/:id/boxes`,
@@ -444,7 +446,7 @@ export default class Query extends BaseModule {
444446
}
445447
private async indexerAssetBalances(
446448
assetId: number,
447-
params: QueryParams = {}
449+
params: QueryParams = {},
448450
) {
449451
return await this.query({
450452
endpoint: `/v2/assets/:id/balances`,
@@ -457,7 +459,7 @@ export default class Query extends BaseModule {
457459
}
458460
private async indexerAssetTransactions(
459461
assetId: number,
460-
params: QueryParams = {}
462+
params: QueryParams = {},
461463
) {
462464
return await this.query({
463465
endpoint: `/v2/assets/:id/transactions`,
@@ -498,21 +500,21 @@ export default class Query extends BaseModule {
498500
private async nodeHealth() {
499501
if (!this.initiated) await this.waitForInit();
500502
const response = await this.fetchData(
501-
`${this.stack.configs.apiUrl}/health`
503+
`${this.stack.configs.apiUrl}/health`,
502504
);
503505
return camelcaseKeys(response, { deep: true });
504506
}
505507
private async nodeStatus() {
506508
if (!this.initiated) await this.waitForInit();
507509
const response = await this.fetchData(
508-
`${this.stack.configs.apiUrl}/v2/status`
510+
`${this.stack.configs.apiUrl}/v2/status`,
509511
);
510512
return camelcaseKeys(response, { deep: true });
511513
}
512514
private async nodeStatusAfter(block: number) {
513515
if (!this.initiated) await this.waitForInit();
514516
const response = await this.fetchData(
515-
`${this.stack.configs.apiUrl}/v2/status/wait-for-block-after/${block}`
517+
`${this.stack.configs.apiUrl}/v2/status/wait-for-block-after/${block}`,
516518
);
517519
return camelcaseKeys(response, { deep: true });
518520
}
@@ -531,7 +533,7 @@ export default class Query extends BaseModule {
531533
private async nodeAccountApplication(
532534
accountId: string,
533535
appId: number,
534-
params: QueryParams = {}
536+
params: QueryParams = {},
535537
) {
536538
return await this.query({
537539
base: ApiUrl.NODE,
@@ -547,7 +549,7 @@ export default class Query extends BaseModule {
547549
private async nodeAccountAsset(
548550
accountId: string,
549551
assetId: number,
550-
params: QueryParams = {}
552+
params: QueryParams = {},
551553
) {
552554
return await this.query({
553555
base: ApiUrl.NODE,
@@ -574,16 +576,16 @@ export default class Query extends BaseModule {
574576
private async nodeDisassembleTeal(b64: string) {
575577
if (!b64?.length) return undefined;
576578
if (!this.initiated) await this.waitForInit();
577-
const programBuffer = Buffer.from(b64, "base64");
579+
const programBuffer = Buffer.from(b64, 'base64');
578580
const response = (await this.fetch(
579581
`${this.stack.configs[ApiUrl.NODE]}/v2/teal/disassemble`,
580582
{
581-
method: "POST",
583+
method: 'POST',
582584
cacheTable: CacheTable.NODE_TEAL,
583585
refreshCache: true,
584-
headers: { "Content-Type": "application/x-binary" },
586+
headers: { 'Content-Type': 'application/x-binary' },
585587
data: programBuffer,
586-
}
588+
},
587589
)) as Payload;
588590
return response?.result;
589591
}
@@ -672,7 +674,7 @@ export default class Query extends BaseModule {
672674
public fetch(
673675
apiUrl: string,
674676
originalParams: QueryParams = {},
675-
client?: AxiosInstance
677+
client?: AxiosInstance,
676678
) {
677679
return new Promise(async (resolve, reject) => {
678680
if (!this.initiated) await this.waitForInit();
@@ -708,7 +710,7 @@ export default class Query extends BaseModule {
708710
data = await this.fetchData(url, params, client);
709711

710712
// cache result
711-
if (this.cache && cacheTable && !noCache && !data.error) {
713+
if (this.cache && cacheTable && !noCache && !data?.error) {
712714
await this.cache.save(cacheTable, data, { params });
713715
}
714716

0 commit comments

Comments
 (0)