Refactor get query serialization
This commit is contained in:
parent
e2d2d00487
commit
33283e7d0f
@ -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
|
||||
*/
|
||||
|
@ -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": {
|
||||
|
17
utils/functions/serialize-query.js
Normal file
17
utils/functions/serialize-query.js
Normal 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;
|
32
utils/get.js
32
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
|
||||
/** @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} */
|
||||
|
Loading…
Reference in New Issue
Block a user