Skip to content
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
30 changes: 18 additions & 12 deletions dist/cjs/helper/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 18 additions & 12 deletions dist/mjs/helper/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 20 additions & 11 deletions src/helper/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof QueryParams>;
let objKeys = Object.keys(mergedQuery) as Array<keyof QueryParams>;
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);
}

Expand Down