Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle drag and drop of a JSON file with missing properties #14

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions resources/drag-and-drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,35 @@ const FirebaseConfigDropArea = (function () {
}

#parseAndUpdateInputs(content) {
// Remove previous class - default style
this.privateKeyFile.removeClass("json-error").removeClass("json-properties-error");

try {
const data = JSON.parse(content);
const clientEmail = data["clientEmail"] || data["client_email"] || "";
const privateKey = data["privateKey"] || data["private_key"] || "";

if (!clientEmail || !privateKey) {
RED.notify(`JSON file: '${privateKey ? "Client Email" : "Private Key"}' property missing.`, "error");
this.privateKeyFile
.addClass("json-properties-error")
.html("<p>Please ensure your JSON file contains the <code>clientEmail</code> and <code>privateKey</code> properties.</p>");
return;
}

$("#node-config-input-clientEmail")
.data("data", data["clientEmail"] || data["client_email"])
.data("data", clientEmail)
.val("__PWRD__");
$("#node-config-input-privateKey")
.data("data", (data["privateKey"] || data["private_key"] || "").trim())
.data("data", privateKey.trim())
.val("__PWRD__");
$("#node-config-input-projectId").val(data["projectId"] || data["project_id"])
$("#node-config-input-projectId")
.val(data["projectId"] || data["project_id"])
.trigger("change"); // No need to trigger everything

this.privateKeyFile.html("The file has been loaded.");
} catch (error) {
this.privateKeyFile.html("An error has occurred :(");
this.privateKeyFile.addClass("json-error").html("An error has occurred :(");
alert(error.message ?? String(error));
}
}
Expand Down
12 changes: 11 additions & 1 deletion resources/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@

#json-drop-target {
cursor: pointer;
display: grid;
place-items: center;
margin: auto;
width: 100%;
height: 150px;
text-align: center;
line-height: 150px;
background-color: lightgrey;
color: darkgray;
border-color: darkgray;
Expand All @@ -57,6 +58,15 @@
box-shadow: 11px 10px 17px -12px rgba(0, 0, 0, 0.75);
}

#json-drop-target.json-error {
color: var(--red-ui-form-input-border-error-color) !important;
}

#json-drop-target.json-properties-error {
color: var(--red-ui-form-input-border-error-color) !important;
font-size: var(--red-ui-primary-font-size);
}

#file-drop-target {
position: absolute;
top: 0;
Expand Down
12 changes: 6 additions & 6 deletions src/lib/nodes/firebase-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,26 @@ export class FirebaseClient {

if (Object.keys(content).length === 0) {
const { credentials } = this.node;

// Get the projectId from URL (v < 0.6)
const projectId = credentials.url
?.split("https://")
.pop()
?.split(/-default-rtdb\.((asia-southeast1|europe-west1)\.firebasedatabase\.app|firebaseio\.com)(\/)?$/)[0];

// For line breaks issue
// For line breaks issue (v < 0.6)
const privateKey = JSON.stringify(credentials.privateKey)
?.replace(/\\\\n/gm, "\n")
.replaceAll('"', "")
.replaceAll("\\", "");

cred.clientEmail = credentials.clientEmail;

// The introduction of 'projectId' in the credentials also introduces the change from 'val()' to 'data("data")'
// which no longer needs to be stringify to solve the line breaks issue
cred.privateKey = privateKey.match("\n") === null ? credentials.privateKey : privateKey;
cred.privateKey = privateKey?.match("\n") === null ? credentials.privateKey : privateKey;
cred.projectId = credentials.projectId || projectId;

// For json input (deprecated)
cred.clientEmail = credentials.clientEmail;
} else {
// For json input (deprecated)
cred.clientEmail = content.clientEmail || content.client_email;
cred.privateKey = content.privateKey || content.private_key;
cred.projectId = content.projectId || content.project_id;
Expand Down