diff --git a/src/PKPass.ts b/src/PKPass.ts index e63c185..bf67c62 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -25,7 +25,7 @@ export default class PKPass extends Bundle { [placeholder: string]: string; }; } = {}; - private [passTypeSymbol]: Schemas.PassTypesProps = undefined; + private [passTypeSymbol]: Schemas.PassTypesProps | undefined = undefined; /** * Either create a pass from another one @@ -39,8 +39,8 @@ export default class PKPass extends Bundle { source: S, props?: Schemas.OverridablePassProps, ): Promise { - let certificates: Schemas.CertificatesSchema = undefined; - let buffers: Schemas.FileBuffers = undefined; + let certificates: Schemas.CertificatesSchema | undefined = undefined; + let buffers: Schemas.FileBuffers | undefined = undefined; if (!source) { throw new TypeError( @@ -206,8 +206,10 @@ export default class PKPass extends Bundle { /** * Allows setting a transitType property - * for a boardingPass. Throws an error if - * the current type is not a boardingPass. + * for a boardingPass. + * + * It will (automatically) throw an exception + * if current type is not "boardingPass". * * @param value */ @@ -224,12 +226,16 @@ export default class PKPass extends Bundle { value, Messages.TRANSIT_TYPE.INVALID, ); + this[propsSymbol]["boardingPass"].transitType = value; } /** * Allows getting the current transitType - * from pass props + * from pass props. + * + * It will (automatically) throw an exception + * if current type is not "boardingPass". */ public get transitType() { @@ -239,9 +245,9 @@ export default class PKPass extends Bundle { /** * Allows accessing to primaryFields object. * - * It will (automatically) throw an error if - * no valid pass.json has been parsed yet or, - * anyway, if it has not a valid type. + * It will (automatically) throw an exception + * if no valid pass.json has been parsed yet + * or, anyway, if it has not a valid type. */ public get primaryFields(): Schemas.Field[] { @@ -251,9 +257,10 @@ export default class PKPass extends Bundle { /** * Allows accessing to secondaryFields object * - * It will (automatically) throw an error if - * no valid pass.json has been parsed yet or, - * anyway, if it has not a valid type. + * It will (automatically) throw an exception + * if no valid pass.json has been parsed yet + * or, anyway, if a valid type has not been + * set yet. */ public get secondaryFields(): Schemas.Field[] { @@ -263,9 +270,10 @@ export default class PKPass extends Bundle { /** * Allows accessing to auxiliaryFields object * - * It will (automatically) throw an error if - * no valid pass.json has been parsed yet or, - * anyway, if it has not a valid type. + * It will (automatically) throw an exception + * if no valid pass.json has been parsed yet + * or, anyway, if a valid type has not been + * set yet. */ public get auxiliaryFields(): Schemas.Field[] { @@ -275,9 +283,10 @@ export default class PKPass extends Bundle { /** * Allows accessing to headerFields object * - * It will (automatically) throw an error if - * no valid pass.json has been parsed yet or, - * anyway, if it has not a valid type. + * It will (automatically) throw an exception + * if no valid pass.json has been parsed yet + * or, anyway, if a valid type has not been + * set yet. */ public get headerFields(): Schemas.Field[] { @@ -287,9 +296,10 @@ export default class PKPass extends Bundle { /** * Allows accessing to backFields object * - * It will (automatically) throw an error if - * no valid pass.json has been parsed yet or, - * anyway, if it has not a valid type. + * It will (automatically) throw an exception + * if no valid pass.json has been parsed yet + * or, anyway, if a valid type has not been + * set yet. */ public get backFields(): Schemas.Field[] { @@ -305,15 +315,18 @@ export default class PKPass extends Bundle { * headerFields, auxiliaryFields, backFields) */ - public set type(type: Schemas.PassTypesProps) { + public set type(maybeNewType: Schemas.PassTypesProps | undefined) { Utils.assertUnfrozen(this); Schemas.assertValidity( Schemas.PassType, - type, + maybeNewType, Messages.PASS_TYPE.INVALID, ); + /** Shut up, typescript strict mode! */ + const type = maybeNewType as Schemas.PassTypesProps; + if (this.type) { /** * Removing reference to previous type and its content because @@ -431,7 +444,7 @@ export default class PKPass extends Bundle { const translationsFileRegexp = /(?[a-zA-Z-]{2,}).lproj\/pass\.strings/; - let match: RegExpMatchArray; + let match: RegExpMatchArray | null; if ((match = pathName.match(translationsFileRegexp))) { const [, lang] = match; @@ -501,7 +514,7 @@ export default class PKPass extends Bundle { secondaryFields = [], auxiliaryFields = [], backFields = [], - } = data[type]; + } = data[type] || {}; this.headerFields.push(...headerFields); this.primaryFields.push(...primaryFields); @@ -810,7 +823,7 @@ export default class PKPass extends Bundle { this[propsSymbol]["beacons"] = Schemas.filterValid( Schemas.Beacon, - beacons, + beacons as Schemas.Beacon[], ); } @@ -846,7 +859,7 @@ export default class PKPass extends Bundle { this[propsSymbol]["locations"] = Schemas.filterValid( Schemas.Location, - locations, + locations as Schemas.Location[], ); } @@ -958,8 +971,8 @@ export default class PKPass extends Bundle { function validateJSONBuffer( buffer: Buffer, schema: Parameters[0], -) { - let contentAsJSON: unknown; +): Schemas.PassProps { + let contentAsJSON: Schemas.PassProps; try { contentAsJSON = JSON.parse(buffer.toString("utf8")); diff --git a/src/Signature.ts b/src/Signature.ts index 08d9797..4c1bdba 100644 --- a/src/Signature.ts +++ b/src/Signature.ts @@ -116,7 +116,10 @@ function parseCertificates(certificates: Schemas.CertificatesSchema) { function getStringCertificates( certificates: Schemas.CertificatesSchema, -): Record { +): Record< + keyof Omit, + string +> & { signerKeyPassphrase?: string } { return { signerKeyPassphrase: certificates.signerKeyPassphrase, wwdr: Buffer.from(certificates.wwdr).toString("utf-8"), diff --git a/src/StringsUtils.ts b/src/StringsUtils.ts index 3f394f2..e44d032 100644 --- a/src/StringsUtils.ts +++ b/src/StringsUtils.ts @@ -30,14 +30,14 @@ export function parse(buffer: Buffer) { /** EOF */ blockEndPoint === fileAsString.length ) { - let match: RegExpMatchArray; + let match: RegExpMatchArray | null; const section = fileAsString.substring( blockStartPoint, blockEndPoint + 1, ); - if ((match = section.match(translationRowRegex))) { + if ((match = section.match(translationRowRegex)) && match.groups) { const { groups: { key, value }, } = match; diff --git a/src/getModelFolderContents.ts b/src/getModelFolderContents.ts index 3544be6..d69f3a8 100644 --- a/src/getModelFolderContents.ts +++ b/src/getModelFolderContents.ts @@ -49,7 +49,9 @@ export default async function getModelFolderContents( .reduce((acc, current) => ({ ...acc, ...current }), {}); return modelRecords; - } catch (err) { + } catch (_err) { + const err = _err as NodeJS.ErrnoException; + if (err?.code === "ENOENT") { if (err.syscall === "open") { // file opening failed diff --git a/src/schemas/index.ts b/src/schemas/index.ts index 554c292..7758e0c 100644 --- a/src/schemas/index.ts +++ b/src/schemas/index.ts @@ -229,7 +229,7 @@ export function filterValid( return []; } - return source.reduce((acc, current) => { + return source.reduce((acc, current) => { try { return [...acc, validate(schema, current)]; } catch (err) { diff --git a/src/utils.ts b/src/utils.ts index bed273a..73e98ff 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,7 +7,7 @@ import type Bundle from "./Bundle"; * @returns */ -export function processDate(date: Date): string | null { +export function processDate(date: Date): string | undefined { if (!(date instanceof Date)) { throw "Invalid date"; } @@ -84,9 +84,9 @@ export function removeHidden(from: Array): Array { * @returns */ -export function cloneRecursive(object: Object) { - const objectCopy = {}; - const objectEntries = Object.entries(object); +export function cloneRecursive(object: T) { + const objectCopy = {} as Record; + const objectEntries = Object.entries(object) as [keyof T, T[keyof T]][]; for (let i = 0; i < objectEntries.length; i++) { const [key, value] = objectEntries[i];