Refactor Code to typescript

This commit is contained in:
Benjamin Toby
2025-01-10 20:10:28 +01:00
parent 549d0abc02
commit eb0992f28d
270 changed files with 3535 additions and 9062 deletions
-46
View File
@@ -1,46 +0,0 @@
export = deleteFile;
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* urlThumbnailPath: string
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
/**
* # Delete File via API
* @async
*
* @param {Object} params - Single Param object containing params
* @param {String} params.key - *FULL ACCESS API Key
* @param { string } params.url - File URL
* @param { string | number } [params.user_id]
*
* @returns { Promise<FunctionReturn> } - Image Url
*/
declare function deleteFile({ key, url, user_id }: {
key: string;
url: string;
user_id?: string | number;
}): Promise<FunctionReturn>;
declare namespace deleteFile {
export { FunctionReturn };
}
type FunctionReturn = {
/**
* - Did the function run successfully?
*/
success: boolean;
/**
* - Payload containing the url for the image and its thumbnail
*/
payload: {
urlPath: string;
urlThumbnailPath: string;
} | null;
/**
* - An optional message
*/
msg?: string;
};
+21 -24
View File
@@ -1,31 +1,28 @@
// @ts-check
import grabHostNames from "../package-shared/utils/grab-host-names";
const http = require("http");
const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
interface Return {
success: boolean;
payload: {
urlPath: string;
urlThumbnailPath: string;
} | null;
msg?: string;
}
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* urlThumbnailPath: string
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
type Param = {
key: string;
url: string;
user_id?: string | number;
};
/**
* # Delete File via API
* @async
*
* @param {Object} params - Single Param object containing params
* @param {String} params.key - *FULL ACCESS API Key
* @param { string } params.url - File URL
* @param { string | number } [params.user_id]
*
* @returns { Promise<FunctionReturn> } - Image Url
*/
async function deleteFile({ key, url, user_id }) {
export default async function deleteFile({
key,
url,
user_id,
}: Param): Promise<Return> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -82,8 +79,8 @@ async function deleteFile({ key, url, user_id }) {
httpsRequest.end();
});
return httpResponse;
} catch (/** @type {*} */ error) {
return httpResponse as Return;
} catch (/** @type {*} */ error: any) {
console.log("Error deleting file: ", error.message);
return {
-26
View File
@@ -1,26 +0,0 @@
export = getSchema;
/**
* @typedef {Object} GetSchemaReturn
* @property {boolean} success - Did the function run successfully?
* @property {import("../package-shared/types").DSQL_DatabaseSchemaType | import("../package-shared/types").DSQL_TableSchemaType | import("../package-shared/types").DSQL_FieldSchemaType | null} payload - Response payload
*/
/**
* # Get Schema for Database, table, or field *
* @param {import("../package-shared/types").GetSchemaAPIParam} params
*
* @returns { Promise<GetSchemaReturn> } - Return Object
*/
declare function getSchema({ key, database, field, table, user_id }: import("../package-shared/types").GetSchemaAPIParam): Promise<GetSchemaReturn>;
declare namespace getSchema {
export { GetSchemaReturn };
}
type GetSchemaReturn = {
/**
* - Did the function run successfully?
*/
success: boolean;
/**
* - Response payload
*/
payload: import("../package-shared/types").DSQL_DatabaseSchemaType | import("../package-shared/types").DSQL_TableSchemaType | import("../package-shared/types").DSQL_FieldSchemaType | null;
};
+37 -23
View File
@@ -1,22 +1,33 @@
// @ts-check
const http = require("http");
const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
import grabHostNames from "../package-shared/utils/grab-host-names";
import {
DSQL_DatabaseSchemaType,
DSQL_FieldSchemaType,
DSQL_TableSchemaType,
GetSchemaAPIParam,
GetSchemaRequestQuery,
} from "../package-shared/types";
/**
* @typedef {Object} GetSchemaReturn
* @property {boolean} success - Did the function run successfully?
* @property {import("../package-shared/types").DSQL_DatabaseSchemaType | import("../package-shared/types").DSQL_TableSchemaType | import("../package-shared/types").DSQL_FieldSchemaType | null} payload - Response payload
*/
type GetSchemaReturn = {
success: boolean;
payload?:
| DSQL_DatabaseSchemaType
| DSQL_TableSchemaType
| DSQL_FieldSchemaType
| null;
};
/**
* # Get Schema for Database, table, or field *
* @param {import("../package-shared/types").GetSchemaAPIParam} params
*
* @returns { Promise<GetSchemaReturn> } - Return Object
*/
async function getSchema({ key, database, field, table, user_id }) {
export default async function getSchema({
key,
database,
field,
table,
user_id,
}: GetSchemaAPIParam): Promise<GetSchemaReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -26,13 +37,10 @@ async function getSchema({ key, database, field, table, user_id }) {
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
/** @type {import("../package-shared/types").GetSchemaRequestQuery} */
const queryObject = { database, field, table };
const queryObject: GetSchemaRequestQuery = { database, field, table };
let query = Object.keys(queryObject)
// @ts-ignore
.filter((k) => queryObject[k])
// @ts-ignore
.map((k) => `${k}=${queryObject[k]}`)
.filter((k) => queryObject[k as keyof GetSchemaRequestQuery])
.map((k) => `${k}=${queryObject[k as keyof GetSchemaRequestQuery]}`)
.join("&");
scheme
@@ -67,18 +75,24 @@ async function getSchema({ key, database, field, table, user_id }) {
});
response.on("end", function () {
resolve(JSON.parse(str));
resolve(
JSON.parse(str) as
| DSQL_DatabaseSchemaType
| DSQL_TableSchemaType
| DSQL_FieldSchemaType
);
});
response.on("error", (err) => {
reject(err);
resolve(null);
});
}
)
.end();
});
return httpResponse;
return {
success: true,
payload: httpResponse as any,
};
}
module.exports = getSchema;
-26
View File
@@ -1,26 +0,0 @@
export = get;
/**
* Make a get request to Datasquirel API
* ==============================================================================
* @async
*
* @param {Object} params - Single object passed
* @param {string} [params.key] - API Key
* @param {string} [params.db] - Database Name
* @param {string} params.query - SQL Query
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
* @param {string | number} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
*/
declare function get({ key, db, query, queryValues, tableName, useLocal, user_id, }: {
key?: string;
db?: string;
query: string;
queryValues?: string[];
tableName?: string;
useLocal?: boolean;
user_id?: string | number;
}): Promise<import("../package-shared/types").GetReturn>;
+39 -46
View File
@@ -1,29 +1,27 @@
// @ts-check
const https = require("node:https");
const path = require("path");
const fs = require("fs");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiGet = require("../package-shared/functions/api/query/get");
const serializeQuery = require("../package-shared/utils/serialize-query");
import https from "node:https";
import path from "path";
import fs from "fs";
import grabHostNames from "../package-shared/utils/grab-host-names";
import apiGet from "../package-shared/functions/api/query/get";
import serializeQuery from "../package-shared/utils/serialize-query";
import { GetReturn } from "../package-shared/types";
type Param = {
key?: string;
db?: string;
query: string;
queryValues?: string[];
tableName?: string;
useLocal?: boolean;
user_id?: string | number;
};
/**
* Make a get request to Datasquirel API
* ==============================================================================
* @async
*
* @param {Object} params - Single object passed
* @param {string} [params.key] - API Key
* @param {string} [params.db] - Database Name
* @param {string} params.query - SQL Query
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
* @param {string | number} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
* # Make a get request to Datasquirel API
*/
async function get({
export default async function get({
key,
db,
query,
@@ -31,7 +29,7 @@ async function get({
tableName,
useLocal,
user_id,
}) {
}: Param): Promise<GetReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -51,7 +49,9 @@ async function get({
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
let dbSchema:
| import("../package-shared/types").DSQL_DatabaseSchemaType
| undefined;
try {
const localDbSchemaPath = path.resolve(
@@ -78,14 +78,17 @@ async function get({
*/
const httpResponse = await new Promise((resolve, reject) => {
/** @type {import("../package-shared/types").GetReqQueryObject} */
const queryObject = {
db: process.env.DSQL_API_DB_NAME || String(db),
query: String(
query.replace(/\n|\r|\n\r/g, "").replace(/ {2,}/g, " ")
),
queryValues: queryValues ? JSON.stringify(queryValues) : undefined,
tableName,
};
const queryObject: import("../package-shared/types").GetReqQueryObject =
{
db: process.env.DSQL_API_DB_NAME || String(db),
query: String(
query.replace(/\n|\r|\n\r/g, "").replace(/ {2,}/g, " ")
),
queryValues: queryValues
? JSON.stringify(queryValues)
: undefined,
tableName,
};
const queryString = serializeQuery({ ...queryObject });
@@ -94,7 +97,7 @@ async function get({
}/get${queryString}`;
/** @type {https.RequestOptions} */
const requestObject = {
const requestObject: https.RequestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
@@ -127,8 +130,8 @@ async function get({
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (/** @type {any} */ error) {
resolve(JSON.parse(str) as GetReturn);
} catch (/** @type {any} */ error: any) {
reject({
error: error.message,
result: str,
@@ -138,22 +141,12 @@ async function get({
response.on("error", (err) => {
console.log("DSQL get Error,", err.message);
reject(err);
resolve(null);
});
}
)
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
return httpResponse as GetReturn;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = get;
-25
View File
@@ -1,25 +0,0 @@
export = post;
/**
* # Make a post request to Datasquirel API
* @async
*
* @param {Object} params - Single object passed
* @param {string} [params.key] - FULL ACCESS API Key
* @param {string} [params.database] - Database Name
* @param {import("../package-shared/types").PostDataPayload | string} params.query - SQL query String or Request Object
* @param {any[]} [params.queryValues] - Query Values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").PostReturn> } - Return Object
*/
declare function post({ key, query, queryValues, database, tableName, useLocal, user_id, }: {
key?: string;
database?: string;
query: import("../package-shared/types").PostDataPayload | string;
queryValues?: any[];
tableName?: string;
useLocal?: boolean;
user_id?: boolean;
}): Promise<import("../package-shared/types").PostReturn>;
+22 -31
View File
@@ -1,26 +1,25 @@
// @ts-check
const path = require("path");
const fs = require("fs");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiPost = require("../package-shared/functions/api/query/post");
import path from "path";
import fs from "fs";
import grabHostNames from "../package-shared/utils/grab-host-names";
import apiPost from "../package-shared/functions/api/query/post";
import { PostDataPayload, PostReturn } from "../package-shared/types";
type Param = {
key?: string;
database?: string;
query: PostDataPayload;
queryValues?: any[];
tableName?: string;
useLocal?: boolean;
user_id?: boolean;
};
/**
* # Make a post request to Datasquirel API
* @async
*
* @param {Object} params - Single object passed
* @param {string} [params.key] - FULL ACCESS API Key
* @param {string} [params.database] - Database Name
* @param {import("../package-shared/types").PostDataPayload | string} params.query - SQL query String or Request Object
* @param {any[]} [params.queryValues] - Query Values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").PostReturn> } - Return Object
*/
async function post({
export default async function post({
key,
query,
queryValues,
@@ -28,7 +27,7 @@ async function post({
tableName,
useLocal,
user_id,
}) {
}: Param): Promise<PostReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -48,7 +47,9 @@ async function post({
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
let dbSchema:
| import("../package-shared/types").DSQL_DatabaseSchemaType
| undefined;
try {
const localDbSchemaPath = path.resolve(
@@ -131,7 +132,7 @@ async function post({
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (/** @type {any} */ error) {
} catch (/** @type {any} */ error: any) {
console.log("Route ERROR:", error.message);
resolve({
@@ -162,15 +163,5 @@ async function post({
httpsRequest.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
return httpResponse as PostReturn;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = post;
-56
View File
@@ -1,56 +0,0 @@
export = uploadImage;
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
/**
* # Upload File via API
* @async
*
* @param {Object} params - Single Param object containing params
* @param {String} params.key - *FULL ACCESS API Key
* @param {{
* fileData: string,
* fileName: string,
* mimeType?: string,
* folder?: string,
* isPrivate?: boolean
* }} params.payload - Image Data Eg.
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
declare function uploadImage({ key, payload, user_id }: {
key: string;
payload: {
fileData: string;
fileName: string;
mimeType?: string;
folder?: string;
isPrivate?: boolean;
};
user_id?: boolean;
}): Promise<FunctionReturn>;
declare namespace uploadImage {
export { FunctionReturn };
}
type FunctionReturn = {
/**
* - Did the function run successfully?
*/
success: boolean;
/**
* - Payload containing the url for the image and its thumbnail
*/
payload: {
urlPath: string;
} | null;
/**
* - An optional message
*/
msg?: string;
};
+26 -43
View File
@@ -1,36 +1,33 @@
// @ts-check
import grabHostNames from "../package-shared/utils/grab-host-names";
const http = require("http");
const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
interface Return {
success: boolean;
payload: {
urlPath: string;
} | null;
msg?: string;
}
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
type Param = {
key: string;
payload: {
fileData: string;
fileName: string;
mimeType?: string;
folder?: string;
isPrivate?: boolean;
};
user_id?: boolean;
};
/**
* # Upload File via API
* @async
*
* @param {Object} params - Single Param object containing params
* @param {String} params.key - *FULL ACCESS API Key
* @param {{
* fileData: string,
* fileName: string,
* mimeType?: string,
* folder?: string,
* isPrivate?: boolean
* }} params.payload - Image Data Eg.
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
async function uploadImage({ key, payload, user_id }) {
export default async function uploadImage({
key,
payload,
user_id,
}: Param): Promise<Return> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -87,16 +84,8 @@ async function uploadImage({ key, payload, user_id }) {
httpsRequest.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
} catch (/** @type {*} */ error) {
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse as Return;
} catch (error: any) {
console.log("Error in uploading file: ", error.message);
return {
@@ -106,9 +95,3 @@ async function uploadImage({ key, payload, user_id }) {
};
}
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = uploadImage;
-60
View File
@@ -1,60 +0,0 @@
export = uploadImage;
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* urlThumbnailPath: string
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
/**
* # Upload Image via API
* @async
*
* @param {Object} params - Single Param object containing params
* @param {String} [params.key] - *FULL ACCESS API Key
* @param {{
* imageData: string,
* imageName: string,
* mimeType?: string,
* thumbnailSize?: number,
* folder?: string,
* isPrivate?: boolean,
* }} params.payload - Image Data Eg.
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
declare function uploadImage({ key, payload, user_id }: {
key?: string;
payload: {
imageData: string;
imageName: string;
mimeType?: string;
thumbnailSize?: number;
folder?: string;
isPrivate?: boolean;
};
user_id?: boolean;
}): Promise<FunctionReturn>;
declare namespace uploadImage {
export { FunctionReturn };
}
type FunctionReturn = {
/**
* - Did the function run successfully?
*/
success: boolean;
/**
* - Payload containing the url for the image and its thumbnail
*/
payload: {
urlPath: string;
urlThumbnailPath: string;
} | null;
/**
* - An optional message
*/
msg?: string;
};
+29 -40
View File
@@ -1,36 +1,37 @@
// @ts-check
const grabHostNames = require("../package-shared/utils/grab-host-names");
import grabHostNames from "../package-shared/utils/grab-host-names";
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* urlThumbnailPath: string
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
interface FunctionReturn {
success: boolean;
payload: {
urlPath: string;
urlThumbnailPath: string;
} | null;
msg?: string;
}
type Param = {
key?: string;
payload: {
imageData: string;
imageName: string;
mimeType?: string;
thumbnailSize?: number;
folder?: string;
isPrivate?: boolean;
};
user_id?: boolean;
};
/**
* # Upload Image via API
* @async
*
* @param {Object} params - Single Param object containing params
* @param {String} [params.key] - *FULL ACCESS API Key
* @param {{
* imageData: string,
* imageName: string,
* mimeType?: string,
* thumbnailSize?: number,
* folder?: string,
* isPrivate?: boolean,
* }} params.payload - Image Data Eg.
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
async function uploadImage({ key, payload, user_id }) {
export default async function uploadImage({
key,
payload,
user_id,
}: Param): Promise<FunctionReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -87,16 +88,8 @@ async function uploadImage({ key, payload, user_id }) {
httpsRequest.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
} catch (/** @type {*} */ error) {
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse as FunctionReturn;
} catch (error: any) {
console.log("Error in uploading image: ", error.message);
return {
@@ -107,8 +100,4 @@ async function uploadImage({ key, payload, user_id }) {
}
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = uploadImage;