@@ -2,16 +2,7 @@ import { headers } from 'next/headers';
2
2
import { redirect } from 'next/navigation' ;
3
3
import { GitBookAPI } from '@gitbook/api' ;
4
4
import { GITBOOK_API_TOKEN , GITBOOK_API_URL , GITBOOK_USER_AGENT } from '@v2/lib/env' ;
5
-
6
- /**
7
- * Generic context about rendering content.
8
- */
9
- export interface GitBookContext {
10
- /**
11
- * API client to fetch data from GitBook.
12
- */
13
- api : GitBookAPI ;
14
- }
5
+ import { GitBookContext } from '@/lib/v2/context' ;
15
6
16
7
/**
17
8
* Context when rendering a site.
@@ -28,18 +19,43 @@ export interface GitBookSiteContext extends GitBookContext {
28
19
siteId : string ;
29
20
}
30
21
22
+ /**
23
+ * Context when rendering a space in a site.
24
+ */
25
+ export interface GitBookSiteSpaceContext extends GitBookSiteContext {
26
+ /**
27
+ * ID of the space.
28
+ */
29
+ spaceId : string ;
30
+
31
+ /**
32
+ * ID of the site section.
33
+ */
34
+ siteSectionId : string | undefined ;
35
+
36
+ /**
37
+ * ID of the site space.
38
+ */
39
+ siteSpaceId : string ;
40
+
41
+ /**
42
+ * Share key of the site.
43
+ */
44
+ siteShareKey : string | undefined ;
45
+ }
46
+
31
47
/**
32
48
* Create a site context, when rendering a static page.
33
49
*/
34
- export async function createStaticSiteContext ( url : string [ ] ) : Promise < GitBookSiteContext > {
50
+ export async function createStaticSiteContext ( url : string [ ] ) : Promise < GitBookSiteSpaceContext > {
35
51
const context = createStaticContext ( ) ;
36
52
return fetchSiteContext ( url , context ) ;
37
53
}
38
54
39
55
/**
40
56
* Create a site context, when rendering a dynamic page.
41
57
*/
42
- export async function createDynamicSiteContext ( url : string [ ] ) : Promise < GitBookSiteContext > {
58
+ export async function createDynamicSiteContext ( url : string [ ] ) : Promise < GitBookSiteSpaceContext > {
43
59
const context = await createDynamicContext ( ) ;
44
60
return fetchSiteContext ( url , context ) ;
45
61
}
@@ -50,7 +66,7 @@ export async function createDynamicSiteContext(url: string[]): Promise<GitBookSi
50
66
async function fetchSiteContext (
51
67
urlParts : string [ ] ,
52
68
baseContext : GitBookContext ,
53
- ) : Promise < GitBookSiteContext > {
69
+ ) : Promise < GitBookSiteSpaceContext > {
54
70
const { api } = baseContext ;
55
71
const url = getURLFromParams ( urlParts ) ;
56
72
const { data } = await api . urls . getPublishedContentByUrl ( {
@@ -70,6 +86,9 @@ async function fetchSiteContext(
70
86
} ) ,
71
87
siteId : data . site ,
72
88
organizationId : data . organization ,
89
+ siteSectionId : data . siteSection ,
90
+ siteSpaceId : data . siteSpace ,
91
+ spaceId : data . space ,
73
92
} ;
74
93
}
75
94
@@ -83,9 +102,7 @@ function createStaticContext(): GitBookContext {
83
102
userAgent : GITBOOK_USER_AGENT ,
84
103
} ) ;
85
104
86
- return {
87
- api,
88
- } ;
105
+ return createContextFromClient ( api ) ;
89
106
}
90
107
91
108
/**
@@ -101,8 +118,36 @@ async function createDynamicContext(): Promise<GitBookContext> {
101
118
userAgent : GITBOOK_USER_AGENT ,
102
119
} ) ;
103
120
121
+ return createContextFromClient ( api ) ;
122
+ }
123
+
124
+ function createContextFromClient ( api : GitBookAPI ) : GitBookContext {
104
125
return {
105
126
api,
127
+ getLink : ( pathname : string ) => pathname ,
128
+ getPublishedContentSite : async ( organizationId : string , siteId : string ) => {
129
+ const { data } = await api . orgs . getPublishedContentSite ( organizationId , siteId ) ;
130
+ return data ;
131
+ } ,
132
+ getRevisionFile : async ( spaceId : string , revisionId : string , file : string ) => {
133
+ throw new Error ( 'Not implemented' ) ;
134
+ } ,
135
+ getUserById : async ( userId : string ) => {
136
+ const { data } = await api . users . getUserById ( userId ) ;
137
+ return data ;
138
+ } ,
139
+ getSpaceById : async ( spaceId : string , shareKey : string | undefined ) => {
140
+ const { data } = await api . spaces . getSpaceById ( spaceId , { shareKey } ) ;
141
+ return data ;
142
+ } ,
143
+ getCollection : async ( collectionId : string ) => {
144
+ const { data } = await api . collections . getCollectionById ( collectionId ) ;
145
+ return data ;
146
+ } ,
147
+ getReusableContent : async ( spaceId : string , revisionId : string , reusableContentId : string ) => {
148
+ const { data } = await api . spaces . getReusableContentInRevisionById ( spaceId , revisionId , reusableContentId ) ;
149
+ return data ;
150
+ } ,
106
151
} ;
107
152
}
108
153
0 commit comments