From 031133891d8a75ff4b00b19e23364d3f0d9f4579 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 17 Oct 2021 13:53:19 +0200 Subject: [PATCH] Removed PKPass.pack to be async and moved an error to messages --- spec/PKPass.ts | 19 ++++++++----------- src/PKPass.ts | 10 +++------- src/messages.ts | 4 ++++ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 8d4d300..9704213 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -1086,18 +1086,15 @@ describe("PKPass", () => { pass[freezeSymbol](); }); - it("should should throw error if not all the files passed are PKPasses", async () => { - await expectAsync( + it("should should throw error if not all the files passed are PKPasses", () => { + expect( // @ts-expect-error - PKPass.pack(pass, "pass.json", pass), - ).toBeRejectedWithError( - Error, - "Cannot pack passes. Only PKPass instances allowed", - ); + () => PKPass.pack(pass, "pass.json", pass), + ).toThrowError(Error, Messages.PACK.INVALID); }); - it("should output a frozen bundle of bundles", async () => { - const pkPassesBundle = await PKPass.pack(pass, pass); + it("should output a frozen bundle of bundles", () => { + const pkPassesBundle = PKPass.pack(pass, pass); expect( pkPassesBundle[filesSymbol]["packed-pass-1.pkpass"], @@ -1109,8 +1106,8 @@ describe("PKPass", () => { expect(pkPassesBundle.isFrozen).toBe(true); }); - it("should output a bundle with pkpasses mimetype", async () => { - const pkPassesBundle = await PKPass.pack(pass, pass); + it("should output a bundle with pkpasses mimetype", () => { + const pkPassesBundle = PKPass.pack(pass, pass); expect(pkPassesBundle.mimeType).toBe( "application/vnd.apple.pkpasses", diff --git a/src/PKPass.ts b/src/PKPass.ts index 0571922..e8e5fdc 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -105,16 +105,12 @@ export default class PKPass extends Bundle { * @param passes */ - static async pack(...passes: PKPass[]): Promise { + static pack(...passes: PKPass[]): Bundle { if (!passes.every((pass) => pass instanceof PKPass)) { - throw new Error( - "Cannot pack passes. Only PKPass instances allowed", - ); + throw new Error(Messages.PACK.INVALID); } - const buffers = await Promise.all( - passes.map((pass) => pass.getAsBuffer()), - ); + const buffers = passes.map((pass) => pass.getAsBuffer()); const [bundle, freezeBundle] = Bundle.freezable( "application/vnd.apple.pkpasses", diff --git a/src/messages.ts b/src/messages.ts index f88f300..fdf0f47 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -78,6 +78,10 @@ export const BUNDLE = { CLOSED: "Cannot add file. Bundle is closed.", } as const; +export const PACK = { + INVALID: "Cannot pack passes. Only PKPass instances allowed", +} as const; + /** * Creates a message with replaced values * @param messageName