@pantheon-systems/p1-next-sdk 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { getSharedP1Client, getSharedSiteId, getSharedBranchId, CSS_QUERY_ID_PREFIX, } from "@pantheon-systems/puck-css/server";
|
|
2
|
+
const FOLLOW_UP_PAGE_SIZE = 100;
|
|
3
|
+
const MAX_FOLLOW_UP_PAGES = 50;
|
|
2
4
|
const inflightQueries = new Map();
|
|
3
5
|
function listQueriesDeduped(endpoint, siteId, branchId) {
|
|
4
6
|
const key = `${siteId}:${branchId}`;
|
|
@@ -11,6 +13,55 @@ function listQueriesDeduped(endpoint, siteId, branchId) {
|
|
|
11
13
|
inflightQueries.set(key, promise);
|
|
12
14
|
return promise;
|
|
13
15
|
}
|
|
16
|
+
function documentIdOf(item) {
|
|
17
|
+
const id = item?.documentId;
|
|
18
|
+
return typeof id === "string" ? id : undefined;
|
|
19
|
+
}
|
|
20
|
+
async function getAllResults(queries, siteId, branchId, queryName, throwOnError) {
|
|
21
|
+
const firstPage = await queries.getResults(siteId, branchId, queryName, {
|
|
22
|
+
includeMetadata: true,
|
|
23
|
+
});
|
|
24
|
+
const totalCount = firstPage.totalCount;
|
|
25
|
+
if (typeof totalCount !== "number" || firstPage.items.length >= totalCount) {
|
|
26
|
+
return firstPage;
|
|
27
|
+
}
|
|
28
|
+
const items = [...firstPage.items];
|
|
29
|
+
const seen = new Set();
|
|
30
|
+
for (const item of items) {
|
|
31
|
+
const id = documentIdOf(item);
|
|
32
|
+
if (id !== undefined)
|
|
33
|
+
seen.add(id);
|
|
34
|
+
}
|
|
35
|
+
let received = firstPage.items.length;
|
|
36
|
+
for (let page = 0; page < MAX_FOLLOW_UP_PAGES && received < totalCount; page++) {
|
|
37
|
+
let next;
|
|
38
|
+
try {
|
|
39
|
+
next = await queries.getResults(siteId, branchId, queryName, {
|
|
40
|
+
includeMetadata: true,
|
|
41
|
+
limit: FOLLOW_UP_PAGE_SIZE,
|
|
42
|
+
offset: received,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
if (throwOnError)
|
|
47
|
+
throw err;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
if (next.items.length === 0)
|
|
51
|
+
break;
|
|
52
|
+
received += next.items.length;
|
|
53
|
+
for (const item of next.items) {
|
|
54
|
+
const id = documentIdOf(item);
|
|
55
|
+
if (id !== undefined) {
|
|
56
|
+
if (seen.has(id))
|
|
57
|
+
continue;
|
|
58
|
+
seen.add(id);
|
|
59
|
+
}
|
|
60
|
+
items.push(item);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { ...firstPage, items, returnedCount: items.length };
|
|
64
|
+
}
|
|
14
65
|
export async function createCssQueryFetchers(opts) {
|
|
15
66
|
const client = opts?.client ?? getSharedP1Client();
|
|
16
67
|
const siteId = getSharedSiteId();
|
|
@@ -35,9 +86,7 @@ export async function createCssQueryFetchers(opts) {
|
|
|
35
86
|
id: `${CSS_QUERY_ID_PREFIX}${query.name}`,
|
|
36
87
|
fetch: async () => {
|
|
37
88
|
try {
|
|
38
|
-
const results = await queries
|
|
39
|
-
includeMetadata: true,
|
|
40
|
-
});
|
|
89
|
+
const results = await getAllResults(queries, siteId, branchId, query.name, throwOnError);
|
|
41
90
|
return results;
|
|
42
91
|
}
|
|
43
92
|
catch (err) {
|
|
@@ -2,7 +2,8 @@ import { NextResponse } from "next/server";
|
|
|
2
2
|
import { normalizePath, ensureLeadingSlash, listRouteTemplateKeysFromDatabase, getPageEditorPreviewParams, loadRemoteDatasourceContext, createAuthenticatedClient, runWithAuthToken, } from "@pantheon-systems/puck-css/server";
|
|
3
3
|
import { extractBearerToken } from "../auth-utils";
|
|
4
4
|
import { createCssQueryFetchers } from "../css-query-fetchers";
|
|
5
|
-
const DATASOURCE_FETCH_TIMEOUT_MS =
|
|
5
|
+
const DATASOURCE_FETCH_TIMEOUT_MS = 15_000;
|
|
6
|
+
const NO_STORE = { "Cache-Control": "no-store" };
|
|
6
7
|
const inflightPreviewParams = new Map();
|
|
7
8
|
function getPreviewParamsDeduped(token, path) {
|
|
8
9
|
const key = `${token ?? ""}:${path}`;
|
|
@@ -21,7 +22,7 @@ export async function getDatasourceContext(request, options) {
|
|
|
21
22
|
const id = url.searchParams.get("id");
|
|
22
23
|
const path = rawPath === "/" ? "/" : normalizePath(ensureLeadingSlash(rawPath));
|
|
23
24
|
if (path === null) {
|
|
24
|
-
return NextResponse.json({ error: "invalid_path" }, { status: 400 });
|
|
25
|
+
return NextResponse.json({ error: "invalid_path" }, { status: 400, headers: NO_STORE });
|
|
25
26
|
}
|
|
26
27
|
const token = extractBearerToken(request);
|
|
27
28
|
const [routeTemplateKeys, savedPreviewParams] = await Promise.all([
|
|
@@ -58,8 +59,8 @@ export async function getDatasourceContext(request, options) {
|
|
|
58
59
|
if (id) {
|
|
59
60
|
const data = context[id] ?? {};
|
|
60
61
|
const failure = failures.find((f) => f.id === id);
|
|
61
|
-
return NextResponse.json(failure ? { id, data, error: { reason: failure.reason } } : { id, data });
|
|
62
|
+
return NextResponse.json(failure ? { id, data, error: { reason: failure.reason } } : { id, data }, { headers: NO_STORE });
|
|
62
63
|
}
|
|
63
64
|
const { urlParams: _urlParams, ...contexts } = context;
|
|
64
|
-
return NextResponse.json({ contexts, errors: failures });
|
|
65
|
+
return NextResponse.json({ contexts, errors: failures }, { headers: NO_STORE });
|
|
65
66
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pantheon-systems/p1-next-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"description": "Next.js SDK for P1 editor integration",
|
|
6
6
|
"publishConfig": {
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"launchdarkly-js-client-sdk": "^3.9.3",
|
|
46
46
|
"server-only": "^0.0.1",
|
|
47
|
-
"@pantheon-systems/css-client": "0.
|
|
48
|
-
"@pantheon-systems/p1-ai-chat": "0.
|
|
49
|
-
"@pantheon-systems/puck-css": "0.
|
|
47
|
+
"@pantheon-systems/css-client": "0.20.0",
|
|
48
|
+
"@pantheon-systems/p1-ai-chat": "0.10.0",
|
|
49
|
+
"@pantheon-systems/puck-css": "0.20.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@pantheon-systems/pds-toolkit-react": ">=2.0.0-alpha.92",
|