mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 20:25:26 +00:00
Completely refactored examples to run only once and have multiple endpoints
This commit is contained in:
76
examples/self-hosted/src/localize.ts
Normal file
76
examples/self-hosted/src/localize.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* .localize() methods example
|
||||
* To see all the included languages, you have to unzip the
|
||||
* .pkpass file and check for .lproj folders
|
||||
*/
|
||||
|
||||
import { app } from "./webserver";
|
||||
import { getCertificates } from "./shared";
|
||||
import path from "path";
|
||||
import { PKPass } from "passkit-generator";
|
||||
/** Symbols are exported just for tests and examples. Replicate only if really needed. */
|
||||
import { localizationSymbol } from "passkit-generator/lib/PKPass";
|
||||
|
||||
app.route("/localize/: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,
|
||||
);
|
||||
|
||||
// Italian, already has an .lproj which gets included...
|
||||
pass.localize("it", {
|
||||
EVENT: "Evento",
|
||||
LOCATION: "Dove",
|
||||
});
|
||||
|
||||
// ...while German doesn't, so it gets created
|
||||
pass.localize("de", {
|
||||
EVENT: "Ereignis",
|
||||
LOCATION: "Ort",
|
||||
});
|
||||
|
||||
// This language does not exist but is still added as .lproj folder
|
||||
pass.localize("zu", {});
|
||||
|
||||
console.log(
|
||||
"Added languages",
|
||||
Object.keys(pass[localizationSymbol]).join(", "),
|
||||
);
|
||||
|
||||
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