diff --git a/dist/cjs/helper/task.js b/dist/cjs/helper/task.js index fffc82f6..60510d05 100644 --- a/dist/cjs/helper/task.js +++ b/dist/cjs/helper/task.js @@ -138,39 +138,45 @@ class ZkWasmServiceHelper { return __awaiter(this, void 0, void 0, function* () { let headers = { "Content-Type": "application/json" }; let queryJson = JSON.parse("{}"); + // Set default total to 2 if not provided + const defaultQuery = { + total: 2, + }; + // Merge the original query with default values + const mergedQuery = Object.assign(Object.assign({}, defaultQuery), query); // Validate query params - if (query.start != null && query.start < 0) { + if (mergedQuery.start != null && mergedQuery.start < 0) { throw new Error("start must be positive"); } - if (query.total != null && query.total <= 0) { + if (mergedQuery.total != null && mergedQuery.total <= 0) { throw new Error("total must be positive"); } - if (query.id != null && query.id != "") { + if (mergedQuery.id != null && mergedQuery.id != "") { // Validate it is a hex string (mongodb objectid) - if (!util_js_1.ZkWasmUtil.isHexString(query.id)) { + if (!util_js_1.ZkWasmUtil.isHexString(mergedQuery.id)) { throw new Error("id must be a hex string or "); } } - if (query.user_address != null && query.user_address != "") { + if (mergedQuery.user_address != null && mergedQuery.user_address != "") { // Validate it is a hex string (ethereum address) - if (!ethers_1.ethers.isAddress(query.user_address)) { + if (!ethers_1.ethers.isAddress(mergedQuery.user_address)) { throw new Error("user_address must be a valid ethereum address"); } } - if (query.md5 != null && query.md5 != "") { + if (mergedQuery.md5 != null && mergedQuery.md5 != "") { // Validate it is a hex string (md5) - if (!util_js_1.ZkWasmUtil.isHexString(query.md5)) { + if (!util_js_1.ZkWasmUtil.isHexString(mergedQuery.md5)) { throw new Error("md5 must be a hex string"); } } //build query JSON - let objKeys = Object.keys(query); + let objKeys = Object.keys(mergedQuery); objKeys.forEach((key) => { - if (query[key] != "" && query[key] != null) - queryJson[key] = query[key]; + if (mergedQuery[key] != "" && mergedQuery[key] != null) + queryJson[key] = mergedQuery[key]; }); if (this.endpoint.enable_logs) { - console.log("params:", query); + console.log("params:", mergedQuery); console.log("json", queryJson); } let tasks = yield this.endpoint.invokeRequest("GET", `/tasks`, queryJson); diff --git a/dist/mjs/helper/task.js b/dist/mjs/helper/task.js index 154f2a8e..26ef7651 100644 --- a/dist/mjs/helper/task.js +++ b/dist/mjs/helper/task.js @@ -94,39 +94,45 @@ export class ZkWasmServiceHelper { async loadTasks(query) { let headers = { "Content-Type": "application/json" }; let queryJson = JSON.parse("{}"); + // Set default total to 2 if not provided + const defaultQuery = { + total: 2, + }; + // Merge the original query with default values + const mergedQuery = { ...defaultQuery, ...query }; // Validate query params - if (query.start != null && query.start < 0) { + if (mergedQuery.start != null && mergedQuery.start < 0) { throw new Error("start must be positive"); } - if (query.total != null && query.total <= 0) { + if (mergedQuery.total != null && mergedQuery.total <= 0) { throw new Error("total must be positive"); } - if (query.id != null && query.id != "") { + if (mergedQuery.id != null && mergedQuery.id != "") { // Validate it is a hex string (mongodb objectid) - if (!ZkWasmUtil.isHexString(query.id)) { + if (!ZkWasmUtil.isHexString(mergedQuery.id)) { throw new Error("id must be a hex string or "); } } - if (query.user_address != null && query.user_address != "") { + if (mergedQuery.user_address != null && mergedQuery.user_address != "") { // Validate it is a hex string (ethereum address) - if (!ethers.isAddress(query.user_address)) { + if (!ethers.isAddress(mergedQuery.user_address)) { throw new Error("user_address must be a valid ethereum address"); } } - if (query.md5 != null && query.md5 != "") { + if (mergedQuery.md5 != null && mergedQuery.md5 != "") { // Validate it is a hex string (md5) - if (!ZkWasmUtil.isHexString(query.md5)) { + if (!ZkWasmUtil.isHexString(mergedQuery.md5)) { throw new Error("md5 must be a hex string"); } } //build query JSON - let objKeys = Object.keys(query); + let objKeys = Object.keys(mergedQuery); objKeys.forEach((key) => { - if (query[key] != "" && query[key] != null) - queryJson[key] = query[key]; + if (mergedQuery[key] != "" && mergedQuery[key] != null) + queryJson[key] = mergedQuery[key]; }); if (this.endpoint.enable_logs) { - console.log("params:", query); + console.log("params:", mergedQuery); console.log("json", queryJson); } let tasks = await this.endpoint.invokeRequest("GET", `/tasks`, queryJson); diff --git a/src/helper/task.ts b/src/helper/task.ts index d53d8e9e..c04bf1df 100644 --- a/src/helper/task.ts +++ b/src/helper/task.ts @@ -182,43 +182,52 @@ export class ZkWasmServiceHelper { let headers = { "Content-Type": "application/json" }; let queryJson = JSON.parse("{}"); + // Set default total to 2 if not provided + const defaultQuery = { + total: 2, + }; + + // Merge the original query with default values + const mergedQuery = { ...defaultQuery, ...query }; + // Validate query params - if (query.start != null && query.start < 0) { + if (mergedQuery.start != null && mergedQuery.start < 0) { throw new Error("start must be positive"); } - if (query.total != null && query.total <= 0) { + if (mergedQuery.total != null && mergedQuery.total <= 0) { throw new Error("total must be positive"); } - if (query.id != null && query.id != "") { + if (mergedQuery.id != null && mergedQuery.id != "") { // Validate it is a hex string (mongodb objectid) - if (!ZkWasmUtil.isHexString(query.id)) { + if (!ZkWasmUtil.isHexString(mergedQuery.id)) { throw new Error("id must be a hex string or "); } } - if (query.user_address != null && query.user_address != "") { + if (mergedQuery.user_address != null && mergedQuery.user_address != "") { // Validate it is a hex string (ethereum address) - if (!ethers.isAddress(query.user_address)) { + if (!ethers.isAddress(mergedQuery.user_address)) { throw new Error("user_address must be a valid ethereum address"); } } - if (query.md5 != null && query.md5 != "") { + if (mergedQuery.md5 != null && mergedQuery.md5 != "") { // Validate it is a hex string (md5) - if (!ZkWasmUtil.isHexString(query.md5)) { + if (!ZkWasmUtil.isHexString(mergedQuery.md5)) { throw new Error("md5 must be a hex string"); } } //build query JSON - let objKeys = Object.keys(query) as Array; + let objKeys = Object.keys(mergedQuery) as Array; objKeys.forEach((key) => { - if (query[key] != "" && query[key] != null) queryJson[key] = query[key]; + if (mergedQuery[key] != "" && mergedQuery[key] != null) + queryJson[key] = mergedQuery[key]; }); if (this.endpoint.enable_logs) { - console.log("params:", query); + console.log("params:", mergedQuery); console.log("json", queryJson); }