diff --git a/src/factory.ts b/src/factory.ts index ec19976..37d6896 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -21,21 +21,15 @@ export async function createPass(options: FactoryOptions): Promise { 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"]) { diff --git a/src/pass.ts b/src/pass.ts index e0adc81..0e4dbe2 100644 --- a/src/pass.ts +++ b/src/pass.ts @@ -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; + 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)); } /** diff --git a/src/schema.ts b/src/schema.ts index e7549cd..a879e7a 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -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 { @@ -32,8 +30,8 @@ export interface PartitionedBundle { } export interface FinalCertificates { - wwdr: string; - signerCert: string; + wwdr: string; + signerCert: string; signerKey: string; } @@ -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[];