Refactor get query serialization

This commit is contained in:
Benjamin Toby 2024-10-18 05:49:04 +01:00
parent e2d2d00487
commit 33283e7d0f
4 changed files with 65 additions and 9 deletions

View File

@ -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
*/

View File

@ -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": {

View File

@ -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;

View File

@ -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
/** @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, "+")}`;
.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} */