Improved message type

This commit is contained in:
Alexander Cerutti
2019-12-05 23:58:26 +01:00
parent 356e68378a
commit c74e9bddc5

View File

@@ -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 `<ErrorName "${name}" is not linked to any error messages>`;
}