Skip to content

Commit ae12546

Browse files
authored
Add typed sub-feature settings (duckduckgo#2462)
* Support a `settings` property on subfeatures. This can be optionally typed, but will default to Record<string, string> * Add typings for Autofill feature. Includes an example of how we can now type subfeature settings, using * Rename d.ts to .ts * Add type for autofill javascriptConfig setting * Prettier
1 parent fd12550 commit ae12546

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

schema/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CookieFeature } from './features/cookie';
44
import { TrackerAllowlistFeature } from './features/tracker-allowlist';
55
import { WebCompatFeature } from './features/webcompat';
66
import { DuckPlayerFeature } from './features/duckplayer';
7+
import { AutofillFeature } from './features/autofill';
78

89
export { WebCompatSettings } from './features/webcompat';
910
export { DuckPlayerSettings } from './features/duckplayer';
@@ -25,6 +26,7 @@ export type ConfigV4<VersionType> = {
2526
features: Record<string, Feature<any, VersionType>> & {
2627
// These features have typed settings
2728
autoconsent: AutoconsentFeature<VersionType>;
29+
autofill: AutofillFeature<VersionType>;
2830
cookie: CookieFeature<VersionType>;
2931
duckPlayer: DuckPlayerFeature<VersionType>;
3032
trackerAllowlist: TrackerAllowlistFeature<VersionType>;

schema/feature.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ type FeatureMeta = {
1717
sampleExcludeRecords?: any;
1818
};
1919

20-
type SubFeature<VersionType> = {
20+
export type SubFeature<VersionType, SettingsType = Record<string, string>> = {
2121
state: FeatureState;
22+
settings?: SettingsType;
2223
rollout?: {
2324
steps: { percent: number }[];
2425
};
@@ -31,13 +32,17 @@ type SubFeature<VersionType> = {
3132
minSupportedVersion?: VersionType;
3233
};
3334

34-
export type Feature<SettingsType, VersionType> = {
35+
export type Feature<
36+
SettingsType,
37+
VersionType,
38+
SubFeatures extends Record<string, SubFeature<VersionType>> = Record<string, SubFeature<VersionType>>,
39+
> = {
3540
readme?: string;
3641
_meta?: FeatureMeta;
3742
state: FeatureState;
3843
exceptions: SiteException[];
3944
settings?: SettingsType;
40-
features?: Record<string, SubFeature<VersionType>>;
45+
features?: SubFeatures;
4146
hash: string;
4247
minSupportedVersion?: VersionType;
4348
};

schema/features/autofill.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { CSSInjectFeatureSettings, Feature, SubFeature } from '../feature';
2+
3+
// Type of the feature `settings` object
4+
type SettingsType = undefined;
5+
6+
type ButtonConfig = {
7+
shouldAutotap: boolean;
8+
path: string;
9+
selectors: string[];
10+
labelTexts: string[];
11+
};
12+
13+
type ImportFromGooglePasswordManager = {
14+
settingsButton: ButtonConfig;
15+
exportButton: ButtonConfig;
16+
signInButton: ButtonConfig;
17+
};
18+
19+
// Any subfeatures that have typed `settings` should be defined here.
20+
// Subfeatures without settings (or just string:string mappings for settings) will be automatically validated.
21+
type SubFeatures<VersionType> = {
22+
canImportFromGooglePasswordManager?: SubFeature<
23+
VersionType,
24+
{
25+
launchUrl: string;
26+
javascriptConfig: CSSInjectFeatureSettings<ImportFromGooglePasswordManager>;
27+
}
28+
>;
29+
};
30+
31+
export type AutofillFeature<VersionType> = Feature<
32+
SettingsType,
33+
VersionType,
34+
SubFeatures<VersionType> & Record<string, SubFeature<VersionType>>
35+
>;

0 commit comments

Comments
 (0)