@destinationstransfers/passkit

中文文档
6.6.1发布于 2019/10/06MITTypeScriptCommonJS近 30 天下载 3000

Apple Wallet Pass generating and pushing updates from Node.js

此软件包已被标记为弃用

WARNING: This project has been renamed to @walletpass/pass-js. Install using @walletpass/pass-js instead.

原始使用文档

npm (scoped) codecov Build Status Known Vulnerabilities DeepScan grade Reliability Rating Lines of Code Maintainability Rating tested with jest install size Language grade: JavaScript

Apple Wallet logo

@destinationstransfers/passkit

A Node.js library for generating Apple Wallet passes with localizations, NFC and web service push updates support. Written in Typescript.




Installation

Install with NPM or yarn:

npm install @destinationstransfers/passkit --save

yarn add @destinationstransfers/passkit

Get your certificates

To start with, you'll need a certificate issued by the iOS Provisioning Portal. You need one certificate per Pass Type ID.

After adding this certificate to your Keychain, you need to export it as a .p12 file first, then convert that file into a .pem file using the passkit-keys command:

./bin/passkit-keys ./pathToKeysFolder

and copy it into the keys directory.

The Apple Worldwide Developer Relations Certification Authority certificate is not needed anymore since it is already included in this package.

Start with a template

Start with a template. A template has all the common data fields that will be shared between your passes.

const { Template } = require("@destinationstransfers/passkit");

// Create a Template from local folder, see __test__/resources/passes for examples
// .load will load all fields from pass.json,
// as well as all images and com.example.passbook.pem file as key
// and localization string too
const template = await Template.load(
  "./path/to/templateFolder",
  "secretKeyPasswod"
);

// or
// create a Template from a Buffer with ZIP content
const s3 = new AWS.S3({ apiVersion: "2006-03-01", region: "us-west-2" });
const s3file = await s3
  .getObject({
    Bucket: "bucket",
    Key: "pass-template.zip"
  })
  .promise();
const template = await Template.fromBuffer(s3file.Body);

// or create it manually
const template = new Template("coupon", {
  passTypeIdentifier: "pass.com.example.passbook",
  teamIdentifier: "MXL",
  backgroundColor: "red",
  sharingProhibited: true
});
await template.images.add("icon", iconPngFileBuffer)
                     .add("logo", pathToLogoPNGfile)

The first argument is the pass style (coupon, eventTicket, etc), and the second optional argument has any fields you want to set on the template.

You can access template fields directly, or from chained accessor methods, e.g:

template.passTypeIdentifier = "pass.com.example.passbook";
template.teamIdentifier = "MXL";

The following template fields are required:

  • passTypeIdentifier - The Apple Pass Type ID, which has the prefix pass.
  • teamIdentifier - May contain an I

You can set any available fields either on a template or pass instance, such as: backgroundColor, foregroundColor, labelColor, logoText, organizationName, suppressStripShine and webServiceURL.

In addition, you need to tell the template where to find the key file:

await template.loadCertificate(
  "/etc/passbook/certificate_and_key.pem",
  "secret"
);
// or set them as strings
template.setCertificate(pemEncodedPassCertificate);
template.setPrivateKey(pemEncodedPrivateKey, optionalKeyPassword);

If you have images that are common to all passes, you may want to specify them once in the template:

// specify a single image with specific density and localization
await pass.images.add("icon", iconFilename, "2x", "ru");
// load all appropriate images in all densities and localizations
await template.images.load("./images");

You can add the image itself or a Buffer. Image format is enforced to be PNG.

Alternatively, if you have one directory containing the template file pass.json, the key com.example.passbook.pem and all the needed images, you can just use this single command:

const template = await Template.load(
  "./path/to/templateFolder",
  "secretKeyPasswod"
);

Create your pass

To create a new pass from a template:

const pass = template.createPass({
  serialNumber: "123456",
  description: "20% off"
});

Just like the template, you can access pass fields directly, e.g:

pass.serialNumber = "12345";
pass.description = "20% off";

In the JSON specification, structure fields (primary fields, secondary fields, etc) are represented as arrays, but items must have distinct key properties. Le sigh.

To make it easier, you can use methods of standard Map object or add that will do the logical thing. For example, to add a primary field:

pass.primaryFields.add({ key: "time", label: "Time", value: "10:00AM" });

To get one or all fields:

const dateField = pass.primaryFields.get("date");
for (const [key, { value }] of pass.primaryFields.entries()) {
  // ...
}

To remove one or all fields:

pass.primaryFields.delete("date");
pass.primaryFields.clear();

Adding images to a pass is the same as adding images to a template (see above).

Working with Dates

If you have dates in your fields make sure they are in ISO 8601 format with timezone or a Date instance. For example:

const { constants } = require('@destinationstransfers/passkit');

pass.primaryFields.add({ key: "updated", label: "Updated at", value: new Date(), dateStyle: constants.dateTimeFormat.SHORT, timeStyle: constants.dateTimeFormat.SHORT });

// there is also a helper setDateTime method
pass.auxiliaryFields.setDateTime(
  'serviceDate',
  'DATE',
  serviceMoment.toDate(),
  {
    dateStyle: constants.dateTimeFormat.MEDIUM,
    timeStyle: constants.dateTimeFormat.NONE,
    changeMessage: 'Service date changed to %@.',
  },
);
// main fields also accept Date objects
pass.relevantDate = new Date(2020, 1, 1, 10, 0);
template.expirationDate = new Date(2020, 10, 10, 10, 10);

Localizations

This library fully supports both string localization and/or images localization:

// everything from template
// will load all localized images and strings from folders like ru.lproj/ or fr-CA.lproj/
await template.load(folderPath);

// Strings

pass.localizations
  .add("en-GB", {
    GATE: "GATE",
    DEPART: "DEPART",
    ARRIVE: "ARRIVE",
    SEAT: "SEAT",
    PASSENGER: "PASSENGER",
    FLIGHT: "FLIGHT"
  })
  .add("ru", {
    GATE: "ВЫХОД",
    DEPART: "ВЫЛЕТ",
    ARRIVE: "ПРИЛЁТ",
    SEAT: "МЕСТО",
    PASSENGER: "ПАССАЖИР",
    FLIGHT: "РЕЙС"
  });

// Images

await template.images.add(
  "logo" | "icon" | etc,
  imageFilePathOrBufferWithPNGdata,
  "1x" | "2x" | "3x" | undefined,
  "ru"
);

Localization applies for all fields' label and value. There is a note about that in documentation.

Generate the file

To generate a file:

const buf = await pass.asBuffer();
await fs.writeFile("pathToPass.pass", buf);

You can send the buffer directly to an HTTP server response:

app.use(async (ctx, next) => {
  ctx.status = 200;
  ctx.type = passkit.constants.PASS_MIME_TYPE;
  ctx.body = await pass.asBuffer();
});

Troubleshooting with Console app

If the pass file generates without errors but you aren't able to open your pass on an iPhone, plug the iPhone into a Mac with macOS 10.14+ and open the 'Console' application. On the left, you can select your iPhone. You will then be able to inspect any errors that occur while adding the pass.

Credits

This projects started as fork of assaf/node-passbook. Since version 5.0 our module is not API compatible, please see Releases for more information.

  • Targeting Node >= 10 and rewritten in Typescript, removing deprecated calls (new Buffer, etc)
  • Replaces openssl spawning with native Javascript RSA implementation (via node-forge)
  • Includes Template.pushUpdates(pushToken) that sends APN update request for a given pass type to a pushToken (get pushToken at your PassKit Web Service implementation)
  • Adds constants for dictionary fields string values
  • Migrated tests to Jest
  • Increased test coverage
  • Adds strict dictionary fields values validation (where possible) to prevent errors earlier
  • Adding support for geolocation fields and Beacon fields
  • Adding easy template and localization load from JSON file
  • We use it in production at Transfers.do

版本与发布记录

@destinationstransfers/passkit 共发布 56 个版本,首次发布于 2017/08/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.6.1latest2019/10/067 年前
6.6.02019/10/067 年前
6.5.32019/09/277 年前
6.5.22019/09/057 年前
6.5.12019/08/227 年前
6.5.02019/07/117 年前

关于 @destinationstransfers/passkit 的常见问题

@destinationstransfers/passkit 是什么?

Apple Wallet Pass generating and pushing updates from Node.js注意:该包已被作者标记为弃用——WARNING: This project has been renamed to @walletpass/pass-js. Install using @walletpass/pass-js instead.

如何安装 @destinationstransfers/passkit?

在项目目录运行 npm install @destinationstransfers/passkit,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 passkit-keys,可以用 npx passkit-keys 直接运行。

@destinationstransfers/passkit 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@destinationstransfers/passkit 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@destinationstransfers/passkit 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@destinationstransfers/passkit 可以商用吗?

@destinationstransfers/passkit 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@destinationstransfers/passkit 的下载量有多少?

根据 npm 官方下载统计,@destinationstransfers/passkit 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,000 次,日均约 100 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@destinationstransfers/passkit 还在维护吗?

最新版本 6.6.1 发布于 2019/10/06(7 年前);至今共发布 56 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/08/07。作者已将其标记为弃用,新项目建议寻找替代方案。