This commit is contained in:
Alexander Cerutti
2019-06-30 02:25:30 +02:00
parent 35789a3d34
commit 8b6f1ba948
4 changed files with 32 additions and 38 deletions

View File

@@ -10,7 +10,6 @@
import app from "./webserver"; import app from "./webserver";
import { createPass } from ".."; import { createPass } from "..";
import { PassWithBarcodeMethods } from "../src/pass";
app.all(async function manageRequest(request, response) { app.all(async function manageRequest(request, response) {
const passName = request.params.modelName + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, ""); const passName = request.params.modelName + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, "");

View File

@@ -52,7 +52,7 @@ app.all(async function manageRequest(request, response) {
pass.localize("zu", {}); pass.localize("zu", {});
// @ts-ignore - ignoring for logging purposes. Do not replicate // @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(); const stream = pass.generate();
response.set({ response.set({

View File

@@ -1,19 +1,15 @@
import { createPass } from ".."; import { createPass } from "..";
import { Pass, PassWithBarcodeMethods } from "../src/pass";
/* // This is used to extract the type of a Promise (like Promise<Pass> => Pass)
* Yes, I know that I'm checking against "private" properties // found here: https://medium.com/@curtistatewilkinson/this-can-be-done-using-conditional-types-like-so-633cf9787c8b
* and that I shouldn't do that, but there's no other way to check type Unpacked<T> = T extends Promise<infer U> ? U : T;
* 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.
*
* Tests created upon Jasmine testing suite. * Tests created upon Jasmine testing suite.
*/ */
describe("Node-Passkit-generator", function () { describe("Node-Passkit-generator", function () {
let pass: Pass; let pass: Unpacked<ReturnType<typeof createPass>>;
beforeEach(async () => { beforeEach(async () => {
pass = await createPass({ pass = await createPass({
model: "examples/models/examplePass.pass", model: "examples/models/examplePass.pass",
@@ -185,6 +181,7 @@ describe("Node-Passkit-generator", function () {
const props = pass.props["barcodes"] || []; const props = pass.props["barcodes"] || [];
const oldAmountOfBarcodes = props && props.length || 0; const oldAmountOfBarcodes = props && props.length || 0;
// @ts-ignore - Ignoring for test purposes
pass.barcodes(); pass.barcodes();
expect(pass.props["barcodes"].length).toBe(oldAmountOfBarcodes); expect(pass.props["barcodes"].length).toBe(oldAmountOfBarcodes);
}); });

View File

@@ -1,8 +1,8 @@
import path from "path"; import path from "path";
import stream, { Stream } from "stream";
import forge from "node-forge"; import forge from "node-forge";
import { ZipFile } from "yazl";
import debug from "debug"; import debug from "debug";
import { Stream } from "stream";
import { ZipFile } from "yazl";
import * as schema from "./schema"; import * as schema from "./schema";
import formatMessage from "./messages"; import formatMessage from "./messages";
@@ -18,20 +18,7 @@ const genericDebug = debug("passkit:generic");
const transitType = Symbol("transitType"); const transitType = Symbol("transitType");
const passProps = Symbol("_props"); const passProps = Symbol("_props");
interface PassIndexSignature { export class Pass {
[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 {
private bundle: schema.BundleUnit; private bundle: schema.BundleUnit;
private l10nBundles: schema.PartitionedBundle["l10nBundle"]; private l10nBundles: schema.PartitionedBundle["l10nBundle"];
private _fields: (keyof schema.PassFields)[]; private _fields: (keyof schema.PassFields)[];
@@ -186,7 +173,7 @@ export class Pass implements PassIndexSignature {
archive.addBuffer(signatureBuffer, "signature"); archive.addBuffer(signatureBuffer, "signature");
archive.addBuffer(Buffer.from(JSON.stringify(manifest)), "manifest.json"); archive.addBuffer(Buffer.from(JSON.stringify(manifest)), "manifest.json");
const passStream = new stream.PassThrough(); const passStream = new Stream.PassThrough();
archive.outputStream.pipe(passStream); archive.outputStream.pipe(passStream);
archive.end(); archive.end();
@@ -223,7 +210,7 @@ export class Pass implements PassIndexSignature {
* @returns {this} * @returns {this}
*/ */
expiration(date: Date | null): this | string { expiration(date: Date | null): this {
if (date === null) { if (date === null) {
delete this[passProps]["expirationDate"]; delete this[passProps]["expirationDate"];
return this; return this;
@@ -298,7 +285,7 @@ export class Pass implements PassIndexSignature {
* @returns {Pass} * @returns {Pass}
*/ */
relevantDate(date: Date | null): this | string { relevantDate(date: Date | null): this {
if (date === null) { if (date === null) {
delete this[passProps]["relevantDate"]; delete this[passProps]["relevantDate"];
return this; return this;
@@ -314,11 +301,13 @@ export class Pass implements PassIndexSignature {
} }
/** /**
* Adds barcodes to "barcode" and "barcodes" properties. * Adds barcodes "barcodes" property.
* It will let to add the missing versions later. * It allows to pass a string to autogenerate all the structures.
* *
* @method barcode * @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 * @return {this} Improved this with length property and other methods
*/ */
@@ -412,7 +401,7 @@ export class Pass implements PassIndexSignature {
} }
if (!(barcodes && barcodes.length)) { if (!(barcodes && barcodes.length)) {
barcodeDebug(formatMessage("BRC_NO_POOL")) barcodeDebug(formatMessage("BRC_NO_POOL"));
return this; return this;
} }
@@ -434,9 +423,10 @@ export class Pass implements PassIndexSignature {
* @method nfc * @method nfc
* @params data - the data to be pushed in the pass * @params data - the data to be pushed in the pass
* @returns {this} * @returns {this}
* @see https://apple.co/2wTxiaC
*/ */
nfc(data: schema.NFC | null): this | schema.NFC { nfc(data: schema.NFC | null): this {
if (data === null) { if (data === null) {
delete this[passProps]["nfc"]; delete this[passProps]["nfc"];
return this; return this;
@@ -452,8 +442,16 @@ export class Pass implements PassIndexSignature {
return this; 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<schema.ValidPass> {
return this[passProps];
} }
/** /**