diff --git a/package-shared/types/general.td.js b/package-shared/types/general.td.js index 6fc2612..fdbeedc 100644 --- a/package-shared/types/general.td.js +++ b/package-shared/types/general.td.js @@ -1,3 +1,5 @@ +// @ts-check + const http = require("http"); /** @@ -17,4 +19,19 @@ const http = require("http"); * }} ImageInputFileToBase64FunctionReturn */ -module.exports = { Request, Response, ImageInputFileToBase64FunctionReturn }; +/** + * @typedef {object} GetReqQueryObject + * @property {string} db + * @property {string} query + * @property {string} [queryValues] + * @property {string} [tableName] + */ + +/** + * @typedef {(param0: SerializeQueryParams) => string} SerializeQueryFnType + */ + +/** + * @typedef {object} SerializeQueryParams + * @property {any} query + */ diff --git a/package.json b/package.json index 2496d8a..81d9ae0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "2.1.3", + "version": "2.1.4", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { diff --git a/utils/functions/serialize-query.js b/utils/functions/serialize-query.js new file mode 100644 index 0000000..ae2b9c0 --- /dev/null +++ b/utils/functions/serialize-query.js @@ -0,0 +1,17 @@ +// @ts-check + +/** @type {import("@/package-shared/types/general.td").SerializeQueryFnType} */ +function serializeQuery({ query }) { + let str = "?"; + const keys = Object.keys(query); + + /** @type {string[]} */ + const queryArr = []; + keys.forEach((key) => { + queryArr.push(`${key}=${query[key]}`); + }); + str += queryArr.join("&"); + return str; +} + +module.exports = serializeQuery; diff --git a/utils/get.js b/utils/get.js index dbbeb06..0b23778 100644 --- a/utils/get.js +++ b/utils/get.js @@ -10,6 +10,7 @@ const https = require("node:https"); const path = require("path"); const fs = require("fs"); const localGet = require("../engine/query/get"); +const serializeQuery = require("./functions/serialize-query"); /** ****************************************************************************** */ /** ****************************************************************************** */ @@ -85,15 +86,36 @@ async function get({ key, db, query, queryValues, tableName }) { * @description make a request to datasquirel.com */ const httpResponse = await new Promise((resolve, reject) => { - let path = `/api/query/get?db=${db}&query=${query - .replace(/\n|\r|\n\r/g, "") - .replace(/ {2,}/g, " ") - .replace(/ /g, "+")}`; + /** @type {import("@/package-shared/types/general.td").GetReqQueryObject} */ + const queryObject = { + db: String(db), + query: String( + query + .replace(/\n|\r|\n\r/g, "") + .replace(/ {2,}/g, " ") + .replace(/ /g, "+") + ), + queryValues: queryValues ? JSON.stringify(queryValues) : undefined, + tableName, + }; + + const queryString = serializeQuery({ query: queryObject }); + + // let path = `/api/query/get?db=${db}&query=${query + // .replace(/\n|\r|\n\r/g, "") + // .replace(/ {2,}/g, " ") + // .replace(/ /g, "+")}`; + + console.log("queryString =>", queryString); + + let path = `/api/query/get${queryString}`; if (queryValues) { - path += `&queryValues=${JSON.stringify(queryValues)}${ - tableName ? `&tableName=${tableName}` : "" - }`; + path += `&queryValues=${JSON.stringify(queryValues)}`; + } + + if (tableName) { + path += `&tableName=${tableName}`; } /** @type {https.RequestOptions} */