Changed how body params are defined to prevent build issues with internals

This commit is contained in:
Alexander Cerutti
2023-07-30 00:07:13 +02:00
parent fb6e6739b5
commit 2a4e21ac12

View File

@@ -19,32 +19,29 @@ admin
const storageRef = admin.storage().bucket(); const storageRef = admin.storage().bucket();
// Declaring our request protocol interface RequestWithBody extends functions.Request {
declare module "firebase-functions" { body: {
interface HttpRequest { passModel: string;
body: { serialNumber: string;
passModel: string; logoText: string;
serialNumber: string; textColor: string;
logoText: string; backgroundColor: string;
textColor: string; labelColor: string;
backgroundColor: string; relevantDate: string;
labelColor: string; expiryDate: string;
relevantDate: string; relevantLocationLat: number | "Blank";
expiryDate: string; relevantLocationLong: number | "Blank";
relevantLocationLat: number | "Blank"; header: { value: string; label: string }[];
relevantLocationLong: number | "Blank"; primary: { value: string; label: string }[];
header: { value: string; label: string }[]; secondary: { value: string; label: string }[];
primary: { value: string; label: string }[]; auxiliary: { value: string; label: string }[];
secondary: { value: string; label: string }[]; codeAlt: string;
auxiliary: { value: string; label: string }[]; qrText: string;
codeAlt: string; transitType: TransitType;
qrText: string; codeType: Barcode["format"];
transitType: TransitType; thumbnailFile: string;
codeType: Barcode["format"]; logoFile: string;
thumbnailFile: string; };
logoFile: string;
};
}
} }
// Declaring our .env contents // Declaring our .env contents
@@ -59,215 +56,217 @@ declare global {
} }
} }
export const pass = functions.https.onRequest(async (request, response) => { export const pass = functions.https.onRequest(
const newPass = await PKPass.from( async (request: RequestWithBody, response) => {
{ const newPass = await PKPass.from(
// Get relevant pass model from model folder (see passkit-generator/examples/models/) {
model: `./model/${request.body.passModel}.pass`, // Get relevant pass model from model folder (see passkit-generator/examples/models/)
certificates: { model: `./model/${request.body.passModel}.pass`,
// Assigning certificates from certs folder (you will need to provide these yourself) certificates: {
wwdr: process.env.WWDR, // Assigning certificates from certs folder (you will need to provide these yourself)
signerCert: process.env.SIGNER_CERT, wwdr: process.env.WWDR,
signerKey: process.env.SIGNER_KEY, signerCert: process.env.SIGNER_CERT,
signerKeyPassphrase: process.env.SIGNER_KEY_PASSPHRASE, signerKey: process.env.SIGNER_KEY,
signerKeyPassphrase: process.env.SIGNER_KEY_PASSPHRASE,
},
}, },
}, {
{ serialNumber: request.body.serialNumber,
serialNumber: request.body.serialNumber, description: "DESCRIPTION",
description: "DESCRIPTION", logoText: request.body.logoText,
logoText: request.body.logoText, foregroundColor: request.body.textColor,
foregroundColor: request.body.textColor, backgroundColor: request.body.backgroundColor,
backgroundColor: request.body.backgroundColor, labelColor: request.body.labelColor,
labelColor: request.body.labelColor, },
}, );
);
if (newPass.type == "boardingPass") { if (newPass.type == "boardingPass") {
newPass.transitType = request.body.transitType; newPass.transitType = request.body.transitType;
}
if (request.body.relevantDate !== "Blank") {
newPass.setRelevantDate(new Date(request.body.relevantDate));
}
if (request.body.expiryDate !== "Blank") {
newPass.setExpirationDate(new Date(request.body.expiryDate));
}
if (
request.body.relevantLocationLat !== "Blank" &&
request.body.relevantLocationLong !== "Blank"
) {
newPass.setLocations({
latitude: request.body.relevantLocationLat,
longitude: request.body.relevantLocationLong,
});
}
for (let i = 0; i < request.body.header.length; i++) {
const field = request.body.header[i];
if (!(field?.label && field.value)) {
continue;
} }
newPass.headerFields.push({ if (request.body.relevantDate !== "Blank") {
key: `header${i}`, newPass.setRelevantDate(new Date(request.body.relevantDate));
label: field.label,
value: field.value,
});
}
for (let i = 0; i < request.body.primary.length; i++) {
const field = request.body.primary[i];
if (!(field?.label && field.value)) {
continue;
} }
newPass.primaryFields.push({ if (request.body.expiryDate !== "Blank") {
key: `primary${i}`, newPass.setExpirationDate(new Date(request.body.expiryDate));
label: field.label,
value:
newPass.type == "boardingPass"
? field.value.toUpperCase()
: field.value,
});
}
for (let i = 0; i < request.body.secondary.length; i++) {
const field = request.body.secondary[i];
if (!(field?.label && field.value)) {
continue;
} }
const isElementInLastTwoPositions = if (
i === request.body.secondary.length - 2 || request.body.relevantLocationLat !== "Blank" &&
i === request.body.secondary.length - 1; request.body.relevantLocationLong !== "Blank"
) {
newPass.secondaryFields.push({ newPass.setLocations({
key: `secondary${i}`, latitude: request.body.relevantLocationLat,
label: field.label, longitude: request.body.relevantLocationLong,
value: field.value, });
textAlignment: isElementInLastTwoPositions
? "PKTextAlignmentRight"
: "PKTextAlignmentLeft",
});
}
for (let i = 0; i < request.body.auxiliary.length; i++) {
const field = request.body.auxiliary[i];
if (!(field?.label && field.value)) {
continue;
} }
const isElementInLastTwoPositions = for (let i = 0; i < request.body.header.length; i++) {
i === request.body.auxiliary.length - 2 || const field = request.body.header[i];
i === request.body.auxiliary.length - 1;
newPass.auxiliaryFields.push({ if (!(field?.label && field.value)) {
key: `auxiliary${i}`, continue;
label: field.label, }
value: field.value,
textAlignment: isElementInLastTwoPositions
? "PKTextAlignmentRight"
: "PKTextAlignmentLeft",
});
}
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") { newPass.headerFields.push({
newPass.setBarcodes({ key: `header${i}`,
message: request.body.qrText, label: field.label,
format: request.body.codeType, value: field.value,
messageEncoding: "iso-8859-1", });
}); }
} else {
newPass.setBarcodes({
message: request.body.qrText,
format: request.body.codeType,
messageEncoding: "iso-8859-1",
altText: request.body.codeAlt,
});
}
// Downloading thumbnail and logo files from Firebase Storage and adding to pass for (let i = 0; i < request.body.primary.length; i++) {
if (newPass.type == "generic" || newPass.type == "eventTicket") { const field = request.body.primary[i];
const thumbnailFile = request.body.thumbnailFile;
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
if (!(field?.label && field.value)) {
continue;
}
newPass.primaryFields.push({
key: `primary${i}`,
label: field.label,
value:
newPass.type == "boardingPass"
? field.value.toUpperCase()
: field.value,
});
}
for (let i = 0; i < request.body.secondary.length; i++) {
const field = request.body.secondary[i];
if (!(field?.label && field.value)) {
continue;
}
const isElementInLastTwoPositions =
i === request.body.secondary.length - 2 ||
i === request.body.secondary.length - 1;
newPass.secondaryFields.push({
key: `secondary${i}`,
label: field.label,
value: field.value,
textAlignment: isElementInLastTwoPositions
? "PKTextAlignmentRight"
: "PKTextAlignmentLeft",
});
}
for (let i = 0; i < request.body.auxiliary.length; i++) {
const field = request.body.auxiliary[i];
if (!(field?.label && field.value)) {
continue;
}
const isElementInLastTwoPositions =
i === request.body.auxiliary.length - 2 ||
i === request.body.auxiliary.length - 1;
newPass.auxiliaryFields.push({
key: `auxiliary${i}`,
label: field.label,
value: field.value,
textAlignment: isElementInLastTwoPositions
? "PKTextAlignmentRight"
: "PKTextAlignmentLeft",
});
}
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") {
newPass.setBarcodes({
message: request.body.qrText,
format: request.body.codeType,
messageEncoding: "iso-8859-1",
});
} else {
newPass.setBarcodes({
message: request.body.qrText,
format: request.body.codeType,
messageEncoding: "iso-8859-1",
altText: request.body.codeAlt,
});
}
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
if (newPass.type == "generic" || newPass.type == "eventTicket") {
const thumbnailFile = request.body.thumbnailFile;
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
try {
await storageRef
.file(`thumbnails/${thumbnailFile}`)
.download({ destination: tempPath1 });
} catch (error) {
console.error(error);
}
let buffer = Buffer.alloc(0);
try {
buffer = fs.readFileSync(tempPath1);
} catch (error) {
console.error(error);
}
newPass.addBuffer("thumbnail.png", buffer);
newPass.addBuffer("thumbnail@2x.png", buffer);
}
const logoFile = request.body.logoFile;
const tempPath2 = path.join(os.tmpdir(), logoFile);
try { try {
await storageRef await storageRef
.file(`thumbnails/${thumbnailFile}`) .file(`logos/${logoFile}`)
.download({ destination: tempPath1 }); .download({ destination: tempPath2 });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
let buffer = Buffer.alloc(0); let buffer = Buffer.alloc(0);
try { try {
buffer = fs.readFileSync(tempPath1); buffer = fs.readFileSync(tempPath2);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
newPass.addBuffer("thumbnail.png", buffer);
newPass.addBuffer("thumbnail@2x.png", buffer);
}
const logoFile = request.body.logoFile; newPass.addBuffer("logo.png", buffer);
const tempPath2 = path.join(os.tmpdir(), logoFile); newPass.addBuffer("logo@2x.png", buffer);
try {
await storageRef
.file(`logos/${logoFile}`)
.download({ destination: tempPath2 });
} catch (error) {
console.error(error);
}
let buffer = Buffer.alloc(0); const bufferData = newPass.getAsBuffer();
try { try {
buffer = fs.readFileSync(tempPath2); console.log("Pass was uploaded successfully.");
} catch (error) {
console.error(error);
}
newPass.addBuffer("logo.png", buffer); response.set("Content-Type", newPass.mimeType);
newPass.addBuffer("logo@2x.png", buffer); response.status(200).send(bufferData);
const bufferData = newPass.getAsBuffer(); // Delete thumbnail file in Firebase Storage
try { storageRef
console.log("Pass was uploaded successfully."); .file(`thumbnails/${request.body.thumbnailFile}`)
.delete()
.then(() => {
console.log("Thumbnail file deleted successfully");
})
.catch((error) => {
console.error(error);
});
response.set("Content-Type", newPass.mimeType); // Delete logo file in Firebase Storage
response.status(200).send(bufferData); storageRef
.file(`logos/${logoFile}`)
.delete()
.then(() => {
console.log("Logo file deleted successfully");
})
.catch((error) => {
console.error(error);
});
} catch (error) {
console.log("Error Uploading pass " + error);
// Delete thumbnail file in Firebase Storage response.send({
storageRef explanation: JSON.stringify(error),
.file(`thumbnails/${request.body.thumbnailFile}`) result: "FAILED",
.delete()
.then(() => {
console.log("Thumbnail file deleted successfully");
})
.catch((error) => {
console.error(error);
}); });
}
// Delete logo file in Firebase Storage },
storageRef );
.file(`logos/${logoFile}`)
.delete()
.then(() => {
console.log("Logo file deleted successfully");
})
.catch((error) => {
console.error(error);
});
} catch (error) {
console.log("Error Uploading pass " + error);
response.send({
explanation: JSON.stringify(error),
result: "FAILED",
});
}
});