updates
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
// @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);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
};
|
||||
// @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);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
};
|
||||
|
||||
@@ -1,113 +1,113 @@
|
||||
/**
|
||||
* Type Definitions
|
||||
* ===============================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} GoogleIdentityPromptNotification
|
||||
* @property {function(): string} getMomentType - Notification moment type
|
||||
* @property {function(): string} getDismissedReason - Notification get Dismissed Reason
|
||||
* @property {function(): string} getNotDisplayedReason - Notification get Not Displayed Reason
|
||||
* @property {function(): string} getSkippedReason - Notification get Skipped Reason
|
||||
* @property {function(): boolean} isDismissedMoment - Notification is Dismissed Moment
|
||||
* @property {function(): boolean} isDisplayMoment - Notification is Display Moment
|
||||
* @property {function(): boolean} isDisplayed - Notification is Displayed
|
||||
* @property {function(): boolean} isNotDisplayed - Notification is Not Displayed
|
||||
* @property {function(): boolean} isSkippedMoment - Notification is Skipped Moment
|
||||
*/
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Login with Google Function
|
||||
* ===============================================================================
|
||||
* @description This function uses google identity api to login a user with datasquirel
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single object passed
|
||||
* @param {string} params.clientId - Google app client ID: {@link https://datasquirel.com/docs}
|
||||
* @param {HTMLElement} params.element - HTML Element to display google login button
|
||||
* @param {boolean} params.triggerPrompt - Whether to trigger Google signing popup or not: {@link https://datasquirel.com/docs}
|
||||
* @param {function(): void} [params.readyStateDispatch] - React setState Function: sets whether the google login button is ready or not
|
||||
*
|
||||
* @returns {Promise<boolean>} - Return
|
||||
*/
|
||||
module.exports = async function getAccessToken({ clientId, element, triggerPrompt, readyStateDispatch }) {
|
||||
/**
|
||||
* == Initialize
|
||||
*
|
||||
* @description Initialize
|
||||
*/
|
||||
const googleScript = document.createElement("script");
|
||||
googleScript.src = "https://accounts.google.com/gsi/client";
|
||||
googleScript.className = "social-script-tag";
|
||||
|
||||
document.body.appendChild(googleScript);
|
||||
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
googleScript.onload = function (e) {
|
||||
if (google) {
|
||||
if (readyStateDispatch) readyStateDispatch(true);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (element) {
|
||||
/**
|
||||
* Handle google credentials response
|
||||
* ========================================================
|
||||
* @param {object} response - Google response with credentials
|
||||
* @param {string} response.credential - Google access token
|
||||
*/
|
||||
function handleCredentialResponse(response) {
|
||||
resolve(response.credential);
|
||||
}
|
||||
|
||||
google.accounts.id.initialize({
|
||||
client_id: clientId,
|
||||
callback: handleCredentialResponse,
|
||||
});
|
||||
|
||||
google.accounts.id.renderButton(element, {
|
||||
theme: "outline",
|
||||
size: "large",
|
||||
logo_alignment: "center",
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (triggerPrompt) {
|
||||
google.accounts.id.prompt(
|
||||
/**
|
||||
* Google prompt notification callback
|
||||
* ========================================================
|
||||
* @param {GoogleIdentityPromptNotification} notification - Notification object
|
||||
*/
|
||||
(notification) => {
|
||||
notification.isDisplayed();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
return response;
|
||||
};
|
||||
/**
|
||||
* Type Definitions
|
||||
* ===============================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} GoogleIdentityPromptNotification
|
||||
* @property {function(): string} getMomentType - Notification moment type
|
||||
* @property {function(): string} getDismissedReason - Notification get Dismissed Reason
|
||||
* @property {function(): string} getNotDisplayedReason - Notification get Not Displayed Reason
|
||||
* @property {function(): string} getSkippedReason - Notification get Skipped Reason
|
||||
* @property {function(): boolean} isDismissedMoment - Notification is Dismissed Moment
|
||||
* @property {function(): boolean} isDisplayMoment - Notification is Display Moment
|
||||
* @property {function(): boolean} isDisplayed - Notification is Displayed
|
||||
* @property {function(): boolean} isNotDisplayed - Notification is Not Displayed
|
||||
* @property {function(): boolean} isSkippedMoment - Notification is Skipped Moment
|
||||
*/
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Login with Google Function
|
||||
* ===============================================================================
|
||||
* @description This function uses google identity api to login a user with datasquirel
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single object passed
|
||||
* @param {string} params.clientId - Google app client ID: {@link https://datasquirel.com/docs}
|
||||
* @param {HTMLElement} params.element - HTML Element to display google login button
|
||||
* @param {boolean} params.triggerPrompt - Whether to trigger Google signing popup or not: {@link https://datasquirel.com/docs}
|
||||
* @param {function(): void} [params.readyStateDispatch] - React setState Function: sets whether the google login button is ready or not
|
||||
*
|
||||
* @returns {Promise<boolean>} - Return
|
||||
*/
|
||||
module.exports = async function getAccessToken({ clientId, element, triggerPrompt, readyStateDispatch }) {
|
||||
/**
|
||||
* == Initialize
|
||||
*
|
||||
* @description Initialize
|
||||
*/
|
||||
const googleScript = document.createElement("script");
|
||||
googleScript.src = "https://accounts.google.com/gsi/client";
|
||||
googleScript.className = "social-script-tag";
|
||||
|
||||
document.body.appendChild(googleScript);
|
||||
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
googleScript.onload = function (e) {
|
||||
if (google) {
|
||||
if (readyStateDispatch) readyStateDispatch(true);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (element) {
|
||||
/**
|
||||
* Handle google credentials response
|
||||
* ========================================================
|
||||
* @param {object} response - Google response with credentials
|
||||
* @param {string} response.credential - Google access token
|
||||
*/
|
||||
function handleCredentialResponse(response) {
|
||||
resolve(response.credential);
|
||||
}
|
||||
|
||||
google.accounts.id.initialize({
|
||||
client_id: clientId,
|
||||
callback: handleCredentialResponse,
|
||||
});
|
||||
|
||||
google.accounts.id.renderButton(element, {
|
||||
theme: "outline",
|
||||
size: "large",
|
||||
logo_alignment: "center",
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (triggerPrompt) {
|
||||
google.accounts.id.prompt(
|
||||
/**
|
||||
* Google prompt notification callback
|
||||
* ========================================================
|
||||
* @param {GoogleIdentityPromptNotification} notification - Notification object
|
||||
*/
|
||||
(notification) => {
|
||||
notification.isDisplayed();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
+146
-146
@@ -1,146 +1,146 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
};
|
||||
/**
|
||||
* 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;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
};
|
||||
|
||||
+47
-47
@@ -1,47 +1,47 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Imports
|
||||
*/
|
||||
const imageInputFileToBase64 = require("./media/imageInputFileToBase64");
|
||||
const imageInputToBase64 = require("./media/imageInputToBase64");
|
||||
const inputFileToBase64 = require("./media/inputFileToBase64");
|
||||
const getAccessToken = require("./auth/google/getAccessToken");
|
||||
const getGithubAccessToken = require("./auth/github/getAccessToken");
|
||||
const logout = require("./auth/logout");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Media Functions Object
|
||||
*/
|
||||
const media = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
inputFileToBase64: inputFileToBase64,
|
||||
};
|
||||
|
||||
/**
|
||||
* User Auth Object
|
||||
*/
|
||||
const auth = {
|
||||
google: {
|
||||
getAccessToken: getAccessToken,
|
||||
},
|
||||
github: {
|
||||
getAccessToken: getGithubAccessToken,
|
||||
},
|
||||
logout: logout,
|
||||
};
|
||||
|
||||
/**
|
||||
* Main Export
|
||||
*/
|
||||
const datasquirelClient = {
|
||||
media: media,
|
||||
auth: auth,
|
||||
};
|
||||
|
||||
module.exports = datasquirelClient;
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Imports
|
||||
*/
|
||||
const imageInputFileToBase64 = require("./media/imageInputFileToBase64");
|
||||
const imageInputToBase64 = require("./media/imageInputToBase64");
|
||||
const inputFileToBase64 = require("./media/inputFileToBase64");
|
||||
const getAccessToken = require("./auth/google/getAccessToken");
|
||||
const getGithubAccessToken = require("./auth/github/getAccessToken");
|
||||
const logout = require("./auth/logout");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Media Functions Object
|
||||
*/
|
||||
const media = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
inputFileToBase64: inputFileToBase64,
|
||||
};
|
||||
|
||||
/**
|
||||
* User Auth Object
|
||||
*/
|
||||
const auth = {
|
||||
google: {
|
||||
getAccessToken: getAccessToken,
|
||||
},
|
||||
github: {
|
||||
getAccessToken: getGithubAccessToken,
|
||||
},
|
||||
logout: logout,
|
||||
};
|
||||
|
||||
/**
|
||||
* Main Export
|
||||
*/
|
||||
const datasquirelClient = {
|
||||
media: media,
|
||||
auth: auth,
|
||||
};
|
||||
|
||||
module.exports = datasquirelClient;
|
||||
|
||||
+49
-49
@@ -1,49 +1,49 @@
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
import imageInputFileToBase64 from "./imageInputFileToBase64";
|
||||
import imageInputToBase64 from "./imageInputToBase64";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Media Functions Object
|
||||
* ==============================================================================
|
||||
*/
|
||||
const media = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
};
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Media Functions Object
|
||||
* ==============================================================================
|
||||
*/
|
||||
const auth = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
};
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Export
|
||||
* ==============================================================================
|
||||
*/
|
||||
const datasquirelClient = {
|
||||
media: media,
|
||||
};
|
||||
|
||||
export default datasquirelClient;
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
import imageInputFileToBase64 from "./imageInputFileToBase64";
|
||||
import imageInputToBase64 from "./imageInputToBase64";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Media Functions Object
|
||||
* ==============================================================================
|
||||
*/
|
||||
const media = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
};
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Media Functions Object
|
||||
* ==============================================================================
|
||||
*/
|
||||
const auth = {
|
||||
imageInputToBase64: imageInputToBase64,
|
||||
imageInputFileToBase64: imageInputFileToBase64,
|
||||
};
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Export
|
||||
* ==============================================================================
|
||||
*/
|
||||
const datasquirelClient = {
|
||||
media: media,
|
||||
};
|
||||
|
||||
export default datasquirelClient;
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
@@ -1,111 +1,111 @@
|
||||
/**
|
||||
* @typedef {{
|
||||
* imageBase64: string,
|
||||
* imageBase64Full: string,
|
||||
* imageName: string,
|
||||
* imageSize: number,
|
||||
* }} FunctionReturn
|
||||
*/
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {{
|
||||
* imageInputFile: { name:string },
|
||||
* maxWidth: number,
|
||||
* imagePreviewNode: HTMLImageElement,
|
||||
* }} params - Single object passed
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> } - Return Object
|
||||
*/
|
||||
module.exports = async function imageInputFileToBase64({ imageInputFile, maxWidth, imagePreviewNode }) {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imageName = imageInputFile.name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
let imageSize;
|
||||
let canvas = document.createElement("canvas");
|
||||
|
||||
const MIME_TYPE = imageInputFile.type;
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
|
||||
const file = imageInputFile; // get the file
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const img = new Image();
|
||||
|
||||
/** ********************* Add source to new image */
|
||||
img.src = blobURL;
|
||||
|
||||
imageDataBase64 = await new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
console.log("Cannot load image");
|
||||
};
|
||||
|
||||
/** ********************* Handle new image when loaded */
|
||||
img.onload = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
|
||||
canvas.width = img.naturalWidth < MAX_WIDTH ? img.naturalWidth : MAX_WIDTH;
|
||||
canvas.height = img.naturalWidth < MAX_WIDTH ? img.naturalHeight : img.naturalHeight * scaleSize;
|
||||
} else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
|
||||
if (imagePreviewNode) {
|
||||
imagePreviewNode.src = srcEncoded;
|
||||
}
|
||||
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
|
||||
imageSize = await new Promise((res, rej) => {
|
||||
canvas.toBlob(
|
||||
(blob) => {
|
||||
res(blob.size);
|
||||
},
|
||||
MIME_TYPE,
|
||||
QUALITY
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
imageBase64: imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
imageSize: imageSize,
|
||||
};
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
|
||||
return {
|
||||
imageBase64: null,
|
||||
imageBase64Full: null,
|
||||
imageName: null,
|
||||
imageSize: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/**
|
||||
* @typedef {{
|
||||
* imageBase64: string,
|
||||
* imageBase64Full: string,
|
||||
* imageName: string,
|
||||
* imageSize: number,
|
||||
* }} FunctionReturn
|
||||
*/
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {{
|
||||
* imageInputFile: { name:string },
|
||||
* maxWidth: number,
|
||||
* imagePreviewNode: HTMLImageElement,
|
||||
* }} params - Single object passed
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> } - Return Object
|
||||
*/
|
||||
module.exports = async function imageInputFileToBase64({ imageInputFile, maxWidth, imagePreviewNode }) {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imageName = imageInputFile.name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
let imageSize;
|
||||
let canvas = document.createElement("canvas");
|
||||
|
||||
const MIME_TYPE = imageInputFile.type;
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
|
||||
const file = imageInputFile; // get the file
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const img = new Image();
|
||||
|
||||
/** ********************* Add source to new image */
|
||||
img.src = blobURL;
|
||||
|
||||
imageDataBase64 = await new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
console.log("Cannot load image");
|
||||
};
|
||||
|
||||
/** ********************* Handle new image when loaded */
|
||||
img.onload = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
|
||||
canvas.width = img.naturalWidth < MAX_WIDTH ? img.naturalWidth : MAX_WIDTH;
|
||||
canvas.height = img.naturalWidth < MAX_WIDTH ? img.naturalHeight : img.naturalHeight * scaleSize;
|
||||
} else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
|
||||
if (imagePreviewNode) {
|
||||
imagePreviewNode.src = srcEncoded;
|
||||
}
|
||||
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
|
||||
imageSize = await new Promise((res, rej) => {
|
||||
canvas.toBlob(
|
||||
(blob) => {
|
||||
res(blob.size);
|
||||
},
|
||||
MIME_TYPE,
|
||||
QUALITY
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
imageBase64: imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
imageSize: imageSize,
|
||||
};
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
|
||||
return {
|
||||
imageBase64: null,
|
||||
imageBase64Full: null,
|
||||
imageName: null,
|
||||
imageSize: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
+101
-101
@@ -1,101 +1,101 @@
|
||||
/**
|
||||
* @typedef {{
|
||||
* imageBase64: string,
|
||||
* imageBase64Full: string,
|
||||
* imageName: string,
|
||||
* }} FunctionReturn
|
||||
*/
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {{
|
||||
* imageInput: HTMLInputElement,
|
||||
* maxWidth: number,
|
||||
* mimeType: [string='image/jpeg']
|
||||
* }} params - Single object passed
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> } - Return Object
|
||||
*/
|
||||
module.exports = async function imageInputToBase64({ imageInput, maxWidth, mimeType }) {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imagePreviewNode = document.querySelector(`[data-imagepreview='image']`);
|
||||
let imageName = imageInput.files[0].name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
|
||||
const MIME_TYPE = mimeType ? mimeType : "image/jpeg";
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
|
||||
const file = imageInput.files[0]; // get the file
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const img = new Image();
|
||||
|
||||
/** ********************* Add source to new image */
|
||||
img.src = blobURL;
|
||||
|
||||
imageDataBase64 = await new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
window.alert("Cannot load image!");
|
||||
};
|
||||
|
||||
/** ********************* Handle new image when loaded */
|
||||
img.onload = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
|
||||
canvas.width = img.naturalWidth < MAX_WIDTH ? img.naturalWidth : MAX_WIDTH;
|
||||
canvas.height = img.naturalWidth < MAX_WIDTH ? img.naturalHeight : img.naturalHeight * scaleSize;
|
||||
} else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
|
||||
if (imagePreviewNode) {
|
||||
document.querySelectorAll(`[data-imagepreview='image']`).forEach((img) => {
|
||||
img.src = srcEncoded;
|
||||
});
|
||||
}
|
||||
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
imageBase64: imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
};
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
|
||||
return {
|
||||
imageBase64: null,
|
||||
imageBase64Full: null,
|
||||
imageName: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/**
|
||||
* @typedef {{
|
||||
* imageBase64: string,
|
||||
* imageBase64Full: string,
|
||||
* imageName: string,
|
||||
* }} FunctionReturn
|
||||
*/
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {{
|
||||
* imageInput: HTMLInputElement,
|
||||
* maxWidth: number,
|
||||
* mimeType: [string='image/jpeg']
|
||||
* }} params - Single object passed
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> } - Return Object
|
||||
*/
|
||||
module.exports = async function imageInputToBase64({ imageInput, maxWidth, mimeType }) {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
try {
|
||||
let imagePreviewNode = document.querySelector(`[data-imagepreview='image']`);
|
||||
let imageName = imageInput.files[0].name.replace(/\..*/, "");
|
||||
let imageDataBase64;
|
||||
|
||||
const MIME_TYPE = mimeType ? mimeType : "image/jpeg";
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
|
||||
const file = imageInput.files[0]; // get the file
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const img = new Image();
|
||||
|
||||
/** ********************* Add source to new image */
|
||||
img.src = blobURL;
|
||||
|
||||
imageDataBase64 = await new Promise((res, rej) => {
|
||||
/** ********************* Handle Errors in loading image */
|
||||
img.onerror = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
window.alert("Cannot load image!");
|
||||
};
|
||||
|
||||
/** ********************* Handle new image when loaded */
|
||||
img.onload = function () {
|
||||
URL.revokeObjectURL(this.src);
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
|
||||
if (MAX_WIDTH) {
|
||||
const scaleSize = MAX_WIDTH / img.naturalWidth;
|
||||
|
||||
canvas.width = img.naturalWidth < MAX_WIDTH ? img.naturalWidth : MAX_WIDTH;
|
||||
canvas.height = img.naturalWidth < MAX_WIDTH ? img.naturalHeight : img.naturalHeight * scaleSize;
|
||||
} else {
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
|
||||
|
||||
if (imagePreviewNode) {
|
||||
document.querySelectorAll(`[data-imagepreview='image']`).forEach((img) => {
|
||||
img.src = srcEncoded;
|
||||
});
|
||||
}
|
||||
|
||||
res(srcEncoded);
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
imageBase64: imageDataBase64.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
};
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
|
||||
return {
|
||||
imageBase64: null,
|
||||
imageBase64Full: null,
|
||||
imageName: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
@@ -1,96 +1,96 @@
|
||||
/**
|
||||
* @typedef {{
|
||||
* fileBase64: string,
|
||||
* fileBase64Full: string,
|
||||
* fileName: string,
|
||||
* fileSize: number,
|
||||
* fileType: string,
|
||||
* }} FunctionReturn
|
||||
*/
|
||||
|
||||
/**
|
||||
* Input File to base64
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description This function takes in a *SINGLE* input file from a HTML file input element.
|
||||
* HTML file input elements usually return an array of input objects, so be sure to select the target
|
||||
* file from the array.
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single object passed
|
||||
* @param {object} params.inputFile - HTML input File
|
||||
* @param {string} params.inputFile.name - Input File Name
|
||||
* @param {number} params.inputFile.size - Input File Size in bytes
|
||||
* @param {string} params.inputFile.type - Input File Type: "JPEG", "PNG", "PDF", etc. Whichever allowed regexp is provided
|
||||
* @param {RegExp} [params.allowedRegex] - Regexp containing the allowed file types
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> } - Return Object
|
||||
*/
|
||||
module.exports = async function inputFileToBase64({ inputFile, allowedRegex }) {
|
||||
/**
|
||||
* == Initialize
|
||||
*
|
||||
* @description Initialize
|
||||
*/
|
||||
const allowedTypesRegex = allowedRegex ? allowedRegex : /image\/*|\/pdf/;
|
||||
|
||||
if (!inputFile?.type?.match(allowedTypesRegex)) {
|
||||
window.alert(`We currently don't support ${inputFile.type} file types. Support is coming soon. For now we support only images and PDFs.`);
|
||||
|
||||
return {
|
||||
fileBase64: null,
|
||||
fileBase64Full: null,
|
||||
fileName: inputFile.name,
|
||||
fileSize: null,
|
||||
fileType: null,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
/** Process File **/
|
||||
let fileName = inputFile.name.replace(/\..*/, "");
|
||||
|
||||
/** Add source to new file **/
|
||||
const fileData = await new Promise((resolve, reject) => {
|
||||
var reader = new FileReader();
|
||||
reader.readAsDataURL(inputFile);
|
||||
reader.onload = function () {
|
||||
resolve(reader.result);
|
||||
};
|
||||
reader.onerror = function (/** @type {*} */ error) {
|
||||
console.log("Error: ", error.message);
|
||||
};
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return {
|
||||
fileBase64: fileData.replace(/.*?base64,/, ""),
|
||||
fileBase64Full: fileData,
|
||||
fileName: fileName,
|
||||
fileSize: inputFile.size,
|
||||
fileType: inputFile.type,
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("File Processing Error! =>", error.message);
|
||||
|
||||
return {
|
||||
fileBase64: null,
|
||||
fileBase64Full: null,
|
||||
fileName: inputFile.name,
|
||||
fileSize: null,
|
||||
fileType: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
/**
|
||||
* @typedef {{
|
||||
* fileBase64: string,
|
||||
* fileBase64Full: string,
|
||||
* fileName: string,
|
||||
* fileSize: number,
|
||||
* fileType: string,
|
||||
* }} FunctionReturn
|
||||
*/
|
||||
|
||||
/**
|
||||
* Input File to base64
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description This function takes in a *SINGLE* input file from a HTML file input element.
|
||||
* HTML file input elements usually return an array of input objects, so be sure to select the target
|
||||
* file from the array.
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - Single object passed
|
||||
* @param {object} params.inputFile - HTML input File
|
||||
* @param {string} params.inputFile.name - Input File Name
|
||||
* @param {number} params.inputFile.size - Input File Size in bytes
|
||||
* @param {string} params.inputFile.type - Input File Type: "JPEG", "PNG", "PDF", etc. Whichever allowed regexp is provided
|
||||
* @param {RegExp} [params.allowedRegex] - Regexp containing the allowed file types
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> } - Return Object
|
||||
*/
|
||||
module.exports = async function inputFileToBase64({ inputFile, allowedRegex }) {
|
||||
/**
|
||||
* == Initialize
|
||||
*
|
||||
* @description Initialize
|
||||
*/
|
||||
const allowedTypesRegex = allowedRegex ? allowedRegex : /image\/*|\/pdf/;
|
||||
|
||||
if (!inputFile?.type?.match(allowedTypesRegex)) {
|
||||
window.alert(`We currently don't support ${inputFile.type} file types. Support is coming soon. For now we support only images and PDFs.`);
|
||||
|
||||
return {
|
||||
fileBase64: null,
|
||||
fileBase64Full: null,
|
||||
fileName: inputFile.name,
|
||||
fileSize: null,
|
||||
fileType: null,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
/** Process File **/
|
||||
let fileName = inputFile.name.replace(/\..*/, "");
|
||||
|
||||
/** Add source to new file **/
|
||||
const fileData = await new Promise((resolve, reject) => {
|
||||
var reader = new FileReader();
|
||||
reader.readAsDataURL(inputFile);
|
||||
reader.onload = function () {
|
||||
resolve(reader.result);
|
||||
};
|
||||
reader.onerror = function (/** @type {*} */ error) {
|
||||
console.log("Error: ", error.message);
|
||||
};
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return {
|
||||
fileBase64: fileData.replace(/.*?base64,/, ""),
|
||||
fileBase64Full: fileData,
|
||||
fileName: fileName,
|
||||
fileSize: inputFile.size,
|
||||
fileType: inputFile.type,
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("File Processing Error! =>", error.message);
|
||||
|
||||
return {
|
||||
fileBase64: null,
|
||||
fileBase64Full: null,
|
||||
fileName: inputFile.name,
|
||||
fileSize: null,
|
||||
fileType: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
@@ -1,65 +1,65 @@
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse request cookies
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description This function takes in a request object and returns the cookies as a JS object
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {object} params.request - HTTPS request object
|
||||
*
|
||||
* @returns {{}|null}
|
||||
*/
|
||||
module.exports = function () {
|
||||
/**
|
||||
* Check inputs
|
||||
*
|
||||
* @description Check inputs
|
||||
*/
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** @type {string|null} */
|
||||
const cookieString = document.cookie;
|
||||
|
||||
if (!cookieString || typeof cookieString !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @type {string[]} */
|
||||
const cookieSplitArray = cookieString.split(";");
|
||||
|
||||
let cookieObject = {};
|
||||
|
||||
cookieSplitArray.forEach((keyValueString) => {
|
||||
const [key, value] = keyValueString.split("=");
|
||||
if (key && typeof key == "string") {
|
||||
cookieObject[key.replace(/^ +| +$/, "")] = value && typeof value == "string" ? value.replace(/^ +| +$/, "") : null;
|
||||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
|
||||
return cookieObject;
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse request cookies
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description This function takes in a request object and returns the cookies as a JS object
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {object} params.request - HTTPS request object
|
||||
*
|
||||
* @returns {{}|null}
|
||||
*/
|
||||
module.exports = function () {
|
||||
/**
|
||||
* Check inputs
|
||||
*
|
||||
* @description Check inputs
|
||||
*/
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** @type {string|null} */
|
||||
const cookieString = document.cookie;
|
||||
|
||||
if (!cookieString || typeof cookieString !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @type {string[]} */
|
||||
const cookieSplitArray = cookieString.split(";");
|
||||
|
||||
let cookieObject = {};
|
||||
|
||||
cookieSplitArray.forEach((keyValueString) => {
|
||||
const [key, value] = keyValueString.split("=");
|
||||
if (key && typeof key == "string") {
|
||||
cookieObject[key.replace(/^ +| +$/, "")] = value && typeof value == "string" ? value.replace(/^ +| +$/, "") : null;
|
||||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
|
||||
return cookieObject;
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user