Changed Pass constructor to conform to the new architecture;

This commit is contained in:
Alexander Cerutti
2019-06-09 11:51:33 +02:00
parent 16a9ebcd5c
commit 8a841301fa
3 changed files with 65 additions and 36 deletions

View File

@@ -21,21 +21,15 @@ export async function createPass(options: FactoryOptions): Promise<Pass> {
getModelContents(options.model),
readCertificatesFromOptions(options.certificates)
]);
return new Pass({
model: bundle,
certificates,
overrides: options.overrides
});
} catch (err) {
// @TODO: analyze the error and stop the execution somehow
}
// Controllo se il model è un oggetto o una stringa
// Se è un oggetto passo avanti
// Se è una stringa controllo se è un path. Se è un path
// faccio readdir
// altrimenti throw
// Creare una funzione che possa controllare ed estrarre i certificati
// Creare una funzione che possa controllare ed estrarre i file
// Entrambe devono ritornare Promise, così faccio await Promise.all
return new Pass();
}
async function getModelContents(model: FactoryOptions["model"]) {

View File

@@ -31,40 +31,57 @@ interface PassIndexSignature {
}
export class Pass implements PassIndexSignature {
private model: string;
// private model: string;
private bundle: schema.BundleUnit;
private l10nBundles: schema.PartitionedBundle["l10nBundle"];
private _fields: string[];
private _props: { [key: string]: any };
private _props: schema.ValidPass;
private type: string;
private fieldsKeys: Set<string>;
private passCore: schema.ValidPass;
Certificates: schema.Certificates;
l10n: { [key: string]: { [key: string]: string } } = {};
shouldOverwrite: boolean;
Certificates: schema.FinalCertificates;
l10nTranslations: { [key: string]: { [key: string]: string } } = {};
[transitType]: string = "";
constructor(options: schema.PassInstance) {
this.Certificates = {
// Even if this assigning will fail, it will be captured below
// in _parseSettings, since this won't match with the schema.
_raw: options.certificates || {},
};
this.Certificates = options.certificates;
this.l10nBundles = options.model.l10nBundle;
this.bundle = { ...options.model.bundle };
options.overrides = options.overrides || {};
this.shouldOverwrite = !(options.hasOwnProperty("shouldOverwrite") && !options.shouldOverwrite);
// getting pass.json
this.passCore = JSON.parse(this.bundle["pass.json"].toString("utf8"));
this._fields = ["primaryFields", "secondaryFields", "auxiliaryFields", "backFields", "headerFields"];
this.type = Object.keys(this.passCore).find(key => /(boardingPass|eventTicket|coupon|generic|storeCard)/.test(key));
if (!this.type) {
throw new Error("Missing type in model");
}
if (this.type === "boardingPass" && this.passCore[this.type]["transitType"]) {
// We might want to generate a boarding pass without setting manually
// in the code the transit type but right in the model;
this[transitType] = this.passCore[this.type]["transitType"];
}
this.fieldsKeys = new Set();
this._fields.forEach(name => {
this[name] = new FieldsArray(this.fieldsKeys);
const typeFields = Object.keys(this.passCore[this.type]);
this._fields = ["primaryFields", "secondaryFields", "auxiliaryFields", "backFields", "headerFields"];
this._fields.forEach(fieldName => {
if (typeFields.includes(fieldName)) {
this[fieldName] = new FieldsArray(
this.fieldsKeys,
...this.passCore[this.type][fieldName]
.filter((field: schema.Field) => schema.isValid(field, "field"))
);
} else {
this[fieldName] = new FieldsArray(this.fieldsKeys);
}
});
this[transitType] = "";
// Assigning model and _props to this
Object.assign(this, this._parseSettings(options));
}
/**

View File

@@ -10,14 +10,12 @@ export interface Certificates {
keyFile: string;
passphrase?: string;
};
_raw?: Certificates;
}
export interface FactoryOptions {
model: { [key: string]: Buffer } | string;
certificates: Certificates;
overrides?: Object;
shouldOverwrite?: boolean;
}
export interface BundleUnit {
@@ -276,6 +274,26 @@ const semantics = Joi.object().keys({
balance: currencyAmount
});
interface ValidPassType {
boardingPass?: PassFields & { transitType: TransitType };
eventTicket?: PassFields;
coupon?: PassFields;
generic?: PassFields;
storeCard?: PassFields;
}
export interface ValidPass extends OverridesSupportedOptions, ValidPassType {
barcode?: Barcode;
barcodes?: Barcode[];
beacons?: Beacon[];
locations?: Location[];
maxDistance?: number;
relevantDate?: string;
nfc?: NFC;
expirationDate?: string;
voided?: boolean;
}
export interface Barcode {
altText?: string;
messageEncoding?: string;
@@ -364,7 +382,7 @@ const locationsDict = Joi.object().keys({
relevantText: Joi.string()
});
export interface Pass {
export interface PassFields {
auxiliaryFields: Field[];
backFields: Field[];
headerFields: Field[];