Improved typings and type checkings

This commit is contained in:
Alexander Cerutti
2023-07-30 00:32:58 +02:00
parent 5895bb7ba0
commit ec8981185f

View File

@@ -27,20 +27,20 @@ interface RequestWithBody extends functions.Request {
textColor: string;
backgroundColor: string;
labelColor: string;
relevantDate: string;
expiryDate: string;
relevantLocationLat: number | "Blank";
relevantLocationLong: number | "Blank";
header: { value: string; label: string }[];
primary: { value: string; label: string }[];
secondary: { value: string; label: string }[];
auxiliary: { value: string; label: string }[];
codeAlt: string;
qrText: string;
transitType: TransitType;
codeType: Barcode["format"];
thumbnailFile: string;
logoFile: string;
relevantDate?: string;
expiryDate?: string;
relevantLocationLat?: number;
relevantLocationLong?: number;
header?: { value: string; label: string }[];
primary?: { value: string; label: string }[];
secondary?: { value: string; label: string }[];
auxiliary?: { value: string; label: string }[];
codeAlt?: string;
qrText?: string;
transitType?: TransitType;
codeType?: Barcode["format"];
thumbnailFile?: string;
logoFile?: string;
};
}
@@ -84,8 +84,11 @@ export const pass = functions.https.onRequest(
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/)
* 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,
@@ -105,20 +108,29 @@ export const pass = functions.https.onRequest(
);
if (newPass.type == "boardingPass") {
if (!request.body.transitType) {
response.status(400);
response.send({
error: "transitType is required",
});
return;
}
newPass.transitType = request.body.transitType;
}
if (request.body.relevantDate !== "Blank") {
if (typeof request.body.relevantDate === "string") {
newPass.setRelevantDate(new Date(request.body.relevantDate));
}
if (request.body.expiryDate !== "Blank") {
if (typeof request.body.expiryDate === "string") {
newPass.setExpirationDate(new Date(request.body.expiryDate));
}
if (
request.body.relevantLocationLat !== "Blank" &&
request.body.relevantLocationLong !== "Blank"
request.body.relevantLocationLat &&
request.body.relevantLocationLong
) {
newPass.setLocations({
latitude: request.body.relevantLocationLat,
@@ -126,6 +138,7 @@ export const pass = functions.https.onRequest(
});
}
if (Array.isArray(request.body.header)) {
for (let i = 0; i < request.body.header.length; i++) {
const field = request.body.header[i];
@@ -139,7 +152,9 @@ export const pass = functions.https.onRequest(
value: field.value,
});
}
}
if (Array.isArray(request.body.primary)) {
for (let i = 0; i < request.body.primary.length; i++) {
const field = request.body.primary[i];
@@ -156,7 +171,9 @@ export const pass = functions.https.onRequest(
: field.value,
});
}
}
if (Array.isArray(request.body.secondary)) {
for (let i = 0; i < request.body.secondary.length; i++) {
const field = request.body.secondary[i];
@@ -177,7 +194,9 @@ export const pass = functions.https.onRequest(
: "PKTextAlignmentLeft",
});
}
}
if (Array.isArray(request.body.auxiliary)) {
for (let i = 0; i < request.body.auxiliary.length; i++) {
const field = request.body.auxiliary[i];
@@ -198,19 +217,14 @@ export const pass = functions.https.onRequest(
: "PKTextAlignmentLeft",
});
}
}
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") {
if (request.body.qrText && request.body.codeType) {
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,
altText: request.body.codeAlt?.trim() ?? "",
});
}