mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 13:25:19 +00:00
Moved everything inside the try-catch and added explicit error code
This commit is contained in:
@@ -58,229 +58,228 @@ declare global {
|
|||||||
|
|
||||||
export const pass = functions.https.onRequest(
|
export const pass = functions.https.onRequest(
|
||||||
async (request: RequestWithBody, response) => {
|
async (request: RequestWithBody, response) => {
|
||||||
if (request.headers["content-type"] !== "application/json") {
|
try {
|
||||||
response.status(400);
|
if (request.headers["content-type"] !== "application/json") {
|
||||||
response.send({
|
|
||||||
error: `Payload with content-type ${request.headers["content-type"]} is not supported. Use "application/json"`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!request.body.passModel) {
|
|
||||||
response.status(400);
|
|
||||||
response.send({
|
|
||||||
error: "Unspecified 'passModel' parameter: which model should be used?",
|
|
||||||
});
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.body.passModel.endsWith(".pass")) {
|
|
||||||
request.body.passModel = request.body.passModel.replace(
|
|
||||||
".pass",
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const newPass = await PKPass.from(
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
|
||||||
* Path seems to get read like the function is in "firebase/" folder and not in "firebase/functions/"
|
|
||||||
*/
|
|
||||||
model: `../../models/${request.body.passModel}.pass`,
|
|
||||||
certificates: {
|
|
||||||
// Assigning certificates from certs folder (you will need to provide these yourself)
|
|
||||||
wwdr: process.env.WWDR,
|
|
||||||
signerCert: process.env.SIGNER_CERT,
|
|
||||||
signerKey: process.env.SIGNER_KEY,
|
|
||||||
signerKeyPassphrase: process.env.SIGNER_KEY_PASSPHRASE,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
serialNumber: request.body.serialNumber,
|
|
||||||
description: "DESCRIPTION",
|
|
||||||
logoText: request.body.logoText,
|
|
||||||
foregroundColor: request.body.textColor,
|
|
||||||
backgroundColor: request.body.backgroundColor,
|
|
||||||
labelColor: request.body.labelColor,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (newPass.type == "boardingPass") {
|
|
||||||
if (!request.body.transitType) {
|
|
||||||
response.status(400);
|
response.status(400);
|
||||||
response.send({
|
response.send({
|
||||||
error: "transitType is required",
|
error: `Payload with content-type ${request.headers["content-type"]} is not supported. Use "application/json"`,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!request.body.passModel) {
|
||||||
|
response.status(400);
|
||||||
|
response.send({
|
||||||
|
error: "Unspecified 'passModel' parameter: which model should be used?",
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
newPass.transitType = request.body.transitType;
|
if (request.body.passModel.endsWith(".pass")) {
|
||||||
}
|
request.body.passModel = request.body.passModel.replace(
|
||||||
|
".pass",
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof request.body.relevantDate === "string") {
|
const newPass = await PKPass.from(
|
||||||
newPass.setRelevantDate(new Date(request.body.relevantDate));
|
{
|
||||||
}
|
/**
|
||||||
|
* Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
||||||
|
* Path seems to get read like the function is in "firebase/" folder and not in "firebase/functions/"
|
||||||
|
*/
|
||||||
|
model: `../../models/${request.body.passModel}.pass`,
|
||||||
|
certificates: {
|
||||||
|
// Assigning certificates from certs folder (you will need to provide these yourself)
|
||||||
|
wwdr: process.env.WWDR,
|
||||||
|
signerCert: process.env.SIGNER_CERT,
|
||||||
|
signerKey: process.env.SIGNER_KEY,
|
||||||
|
signerKeyPassphrase: process.env.SIGNER_KEY_PASSPHRASE,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
serialNumber: request.body.serialNumber,
|
||||||
|
description: "DESCRIPTION",
|
||||||
|
logoText: request.body.logoText,
|
||||||
|
foregroundColor: request.body.textColor,
|
||||||
|
backgroundColor: request.body.backgroundColor,
|
||||||
|
labelColor: request.body.labelColor,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if (typeof request.body.expiryDate === "string") {
|
if (newPass.type == "boardingPass") {
|
||||||
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
if (!request.body.transitType) {
|
||||||
}
|
response.status(400);
|
||||||
|
response.send({
|
||||||
|
error: "transitType is required",
|
||||||
|
});
|
||||||
|
|
||||||
if (
|
return;
|
||||||
request.body.relevantLocationLat &&
|
|
||||||
request.body.relevantLocationLong
|
|
||||||
) {
|
|
||||||
newPass.setLocations({
|
|
||||||
latitude: request.body.relevantLocationLat,
|
|
||||||
longitude: request.body.relevantLocationLong,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(request.body.header)) {
|
|
||||||
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({
|
newPass.transitType = request.body.transitType;
|
||||||
key: `header${i}`,
|
}
|
||||||
label: field.label,
|
|
||||||
value: field.value,
|
if (typeof request.body.relevantDate === "string") {
|
||||||
|
newPass.setRelevantDate(new Date(request.body.relevantDate));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof request.body.expiryDate === "string") {
|
||||||
|
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
request.body.relevantLocationLat &&
|
||||||
|
request.body.relevantLocationLong
|
||||||
|
) {
|
||||||
|
newPass.setLocations({
|
||||||
|
latitude: request.body.relevantLocationLat,
|
||||||
|
longitude: request.body.relevantLocationLong,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(request.body.primary)) {
|
if (Array.isArray(request.body.header)) {
|
||||||
for (let i = 0; i < request.body.primary.length; i++) {
|
for (let i = 0; i < request.body.header.length; i++) {
|
||||||
const field = request.body.primary[i];
|
const field = request.body.header[i];
|
||||||
|
|
||||||
if (!(field?.label && field.value)) {
|
if (!(field?.label && field.value)) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
newPass.headerFields.push({
|
||||||
|
key: `header${i}`,
|
||||||
|
label: field.label,
|
||||||
|
value: field.value,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
newPass.primaryFields.push({
|
|
||||||
key: `primary${i}`,
|
|
||||||
label: field.label,
|
|
||||||
value:
|
|
||||||
newPass.type == "boardingPass"
|
|
||||||
? field.value.toUpperCase()
|
|
||||||
: field.value,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(request.body.secondary)) {
|
if (Array.isArray(request.body.primary)) {
|
||||||
for (let i = 0; i < request.body.secondary.length; i++) {
|
for (let i = 0; i < request.body.primary.length; i++) {
|
||||||
const field = request.body.secondary[i];
|
const field = request.body.primary[i];
|
||||||
|
|
||||||
if (!(field?.label && field.value)) {
|
if (!(field?.label && field.value)) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
newPass.primaryFields.push({
|
||||||
|
key: `primary${i}`,
|
||||||
|
label: field.label,
|
||||||
|
value:
|
||||||
|
newPass.type == "boardingPass"
|
||||||
|
? field.value.toUpperCase()
|
||||||
|
: field.value,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(request.body.auxiliary)) {
|
if (Array.isArray(request.body.secondary)) {
|
||||||
for (let i = 0; i < request.body.auxiliary.length; i++) {
|
for (let i = 0; i < request.body.secondary.length; i++) {
|
||||||
const field = request.body.auxiliary[i];
|
const field = request.body.secondary[i];
|
||||||
|
|
||||||
if (!(field?.label && field.value)) {
|
if (!(field?.label && field.value)) {
|
||||||
continue;
|
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",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const isElementInLastTwoPositions =
|
if (Array.isArray(request.body.auxiliary)) {
|
||||||
i === request.body.auxiliary.length - 2 ||
|
for (let i = 0; i < request.body.auxiliary.length; i++) {
|
||||||
i === request.body.auxiliary.length - 1;
|
const field = request.body.auxiliary[i];
|
||||||
|
|
||||||
newPass.auxiliaryFields.push({
|
if (!(field?.label && field.value)) {
|
||||||
key: `auxiliary${i}`,
|
continue;
|
||||||
label: field.label,
|
}
|
||||||
value: field.value,
|
|
||||||
textAlignment: isElementInLastTwoPositions
|
const isElementInLastTwoPositions =
|
||||||
? "PKTextAlignmentRight"
|
i === request.body.auxiliary.length - 2 ||
|
||||||
: "PKTextAlignmentLeft",
|
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.qrText && request.body.codeType) {
|
||||||
|
newPass.setBarcodes({
|
||||||
|
message: request.body.qrText,
|
||||||
|
format: request.body.codeType,
|
||||||
|
messageEncoding: "iso-8859-1",
|
||||||
|
altText: request.body.codeAlt?.trim() ?? "",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (request.body.qrText && request.body.codeType) {
|
const { thumbnailFile, logoFile } = request.body;
|
||||||
newPass.setBarcodes({
|
|
||||||
message: request.body.qrText,
|
|
||||||
format: request.body.codeType,
|
|
||||||
messageEncoding: "iso-8859-1",
|
|
||||||
altText: request.body.codeAlt?.trim() ?? "",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const { thumbnailFile, logoFile } = request.body;
|
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
||||||
|
if (newPass.type == "generic" || newPass.type == "eventTicket") {
|
||||||
|
if (thumbnailFile) {
|
||||||
|
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
|
||||||
|
|
||||||
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
try {
|
||||||
if (newPass.type == "generic" || newPass.type == "eventTicket") {
|
await storageRef
|
||||||
if (thumbnailFile) {
|
.file(`thumbnails/${thumbnailFile}`)
|
||||||
const tempPath1 = path.join(os.tmpdir(), 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (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("logo.png", buffer);
|
||||||
newPass.addBuffer("thumbnail@2x.png", buffer);
|
newPass.addBuffer("logo@2x.png", buffer);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (logoFile) {
|
|
||||||
const tempPath2 = path.join(os.tmpdir(), logoFile);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await storageRef
|
|
||||||
.file(`logos/${logoFile}`)
|
|
||||||
.download({ destination: tempPath2 });
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer = Buffer.alloc(0);
|
const bufferData = newPass.getAsBuffer();
|
||||||
try {
|
|
||||||
buffer = fs.readFileSync(tempPath2);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
newPass.addBuffer("logo.png", buffer);
|
|
||||||
newPass.addBuffer("logo@2x.png", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
const bufferData = newPass.getAsBuffer();
|
|
||||||
try {
|
|
||||||
console.log("Pass was uploaded successfully.");
|
|
||||||
|
|
||||||
response.set("Content-Type", newPass.mimeType);
|
response.set("Content-Type", newPass.mimeType);
|
||||||
response.status(200).send(bufferData);
|
response.status(200).send(bufferData);
|
||||||
@@ -309,6 +308,7 @@ export const pass = functions.https.onRequest(
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error Uploading pass " + error);
|
console.log("Error Uploading pass " + error);
|
||||||
|
|
||||||
|
response.status(500);
|
||||||
response.send({
|
response.send({
|
||||||
explanation: JSON.stringify(error),
|
explanation: JSON.stringify(error),
|
||||||
result: "FAILED",
|
result: "FAILED",
|
||||||
|
|||||||
Reference in New Issue
Block a user