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 { 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, "");

View File

@@ -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({

View File

@@ -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> => Pass)
// found here: https://medium.com/@curtistatewilkinson/this-can-be-done-using-conditional-types-like-so-633cf9787c8b
type Unpacked<T> = T extends Promise<infer U> ? U : T;
/**
* Tests created upon Jasmine testing suite.
*/
describe("Node-Passkit-generator", function () {
let pass: Pass;
let pass: Unpacked<ReturnType<typeof createPass>>;
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);
});

View File

@@ -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<schema.ValidPass> {
return this[passProps];
}
/**