-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: transform config to have proper Date objects
- Loading branch information
Showing
6 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ConfigBody } from '@devcycle/types' | ||
|
||
/** | ||
* Transform the config to ensure all dates are valid Date objects | ||
* @param configData | ||
* @returns | ||
*/ | ||
export const transformConfig = (configData: ConfigBody): ConfigBody => { | ||
if (!configData.features || !configData.features.length) return configData | ||
|
||
configData.features = configData.features.map((feature) => { | ||
if (!feature || !feature.configuration.targets.length) return feature | ||
|
||
feature.configuration.targets = feature.configuration.targets.map( | ||
(target) => { | ||
const { rollout } = target | ||
if (!rollout) return target | ||
|
||
rollout.startDate = transformDate(rollout.startDate) | ||
if (!rollout.stages || !rollout.stages.length) return target | ||
|
||
rollout.stages = rollout.stages.map((stage) => { | ||
stage.date = transformDate(stage.date) | ||
return stage | ||
}) | ||
return target | ||
}, | ||
) | ||
return feature | ||
}) | ||
return configData | ||
} | ||
|
||
const transformDate = (date: unknown): Date => { | ||
if (!date) throw new Error('Missing rollout date in config') | ||
if (date instanceof Date) return date | ||
if (typeof date === 'string' || typeof date === 'number') | ||
return new Date(date) | ||
throw new Error('Missing rollout date in config') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters