Converted project to Typescript

This commit is contained in:
Alexander Cerutti
2023-07-29 19:31:05 +02:00
parent 99f1dddef1
commit f2442fc4ce
4 changed files with 103 additions and 26 deletions

View File

@@ -9,7 +9,8 @@
"dependencies": { "dependencies": {
"axios": "^1.2.6", "axios": "^1.2.6",
"firebase-admin": "^11.10.1", "firebase-admin": "^11.10.1",
"firebase-functions": "^4.4.1" "firebase-functions": "^4.4.1",
"tslib": "^2.6.1"
}, },
"devDependencies": { "devDependencies": {
"firebase-functions-test": "^0.2.3", "firebase-functions-test": "^0.2.3",
@@ -7374,9 +7375,9 @@
} }
}, },
"node_modules/tslib": { "node_modules/tslib": {
"version": "2.5.0", "version": "2.6.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
"integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==" "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig=="
}, },
"node_modules/tunnel-agent": { "node_modules/tunnel-agent": {
"version": "0.6.0", "version": "0.6.0",
@@ -13824,9 +13825,9 @@
"dev": true "dev": true
}, },
"tslib": { "tslib": {
"version": "2.5.0", "version": "2.6.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
"integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==" "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig=="
}, },
"tunnel-agent": { "tunnel-agent": {
"version": "0.6.0", "version": "0.6.0",

View File

@@ -10,7 +10,8 @@
"service:link-pg": "cd ../.. && npm run build && npm link", "service:link-pg": "cd ../.. && npm run build && npm link",
"preinstall": "npm run clear:deps", "preinstall": "npm run clear:deps",
"postinstall": "npm run service:link-pg && npm link passkit-generator", "postinstall": "npm run service:link-pg && npm link passkit-generator",
"clear:deps": "rm -rf node_modules" "clear:deps": "rm -rf node_modules",
"build": "rm -rf lib && npx tsc"
}, },
"engines": { "engines": {
"node": "16" "node": "16"
@@ -19,7 +20,8 @@
"dependencies": { "dependencies": {
"axios": "^1.2.6", "axios": "^1.2.6",
"firebase-admin": "^11.10.1", "firebase-admin": "^11.10.1",
"firebase-functions": "^4.4.1" "firebase-functions": "^4.4.1",
"tslib": "^2.6.1"
}, },
"peerDependencies": { "peerDependencies": {
"passkit-generator": "latest" "passkit-generator": "latest"

View File

@@ -1,10 +1,10 @@
const functions = require("firebase-functions"); import functions from "firebase-functions";
const admin = require("firebase-admin"); import admin from "firebase-admin";
const { PKPass } = require("passkit-generator"); import { PKPass } from "passkit-generator";
const fs = require("node:fs"); import type { Barcode, TransitType } from "passkit-generator";
const path = require("node:path"); import fs from "node:fs";
const axios = require("axios"); import path from "node:path";
const os = require("node:os"); import os from "node:os";
// Firebase init // Firebase init
admin admin
@@ -17,11 +17,51 @@ admin
const storageRef = admin.storage().bucket(); const storageRef = admin.storage().bucket();
// Declaring our request protocol
declare module "firebase-functions" {
interface HttpRequest {
body: {
passModel: string;
serialNumber: string;
logoText: string;
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;
};
}
}
// Declaring our .env contents
declare global {
namespace NodeJS {
interface ProcessEnv {
WWDR: string;
SIGNER_CERT: string;
SIGNER_KEY: string;
SIGNER_KEY_PASSPHRASE: string;
}
}
}
exports.pass = functions.https.onRequest(async (request, response) => { exports.pass = functions.https.onRequest(async (request, response) => {
const newPass = await PKPass.from( const newPass = await PKPass.from(
{ {
// Get relevant pass model from model folder (see passkit-generator/examples/models/) // Get relevant pass model from model folder (see passkit-generator/examples/models/)
model: `./model/${request.body.passType}.pass`, model: `./model/${request.body.passModel}.pass`,
certificates: { certificates: {
// Assigning certificates from certs folder (you will need to provide these yourself) // Assigning certificates from certs folder (you will need to provide these yourself)
wwdr: process.env.WWDR, wwdr: process.env.WWDR,
@@ -41,7 +81,7 @@ exports.pass = functions.https.onRequest(async (request, response) => {
); );
if (newPass.type == "boardingPass") { if (newPass.type == "boardingPass") {
newPass.transitType = `PKTransitType${request.body.transitType}`; newPass.transitType = request.body.transitType;
} }
if (request.body.relevantDate !== "Blank") { if (request.body.relevantDate !== "Blank") {
@@ -65,7 +105,7 @@ exports.pass = functions.https.onRequest(async (request, response) => {
for (let i = 0; i < request.body.header.length; i++) { for (let i = 0; i < request.body.header.length; i++) {
const field = request.body.header[i]; const field = request.body.header[i];
if (!(field.label && field.value)) { if (!(field?.label && field.value)) {
continue; continue;
} }
@@ -79,7 +119,7 @@ exports.pass = functions.https.onRequest(async (request, response) => {
for (let i = 0; i < request.body.primary.length; i++) { for (let i = 0; i < request.body.primary.length; i++) {
const field = request.body.primary[i]; const field = request.body.primary[i];
if (!(field.label && field.value)) { if (!(field?.label && field.value)) {
continue; continue;
} }
@@ -96,7 +136,7 @@ exports.pass = functions.https.onRequest(async (request, response) => {
for (let i = 0; i < request.body.secondary.length; i++) { for (let i = 0; i < request.body.secondary.length; i++) {
const field = request.body.secondary[i]; const field = request.body.secondary[i];
if (!(field.label && field.value)) { if (!(field?.label && field.value)) {
continue; continue;
} }
@@ -117,7 +157,7 @@ exports.pass = functions.https.onRequest(async (request, response) => {
for (let i = 0; i < request.body.auxiliary.length; i++) { for (let i = 0; i < request.body.auxiliary.length; i++) {
const field = request.body.auxiliary[i]; const field = request.body.auxiliary[i];
if (!(field.label && field.value)) { if (!(field?.label && field.value)) {
continue; continue;
} }
@@ -138,13 +178,13 @@ exports.pass = functions.https.onRequest(async (request, response) => {
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") { if (!request.body.codeAlt || request.body.codeAlt.trim() === "") {
newPass.setBarcodes({ newPass.setBarcodes({
message: request.body.qrText, message: request.body.qrText,
format: `PKBarcodeFormat${request.body.codeType}`, format: request.body.codeType,
messageEncoding: "iso-8859-1", messageEncoding: "iso-8859-1",
}); });
} else { } else {
newPass.setBarcodes({ newPass.setBarcodes({
message: request.body.qrText, message: request.body.qrText,
format: `PKBarcodeFormat${request.body.codeType}`, format: request.body.codeType,
messageEncoding: "iso-8859-1", messageEncoding: "iso-8859-1",
altText: request.body.codeAlt, altText: request.body.codeAlt,
}); });
@@ -154,6 +194,7 @@ exports.pass = functions.https.onRequest(async (request, response) => {
if (newPass.type == "generic" || newPass.type == "eventTicket") { if (newPass.type == "generic" || newPass.type == "eventTicket") {
const thumbnailFile = request.body.thumbnailFile; const thumbnailFile = request.body.thumbnailFile;
const tempPath1 = path.join(os.tmpdir(), thumbnailFile); const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
try { try {
await storageRef await storageRef
.file(`thumbnails/${thumbnailFile}`) .file(`thumbnails/${thumbnailFile}`)
@@ -180,24 +221,27 @@ exports.pass = functions.https.onRequest(async (request, response) => {
} 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(tempPath2); buffer = fs.readFileSync(tempPath2);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
newPass.addBuffer("logo.png", buffer); newPass.addBuffer("logo.png", buffer);
newPass.addBuffer("logo@2x.png", buffer); newPass.addBuffer("logo@2x.png", buffer);
const bufferData = newPass.getAsBuffer(); const bufferData = newPass.getAsBuffer();
try { try {
console.log("Pass was uploaded successfully."); 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);
// Delete thumbnail file in Firebase Storage // Delete thumbnail file in Firebase Storage
storageRef storageRef
.file(`thumbnails/${thumbnailFile}`) .file(`thumbnails/${request.body.thumbnailFile}`)
.delete() .delete()
.then(() => { .then(() => {
console.log("Thumbnail file deleted successfully"); console.log("Thumbnail file deleted successfully");
@@ -218,8 +262,9 @@ exports.pass = functions.https.onRequest(async (request, response) => {
}); });
} catch (error) { } catch (error) {
console.log("Error Uploading pass " + error); console.log("Error Uploading pass " + error);
response.send({ response.send({
explanation: error.message, explanation: JSON.stringify(error),
result: "FAILED", result: "FAILED",
}); });
} }

View File

@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es2016",
"module": "NodeNext",
"rootDir": "src",
"moduleResolution": "NodeNext",
"outDir": "lib",
"importHelpers": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false
}
}