Skip to content

Commit 6ca6239

Browse files
committed
Restrict serialized fields
1 parent 4b16b8b commit 6ca6239

File tree

1 file changed

+82
-9
lines changed

1 file changed

+82
-9
lines changed

packages/gitbook/src/components/AdminToolbar/AdminToolbar.tsx

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,49 @@ export interface AdminToolbarProps {
66
context: GitBookSiteContext;
77
}
88

9-
// Serializable version of GitBookSiteContext (excludes functions)
10-
export type SerializableGitBookSiteContext = Omit<
11-
GitBookSiteContext,
12-
'linker' | 'imageResizer' | 'dataFetcher'
13-
>;
9+
// Minimal types containing only the fields needed for AdminToolbar to restrict what gets serialized
10+
export type MinimalChangeRequest = {
11+
id: string;
12+
number: number;
13+
subject: string | null;
14+
revision: string;
15+
updatedAt: string;
16+
createdBy: {
17+
displayName: string;
18+
};
19+
urls: {
20+
app: string;
21+
};
22+
};
23+
24+
export type MinimalRevision = {
25+
createdAt: string;
26+
urls: {
27+
app: string;
28+
};
29+
git?: {
30+
url: string | undefined;
31+
} | null;
32+
};
33+
34+
export type AdminToolbarContext = {
35+
space: {
36+
id: string;
37+
revision: string;
38+
};
39+
changeRequest: MinimalChangeRequest | null;
40+
revision: MinimalRevision;
41+
revisionId: string;
42+
site: {
43+
title: string;
44+
urls: {
45+
published: string | undefined;
46+
};
47+
};
48+
};
1449

1550
export interface AdminToolbarClientProps {
16-
context: SerializableGitBookSiteContext;
51+
context: AdminToolbarContext;
1752
}
1853

1954
/**
@@ -29,9 +64,47 @@ export async function AdminToolbar(props: AdminToolbarProps) {
2964
}
3065

3166
if (context.changeRequest || context.revisionId !== context.space.revision) {
32-
// Create a serializable version of the context by removing function-containing objects
33-
const { linker, imageResizer, dataFetcher, ...serializableContext } = context;
67+
// Create a minimal context with only the fields needed for AdminToolbar
68+
const minimalContext: AdminToolbarContext = {
69+
space: {
70+
id: context.space.id,
71+
revision: context.space.revision,
72+
},
73+
changeRequest: context.changeRequest
74+
? {
75+
id: context.changeRequest.id,
76+
number: context.changeRequest.number,
77+
subject: context.changeRequest.subject,
78+
revision: context.changeRequest.revision,
79+
updatedAt: context.changeRequest.updatedAt,
80+
createdBy: {
81+
displayName: context.changeRequest.createdBy.displayName,
82+
},
83+
urls: {
84+
app: context.changeRequest.urls.app,
85+
},
86+
}
87+
: null,
88+
revision: {
89+
createdAt: context.revision.createdAt,
90+
urls: {
91+
app: context.revision.urls.app,
92+
},
93+
git: context.revision.git
94+
? {
95+
url: context.revision.git.url,
96+
}
97+
: null,
98+
},
99+
revisionId: context.revisionId,
100+
site: {
101+
title: context.site.title,
102+
urls: {
103+
published: context.site.urls.published,
104+
},
105+
},
106+
};
34107

35-
return <AdminToolbarClient context={serializableContext} />;
108+
return <AdminToolbarClient context={minimalContext} />;
36109
}
37110
}

0 commit comments

Comments
 (0)