Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typescript type definition #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
type DirectLyEnabled = boolean
felipesere marked this conversation as resolved.
Show resolved Hide resolved
type EnabledEnvironments = { [k: string]: boolean }
export type Flag = {
enabled: DirectLyEnabled | EnabledEnvironments
description: string
name?: string
}

type CurrentEnvironment = Readonly<{ current: string }>
type AvailableEnvironments = string[]
type Environments = Readonly<
{ available: AvailableEnvironments } & CurrentEnvironment
>

type FlagWithEnvironment = Readonly<{
environment_flag: string
}>

export interface Config {
flags: { [key: string]: Flag }
environments?: Environments
sources?: Source[]
}

type SourceFn = (flag: Flag) => boolean
type SourceObject = Readonly<{ isEnabled: SourceFn }>
type Source = SourceFn | SourceObject

type State = Flag[]
type ChangedFlag<TFlagName> = { flag: TFlagName; originalValue: boolean }

interface TestBox<TFlagName> {
enable: (flag: TFlagName) => void
disable: (flag: TFlagName) => void
reset: () => void
}

type SyncTogglingClosure<TFlagName> = <TResult>(
name: TFlagName,
closure: () => TResult
) => TResult
type AsyncTogglingClosure<TFlagName> = <TResult>(
name: TFlagName,
closure: () => Promise<TResult>
) => Promise<TResult>

export interface FeatureFlags<
TConfig extends Config,
TFlagName extends keyof TConfig['flags'] & string
> {
isEnabled: (key: TFlagName) => boolean
summary: () => string
state: () => State
enabling: SyncTogglingClosure<TFlagName>
disabling: SyncTogglingClosure<TFlagName>
enablingAsync: AsyncTogglingClosure<TFlagName>
disablingAsync: AsyncTogglingClosure<TFlagName>
testBox: () => TestBox<TFlagName>
}
export declare function wrap<
TConfig extends Config,
TFlagName extends keyof TConfig['flags'] & string
>(config: TConfig): FeatureFlags<TConfig, TFlagName>

export declare namespace sources {
export function processEnvSource(flag: any): boolean
}
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const errors = {
unknown_env_enabled: (flagName, envInFlag, configuredEnvs) =>
`The feature flag ${flagName} is configured for ${envInFlag} that is not listed in environments.available: ${configuredEnvs}. Please check if this is an error.`,
missing_environment: () =>
'You need to configure which environments are available to your application under config.environments.available as an array of strings. This allows us to check your config for consistency'
'You need to configure which environments are available to your application under config.environments.available as an array of strings. This allows us to check your config for consistency',
}

const checkConsistency = (config) => {
Expand Down Expand Up @@ -94,8 +94,8 @@ module.exports = {
}

return {
isEnabled: function(flagName) {
flag = flags[flagName]
isEnabled: function (flagName) {
const flag = flags[flagName]
if (flag) {
flag.name = flagName
return (
Expand All @@ -108,41 +108,41 @@ module.exports = {
}
},

state: function() {
state: function () {
return Object.keys(flags).map((flagName) => {
return {
name: flagName,
enabled: this.isEnabled(flagName),
description: flags[flagName].description
description: flags[flagName].description,
}
})
},

enabling: function(flagName, closure) {
enabling: function (flagName, closure) {
return toggle(this, flagName, { to: true, around: closure })
},

disabling: function(flagName, closure) {
disabling: function (flagName, closure) {
return toggle(this, flagName, { to: false, around: closure })
},

enablingAsync: async function(flagName, closure) {
enablingAsync: async function (flagName, closure) {
return toggleAsync(this, flagName, { to: true, around: closure })
},

disablingAsync: async function(flagName, closure) {
disablingAsync: async function (flagName, closure) {
return toggleAsync(this, flagName, { to: false, around: closure })
},

testBox: function() {
testBox: function () {
const features = this
let changedFlags = []

const set = (flagName, { to: value }) => {
const isEnabled = features.isEnabled(flagName)
changedFlags = changedFlags.concat({
flag: flagName,
originalValue: isEnabled
originalValue: isEnabled,
})
flags[flagName].enabled = value
}
Expand All @@ -158,15 +158,15 @@ module.exports = {
)

changedFlags = []
}
},
}
}
},
}
},
sources: {
processEnvSource: (flag) => {
const value = process.env[flag.environment_flag]
return value === '1' || value === 'true'
}
}
},
},
}
Loading