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
-7
View File
@@ -1,7 +0,0 @@
declare function _exports({ clientId, redirectUrl, setLoading, scopes }: {
clientId: string;
redirectUrl: string;
setLoading?: (arg0: boolean) => void;
scopes?: string[];
}): void;
export = _exports;
-36
View File
@@ -1,36 +0,0 @@
// @ts-check
/**
* Login with Github Function
* ===============================================================================
* @description This function uses github api to login a user with datasquirel
*
* @async
*
* @param {object} params - Single object passed
* @param {string} params.clientId - Github app client ID: {@link https://datasquirel.com/docs}
* @param {string} params.redirectUrl - Github Redirect URL as listed in your oauth app settings: {@link https://datasquirel.com/docs}
* @param {function(boolean): void} [params.setLoading] - React setState Function: sets whether the google login button is ready or not
* @param {string[]} [params.scopes] - Scopes to be requested from the user
*
* @returns {void} - Return
*/
module.exports = function getAccessToken({ clientId, redirectUrl, setLoading, scopes }) {
/**
* == Initialize
*
* @description Initialize
*/
if (setLoading) setLoading(true);
const scopeString = scopes ? scopes.join("%20") : "read:user";
const fetchUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=${scopeString}&redirect_uri=${redirectUrl}`;
window.location.assign(fetchUrl);
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
};
+24
View File
@@ -0,0 +1,24 @@
type Param = {
clientId: string;
redirectUrl: string;
setLoading?: (arg0: boolean) => void;
scopes?: string[];
};
/**
* Login with Github Function
* ===============================================================================
* @description This function uses github api to login a user with datasquirel
*/
export default function getAccessToken({
clientId,
redirectUrl,
setLoading,
scopes,
}: Param): void {
if (setLoading) setLoading(true);
const scopeString = scopes ? scopes.join("%20") : "read:user";
const fetchUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=${scopeString}&redirect_uri=${redirectUrl}`;
window.location.assign(fetchUrl);
}
-19
View File
@@ -1,19 +0,0 @@
declare namespace _exports {
export { GoogleGetAccessTokenFunctionParams };
}
declare function _exports(params: GoogleGetAccessTokenFunctionParams): Promise<string>;
export = _exports;
type GoogleGetAccessTokenFunctionParams = {
/**
* - Google app client ID: {@link https://datasquirel.com/docs}
*/
clientId: string;
/**
* - Whether to trigger Google signing popup or not: {@link https://datasquirel.com/docs}
*/
triggerPrompt?: boolean;
/**
* - React setState Function: sets whether the google login button is ready or not
*/
setLoading?: React.Dispatch<React.SetStateAction<boolean>>;
};
@@ -1,33 +1,22 @@
// @ts-check
interface GoogleGetAccessTokenFunctionParams {
clientId: string;
triggerPrompt?: boolean;
setLoading?: React.Dispatch<React.SetStateAction<boolean>>;
}
/**
* @typedef {object} GoogleGetAccessTokenFunctionParams
* @property {string} clientId - Google app client ID: {@link https://datasquirel.com/docs}
* @property {boolean} [triggerPrompt] - Whether to trigger Google signing popup or not: {@link https://datasquirel.com/docs}
* @property {React.Dispatch<React.SetStateAction<boolean>>} [setLoading] - React setState Function: sets whether the google login button is ready or not
*
*/
/** @type {any} */
let interval;
let interval: any;
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*
* @async
*
* @requires script "https://accounts.google.com/gsi/client" async script added to head
*
* @param {GoogleGetAccessTokenFunctionParams} params - Single object passed
* @returns {Promise<string>} - Access Token String
*/
module.exports = async function getAccessToken(params) {
export default async function getAccessToken(
params: GoogleGetAccessTokenFunctionParams
): Promise<string> {
params.setLoading?.(true);
const response = await new Promise((resolve, reject) => {
const response = (await new Promise((resolve, reject) => {
interval = setInterval(() => {
// @ts-ignore
let google = window.google;
@@ -37,20 +26,22 @@ module.exports = async function getAccessToken(params) {
resolve(googleLogin({ ...params, google }));
}
}, 500);
});
})) as any;
params.setLoading?.(false);
return response;
};
}
/**
* # Google Login Function
*
* @param {GoogleGetAccessTokenFunctionParams & { google: any }} params
* @returns
*/
function googleLogin({ google, clientId, setLoading, triggerPrompt }) {
export function googleLogin({
google,
clientId,
setLoading,
triggerPrompt,
}: GoogleGetAccessTokenFunctionParams & { google: any }) {
setTimeout(() => {
setLoading?.(false);
}, 3000);
@@ -60,7 +51,9 @@ function googleLogin({ google, clientId, setLoading, triggerPrompt }) {
* # Callback Function
* @param {import("../../../package-shared/types").GoogleAccessTokenObject} response
*/
function handleCredentialResponse(response) {
function handleCredentialResponse(
response: import("../../../package-shared/types").GoogleAccessTokenObject
) {
resolve(response.access_token);
}
@@ -81,7 +74,9 @@ function googleLogin({ google, clientId, setLoading, triggerPrompt }) {
* ========================================================
* @param {import("../../../package-shared/types").GoogleIdentityPromptNotification} notification
*/
function triggerGooglePromptCallback(notification) {
function triggerGooglePromptCallback(
notification: import("../../../package-shared/types").GoogleIdentityPromptNotification
) {
console.log(notification);
}
});
-2
View File
@@ -1,2 +0,0 @@
declare function _exports(params: object | null): Promise<boolean>;
export = _exports;
-146
View File
@@ -1,146 +0,0 @@
/**
* Type Definitions
* ===============================================================================
*/
const parseClientCookies = require("../utils/parseClientCookies");
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*
* @async
*
* @param {object|null} params - Single object passed
* @param {string|null} params.googleClientId - Google client Id if applicable
*
* @requires localStorageUser - a "user" JSON string stored in local storage with all
* the necessary user data gotten from the server
*
* @returns {Promise<boolean>} - Return
*/
module.exports = async function logout(params) {
/**
* == Initialize
*
* @description Initialize
*/
const localUser = localStorage.getItem("user");
let targetUser;
try {
targetUser = JSON.parse(localUser);
} catch (error) {
console.log(error);
}
if (!targetUser) {
return false;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const cookies = parseClientCookies();
const socialId = cookies?.datasquirel_social_id && typeof cookies.datasquirel_social_id == "string" && !cookies.datasquirel_social_id.match(/^null$/i) ? cookies.datasquirel_social_id : null;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
localStorage.setItem("user", "{}");
localStorage.removeItem("csrf");
document.cookie = `datasquirel_social_id=null;samesite=strict;path=/`;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const response = await new Promise((resolve, reject) => {
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
if (socialId && !socialId?.match(/^null$/i)) {
const googleClientId = params?.googleClientId;
if (googleClientId) {
const googleScript = document.createElement("script");
googleScript.src = "https://accounts.google.com/gsi/client";
googleScript.className = "social-script-tag";
document.body.appendChild(googleScript);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
googleScript.onload = function (e) {
if (google) {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
google.accounts.id.initialize({
client_id: googleClientId,
});
google.accounts.id.revoke(socialId, (done) => {
console.log(done.error);
resolve(true);
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
};
} else {
resolve(true);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} else {
resolve(true);
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
});
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
return response;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
};
+103
View File
@@ -0,0 +1,103 @@
import parseClientCookies from "../utils/parseClientCookies";
/**
* Login with Google Function
* ===============================================================================
* @description This function uses google identity api to login a user with datasquirel
*/
export default async function logout(
params: { [s: string]: any } | null
): Promise<boolean> {
try {
const localUser = localStorage.getItem("user");
let targetUser;
try {
targetUser = JSON.parse(localUser || "");
} catch (error) {
console.log(error);
}
if (!targetUser) {
return false;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const cookies = parseClientCookies();
const socialId =
cookies?.datasquirel_social_id &&
typeof cookies.datasquirel_social_id == "string" &&
!cookies.datasquirel_social_id.match(/^null$/i)
? cookies.datasquirel_social_id
: null;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
localStorage.setItem("user", "{}");
localStorage.removeItem("csrf");
document.cookie = `datasquirel_social_id=null;samesite=strict;path=/`;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const response: boolean = await new Promise((resolve, reject) => {
if (socialId && !socialId?.match(/^null$/i)) {
const googleClientId = params?.googleClientId;
if (googleClientId) {
const googleScript = document.createElement("script");
googleScript.src = "https://accounts.google.com/gsi/client";
googleScript.className = "social-script-tag";
document.body.appendChild(googleScript);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
googleScript.onload = function (e) {
// @ts-ignore
const google = window.google;
if (google) {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
google.accounts.id.initialize({
client_id: googleClientId,
});
google.accounts.id.revoke(socialId, (done: any) => {
console.log(done.error);
resolve(true);
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
};
} else {
resolve(true);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} else {
resolve(true);
}
});
return response;
} catch (error) {
return false;
}
}