From c74e9bddc58e3be91c660790e3c1b8723c55d635 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Thu, 5 Dec 2019 23:58:26 +0100 Subject: [PATCH] Improved message type --- src/messages.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/messages.ts b/src/messages.ts index 7b9ee75..94f9cf0 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -1,8 +1,4 @@ -interface MessageGroup { - [key: string]: string; -} - -const errors: MessageGroup = { +const errors = { CP_INIT_ERROR: "Something went really bad in the %s initialization! Look at the log below this message. It should contain all the infos about the problem: \n%s", CP_NO_OPTS: "Cannot initialize the pass or abstract model creation: no options were passed.", CP_NO_CERTS: "Cannot initialize the pass creation: no valid certificates were passed.", @@ -19,7 +15,7 @@ const errors: MessageGroup = { NO_PASS_TYPE: "Cannot proceed with pass creation. Model definition (pass.json) has no valid type in it.\nRefer to https://apple.co/2wzyL5J to choose a valid pass type." }; -const debugMessages: MessageGroup = { +const debugMessages = { TRSTYPE_NOT_VALID: "Transit type changing rejected as not compliant with Apple Specifications. Transit type would become \"%s\" but should be in [PKTransitTypeAir, PKTransitTypeBoat, PKTransitTypeBus, PKTransitTypeGeneric, PKTransitTypeTrain]", BRC_NOT_SUPPORTED: "Format not found among barcodes. Cannot set backward compatibility.", BRC_FORMATTYPE_UNMATCH: "Format must be a string or null. Cannot set backward compatibility.", @@ -32,13 +28,15 @@ const debugMessages: MessageGroup = { PRS_REMOVED: "Personalization has been removed as it requires an NFC-enabled pass to work." }; +type AllMessages = keyof (typeof debugMessages & typeof errors); + /** * Creates a message with replaced values * @param {string} messageName * @param {any[]} values */ -export default function format(messageName: string, ...values: any[]) { +export default function format(messageName: AllMessages, ...values: any[]) { // reversing because it is better popping than shifting. let replaceValues = values.reverse(); return resolveMessageName(messageName).replace(/%s/g, () => { @@ -52,7 +50,7 @@ export default function format(messageName: string, ...values: any[]) { * @param {string} name */ -function resolveMessageName(name: string): string { +function resolveMessageName(name: AllMessages): string { if (!errors[name] && !debugMessages[name]) { return ``; }