bellstrand/totp-generator

Expose algorithm values as a type

Closed this issue · 2 comments

Would you mind exposing the valid algorithm values as a type rather than an anonymous list of strings. This will make it easier to write code that takes the algorithm type as input. Currently to make our code typesafe we have to copy the valid values from the Options type into the code we are writing.

If the definition of Options was changed to the example below it would be much nicer to work with and would not require any changes if you add/remove algorithms in the future.

type AlgorithmOptions = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512";

type Options = {
	digits?: number
	algorithm?:  AlgorithmOptions
	period?: number
	timestamp?: number
}

Thanks for this library btw :)

@kragoth I could do that in the next update if it makes things convenient.
And in the meantime, you could always pick out the type from the function like this
type AlgorithmOptions = NonNullable<NonNullable<Parameters<typeof TOTP.generate>[1]>["algorithm"]>

@kragoth I could do that in the next update if it makes things convenient. And in the meantime, you could always pick out the type from the function like this type AlgorithmOptions = NonNullable<NonNullable<Parameters<typeof TOTP.generate>[1]>["algorithm"]>

Oh wow, I did not know you could do that!
Thanks!