-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathv1.ts
405 lines (349 loc) · 12.2 KB
/
v1.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import {
type GitBookBaseContext,
type GitBookSiteContext,
fetchSiteContextByIds,
fetchSpaceContextByIds,
} from '@v2/lib/context';
import type { GitBookDataFetcher } from '@v2/lib/data/types';
import { createImageResizer } from '@v2/lib/images';
import { createLinker } from '@v2/lib/links';
import { RevisionPageType } from '@gitbook/api';
import { DataFetcherError, wrapDataFetcherError } from '@v2/lib/data';
import { headers } from 'next/headers';
import {
type SiteContentPointer,
type SpaceContentPointer,
api,
getChangeRequest,
getComputedDocument,
getDocument,
getEmbedByUrlInSpace,
getLatestOpenAPISpecVersionContent,
getPublishedContentSite,
getReusableContent,
getRevision,
getRevisionFile,
getRevisionPageByPath,
getRevisionPages,
getSiteRedirectBySource,
getSpace,
getUserById,
renderIntegrationUi,
searchSiteContent,
} from './api';
import { getDynamicCustomizationSettings } from './customization';
import { withLeadingSlash, withTrailingSlash } from './paths';
import { assertIsNotV2 } from './v2';
/*
* Code that will be used until the migration to v2 is complete.
*/
/**
* Get the base context for the V1.
*/
export async function getV1BaseContext(): Promise<GitBookBaseContext> {
const host = await getHost();
const basePath = await getBasePath();
const siteBasePath = await getSiteBasePath();
const linker = createLinker({
host,
spaceBasePath: basePath,
siteBasePath: siteBasePath,
});
// On V1, we use hard-navigation between different spaces because of layout issues
linker.toLinkForContent = (url) => {
return url;
};
const dataFetcher = await getDataFetcherV1();
const imageResizer = createImageResizer({
imagesContextId: host,
// In V1, we always resize at the top level of the hostname, not relative to the content.
linker: createLinker({
host,
spaceBasePath: '/',
siteBasePath: '/',
}),
});
return {
linker,
dataFetcher,
imageResizer,
};
}
/**
* Data fetcher that uses the old code of the v1.
* Try not to use this as much as possible, and instead take the data fetcher from the props.
* This data fetcher should only be used at the top of the tree.
*/
async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
const dataFetcher: GitBookDataFetcher = {
async api() {
const result = await api();
return result.client;
},
withToken() {
// In v1, the token is global and controlled by the middleware.
// We don't need to do anything special here.
return dataFetcher;
},
getUserById(userId) {
return wrapDataFetcherError(async () => {
const user = await getUserById(userId);
if (!user) {
throw new DataFetcherError('User not found', 404);
}
return user;
});
},
getPublishedContentSite(params) {
return wrapDataFetcherError(async () => {
return getPublishedContentSite(params);
});
},
getSpace(params) {
return wrapDataFetcherError(async () => {
return getSpace(params.spaceId, params.shareKey);
});
},
getChangeRequest(params) {
return wrapDataFetcherError(async () => {
const changeRequest = await getChangeRequest(
params.spaceId,
params.changeRequestId
);
if (!changeRequest) {
throw new DataFetcherError('Change request not found', 404);
}
return changeRequest;
});
},
getRevision(params) {
return wrapDataFetcherError(async () => {
const { tags, ...revision } = await getRevision(params.spaceId, params.revisionId, {
metadata: params.metadata,
computed: false,
});
if (
Object.values(revision.pages).some(
(page) => page.type === RevisionPageType.Computed
)
) {
const { tags: _tags, ...revision } = await getRevision(
params.spaceId,
params.revisionId,
{
metadata: params.metadata,
computed: true,
tags,
}
);
return revision;
}
return revision;
});
},
getRevisionFile(params) {
return wrapDataFetcherError(async () => {
const revisionFile = await getRevisionFile(
params.spaceId,
params.revisionId,
params.fileId
);
if (!revisionFile) {
throw new DataFetcherError('Revision file not found', 404);
}
return revisionFile;
});
},
getRevisionPageMarkdown() {
throw new Error('Not implemented in v1');
},
getDocument(params) {
return wrapDataFetcherError(async () => {
const document = await getDocument(params.spaceId, params.documentId);
if (!document) {
throw new DataFetcherError('Document not found', 404);
}
return document;
});
},
getComputedDocument(params) {
return wrapDataFetcherError(() => {
return getComputedDocument(
params.organizationId,
params.spaceId,
params.source,
params.seed
);
});
},
getRevisionPages(params) {
return wrapDataFetcherError(async () => {
const { pages, tags } = await getRevisionPages(params.spaceId, params.revisionId, {
metadata: params.metadata,
computed: false,
});
if (pages.some((page) => page.type === RevisionPageType.Computed)) {
const { pages } = await getRevisionPages(params.spaceId, params.revisionId, {
metadata: params.metadata,
computed: true,
tags,
});
return pages;
}
return pages;
});
},
getRevisionPageByPath(params) {
return wrapDataFetcherError(async () => {
const revisionPage = await getRevisionPageByPath(
params.spaceId,
params.revisionId,
params.path
);
if (!revisionPage) {
throw new DataFetcherError('Revision page not found', 404);
}
return revisionPage;
});
},
getReusableContent(params) {
return wrapDataFetcherError(async () => {
const reusableContent = await getReusableContent(
params.spaceId,
params.revisionId,
params.reusableContentId
);
if (!reusableContent) {
throw new DataFetcherError('Reusable content not found', 404);
}
return reusableContent;
});
},
getLatestOpenAPISpecVersionContent(params) {
return wrapDataFetcherError(async () => {
const openAPISpecVersionContent = await getLatestOpenAPISpecVersionContent(
params.organizationId,
params.slug
);
if (!openAPISpecVersionContent) {
throw new DataFetcherError('OpenAPI spec version content not found', 404);
}
return openAPISpecVersionContent;
});
},
getSiteRedirectBySource(params) {
return wrapDataFetcherError(async () => {
const siteRedirect = await getSiteRedirectBySource(params);
if (!siteRedirect) {
throw new DataFetcherError('Site redirect not found', 404);
}
return siteRedirect;
});
},
getEmbedByUrl(params) {
return wrapDataFetcherError(() => {
return getEmbedByUrlInSpace(params.spaceId, params.url);
});
},
searchSiteContent(params) {
return wrapDataFetcherError(async () => {
const { organizationId, siteId, query, cacheBust, scope } = params;
const result = await searchSiteContent(
organizationId,
siteId,
query,
scope,
cacheBust
);
return result.items;
});
},
renderIntegrationUi(params) {
return wrapDataFetcherError(async () => {
const result = await renderIntegrationUi(params.integrationName, params.request);
return result;
});
},
streamAIResponse() {
throw new Error('Not implemented in v1');
},
};
return dataFetcher;
}
/**
* Fetch the context for a site pointer.
*/
export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer) {
const baseContext = await getV1BaseContext();
const context = await fetchSiteContextByIds(baseContext, {
organization: pointer.organizationId,
site: pointer.siteId,
siteSection: pointer.siteSectionId,
siteSpace: pointer.siteSpaceId,
space: pointer.spaceId,
shareKey: pointer.siteShareKey,
changeRequest: pointer.changeRequestId,
revision: pointer.revisionId,
});
context.customization = await getDynamicCustomizationSettings(context.customization);
return context;
}
/**
* Fetch the context for a space pointer.
*/
export async function fetchV1ContextForSpacePointer(pointer: SpaceContentPointer) {
const baseContext = await getV1BaseContext();
return fetchSpaceContextByIds(baseContext, {
space: pointer.spaceId,
shareKey: undefined,
changeRequest: pointer.changeRequestId,
revision: pointer.revisionId,
});
}
/**
* Get the site pointer (ids) from a site context.
*/
export function getSitePointerFromContext(context: GitBookSiteContext): SiteContentPointer {
return {
organizationId: context.organizationId,
siteId: context.site.id,
siteSectionId: context.sections?.current?.id,
siteSpaceId: context.siteSpace.id,
spaceId: context.space.id,
revisionId: context.revisionId,
changeRequestId: context.changeRequest?.id,
siteShareKey: context.shareKey,
};
}
/**
* Return the base path for the current request.
* The value will start and finish with /
*/
async function getBasePath(): Promise<string> {
assertIsNotV2();
const headersList = await headers();
const path = headersList.get('x-gitbook-basepath') ?? '/';
return withTrailingSlash(withLeadingSlash(path));
}
/**
* Return the site base path for the current request.
* The value will start and finish with /
*/
async function getSiteBasePath(): Promise<string> {
assertIsNotV2();
const headersList = await headers();
const path = headersList.get('x-gitbook-site-basepath') ?? '/';
return withTrailingSlash(withLeadingSlash(path));
}
/**
* Return the current host for the current request.
*/
async function getHost(): Promise<string> {
assertIsNotV2();
const headersList = await headers();
const mode = headersList.get('x-gitbook-mode');
if (mode === 'proxy') {
return headersList.get('x-forwarded-host') ?? '';
}
return headersList.get('x-gitbook-host') ?? headersList.get('host') ?? '';
}