Added more return types

This commit is contained in:
Alexander Cerutti
2019-05-20 23:51:13 +02:00
parent a549d94d67
commit ffdaf8ac61
2 changed files with 17 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import { promisify } from "util"; import { promisify } from "util";
import stream from "stream"; import stream, { Stream } from "stream";
import forge from "node-forge"; import forge from "node-forge";
import archiver from "archiver"; import archiver from "archiver";
import debug from "debug"; import debug from "debug";
@@ -75,7 +75,7 @@ export class Pass implements PassIndexSignature {
* @return {Promise<Stream>} A Promise containing the stream of the generated pass. * @return {Promise<Stream>} A Promise containing the stream of the generated pass.
*/ */
async generate() { async generate(): Promise<Stream> {
try { try {
// Reading the model // Reading the model
const modelFilesList = await readdir(this.model); const modelFilesList = await readdir(this.model);
@@ -216,7 +216,7 @@ export class Pass implements PassIndexSignature {
* @see https://apple.co/2KOv0OW - Passes support localization * @see https://apple.co/2KOv0OW - Passes support localization
*/ */
localize(lang, translations) { localize(lang: string, translations?: { [key: string]: string }) {
if (lang && typeof lang === "string" && (typeof translations === "object" || translations === undefined)) { if (lang && typeof lang === "string" && (typeof translations === "object" || translations === undefined)) {
this.l10n[lang] = translations || {}; this.l10n[lang] = translations || {};
} }
@@ -233,7 +233,7 @@ export class Pass implements PassIndexSignature {
* @returns {this} * @returns {this}
*/ */
expiration(date, format) { expiration(date: string | Date, format?: string) {
if (typeof date !== "string" && !(date instanceof Date)) { if (typeof date !== "string" && !(date instanceof Date)) {
return this; return this;
} }
@@ -257,7 +257,7 @@ export class Pass implements PassIndexSignature {
* @return {this} * @return {this}
*/ */
void() { void(): this {
this._props.voided = true; this._props.voided = true;
return this; return this;
} }
@@ -272,7 +272,7 @@ export class Pass implements PassIndexSignature {
* @return {Number} The quantity of data pushed * @return {Number} The quantity of data pushed
*/ */
relevance(type, data, relevanceDateFormat) { relevance(type: string, data: any, relevanceDateFormat?: string) {
let types = ["beacons", "locations", "maxDistance", "relevantDate"]; let types = ["beacons", "locations", "maxDistance", "relevantDate"];
if (!type || !data || !types.includes(type)) { if (!type || !data || !types.includes(type)) {
@@ -470,7 +470,7 @@ export class Pass implements PassIndexSignature {
* @returns {this} * @returns {this}
*/ */
nfc(data) { nfc(data: schema.NFC) {
if (!(typeof data === "object" && !Array.isArray(data) && schema.isValid(data, "nfcDict"))) { if (!(typeof data === "object" && !Array.isArray(data) && schema.isValid(data, "nfcDict"))) {
genericDebug("Invalid NFC data provided"); genericDebug("Invalid NFC data provided");
return this; return this;
@@ -489,7 +489,7 @@ export class Pass implements PassIndexSignature {
* @returns {Boolean} true if type is supported, false otherwise. * @returns {Boolean} true if type is supported, false otherwise.
*/ */
_hasValidType(passFile) { _hasValidType(passFile: schema.Pass): boolean {
let passTypes = ["boardingPass", "eventTicket", "coupon", "generic", "storeCard"]; let passTypes = ["boardingPass", "eventTicket", "coupon", "generic", "storeCard"];
this.type = passTypes.find(type => passFile.hasOwnProperty(type)); this.type = passTypes.find(type => passFile.hasOwnProperty(type));
@@ -509,7 +509,7 @@ export class Pass implements PassIndexSignature {
* @return {Promise<Buffer>} The patched pass.json buffer * @return {Promise<Buffer>} The patched pass.json buffer
*/ */
async _extractPassDefinition() { async _extractPassDefinition(): Promise<Buffer> {
const passStructBuffer = await readFile(path.resolve(this.model, "pass.json")) const passStructBuffer = await readFile(path.resolve(this.model, "pass.json"))
const parsedPassDefinition = JSON.parse(passStructBuffer.toString("utf8")); const parsedPassDefinition = JSON.parse(passStructBuffer.toString("utf8"));
@@ -529,7 +529,7 @@ export class Pass implements PassIndexSignature {
* @returns {Buffer} * @returns {Buffer}
*/ */
_sign(manifest) { _sign(manifest: { [key: string]: string }): Buffer {
let signature = forge.pkcs7.createSignedData(); let signature = forge.pkcs7.createSignedData();
signature.content = forge.util.createBuffer(JSON.stringify(manifest), "utf8"); signature.content = forge.util.createBuffer(JSON.stringify(manifest), "utf8");
@@ -592,7 +592,7 @@ export class Pass implements PassIndexSignature {
* @returns {Promise<Buffer>} Edited pass.json buffer or Object containing error. * @returns {Promise<Buffer>} Edited pass.json buffer or Object containing error.
*/ */
_patch(passFile) { _patch(passFile: schema.Pass): Buffer {
if (Object.keys(this._props).length) { if (Object.keys(this._props).length) {
// We filter the existing (in passFile) and non-valid keys from // We filter the existing (in passFile) and non-valid keys from
// the below array keys that accept rgb values // the below array keys that accept rgb values
@@ -645,7 +645,7 @@ export class Pass implements PassIndexSignature {
* @returns {Object} - model path and filtered options * @returns {Object} - model path and filtered options
*/ */
_parseSettings(options) { _parseSettings(options: schema.PassInstance): { model: string, _props: Object } {
if (!schema.isValid(options, "instance")) { if (!schema.isValid(options, "instance")) {
throw new Error(formatMessage("REQUIR_VALID_FAILED")); throw new Error(formatMessage("REQUIR_VALID_FAILED"));
} }
@@ -667,7 +667,7 @@ export class Pass implements PassIndexSignature {
}; };
} }
set transitType(v) { set transitType(v: string) {
if (schema.isValid(v, "transitType")) { if (schema.isValid(v, "transitType")) {
this[transitType] = v; this[transitType] = v;
} else { } else {
@@ -676,7 +676,7 @@ export class Pass implements PassIndexSignature {
} }
} }
get transitType() { get transitType(): string {
return this[transitType]; return this[transitType];
} }
} }
@@ -767,7 +767,7 @@ function parsePEM(pemName, element, passphrase) {
* @return {Object[]} Object array barcodeDict compliant * @return {Object[]} Object array barcodeDict compliant
*/ */
function barcodesFromUncompleteData(origin) { function barcodesFromUncompleteData(origin: schema.Barcode): schema.Barcode[] {
if (!(origin.message && typeof origin.message === "string")) { if (!(origin.message && typeof origin.message === "string")) {
barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA")); barcodeDebug(formatMessage("BRC_AUTC_MISSING_DATA"));
return []; return [];
@@ -780,7 +780,7 @@ function barcodesFromUncompleteData(origin) {
"PKBarcodeFormatCode128" "PKBarcodeFormatCode128"
].map(format => ].map(format =>
schema.getValidated( schema.getValidated(
Object.assign(origin, { format }), Object.assign({}, origin, { format }),
"barcode" "barcode"
) )
); );

View File

@@ -430,7 +430,7 @@ export function isValid(opts: any, schemaName: keyof Schemas): boolean {
* @returns {object} the filtered value or empty object * @returns {object} the filtered value or empty object
*/ */
export function getValidated(opts: any, schemaName: keyof Schemas): Object { export function getValidated<T extends Object>(opts: any, schemaName: keyof Schemas): T {
let resolvedSchema = resolveSchemaName(schemaName); let resolvedSchema = resolveSchemaName(schemaName);
let validation = Joi.validate(opts, resolvedSchema, { stripUnknown: true }); let validation = Joi.validate(opts, resolvedSchema, { stripUnknown: true });