alexandercerutti / passkit-generator

The easiest way to generate custom Apple Wallet passes in Node.js
MIT License
893 stars 109 forks source link

Adding RemoteFiles for Pass Assets #143

Closed shawnchapiewski closed 1 year ago

shawnchapiewski commented 1 year ago

Running OS

macOS v12.6 ()

Running Node Version

v12.19.0

Description

Hi, I really appreciate the effort you put into creating this package and also the responsiveness I've seen on the issues created. I have been able to create passes with dynamic content besides the pass assets:

I would like a way to pull in image data from a remote source (www.example.com/icon.png) since my application deals with multiple different organizations who would like to personalize their passes and I could run some logic in the backend to show pass assets depending on the organization. I wanted to reference a feature request that was brought up at one point: #3 that had a fix to this issue back in version 1.4.0, but the function pass.load() seems to have been removed at least in the latest version of this package (not sure which version originally removed this functionality).

If there is documentation or examples that I may have missed that goes over this, apologies in advanced - if you could point me in the right direction, that would be great!

Current Code:

const pass = await PKPass.from(
      {
        /**
         * Note: .pass extension is enforced when reading a
         * model from FS, even if not specified here below
         */
        model: "/usr/src/app/cloud/model/activityPassModel.pass",
        certificates: {
          wwdr: certificates.wwdr,
          signerCert: certificates.signerCert,
          signerKey: certificates.signerKey,
          signerKeyPassphrase: certificates.signerKeyPassphrase,
        },
      },
      {
        // keys to be added or overridden
        serialNumber: req.query.id,
        logoText: "[TEST]",
      }
    );

    // relevant date determines when push notifications happen
    pass.setRelevantDate(checkOutDate);
    // expiration date sets when passes will no longer show mobile device
    pass.setExpirationDate(formattedExpirationDate);
    pass.load(
      "https://test.s3.amazonaws.com/IOhKfHx5Fg/icon.png",
      "icon.png"
    );

    // get pass as stream
    const stream = pass.getAsStream();
    // respond back with pass file
    res.setHeader("Content-Type", pass.mimeType);
    res.setHeader("Content-Disposition", 'attachment; filename=pass.pkpass');
    // serve the pass
    stream.pipe(res);

Currently receiving an error: TypeError: pass.load is not a function

Expected behavior

loadRemoteFile() function that takes an image url as a parameter and generates/updates pass image assets like the icon.png, logo.png or strip.png files in pkpass file.

Steps to reproduce

  1. Hit endpoint to generate pass
  2. Issue code: pass.load("https://test.s3.amazonaws.com/IOhKfHx5Fg/icon.png", "icon.png")
alexandercerutti commented 1 year ago

Hey there @shawnchapiewski, thank you for using passkit-generator!

Exactly, you are right. As you can see from the migration guide from v1 to v2, there was a load method that got removed.

I wanted to remove the need for people to possibly have a third library to download as node_module to make http requests and make instead passkit-generator to be the last "piece of puzzle" of the whole flow.

Sorry but I've not the intention to add it again. You should fetch the "branding" data before generating the pass and add the file through addBuffer method.

Let me know!

shawnchapiewski commented 1 year ago

I appreciate the super quick response @alexandercerutti! I was able to get this working and wanted to share my solution in case others run into a similar issue while working with this package.

let getIcon = await axios.get(iconURL, {responseType: "arraybuffer"});
let bufferIcon = Buffer.from(getIcon.data, "utf-8");
pass.addBuffer("icon.png", bufferIcon);

Closing the ticket as my issue is now solved, thanks!