diff --git a/spec/PKPass.ts b/spec/PKPass.ts index d66491f..d9999e2 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -32,6 +32,18 @@ describe("PKPass", () => { ); }); + describe("constructor", () => { + it("should warn about a non-object buffer parameter", () => { + console.warn = jasmine.createSpy("warn"); + + pass = new PKPass(undefined, baseCerts); + + expect(console.warn).toHaveBeenCalledWith( + Messages.INIT.INVALID_BUFFERS.replace("%s", "undefined"), + ); + }); + }); + describe("setBeacons", () => { it("should reset instance.props['beacons'] if 'null' is passed as value", () => { pass.setBeacons({ diff --git a/src/PKPass.ts b/src/PKPass.ts index 498d71a..8cc9ca7 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -130,15 +130,21 @@ export default class PKPass extends Bundle { ) { super("application/vnd.apple.pkpass"); - const buffersEntries = Object.entries(buffers); + if (buffers && typeof buffers === "object") { + const buffersEntries = Object.entries(buffers); - for ( - let i = buffersEntries.length, buffer: [string, Buffer]; - (buffer = buffersEntries[--i]); + for ( + let i = buffersEntries.length, buffer: [string, Buffer]; + (buffer = buffersEntries[--i]); - ) { - const [fileName, contentBuffer] = buffer; - this.addBuffer(fileName, contentBuffer); + ) { + const [fileName, contentBuffer] = buffer; + this.addBuffer(fileName, contentBuffer); + } + } else { + console.warn( + Messages.format(Messages.INIT.INVALID_BUFFERS, typeof buffers), + ); } if (props) { diff --git a/src/messages.ts b/src/messages.ts index faf956c..dd8ca5d 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -1,3 +1,8 @@ +export const INIT = { + INVALID_BUFFERS: + "Cannot set buffers in constructor: expected object but received %s", +} as const; + export const CERTIFICATES = { INVALID: "Invalid certificate(s) loaded. %s. Please provide valid WWDR certificates and developer signer certificate and key (with passphrase).\nRefer to docs to obtain them",