Skip to content

Commit

Permalink
Improve features types
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire committed Jun 26, 2024
1 parent 12ee06f commit 7590d88
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions vite/features.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
import merge from 'ts-deepmerge'
import ValidLanguages from '../src/i18n/languages.mjs'

// Define constants for valid values
const ValidLogins = ['web2', 'web3', 'recovery'] as const
const ValidCensus = ['spreadsheet', 'token', 'web3', 'csp', 'gitcoin'] as const
const UnimplementedCensuses = ['phone', 'email', 'crm', 'database', 'digital_certificate'] as const
const ValidVotingTypes = ['single', 'approval'] as const
const UnimplementedVotingTypes = ['multi', 'participatory', 'borda'] as const

// Convert constant arrays to union types
type Login = (typeof ValidLogins)[number]
type Census = (typeof ValidCensus)[number]
type UnimplementedCensus = (typeof UnimplementedCensuses)[number]
type VotingType = (typeof ValidVotingTypes)[number]
type UnimplementedVotingType = (typeof UnimplementedVotingTypes)[number]
type Language = (typeof ValidLanguages)[number]

type Features = {
faucet: boolean
vote: {
anonymous: boolean
overwrite: boolean
secret: boolean
customization: boolean
}
login: Login[]
census: Census[]
unimplemented_census: UnimplementedCensus[]
voting_type: VotingType[]
unimplemented_voting_type: UnimplementedVotingType[]
languages: Language[]
_census: {
spreadsheet: boolean
token: boolean
web3: boolean
csp: boolean
gitcoin: boolean
}
}

type Defaults = Omit<Features, '_census'>

const features = () => {
const defaults = {
const defaults: Defaults = {
faucet: true,
vote: {
anonymous: true,
Expand All @@ -14,19 +55,22 @@ const features = () => {
unimplemented_census: [],
voting_type: ['single', 'approval'],
unimplemented_voting_type: [],
languages: ['ca', 'en', 'es'],
languages: ValidLanguages,
}

const features = merge.withOptions({ mergeArrays: false }, defaults, JSON.parse(process.env.FEATURES || '{}'))
const unimplemented_census = ['phone', 'email', 'crm', 'database', 'digital_certificate']
const unimplemented_voting_type = ['multi', 'participatory', 'borda']
const features = merge.withOptions(
{ mergeArrays: false },
defaults,
JSON.parse(process.env.FEATURES || '{}')
) as unknown as Features

features.unimplemented_census.forEach((el) => {
if (!unimplemented_census.includes(el)) throw new Error(`Unimplemented census ${el} does not exist`)
if (!UnimplementedCensuses.includes(el)) throw new Error(`Unimplemented census ${el} does not exist`)
})
features.unimplemented_voting_type.forEach((el) => {
if (!unimplemented_voting_type.includes(el)) throw new Error(`Unimplemented voting type ${el} does not exist`)
if (!UnimplementedVotingTypes.includes(el)) throw new Error(`Unimplemented voting type ${el} does not exist`)
})

// Ensure at least one item is loaded in each feature array
if (!features.login.length) {
features.login = ['web3']
Expand All @@ -37,14 +81,15 @@ const features = () => {
if (!features.languages.length) {
features.languages = ['en']
}
// verify login options are valid
const validLogins = ['web2', 'web3', 'recovery']

// Verify login options are valid
features.login.forEach((login) => {
if (!validLogins.includes(login)) {
if (!ValidLogins.includes(login)) {
throw new Error(`Invalid login option: ${login}`)
}
})
// We need pure booleans in order to ensure rollup tree-shakes non enabled features.

// We need pure booleans in order to ensure rollup tree-shakes non-enabled features.
// Using functions like `.includes()` would prevent such tree-shaking, resulting in a bigger bundle.
features._census = {
spreadsheet: false,
Expand Down

3 comments on commit 7590d88

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸŽ‰ Published on https://onvote-stg.netlify.app as production
πŸš€ Deployed on https://667beb6b76cc4b1cb2b994e8--onvote-stg.netlify.app

Please sign in to comment.