Skip to content

Commit fcc33a7

Browse files
committed
fix: surface macos_prefs as top-level field in config API response
GET /api/configs/[slug] now extracts macos_prefs from the snapshot object and includes it at the top level of the config response. This ensures exported configs contain macos_prefs in a format that the CLI import can read directly. Also hoists snapshot JSON parse to a single call site (was parsed twice) and adds error logging for failed snapshot parsing.
1 parent 317874a commit fcc33a7

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/routes/api/configs/[slug]/+server.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@ export const GET: RequestHandler = async ({ platform, cookies, params, request }
2525
const rawPkgs = JSON.parse((config.packages as string) || '[]');
2626
const needsTypeInference = rawPkgs.length > 0 && typeof rawPkgs[0] === 'string';
2727

28-
let caskSet = new Set<string>();
29-
if (needsTypeInference && config.snapshot) {
28+
// Parse snapshot once and reuse for cask inference and macos_prefs extraction.
29+
let parsedSnapshot: Record<string, unknown> | null = null;
30+
if (config.snapshot) {
3031
try {
31-
const snapshot = JSON.parse(config.snapshot as string);
32-
const casks: string[] = snapshot.packages?.casks || [];
33-
for (const c of casks) {
34-
caskSet.add(c);
35-
}
36-
} catch {}
32+
parsedSnapshot = JSON.parse(config.snapshot as string);
33+
} catch (err) {
34+
console.error(`[api/configs] failed to parse snapshot for slug ${params.slug}:`, err);
35+
}
36+
}
37+
38+
let caskSet = new Set<string>();
39+
if (needsTypeInference && parsedSnapshot) {
40+
const casks: string[] = (parsedSnapshot as any).packages?.casks || [];
41+
for (const c of casks) {
42+
caskSet.add(c);
43+
}
3744
}
3845

3946
const packages = rawPkgs.map((p: any) => {
@@ -46,20 +53,36 @@ export const GET: RequestHandler = async ({ platform, cookies, params, request }
4653
return p;
4754
});
4855

49-
let parsedSnapshot = null;
50-
if (config.snapshot) {
51-
try {
52-
parsedSnapshot = JSON.parse(config.snapshot as string);
53-
} catch {
54-
parsedSnapshot = null;
56+
let macosPrefs: { domain: string; key: string; type?: string; value: string; desc?: string }[] | null = null;
57+
if (parsedSnapshot) {
58+
const rawPrefs = (parsedSnapshot as any).macos_prefs;
59+
if (Array.isArray(rawPrefs) && rawPrefs.length > 0) {
60+
const filtered = rawPrefs
61+
.filter(
62+
(p: unknown): p is Record<string, unknown> =>
63+
typeof p === 'object' &&
64+
p !== null &&
65+
typeof (p as Record<string, unknown>).domain === 'string' &&
66+
typeof (p as Record<string, unknown>).key === 'string' &&
67+
typeof (p as Record<string, unknown>).value === 'string'
68+
)
69+
.map((p: Record<string, unknown>) => ({
70+
domain: p.domain as string,
71+
key: p.key as string,
72+
type: typeof p.type === 'string' ? p.type : '',
73+
value: p.value as string,
74+
desc: typeof p.desc === 'string' ? p.desc : ''
75+
}));
76+
if (filtered.length > 0) macosPrefs = filtered;
5577
}
5678
}
5779

5880
return json({
5981
config: {
6082
...config,
6183
packages,
62-
snapshot: parsedSnapshot
84+
snapshot: parsedSnapshot,
85+
macos_prefs: macosPrefs
6386
},
6487
install_url: installUrl
6588
});

0 commit comments

Comments
 (0)