mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 22:25:24 +00:00
Added PKPasses generation example
This commit is contained in:
111
examples/PKPasses.ts
Normal file
111
examples/PKPasses.ts
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
/**
|
||||||
|
* PKPasses generation through PKPass.pack static method
|
||||||
|
* example.
|
||||||
|
* Here it is showed manual model reading and
|
||||||
|
* creating through another PKPass because in the other
|
||||||
|
* examples, creation through templates is already shown
|
||||||
|
*/
|
||||||
|
|
||||||
|
import app from "./webserver";
|
||||||
|
import path from "path";
|
||||||
|
import { promises as fs } from "fs";
|
||||||
|
import { PKPass } from "passkit-generator";
|
||||||
|
|
||||||
|
const iconFromModel = await fs.readFile(
|
||||||
|
path.resolve(__dirname, "models/exampleBooking.pass/icon.png"),
|
||||||
|
);
|
||||||
|
|
||||||
|
// *************************** //
|
||||||
|
// *** EXAMPLE FROM NOW ON *** //
|
||||||
|
// *************************** //
|
||||||
|
|
||||||
|
async function generatePass(props: Object) {
|
||||||
|
const pass = new PKPass(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
wwdr: path.resolve(__dirname, "../../certificates/WWDR.pem"),
|
||||||
|
signerCert: path.resolve(
|
||||||
|
__dirname,
|
||||||
|
"../../certificates/signerCert.pem",
|
||||||
|
),
|
||||||
|
signerKey: path.resolve(
|
||||||
|
__dirname,
|
||||||
|
"../../certificates/signerKey.pem",
|
||||||
|
),
|
||||||
|
signerKeyPassphrase: "123456",
|
||||||
|
},
|
||||||
|
props,
|
||||||
|
);
|
||||||
|
|
||||||
|
pass.type = "boardingPass";
|
||||||
|
pass.transitType = "PKTransitTypeAir";
|
||||||
|
|
||||||
|
pass.headerFields.push(
|
||||||
|
{
|
||||||
|
key: "header-field-test-1",
|
||||||
|
value: "Unknown",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "header-field-test-2",
|
||||||
|
value: "unknown",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
pass.primaryFields.push(
|
||||||
|
{
|
||||||
|
key: "primaryField-1",
|
||||||
|
value: "NAP",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "primaryField-2",
|
||||||
|
value: "VCE",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Required by Apple. If one is not available, a
|
||||||
|
* pass might be openable on a Mac but not on a
|
||||||
|
* specific iPhone model
|
||||||
|
*/
|
||||||
|
|
||||||
|
pass.addBuffer("icon.png", iconFromModel);
|
||||||
|
pass.addBuffer("icon@2x.png", iconFromModel);
|
||||||
|
pass.addBuffer("icon@3x.png", iconFromModel);
|
||||||
|
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
app.all(async function manageRequest(request, response) {
|
||||||
|
const passName =
|
||||||
|
request.params.modelName +
|
||||||
|
"_" +
|
||||||
|
new Date().toISOString().split("T")[0].replace(/-/gi, "");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const passes = await Promise.all([
|
||||||
|
generatePass(request.body || request.params || request.query),
|
||||||
|
generatePass(request.body || request.params || request.query),
|
||||||
|
generatePass(request.body || request.params || request.query),
|
||||||
|
generatePass(request.body || request.params || request.query),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const pkpasses = PKPass.pack(...passes);
|
||||||
|
|
||||||
|
response.set({
|
||||||
|
"Content-type": pkpasses.mimeType,
|
||||||
|
"Content-disposition": `attachment; filename=${passName}.pkpass`,
|
||||||
|
});
|
||||||
|
|
||||||
|
const stream = pkpasses.getAsStream();
|
||||||
|
|
||||||
|
stream.pipe(response);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
|
||||||
|
response.set({
|
||||||
|
"Content-type": "text/html",
|
||||||
|
});
|
||||||
|
|
||||||
|
response.send(err.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user