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), getModelContents(options.model),
readCertificatesFromOptions(options.certificates) readCertificatesFromOptions(options.certificates)
]); ]);
return new Pass({
model: bundle,
certificates,
overrides: options.overrides
});
} catch (err) { } catch (err) {
// @TODO: analyze the error and stop the execution somehow // @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"]) { async function getModelContents(model: FactoryOptions["model"]) {

View File

@@ -31,40 +31,57 @@ interface PassIndexSignature {
} }
export class Pass implements 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 _fields: string[];
private _props: { [key: string]: any }; private _props: schema.ValidPass;
private type: string; private type: string;
private fieldsKeys: Set<string>; private fieldsKeys: Set<string>;
private passCore: schema.ValidPass;
Certificates: schema.Certificates; Certificates: schema.FinalCertificates;
l10n: { [key: string]: { [key: string]: string } } = {}; l10nTranslations: { [key: string]: { [key: string]: string } } = {};
shouldOverwrite: boolean;
[transitType]: string = ""; [transitType]: string = "";
constructor(options: schema.PassInstance) { constructor(options: schema.PassInstance) {
this.Certificates = { this.Certificates = options.certificates;
// Even if this assigning will fail, it will be captured below this.l10nBundles = options.model.l10nBundle;
// in _parseSettings, since this won't match with the schema. this.bundle = { ...options.model.bundle };
_raw: options.certificates || {},
};
options.overrides = options.overrides || {}; 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.fieldsKeys = new Set();
this._fields.forEach(name => { const typeFields = Object.keys(this.passCore[this.type]);
this[name] = new FieldsArray(this.fieldsKeys);
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; keyFile: string;
passphrase?: string; passphrase?: string;
}; };
_raw?: Certificates;
} }
export interface FactoryOptions { export interface FactoryOptions {
model: { [key: string]: Buffer } | string; model: { [key: string]: Buffer } | string;
certificates: Certificates; certificates: Certificates;
overrides?: Object; overrides?: Object;
shouldOverwrite?: boolean;
} }
export interface BundleUnit { export interface BundleUnit {
@@ -276,6 +274,26 @@ const semantics = Joi.object().keys({
balance: currencyAmount 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 { export interface Barcode {
altText?: string; altText?: string;
messageEncoding?: string; messageEncoding?: string;
@@ -364,7 +382,7 @@ const locationsDict = Joi.object().keys({
relevantText: Joi.string() relevantText: Joi.string()
}); });
export interface Pass { export interface PassFields {
auxiliaryFields: Field[]; auxiliaryFields: Field[];
backFields: Field[]; backFields: Field[];
headerFields: Field[]; headerFields: Field[];