mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 17:25:21 +00:00
Completely refactored examples to run only once and have multiple endpoints
This commit is contained in:
82
examples/self-hosted/src/setBarcodes.ts
Normal file
82
examples/self-hosted/src/setBarcodes.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* .barcodes() methods example
|
||||
* Here we set the barcode. To see all the results, you can
|
||||
* both unzip .pkpass file or check the properties before
|
||||
* generating the whole bundle
|
||||
*
|
||||
* Pass ?alt=true as querystring to test a barcode generate
|
||||
* by a string
|
||||
*/
|
||||
|
||||
import { app } from "./webserver";
|
||||
import { getCertificates } from "./shared";
|
||||
import { PKPass } from "passkit-generator";
|
||||
import path from "path";
|
||||
|
||||
app.route("/barcodes/:modelName").get(async (request, response) => {
|
||||
const passName =
|
||||
request.params.modelName +
|
||||
"_" +
|
||||
new Date().toISOString().split("T")[0].replace(/-/gi, "");
|
||||
|
||||
const certificates = await getCertificates();
|
||||
|
||||
try {
|
||||
const pass = await PKPass.from(
|
||||
{
|
||||
model: path.resolve(
|
||||
__dirname,
|
||||
`../../models/${request.params.modelName}`,
|
||||
),
|
||||
certificates: {
|
||||
wwdr: certificates.wwdr,
|
||||
signerCert: certificates.signerCert,
|
||||
signerKey: certificates.signerKey,
|
||||
signerKeyPassphrase: certificates.signerKeyPassphrase,
|
||||
},
|
||||
},
|
||||
request.body || request.params || request.query || {},
|
||||
);
|
||||
|
||||
if (request.query.alt === "true") {
|
||||
// After this, pass.props["barcodes"] will have support for all the formats
|
||||
pass.setBarcodes("Thank you for using this package <3");
|
||||
|
||||
console.log(
|
||||
"Barcodes support is autocompleted:",
|
||||
pass.props["barcodes"],
|
||||
);
|
||||
} else {
|
||||
// After this, pass.props["barcodes"] will have support for just two of three
|
||||
// of the passed format (the valid ones);
|
||||
|
||||
pass.setBarcodes(
|
||||
{
|
||||
message: "Thank you for using this package <3",
|
||||
format: "PKBarcodeFormatCode128",
|
||||
},
|
||||
{
|
||||
message: "Thank you for using this package <3",
|
||||
format: "PKBarcodeFormatPDF417",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const stream = pass.getAsStream();
|
||||
|
||||
response.set({
|
||||
"Content-type": pass.mimeType,
|
||||
"Content-disposition": `attachment; filename=${passName}.pkpass`,
|
||||
});
|
||||
|
||||
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