-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathconfig.ts
More file actions
169 lines (153 loc) · 4.58 KB
/
config.ts
File metadata and controls
169 lines (153 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import Z from 'zod';
import { localeCodeSchema } from './locales';
import { bucketTypeSchema } from './formats';
// common
export const localeSchema = Z.object({
source: localeCodeSchema,
targets: Z.array(localeCodeSchema),
});
// factories
type ConfigDefinition<T extends Z.ZodRawShape, P extends Z.ZodRawShape> = {
schema: Z.ZodObject<T>;
defaultValue: Z.infer<Z.ZodObject<T>>;
parse: (rawConfig: unknown) => Z.infer<Z.ZodObject<T>>;
};
const createConfigDefinition = <T extends Z.ZodRawShape, P extends Z.ZodRawShape>(
definition: ConfigDefinition<T, P>,
) => definition;
type ConfigDefinitionExtensionParams<T extends Z.ZodRawShape, P extends Z.ZodRawShape> = {
createSchema: (baseSchema: Z.ZodObject<P>) => Z.ZodObject<T>;
createDefaultValue: (baseDefaultValue: Z.infer<Z.ZodObject<P>>) => Z.infer<Z.ZodObject<T>>;
createUpgrader: (
config: Z.infer<Z.ZodObject<P>>,
schema: Z.ZodObject<T>,
defaultValue: Z.infer<Z.ZodObject<T>>,
) => Z.infer<Z.ZodObject<T>>;
};
const extendConfigDefinition = <T extends Z.ZodRawShape, P extends Z.ZodRawShape>(
definition: ConfigDefinition<P, any>,
params: ConfigDefinitionExtensionParams<T, P>,
) => {
const schema = params.createSchema(definition.schema);
const defaultValue = params.createDefaultValue(definition.defaultValue);
const upgrader = (config: Z.infer<Z.ZodObject<P>>) => params.createUpgrader(config, schema, defaultValue);
return createConfigDefinition({
schema,
defaultValue,
parse: (rawConfig) => {
const safeResult = schema.safeParse(rawConfig);
if (safeResult.success) {
return safeResult.data;
}
const baseConfig = definition.parse(rawConfig);
const result = upgrader(baseConfig);
return result;
}
});
};
// any -> v0
const configV0Schema = Z.object({
version: Z.number().default(0),
});
export const configV0Definition = createConfigDefinition({
schema: configV0Schema,
defaultValue: { version: 0 },
parse: (rawConfig) => {
return configV0Schema.parse(rawConfig);
},
});
// v0 -> v1
export const configV1Definition = extendConfigDefinition(configV0Definition, {
createSchema: (baseSchema) => baseSchema.extend({
locale: localeSchema,
buckets: Z.record(
Z.string(),
bucketTypeSchema,
).default({}).optional(),
}),
createDefaultValue: () => ({
version: 1,
locale: {
source: 'en' as const,
targets: [
'es' as const,
],
},
buckets: {},
}),
createUpgrader: () => ({
version: 1,
locale: {
source: 'en' as const,
targets: [
'es' as const,
],
},
buckets: {},
}),
});
// v1 -> v1.1
export const configV1_1Definition = extendConfigDefinition(configV1Definition, {
createSchema: (baseSchema) => baseSchema.extend({
buckets: Z.record(
bucketTypeSchema,
Z.object({
include: Z.array(Z.string()).default([]),
exclude: Z.array(Z.string()).default([]).optional(),
}),
).default({}),
}),
createDefaultValue: (baseDefaultValue) => ({
...baseDefaultValue,
version: 1.1,
buckets: {},
}),
createUpgrader: (oldConfig, schema) => {
const upgradedConfig: Z.infer<typeof schema> = {
...oldConfig,
version: 1.1,
buckets: {},
};
// Transform buckets from v1 to v1.1 format
if (oldConfig.buckets) {
for (const [bucketPath, bucketType] of Object.entries(oldConfig.buckets)) {
if (!upgradedConfig.buckets[bucketType]) {
upgradedConfig.buckets[bucketType] = {
include: [],
};
}
upgradedConfig.buckets[bucketType]?.include.push(bucketPath);
}
}
return upgradedConfig;
},
});
// v1.1 -> v1.2
// Changes: Add "extraSource" optional field to the locale node of the config
export const configV1_2Definition = extendConfigDefinition(configV1_1Definition, {
createSchema: (baseSchema) => baseSchema.extend({
locale: localeSchema.extend({
extraSource: localeCodeSchema.optional(),
}),
}),
createDefaultValue: (baseDefaultValue) => ({
...baseDefaultValue,
version: 1.2,
}),
createUpgrader: (oldConfig) => ({
...oldConfig,
version: 1.2,
}),
});
// exports
const LATEST_CONFIG_DEFINITION = configV1_2Definition;
export type I18nConfig = Z.infer<typeof LATEST_CONFIG_DEFINITION['schema']>;
export function parseI18nConfig(rawConfig: unknown) {
try {
const result = LATEST_CONFIG_DEFINITION.parse(rawConfig);
return result;
} catch (error: any) {
throw new Error(`Failed to parse config: ${error.message}`);
}
}
export const defaultConfig = LATEST_CONFIG_DEFINITION.defaultValue;