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
-80
View File
@@ -1,80 +0,0 @@
export = githubAuth;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* @typedef {object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{id: number, first_name: string, last_name: string, csrf_k: string, social_id: string} | null} user - Returned User
* @property {number} [dsqlUserId] - Dsql User Id
* @property {string} [msg] - Response message
*/
/**
* SERVER FUNCTION: Login with google Function
* ==============================================================================
*
* @async
*
* @param {object} params - main params object
* @param {string} params.key - API full access key
* @param {string} params.code - Github access code gotten from the client side
* @param {string?} params.email - Email gotten from the client side if available
* @param {string} params.database - Target database name(slug)
* @param {string} params.clientId - Github client id
* @param {string} params.clientSecret - Github client Secret
* @param {http.ServerResponse} params.response - HTTPS response object
* @param {string} params.encryptionKey - Encryption key
* @param {string} params.encryptionSalt - Encryption salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {Object<string,string|number>} [params.additionalData] - Additional Data to by added on creation of User
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn | undefined> }
*/
declare function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields, user_id, additionalData, }: {
key: string;
code: string;
email: string | null;
database: string;
clientId: string;
clientSecret: string;
response: http.ServerResponse;
encryptionKey: string;
encryptionSalt: string;
additionalFields?: string[];
additionalData?: {
[x: string]: string | number;
};
user_id?: boolean;
}): Promise<FunctionReturn | undefined>;
declare namespace githubAuth {
export { FunctionReturn };
}
import http = require("http");
type FunctionReturn = {
/**
* - Did the function run successfully?
*/
success: boolean;
/**
* - Returned User
*/
user: {
id: number;
first_name: string;
last_name: string;
csrf_k: string;
social_id: string;
} | null;
/**
* - Dsql User Id
*/
dsqlUserId?: number;
/**
* - Response message
*/
msg?: string;
};
@@ -1,56 +1,42 @@
// @ts-check
import http from "http";
import fs from "fs";
import path from "path";
import encrypt from "../../package-shared/functions/dsql/encrypt";
import grabHostNames from "../../package-shared/utils/grab-host-names";
import apiGithubLogin from "../../package-shared/functions/api/users/social/api-github-login";
interface FunctionReturn {
success: boolean;
user: {
id: number;
first_name: string;
last_name: string;
csrf_k: string;
social_id: string;
} | null;
dsqlUserId?: number;
msg?: string;
}
type Param = {
key: string;
code: string;
email: string | null;
database: string;
clientId: string;
clientSecret: string;
response: http.ServerResponse;
encryptionKey: string;
encryptionSalt: string;
additionalFields?: string[];
additionalData?: { [s: string]: string | number };
user_id?: boolean;
};
/**
* ==============================================================================
* Imports
* ==============================================================================
* # SERVER FUNCTION: Login with google Function
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../package-shared/functions/dsql/encrypt");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
const apiGithubLogin = require("../../package-shared/functions/api/users/social/api-github-login");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* @typedef {object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{id: number, first_name: string, last_name: string, csrf_k: string, social_id: string} | null} user - Returned User
* @property {number} [dsqlUserId] - Dsql User Id
* @property {string} [msg] - Response message
*/
/**
* SERVER FUNCTION: Login with google Function
* ==============================================================================
*
* @async
*
* @param {object} params - main params object
* @param {string} params.key - API full access key
* @param {string} params.code - Github access code gotten from the client side
* @param {string?} params.email - Email gotten from the client side if available
* @param {string} params.database - Target database name(slug)
* @param {string} params.clientId - Github client id
* @param {string} params.clientSecret - Github client Secret
* @param {http.ServerResponse} params.response - HTTPS response object
* @param {string} params.encryptionKey - Encryption key
* @param {string} params.encryptionSalt - Encryption salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {Object<string,string|number>} [params.additionalData] - Additional Data to by added on creation of User
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn | undefined> }
*/
async function githubAuth({
export default async function githubAuth({
key,
code,
email,
@@ -63,7 +49,7 @@ async function githubAuth({
additionalFields,
user_id,
additionalData,
}) {
}: Param): Promise<FunctionReturn | undefined> {
/**
* Check inputs
*
@@ -96,10 +82,6 @@ async function githubAuth({
};
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Initialize HTTP response variable
*/
@@ -127,7 +109,10 @@ async function githubAuth({
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined | undefined} */
let dbSchema;
let dbSchema:
| import("../../package-shared/types").DSQL_DatabaseSchemaType
| undefined
| undefined;
try {
const localDbSchemaPath = path.resolve(
@@ -153,7 +138,7 @@ async function githubAuth({
* @description make a request to datasquirel.com
* @type {FunctionReturn} - Https response object
*/
httpResponse = await new Promise((resolve, reject) => {
httpResponse = (await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
code,
email,
@@ -215,7 +200,7 @@ async function githubAuth({
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
})) as any;
}
////////////////////////////////////////
@@ -247,15 +232,5 @@ async function githubAuth({
]);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return httpResponse;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
module.exports = githubAuth;
-36
View File
@@ -1,36 +0,0 @@
export = googleAuth;
/**
* SERVER FUNCTION: Login with google Function
* ==============================================================================
*
* @async
*
* @param {object} params - main params object
* @param {string} [params.key] - API full access key
* @param {string} params.token - Google access token gotten from the client side
* @param {string} [params.database] - Target database name
* @param {http.ServerResponse} [params.response] - HTTPS response object
* @param {string} [params.encryptionKey] - Encryption key
* @param {string} [params.encryptionSalt] - Encryption salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {Object<string,string|number>} [params.additionalData] - Additional Data to by added on creation of User
* @param {string | number} [params.apiUserID] - API user ID
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
*
* @returns { Promise<import("../../package-shared/types").APILoginFunctionReturn> }
*/
declare function googleAuth({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, additionalData, apiUserID, useLocal, }: {
key?: string;
token: string;
database?: string;
response?: http.ServerResponse;
encryptionKey?: string;
encryptionSalt?: string;
additionalFields?: string[];
additionalData?: {
[x: string]: string | number;
};
apiUserID?: string | number;
useLocal?: boolean;
}): Promise<import("../../package-shared/types").APILoginFunctionReturn>;
import http = require("http");
@@ -1,35 +1,28 @@
// @ts-check
import http from "http";
import encrypt from "../../package-shared/functions/dsql/encrypt";
import grabHostNames from "../../package-shared/utils/grab-host-names";
import apiGoogleLogin from "../../package-shared/functions/api/users/social/api-google-login";
import getAuthCookieNames from "../../package-shared/functions/backend/cookies/get-auth-cookie-names";
import { writeAuthFile } from "../../package-shared/functions/backend/auth/write-auth-files";
import { APILoginFunctionReturn } from "../../package-shared/types";
const http = require("http");
const encrypt = require("../../package-shared/functions/dsql/encrypt");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
const apiGoogleLogin = require("../../package-shared/functions/api/users/social/api-google-login");
const getAuthCookieNames = require("../../package-shared/functions/backend/cookies/get-auth-cookie-names");
const {
writeAuthFile,
} = require("../../package-shared/functions/backend/auth/write-auth-files");
type Param = {
key?: string;
token: string;
database?: string;
response?: http.ServerResponse;
encryptionKey?: string;
encryptionSalt?: string;
additionalFields?: string[];
additionalData?: { [s: string]: string | number };
apiUserID?: string | number;
useLocal?: boolean;
};
/**
* SERVER FUNCTION: Login with google Function
* ==============================================================================
*
* @async
*
* @param {object} params - main params object
* @param {string} [params.key] - API full access key
* @param {string} params.token - Google access token gotten from the client side
* @param {string} [params.database] - Target database name
* @param {http.ServerResponse} [params.response] - HTTPS response object
* @param {string} [params.encryptionKey] - Encryption key
* @param {string} [params.encryptionSalt] - Encryption salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {Object<string,string|number>} [params.additionalData] - Additional Data to by added on creation of User
* @param {string | number} [params.apiUserID] - API user ID
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
*
* @returns { Promise<import("../../package-shared/types").APILoginFunctionReturn> }
* # SERVER FUNCTION: Login with google Function
*/
async function googleAuth({
export default async function googleAuth({
key,
token,
database,
@@ -40,7 +33,7 @@ async function googleAuth({
additionalData,
apiUserID,
useLocal,
}) {
}: Param): Promise<APILoginFunctionReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -85,9 +78,10 @@ async function googleAuth({
*/
/** @type {import("../../package-shared/types").APILoginFunctionReturn} */
let httpResponse = {
success: false,
};
let httpResponse: import("../../package-shared/types").APILoginFunctionReturn =
{
success: false,
};
/**
* Check for local DB settings
@@ -207,5 +201,3 @@ async function googleAuth({
return httpResponse;
}
module.exports = googleAuth;