From 8b6f1ba948fa4b385666ec157318a4324494abf3 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 30 Jun 2019 02:25:30 +0200 Subject: [PATCH] Cleanup --- examples/barcode.ts | 1 - examples/localization.ts | 2 +- spec/index.ts | 17 ++++++-------- src/pass.ts | 50 +++++++++++++++++++--------------------- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/examples/barcode.ts b/examples/barcode.ts index 81ae8fb..a39e84d 100644 --- a/examples/barcode.ts +++ b/examples/barcode.ts @@ -10,7 +10,6 @@ import app from "./webserver"; import { createPass } from ".."; -import { PassWithBarcodeMethods } from "../src/pass"; app.all(async function manageRequest(request, response) { const passName = request.params.modelName + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, ""); diff --git a/examples/localization.ts b/examples/localization.ts index 75e2523..0226fed 100644 --- a/examples/localization.ts +++ b/examples/localization.ts @@ -52,7 +52,7 @@ app.all(async function manageRequest(request, response) { pass.localize("zu", {}); // @ts-ignore - ignoring for logging purposes. Do not replicate - console.log("Added languages", pass.localize().join(", ")) + console.log("Added languages", Object.keys(pass.l10nTranslations).join(", ")) const stream = pass.generate(); response.set({ diff --git a/spec/index.ts b/spec/index.ts index d092e80..68186aa 100644 --- a/spec/index.ts +++ b/spec/index.ts @@ -1,19 +1,15 @@ import { createPass } from ".."; -import { Pass, PassWithBarcodeMethods } from "../src/pass"; -/* - * Yes, I know that I'm checking against "private" properties - * and that I shouldn't do that, but there's no other way to check - * the final results for each test. The only possible way is to - * read the generated stream of the zip file, unzip it - * (hopefully in memory) and check each property in pass.json file - * and .lproj directories. I hope who is reading this, will understand. - * +// This is used to extract the type of a Promise (like Promise => Pass) +// found here: https://medium.com/@curtistatewilkinson/this-can-be-done-using-conditional-types-like-so-633cf9787c8b +type Unpacked = T extends Promise ? U : T; + +/** * Tests created upon Jasmine testing suite. */ describe("Node-Passkit-generator", function () { - let pass: Pass; + let pass: Unpacked>; beforeEach(async () => { pass = await createPass({ model: "examples/models/examplePass.pass", @@ -185,6 +181,7 @@ describe("Node-Passkit-generator", function () { const props = pass.props["barcodes"] || []; const oldAmountOfBarcodes = props && props.length || 0; + // @ts-ignore - Ignoring for test purposes pass.barcodes(); expect(pass.props["barcodes"].length).toBe(oldAmountOfBarcodes); }); diff --git a/src/pass.ts b/src/pass.ts index 62ce64b..eec3375 100644 --- a/src/pass.ts +++ b/src/pass.ts @@ -1,8 +1,8 @@ import path from "path"; -import stream, { Stream } from "stream"; import forge from "node-forge"; -import { ZipFile } from "yazl"; import debug from "debug"; +import { Stream } from "stream"; +import { ZipFile } from "yazl"; import * as schema from "./schema"; import formatMessage from "./messages"; @@ -18,20 +18,7 @@ const genericDebug = debug("passkit:generic"); const transitType = Symbol("transitType"); const passProps = Symbol("_props"); -interface PassIndexSignature { - [key: string]: any; -} - -export interface PassWithLengthField extends Pass { - length: number; -} - -export interface PassWithBarcodeMethods extends PassWithLengthField { - backward: (format: schema.BarcodeFormat) => Pass; - autocomplete: () => Pass; -} - -export class Pass implements PassIndexSignature { +export class Pass { private bundle: schema.BundleUnit; private l10nBundles: schema.PartitionedBundle["l10nBundle"]; private _fields: (keyof schema.PassFields)[]; @@ -186,7 +173,7 @@ export class Pass implements PassIndexSignature { archive.addBuffer(signatureBuffer, "signature"); archive.addBuffer(Buffer.from(JSON.stringify(manifest)), "manifest.json"); - const passStream = new stream.PassThrough(); + const passStream = new Stream.PassThrough(); archive.outputStream.pipe(passStream); archive.end(); @@ -223,7 +210,7 @@ export class Pass implements PassIndexSignature { * @returns {this} */ - expiration(date: Date | null): this | string { + expiration(date: Date | null): this { if (date === null) { delete this[passProps]["expirationDate"]; return this; @@ -298,7 +285,7 @@ export class Pass implements PassIndexSignature { * @returns {Pass} */ - relevantDate(date: Date | null): this | string { + relevantDate(date: Date | null): this { if (date === null) { delete this[passProps]["relevantDate"]; return this; @@ -314,11 +301,13 @@ export class Pass implements PassIndexSignature { } /** - * Adds barcodes to "barcode" and "barcodes" properties. - * It will let to add the missing versions later. + * Adds barcodes "barcodes" property. + * It allows to pass a string to autogenerate all the structures. * * @method barcode - * @params data - the data to be added + * @params first - a structure or the string (message) that will generate + * all the barcodes + * @params data - other barcodes support * @return {this} Improved this with length property and other methods */ @@ -412,7 +401,7 @@ export class Pass implements PassIndexSignature { } if (!(barcodes && barcodes.length)) { - barcodeDebug(formatMessage("BRC_NO_POOL")) + barcodeDebug(formatMessage("BRC_NO_POOL")); return this; } @@ -434,9 +423,10 @@ export class Pass implements PassIndexSignature { * @method nfc * @params data - the data to be pushed in the pass * @returns {this} + * @see https://apple.co/2wTxiaC */ - nfc(data: schema.NFC | null): this | schema.NFC { + nfc(data: schema.NFC | null): this { if (data === null) { delete this[passProps]["nfc"]; return this; @@ -452,8 +442,16 @@ export class Pass implements PassIndexSignature { return this; } - get props(): schema.ValidPass { - return deepCopy(this[passProps]); + /** + * Allows to get the current inserted props; + * will return all props from valid overrides, + * template's pass.json and methods-inserted ones; + * + * @returns The properties will be inserted in the pass. + */ + + get props(): Readonly { + return this[passProps]; } /**