@preprio/toolkit 0.6.0-alpha.8 → 1.0.0-rc.1
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.
- package/README.md +111 -1363
- package/dist/astro/index.cjs +1555 -182
- package/dist/astro/index.d.cts +201 -1
- package/dist/astro/index.d.ts +201 -1
- package/dist/astro/index.js +1557 -29
- package/dist/{chunk-ATPB7GEC.js → chunk-33GWVTRJ.js} +1 -1
- package/dist/chunk-A2J7R4LR.js +256 -0
- package/dist/{chunk-T7ESBVBI.js → chunk-DSJ22P6J.js} +6 -1
- package/dist/chunk-KM2GME62.js +1255 -0
- package/dist/chunk-UTKH7DOI.js +58 -0
- package/dist/chunk-V7MCHPE7.js +85 -0
- package/dist/{chunk-2QGUP7VQ.js → chunk-WBMN4L7G.js} +1680 -579
- package/dist/chunk-Y7JWV7WV.js +374 -0
- package/dist/codegen/index.cjs +82 -0
- package/dist/codegen/index.d.cts +70 -0
- package/dist/codegen/index.d.ts +70 -0
- package/dist/codegen/index.js +64 -0
- package/dist/index.cjs +3729 -553
- package/dist/index.d.cts +403 -19
- package/dist/index.d.ts +403 -19
- package/dist/index.js +345 -24
- package/dist/live-preview-HCBNBC6C.js +19 -0
- package/dist/nextjs/components.cjs +1872 -729
- package/dist/nextjs/components.d.cts +5 -51
- package/dist/nextjs/components.d.ts +5 -51
- package/dist/nextjs/components.js +8 -274
- package/dist/nextjs/index.cjs +1228 -181
- package/dist/nextjs/index.d.cts +247 -92
- package/dist/nextjs/index.d.ts +247 -92
- package/dist/nextjs/index.js +1629 -111
- package/dist/nextjs/live-action.cjs +298 -0
- package/dist/nextjs/live-action.d.cts +12 -0
- package/dist/nextjs/live-action.d.ts +12 -0
- package/dist/nextjs/live-action.js +68 -0
- package/dist/nextjs/live.cjs +2010 -0
- package/dist/nextjs/live.d.cts +54 -0
- package/dist/nextjs/live.d.ts +54 -0
- package/dist/nextjs/live.js +287 -0
- package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
- package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
- package/dist/nuxt/index.cjs +15 -16
- package/dist/nuxt/index.d.cts +167 -6
- package/dist/nuxt/index.d.ts +167 -6
- package/dist/nuxt/index.js +402 -15
- package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
- package/dist/nuxt/live.cjs +1928 -0
- package/dist/nuxt/live.d.cts +45 -0
- package/dist/nuxt/live.d.ts +45 -0
- package/dist/nuxt/live.js +216 -0
- package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
- package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
- package/dist/react/components.cjs +1865 -480
- package/dist/react/components.d.cts +43 -8
- package/dist/react/components.d.ts +43 -8
- package/dist/react/components.js +1865 -480
- package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
- package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
- package/dist/sveltekit/index.cjs +15 -16
- package/dist/sveltekit/index.d.cts +111 -10
- package/dist/sveltekit/index.d.ts +111 -10
- package/dist/sveltekit/index.js +398 -15
- package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
- package/dist/sveltekit/live.cjs +1940 -0
- package/dist/sveltekit/live.d.cts +61 -0
- package/dist/sveltekit/live.d.ts +61 -0
- package/dist/sveltekit/live.js +220 -0
- package/package.json +132 -71
- package/src/astro/PreprToolbar.astro +7 -1
- package/src/astro/PreprTrackingPixel.astro +2 -4
- package/src/nuxt/PreprToolbar.vue +12 -1
- package/src/nuxt/PreprTrackingPixel.vue +2 -4
- package/src/sveltekit/PreprToolbar.svelte +17 -2
- package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
- package/dist/astro/chunk-R2YPGDVU.js +0 -173
- package/dist/astro/graphql.cjs +0 -1078
- package/dist/astro/graphql.d.cts +0 -300
- package/dist/astro/graphql.d.ts +0 -300
- package/dist/astro/graphql.js +0 -984
- package/dist/astro/index-XT_z-RYT.d.cts +0 -130
- package/dist/astro/index-XT_z-RYT.d.ts +0 -130
- package/dist/chunk-3NCEVJJ3.js +0 -10
- package/dist/chunk-OY7OKO6Q.js +0 -499
- package/dist/chunk-S5RWVMDJ.js +0 -148
- package/dist/graphql/codegen.cjs +0 -557
- package/dist/graphql/codegen.d.cts +0 -23
- package/dist/graphql/codegen.d.ts +0 -23
- package/dist/graphql/codegen.js +0 -99
- package/dist/graphql.cjs +0 -1020
- package/dist/graphql.d.cts +0 -335
- package/dist/graphql.d.ts +0 -335
- package/dist/graphql.js +0 -448
- package/dist/nextjs/chunk-O2SBSBDG.js +0 -549
- package/dist/nextjs/graphql.cjs +0 -1811
- package/dist/nextjs/graphql.d.cts +0 -498
- package/dist/nextjs/graphql.d.ts +0 -498
- package/dist/nextjs/graphql.js +0 -1397
- package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
- package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
- package/dist/nuxt/chunk-SX3MSVPK.js +0 -417
- package/dist/nuxt/graphql.cjs +0 -1085
- package/dist/nuxt/graphql.d.cts +0 -297
- package/dist/nuxt/graphql.d.ts +0 -297
- package/dist/nuxt/graphql.js +0 -985
- package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
- package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
- package/dist/pixel-DwW7cXJm.d.cts +0 -49
- package/dist/pixel-DwW7cXJm.d.ts +0 -49
- package/dist/server-Brr1UgtW.d.ts +0 -59
- package/dist/server-DJuS3YU0.d.cts +0 -59
- package/dist/sveltekit/chunk-UOASSHZO.js +0 -413
- package/dist/sveltekit/graphql.cjs +0 -1091
- package/dist/sveltekit/graphql.d.cts +0 -302
- package/dist/sveltekit/graphql.d.ts +0 -302
- package/dist/sveltekit/graphql.js +0 -991
- package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
- package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// src/core/live-preview/guards.ts
|
|
2
|
+
function isScalar(value) {
|
|
3
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
4
|
+
}
|
|
5
|
+
function isObject(value) {
|
|
6
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/core/live-preview/query-document.ts
|
|
10
|
+
var NAME_START = /[_A-Za-z]/;
|
|
11
|
+
var NAME_CONTINUE = /[_0-9A-Za-z]/;
|
|
12
|
+
var PUNCT = /* @__PURE__ */ new Set([
|
|
13
|
+
"!",
|
|
14
|
+
"$",
|
|
15
|
+
"&",
|
|
16
|
+
"(",
|
|
17
|
+
")",
|
|
18
|
+
":",
|
|
19
|
+
"=",
|
|
20
|
+
"@",
|
|
21
|
+
"[",
|
|
22
|
+
"]",
|
|
23
|
+
"{",
|
|
24
|
+
"|",
|
|
25
|
+
"}"
|
|
26
|
+
]);
|
|
27
|
+
function tokenize(source) {
|
|
28
|
+
const tokens = [];
|
|
29
|
+
let i = 0;
|
|
30
|
+
const n = source.length;
|
|
31
|
+
while (i < n) {
|
|
32
|
+
const ch = source[i];
|
|
33
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "," || ch === "\uFEFF") {
|
|
34
|
+
i++;
|
|
35
|
+
} else if (ch === "#") {
|
|
36
|
+
while (i < n && source[i] !== "\n" && source[i] !== "\r") i++;
|
|
37
|
+
} else if (ch === "." && source.startsWith("...", i)) {
|
|
38
|
+
tokens.push({ type: "punct", value: "..." });
|
|
39
|
+
i += 3;
|
|
40
|
+
} else if (PUNCT.has(ch)) {
|
|
41
|
+
tokens.push({ type: "punct", value: ch });
|
|
42
|
+
i++;
|
|
43
|
+
} else if (NAME_START.test(ch)) {
|
|
44
|
+
const start = i;
|
|
45
|
+
while (i < n && NAME_CONTINUE.test(source[i])) i++;
|
|
46
|
+
tokens.push({ type: "name", value: source.slice(start, i) });
|
|
47
|
+
} else if (ch === '"') {
|
|
48
|
+
if (source.startsWith('"""', i)) {
|
|
49
|
+
i += 3;
|
|
50
|
+
while (i < n && !source.startsWith('"""', i)) {
|
|
51
|
+
i += source.startsWith('\\"""', i) ? 4 : 1;
|
|
52
|
+
}
|
|
53
|
+
if (i >= n) throw new SyntaxError("Unterminated block string");
|
|
54
|
+
i += 3;
|
|
55
|
+
} else {
|
|
56
|
+
i++;
|
|
57
|
+
while (i < n && source[i] !== '"') {
|
|
58
|
+
if (source[i] === "\n" || source[i] === "\r") {
|
|
59
|
+
throw new SyntaxError("Unterminated string");
|
|
60
|
+
}
|
|
61
|
+
i += source[i] === "\\" ? 2 : 1;
|
|
62
|
+
}
|
|
63
|
+
if (i >= n) throw new SyntaxError("Unterminated string");
|
|
64
|
+
i++;
|
|
65
|
+
}
|
|
66
|
+
tokens.push({ type: "value" });
|
|
67
|
+
} else if (ch === "-" || ch >= "0" && ch <= "9") {
|
|
68
|
+
i++;
|
|
69
|
+
while (i < n && /[0-9.eE+-]/.test(source[i])) i++;
|
|
70
|
+
tokens.push({ type: "value" });
|
|
71
|
+
} else {
|
|
72
|
+
throw new SyntaxError(`Unexpected character "${ch}"`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return tokens;
|
|
76
|
+
}
|
|
77
|
+
function parseSource(source, operationName) {
|
|
78
|
+
const tokens = tokenize(source);
|
|
79
|
+
let pos = 0;
|
|
80
|
+
const peek = () => tokens[pos];
|
|
81
|
+
const isPunct = (value) => {
|
|
82
|
+
const t = tokens[pos];
|
|
83
|
+
return t?.type === "punct" && t.value === value;
|
|
84
|
+
};
|
|
85
|
+
const expectPunct = (value) => {
|
|
86
|
+
if (!isPunct(value)) throw new SyntaxError(`Expected "${value}"`);
|
|
87
|
+
pos++;
|
|
88
|
+
};
|
|
89
|
+
const expectName = () => {
|
|
90
|
+
const t = tokens[pos];
|
|
91
|
+
if (t?.type !== "name") throw new SyntaxError("Expected a name");
|
|
92
|
+
pos++;
|
|
93
|
+
return t.value;
|
|
94
|
+
};
|
|
95
|
+
const skipParens = () => {
|
|
96
|
+
expectPunct("(");
|
|
97
|
+
let depth = 1;
|
|
98
|
+
while (depth > 0) {
|
|
99
|
+
const t = tokens[pos++];
|
|
100
|
+
if (!t) throw new SyntaxError("Unbalanced parentheses");
|
|
101
|
+
if (t.type === "punct" && t.value === "(") depth++;
|
|
102
|
+
else if (t.type === "punct" && t.value === ")") depth--;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const skipDirectives = () => {
|
|
106
|
+
while (isPunct("@")) {
|
|
107
|
+
pos++;
|
|
108
|
+
expectName();
|
|
109
|
+
if (isPunct("(")) skipParens();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const parseSelectionSet = () => {
|
|
113
|
+
expectPunct("{");
|
|
114
|
+
const selections = [];
|
|
115
|
+
while (!isPunct("}")) {
|
|
116
|
+
if (!peek()) throw new SyntaxError("Unterminated selection set");
|
|
117
|
+
selections.push(parseSelection());
|
|
118
|
+
}
|
|
119
|
+
pos++;
|
|
120
|
+
return selections;
|
|
121
|
+
};
|
|
122
|
+
const parseSelection = () => {
|
|
123
|
+
if (isPunct("...")) {
|
|
124
|
+
pos++;
|
|
125
|
+
const t = peek();
|
|
126
|
+
if (t?.type === "name" && t.value !== "on") {
|
|
127
|
+
pos++;
|
|
128
|
+
skipDirectives();
|
|
129
|
+
return { kind: "spread", name: t.value };
|
|
130
|
+
}
|
|
131
|
+
let on = null;
|
|
132
|
+
if (t?.type === "name") {
|
|
133
|
+
pos++;
|
|
134
|
+
on = expectName();
|
|
135
|
+
}
|
|
136
|
+
skipDirectives();
|
|
137
|
+
return { kind: "inline", on, selections: parseSelectionSet() };
|
|
138
|
+
}
|
|
139
|
+
let name = expectName();
|
|
140
|
+
const key = name;
|
|
141
|
+
if (isPunct(":")) {
|
|
142
|
+
pos++;
|
|
143
|
+
name = expectName();
|
|
144
|
+
}
|
|
145
|
+
let hasArgs = false;
|
|
146
|
+
if (isPunct("(")) {
|
|
147
|
+
hasArgs = true;
|
|
148
|
+
skipParens();
|
|
149
|
+
}
|
|
150
|
+
skipDirectives();
|
|
151
|
+
const selections = isPunct("{") ? parseSelectionSet() : null;
|
|
152
|
+
return { kind: "field", name, key, hasArgs, selections };
|
|
153
|
+
};
|
|
154
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
155
|
+
const operations = [];
|
|
156
|
+
while (pos < tokens.length) {
|
|
157
|
+
if (isPunct("{")) {
|
|
158
|
+
operations.push({ name: null, selections: parseSelectionSet() });
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const keyword = expectName();
|
|
162
|
+
if (keyword === "fragment") {
|
|
163
|
+
const name = expectName();
|
|
164
|
+
if (expectName() !== "on") throw new SyntaxError('Expected "on"');
|
|
165
|
+
const on = expectName();
|
|
166
|
+
skipDirectives();
|
|
167
|
+
fragments.set(name, { on, selections: parseSelectionSet() });
|
|
168
|
+
} else if (keyword === "query" || keyword === "mutation" || keyword === "subscription") {
|
|
169
|
+
const t = peek();
|
|
170
|
+
let name = null;
|
|
171
|
+
if (t?.type === "name") name = expectName();
|
|
172
|
+
if (isPunct("(")) skipParens();
|
|
173
|
+
skipDirectives();
|
|
174
|
+
operations.push({ name, selections: parseSelectionSet() });
|
|
175
|
+
} else {
|
|
176
|
+
throw new SyntaxError(`Unexpected "${keyword}"`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
180
|
+
}
|
|
181
|
+
function pickOperation(operations, operationName) {
|
|
182
|
+
const op = operationName ? operations.find((o) => o.name === operationName) : operations[0];
|
|
183
|
+
if (!op) throw new SyntaxError("The document has no operation");
|
|
184
|
+
return op.selections;
|
|
185
|
+
}
|
|
186
|
+
function fromAstSelections(set) {
|
|
187
|
+
return set.selections.map((s) => {
|
|
188
|
+
if (s.kind === "FragmentSpread")
|
|
189
|
+
return { kind: "spread", name: s.name.value };
|
|
190
|
+
if (s.kind === "InlineFragment") {
|
|
191
|
+
return {
|
|
192
|
+
kind: "inline",
|
|
193
|
+
on: s.typeCondition?.name.value ?? null,
|
|
194
|
+
selections: fromAstSelections(s.selectionSet)
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const name = s.name.value;
|
|
198
|
+
return {
|
|
199
|
+
kind: "field",
|
|
200
|
+
name,
|
|
201
|
+
key: s.alias?.value ?? name,
|
|
202
|
+
hasArgs: Boolean(s.arguments?.length),
|
|
203
|
+
selections: s.selectionSet ? fromAstSelections(s.selectionSet) : null
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
function fromAst(doc, operationName) {
|
|
208
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
209
|
+
const operations = [];
|
|
210
|
+
for (const def of doc.definitions ?? []) {
|
|
211
|
+
if (def.kind === "FragmentDefinition" && def.name && def.typeCondition && def.selectionSet) {
|
|
212
|
+
fragments.set(def.name.value, {
|
|
213
|
+
on: def.typeCondition.name.value,
|
|
214
|
+
selections: fromAstSelections(def.selectionSet)
|
|
215
|
+
});
|
|
216
|
+
} else if (def.kind === "OperationDefinition" && def.selectionSet) {
|
|
217
|
+
operations.push({
|
|
218
|
+
name: def.name?.value ?? null,
|
|
219
|
+
selections: fromAstSelections(def.selectionSet)
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
224
|
+
}
|
|
225
|
+
var astCache = /* @__PURE__ */ new WeakMap();
|
|
226
|
+
var sourceCache = /* @__PURE__ */ new Map();
|
|
227
|
+
var SOURCE_CACHE_LIMIT = 200;
|
|
228
|
+
function readQueryDocument(query, operationName) {
|
|
229
|
+
const cacheKey = operationName ?? "";
|
|
230
|
+
if (typeof query === "object" && query !== null && query.kind === "Document") {
|
|
231
|
+
let byName = astCache.get(query);
|
|
232
|
+
if (!byName) astCache.set(query, byName = /* @__PURE__ */ new Map());
|
|
233
|
+
let parsed2 = byName.get(cacheKey);
|
|
234
|
+
if (!parsed2) {
|
|
235
|
+
parsed2 = fromAst(
|
|
236
|
+
query,
|
|
237
|
+
operationName
|
|
238
|
+
);
|
|
239
|
+
byName.set(cacheKey, parsed2);
|
|
240
|
+
}
|
|
241
|
+
return parsed2;
|
|
242
|
+
}
|
|
243
|
+
const source = String(query);
|
|
244
|
+
const sourceKey = `${cacheKey}
|
|
245
|
+
${source}`;
|
|
246
|
+
let parsed = sourceCache.get(sourceKey);
|
|
247
|
+
if (!parsed) {
|
|
248
|
+
parsed = parseSource(source, operationName);
|
|
249
|
+
if (sourceCache.size >= SOURCE_CACHE_LIMIT) {
|
|
250
|
+
sourceCache.delete(sourceCache.keys().next().value);
|
|
251
|
+
}
|
|
252
|
+
sourceCache.set(sourceKey, parsed);
|
|
253
|
+
}
|
|
254
|
+
return parsed;
|
|
255
|
+
}
|
|
256
|
+
var fieldsCache = /* @__PURE__ */ new WeakMap();
|
|
257
|
+
function collectFields(selections, typename, doc, options = {}) {
|
|
258
|
+
const cacheKey = `${options.strict ? "s" : "l"}:${typename ?? ""}`;
|
|
259
|
+
let cached = fieldsCache.get(selections);
|
|
260
|
+
if (!cached) {
|
|
261
|
+
cached = /* @__PURE__ */ new Map();
|
|
262
|
+
fieldsCache.set(selections, cached);
|
|
263
|
+
}
|
|
264
|
+
const hit = cached.get(cacheKey);
|
|
265
|
+
if (hit) return hit;
|
|
266
|
+
const fields = resolveFields(selections, typename, doc, options);
|
|
267
|
+
cached.set(cacheKey, fields);
|
|
268
|
+
return fields;
|
|
269
|
+
}
|
|
270
|
+
function resolveFields(selections, typename, doc, options) {
|
|
271
|
+
const exact = /* @__PURE__ */ new Map();
|
|
272
|
+
const loose = /* @__PURE__ */ new Map();
|
|
273
|
+
const merged = /* @__PURE__ */ new Map();
|
|
274
|
+
const walkedMatched = /* @__PURE__ */ new Set();
|
|
275
|
+
const walkedLoose = /* @__PURE__ */ new Set();
|
|
276
|
+
const add = (target, s) => {
|
|
277
|
+
let field = target.get(s.key);
|
|
278
|
+
if (!field) {
|
|
279
|
+
field = { name: s.name, key: s.key, hasArgs: s.hasArgs, selections: [] };
|
|
280
|
+
target.set(s.key, field);
|
|
281
|
+
merged.set(field, /* @__PURE__ */ new Set());
|
|
282
|
+
}
|
|
283
|
+
const done = merged.get(field);
|
|
284
|
+
if (s.selections && !done.has(s.selections)) {
|
|
285
|
+
done.add(s.selections);
|
|
286
|
+
field.selections = [...field.selections, ...s.selections];
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
const applies = (on, list) => conditionMatches(on, typename) || typename !== void 0 && typeConditions(list, doc).includes(typename);
|
|
290
|
+
const walk = (list, matched) => {
|
|
291
|
+
for (const s of list) {
|
|
292
|
+
if (s.kind === "field") {
|
|
293
|
+
add(matched ? exact : loose, s);
|
|
294
|
+
} else if (s.kind === "inline") {
|
|
295
|
+
walk(s.selections, matched && applies(s.on, s.selections));
|
|
296
|
+
} else {
|
|
297
|
+
const fragment = doc.fragments.get(s.name);
|
|
298
|
+
if (!fragment) continue;
|
|
299
|
+
const asMatched = matched && applies(fragment.on, fragment.selections);
|
|
300
|
+
if (walkedMatched.has(fragment)) continue;
|
|
301
|
+
if (!asMatched && walkedLoose.has(fragment)) continue;
|
|
302
|
+
(asMatched ? walkedMatched : walkedLoose).add(fragment);
|
|
303
|
+
walk(fragment.selections, asMatched);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
walk(selections, true);
|
|
308
|
+
if (!options.strict) {
|
|
309
|
+
for (const [key, field] of loose) {
|
|
310
|
+
if (!exact.has(key)) exact.set(key, field);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return exact;
|
|
314
|
+
}
|
|
315
|
+
function conditionMatches(on, typename) {
|
|
316
|
+
return on === null || typename === void 0 || on === typename;
|
|
317
|
+
}
|
|
318
|
+
var conditionsCache = /* @__PURE__ */ new WeakMap();
|
|
319
|
+
function typeConditions(selections, doc) {
|
|
320
|
+
const hit = conditionsCache.get(selections);
|
|
321
|
+
if (hit) return hit;
|
|
322
|
+
const names = /* @__PURE__ */ new Set();
|
|
323
|
+
const seen = /* @__PURE__ */ new Set();
|
|
324
|
+
const walk = (list) => {
|
|
325
|
+
for (const s of list) {
|
|
326
|
+
if (s.kind === "inline") {
|
|
327
|
+
if (s.on) names.add(s.on);
|
|
328
|
+
walk(s.selections);
|
|
329
|
+
} else if (s.kind === "spread") {
|
|
330
|
+
const fragment = doc.fragments.get(s.name);
|
|
331
|
+
if (!fragment || seen.has(fragment)) continue;
|
|
332
|
+
seen.add(fragment);
|
|
333
|
+
names.add(fragment.on);
|
|
334
|
+
walk(fragment.selections);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
walk(selections);
|
|
339
|
+
const conditions = [...names];
|
|
340
|
+
conditionsCache.set(selections, conditions);
|
|
341
|
+
return conditions;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export {
|
|
345
|
+
isScalar,
|
|
346
|
+
isObject,
|
|
347
|
+
readQueryDocument,
|
|
348
|
+
collectFields,
|
|
349
|
+
typeConditions
|
|
350
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/core/constants.ts
|
|
2
|
+
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
3
|
+
var COOKIE_SEGMENT = "Prepr-Segments";
|
|
4
|
+
var COOKIE_VARIANT = "Prepr-ABtesting";
|
|
5
|
+
var COOKIE_UID = "__prepr_uid";
|
|
6
|
+
var COOKIE_HUBSPOT = "hubspotutk";
|
|
7
|
+
var PARAM_SEGMENT = "prepr_preview_segment";
|
|
8
|
+
var PARAM_VARIANT = "prepr_preview_ab";
|
|
9
|
+
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
10
|
+
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
11
|
+
var ATTR_ENCODED = "data-prepr-encoded";
|
|
12
|
+
var ENCODED_SELECTOR = `[${ATTR_ENCODED}]`;
|
|
13
|
+
var EVENT_INIT = "prepr:initVE";
|
|
14
|
+
var EVENT_ITEM_CHANGED = "prepr:itemChanged";
|
|
15
|
+
var EVENT_FIELD_CHANGE = "prepr:fieldChange";
|
|
16
|
+
var EVENT_LIST_CHANGE = "prepr:listChange";
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
COOKIE_PREVIEW_MODE,
|
|
20
|
+
COOKIE_SEGMENT,
|
|
21
|
+
COOKIE_VARIANT,
|
|
22
|
+
COOKIE_UID,
|
|
23
|
+
COOKIE_HUBSPOT,
|
|
24
|
+
PARAM_SEGMENT,
|
|
25
|
+
PARAM_VARIANT,
|
|
26
|
+
PARAM_HIDE_BAR,
|
|
27
|
+
ONE_YEAR_SECONDS,
|
|
28
|
+
EVENT_INIT,
|
|
29
|
+
EVENT_ITEM_CHANGED,
|
|
30
|
+
EVENT_FIELD_CHANGE,
|
|
31
|
+
EVENT_LIST_CHANGE
|
|
32
|
+
};
|
package/dist/nuxt/index.cjs
CHANGED
|
@@ -30,13 +30,12 @@ __export(nuxt_exports, {
|
|
|
30
30
|
getPreprUUID: () => getPreprUUID,
|
|
31
31
|
getToolbarProps: () => getToolbarProps,
|
|
32
32
|
handlePreprRequest: () => handlePreprRequest,
|
|
33
|
-
isPersonalizedResponse: () => isPersonalizedResponse,
|
|
34
33
|
validatePreprToken: () => validatePreprToken
|
|
35
34
|
});
|
|
36
35
|
module.exports = __toCommonJS(nuxt_exports);
|
|
37
36
|
|
|
38
37
|
// src/version.ts
|
|
39
|
-
var VERSION = "0.
|
|
38
|
+
var VERSION = "1.0.0-rc.1";
|
|
40
39
|
|
|
41
40
|
// src/core/constants.ts
|
|
42
41
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -48,6 +47,8 @@ var PARAM_SEGMENT = "prepr_preview_segment";
|
|
|
48
47
|
var PARAM_VARIANT = "prepr_preview_ab";
|
|
49
48
|
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
50
49
|
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
50
|
+
var ATTR_ENCODED = "data-prepr-encoded";
|
|
51
|
+
var ENCODED_SELECTOR = `[${ATTR_ENCODED}]`;
|
|
51
52
|
|
|
52
53
|
// src/core/features.ts
|
|
53
54
|
function isEnabled(config) {
|
|
@@ -76,6 +77,16 @@ var UTM_PARAMS = [
|
|
|
76
77
|
"utm_campaign"
|
|
77
78
|
];
|
|
78
79
|
var MAX_HEADER_VALUE_LENGTH = 2048;
|
|
80
|
+
function randomUUID() {
|
|
81
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
82
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
83
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
84
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
85
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
86
|
+
""
|
|
87
|
+
);
|
|
88
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
89
|
+
}
|
|
79
90
|
function sanitizeHeaderValue(value) {
|
|
80
91
|
const stripped = value.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
81
92
|
return stripped.length > MAX_HEADER_VALUE_LENGTH ? stripped.slice(0, MAX_HEADER_VALUE_LENGTH) : stripped;
|
|
@@ -161,7 +172,7 @@ function processFrameworkRequest(request, opts, framework) {
|
|
|
161
172
|
}
|
|
162
173
|
let uid = sanitizeHeaderValue(requestCookies.get(COOKIE_UID) ?? "");
|
|
163
174
|
if (!uid) {
|
|
164
|
-
uid =
|
|
175
|
+
uid = randomUUID();
|
|
165
176
|
responseCookies.push({
|
|
166
177
|
name: COOKIE_UID,
|
|
167
178
|
value: uid,
|
|
@@ -230,10 +241,6 @@ function processFrameworkRequest(request, opts, framework) {
|
|
|
230
241
|
});
|
|
231
242
|
}
|
|
232
243
|
}
|
|
233
|
-
if (requestHeaders.has("Prepr-Segments")) {
|
|
234
|
-
requestHeaders.delete("Prepr-Customer-Id");
|
|
235
|
-
requestHeaders.delete("Prepr-Customer-Id-Created");
|
|
236
|
-
}
|
|
237
244
|
return { requestHeaders, responseCookies };
|
|
238
245
|
}
|
|
239
246
|
|
|
@@ -431,7 +438,7 @@ async function getToolbarPropsFromHeaders(headers, token, features) {
|
|
|
431
438
|
};
|
|
432
439
|
}
|
|
433
440
|
|
|
434
|
-
// src/
|
|
441
|
+
// src/core/request-helpers.ts
|
|
435
442
|
function getPreprUUID(headers) {
|
|
436
443
|
return getPreprUUIDFromHeaders(headers);
|
|
437
444
|
}
|
|
@@ -447,13 +454,6 @@ function getPreprHeaders(headers) {
|
|
|
447
454
|
async function getToolbarProps(headers, token, features) {
|
|
448
455
|
return getToolbarPropsFromHeaders(headers, token, features);
|
|
449
456
|
}
|
|
450
|
-
|
|
451
|
-
// src/core/personalization.ts
|
|
452
|
-
var PERSONALIZED_VARY = /\bPrepr-(Segments|ABTesting)/i;
|
|
453
|
-
function isPersonalizedResponse(source) {
|
|
454
|
-
const headers = "headers" in source ? source.headers : source;
|
|
455
|
-
return PERSONALIZED_VARY.test(headers.get("vary") ?? "");
|
|
456
|
-
}
|
|
457
457
|
// Annotate the CommonJS export names for ESM import in node:
|
|
458
458
|
0 && (module.exports = {
|
|
459
459
|
PREPR_CONTEXT_KEY,
|
|
@@ -466,6 +466,5 @@ function isPersonalizedResponse(source) {
|
|
|
466
466
|
getPreprUUID,
|
|
467
467
|
getToolbarProps,
|
|
468
468
|
handlePreprRequest,
|
|
469
|
-
isPersonalizedResponse,
|
|
470
469
|
validatePreprToken
|
|
471
470
|
});
|
package/dist/nuxt/index.d.cts
CHANGED
|
@@ -1,5 +1,144 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/** A segment from Prepr, as listed in the toolbar's segment picker. */
|
|
2
|
+
interface PreprSegment {
|
|
3
|
+
readonly _id: string;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Per-feature config. `false` is shorthand for `{ enabled: false }`; the object
|
|
8
|
+
* form leaves room for per-feature options later without a breaking change.
|
|
9
|
+
*/
|
|
10
|
+
type PreprFeatureConfig = boolean | {
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
};
|
|
13
|
+
/** Toolkit features a consumer can turn off. Omitted keys stay enabled. */
|
|
14
|
+
interface PreprFeatures {
|
|
15
|
+
/** Segment picker, `Prepr-Segments` header and its cookie/query param. */
|
|
16
|
+
segments?: PreprFeatureConfig;
|
|
17
|
+
/** A/B variant switch, `Prepr-ABtesting` header and its cookie/query param. */
|
|
18
|
+
abTesting?: PreprFeatureConfig;
|
|
19
|
+
/**
|
|
20
|
+
* The site's own click-to-edit: the toolbar's Edit mode control, the stega
|
|
21
|
+
* tooltip/overlay and the close-edit pill.
|
|
22
|
+
*
|
|
23
|
+
* NOT a security control, and NOT the Prepr editor's live preview. When the
|
|
24
|
+
* site is framed by the editor, `prepr:initVE` still activates edit mode —
|
|
25
|
+
* that is the CMS operating inside its own iframe, not an affordance offered
|
|
26
|
+
* to visitors. See iframe-bridge.ts.
|
|
27
|
+
*/
|
|
28
|
+
editMode?: PreprFeatureConfig;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The data the toolbar shows. `getToolbarProps` builds it on the server from
|
|
32
|
+
* the request headers.
|
|
33
|
+
*/
|
|
34
|
+
interface PreprToolbarProps {
|
|
35
|
+
/**
|
|
36
|
+
* Server-resolved active segment. Optional: omit it with `segments`
|
|
37
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
38
|
+
* the persisted cookie is used.
|
|
39
|
+
*/
|
|
40
|
+
readonly activeSegment?: string | null;
|
|
41
|
+
/**
|
|
42
|
+
* Server-resolved active A/B variant. Optional: omit it with `abTesting`
|
|
43
|
+
* disabled, where there is nothing to resolve. When enabled and omitted,
|
|
44
|
+
* the persisted cookie is used.
|
|
45
|
+
*/
|
|
46
|
+
readonly activeVariant?: string | null;
|
|
47
|
+
/** Available segments to personalize on. */
|
|
48
|
+
readonly segments?: readonly PreprSegment[];
|
|
49
|
+
/** @deprecated Alias for `segments`. */
|
|
50
|
+
readonly data?: readonly PreprSegment[];
|
|
51
|
+
}
|
|
52
|
+
/** The two sides of an A/B test. */
|
|
53
|
+
type PreprVariant = 'A' | 'B';
|
|
54
|
+
/**
|
|
55
|
+
* The headers to send with a Prepr GraphQL request so the API personalizes
|
|
56
|
+
* the response for this visitor. `getPreprHeaders` returns them; spread them
|
|
57
|
+
* into the request's `headers`.
|
|
58
|
+
*/
|
|
59
|
+
interface PreprHeaders {
|
|
60
|
+
readonly 'prepr-customer-id'?: string;
|
|
61
|
+
readonly 'Prepr-Segments'?: string;
|
|
62
|
+
readonly 'Prepr-ABtesting'?: PreprVariant;
|
|
63
|
+
readonly 'Prepr-Preview-Bar'?: 'true';
|
|
64
|
+
readonly 'Prepr-Context-utm_source'?: string;
|
|
65
|
+
readonly 'Prepr-Context-utm_medium'?: string;
|
|
66
|
+
readonly 'Prepr-Context-utm_term'?: string;
|
|
67
|
+
readonly 'Prepr-Context-utm_content'?: string;
|
|
68
|
+
readonly 'Prepr-Context-utm_campaign'?: string;
|
|
69
|
+
readonly 'Prepr-Context-initial_referral'?: string;
|
|
70
|
+
readonly 'Prepr-Visitor-IP'?: string;
|
|
71
|
+
readonly 'Prepr-Hubspot-Id'?: string;
|
|
72
|
+
readonly 'Prepr-Customer-Id-Created'?: 'true';
|
|
73
|
+
/** The visitor's browser User-Agent, used by the API for device detection. */
|
|
74
|
+
readonly 'User-Agent'?: string;
|
|
75
|
+
/** @deprecated Never populated; the visitor's value is under `User-Agent`. */
|
|
76
|
+
readonly 'Prepr-User-Agent'?: string;
|
|
77
|
+
readonly 'Prepr-Package'?: string;
|
|
78
|
+
}
|
|
79
|
+
/** Why a `PreprError` was thrown; read it from `error.code`. */
|
|
80
|
+
type PreprErrorCode = 'INVALID_TOKEN' | 'MISSING_TOKEN' | 'HTTP_ERROR' | 'FETCH_ERROR' | 'INVALID_RESPONSE' | 'CONTEXT_ERROR';
|
|
81
|
+
|
|
82
|
+
/** Options for the Prepr middleware of every framework. */
|
|
83
|
+
interface PreprMiddlewareOptions {
|
|
84
|
+
/** Enable preview-mode headers (segment/AB overrides, preview bar). */
|
|
85
|
+
preview?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Disable features app-wide. Pass the same object you give the toolbar: a
|
|
88
|
+
* disabled feature injects no header and persists no cookie, whatever the
|
|
89
|
+
* request carries.
|
|
90
|
+
*/
|
|
91
|
+
features?: PreprFeatures;
|
|
92
|
+
/** Override the version reported in Prepr-Package (mainly for tests). */
|
|
93
|
+
version?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Structural stand-in for h3's `H3Event`, so this package needs no build-time
|
|
98
|
+
* dependency on `h3`/`nuxt`. Only the fields `handlePreprRequest` uses.
|
|
99
|
+
*
|
|
100
|
+
* `node` ties this to Nitro's Node.js preset (the Nuxt default). Edge presets
|
|
101
|
+
* expose no raw Node request/response to fold headers back onto.
|
|
102
|
+
*/
|
|
103
|
+
interface H3EventLike {
|
|
104
|
+
node: {
|
|
105
|
+
req: {
|
|
106
|
+
url?: string;
|
|
107
|
+
headers: Record<string, string | string[] | undefined>;
|
|
108
|
+
};
|
|
109
|
+
res: {
|
|
110
|
+
/** Node's `ServerResponse.appendHeader` (Node >= 18.3). */
|
|
111
|
+
appendHeader(name: string, value: string): unknown;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
/** Request-scoped bag shared with route handlers and Vue via `useRequestEvent`. */
|
|
115
|
+
context: Record<string, unknown>;
|
|
116
|
+
}
|
|
117
|
+
/** Key under which `handlePreprRequest` mirrors the forwarded Prepr `Headers`. */
|
|
118
|
+
declare const PREPR_CONTEXT_KEY = "prepr";
|
|
119
|
+
/**
|
|
120
|
+
* Nuxt server middleware entry point — call it inside `defineEventHandler` in
|
|
121
|
+
* `server/middleware/prepr.ts`:
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* export default defineEventHandler(event => {
|
|
125
|
+
* handlePreprRequest(event, { preview: import.meta.dev });
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* The computed Prepr headers are folded back onto the raw Node request
|
|
130
|
+
* (lowercased, as Node stores them), which is what `useRequestHeaders()` and
|
|
131
|
+
* downstream handlers read. The same `Headers` are mirrored onto
|
|
132
|
+
* `event.context.prepr` for callers who prefer an explicit read
|
|
133
|
+
* (`getPreprHeadersFromEvent`). Cookies are appended to the response.
|
|
134
|
+
*/
|
|
135
|
+
declare function handlePreprRequest(event: H3EventLike, options?: PreprMiddlewareOptions): void;
|
|
136
|
+
/**
|
|
137
|
+
* Personalization headers to forward to your GraphQL fetch, from the h3 event.
|
|
138
|
+
* Empty `Headers` if `handlePreprRequest` did not run, so the result is always
|
|
139
|
+
* safe to spread.
|
|
140
|
+
*/
|
|
141
|
+
declare function getPreprHeadersFromEvent(event: Pick<H3EventLike, 'context'>): Headers;
|
|
3
142
|
|
|
4
143
|
/** Returns the Prepr Customer ID from the request headers. */
|
|
5
144
|
declare function getPreprUUID(headers: Headers): string | null;
|
|
@@ -10,10 +149,32 @@ declare function getActiveVariant(headers: Headers): string | null;
|
|
|
10
149
|
/** Returns all Prepr headers from the request headers. */
|
|
11
150
|
declare function getPreprHeaders(headers: Headers): PreprHeaders;
|
|
12
151
|
/**
|
|
13
|
-
* Props needed to mount `PreprToolbar
|
|
14
|
-
* Prepr GraphQL token.
|
|
15
|
-
*
|
|
152
|
+
* Props needed to mount the `PreprToolbar` component, from the request
|
|
153
|
+
* headers plus a Prepr GraphQL token.
|
|
154
|
+
*
|
|
155
|
+
* Ungated: call it only when your app is in preview mode. Resolve that however
|
|
156
|
+
* your deployment already does — the toolkit reads no environment variables of
|
|
157
|
+
* its own.
|
|
16
158
|
*/
|
|
17
159
|
declare function getToolbarProps(headers: Headers, token: string, features?: PreprFeatures): Promise<PreprToolbarProps>;
|
|
18
160
|
|
|
19
|
-
|
|
161
|
+
/** Error thrown by the token helpers and the segments fetch. */
|
|
162
|
+
declare class PreprError extends Error {
|
|
163
|
+
readonly code: PreprErrorCode;
|
|
164
|
+
readonly context?: string | undefined;
|
|
165
|
+
readonly originalError?: Error | undefined;
|
|
166
|
+
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Asserts `token` is the full `https://graphql.prepr.io/<token>` URL.
|
|
170
|
+
* @throws PreprError if the token is missing or not an HTTPS URL.
|
|
171
|
+
*/
|
|
172
|
+
declare function validatePreprToken(token: string): void;
|
|
173
|
+
/**
|
|
174
|
+
* Pulls the access token out of a GraphQL URL: `https://graphql.prepr.io/abc123`
|
|
175
|
+
* -> `abc123`.
|
|
176
|
+
* @throws PreprError if the URL is not a valid `graphql.prepr.io` URL.
|
|
177
|
+
*/
|
|
178
|
+
declare function extractAccessToken(graphqlUrl: string): string;
|
|
179
|
+
|
|
180
|
+
export { type H3EventLike, PREPR_CONTEXT_KEY, PreprError, type PreprMiddlewareOptions, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprHeadersFromEvent, getPreprUUID, getToolbarProps, handlePreprRequest, validatePreprToken };
|