@wpnuxt/core 2.0.0-alpha.1 → 2.0.0-alpha.3
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/dist/module.json +1 -1
- package/dist/module.mjs +203 -35
- package/dist/runtime/composables/usePrevNextPost.d.ts +0 -0
- package/dist/runtime/composables/usePrevNextPost.js +22 -0
- package/dist/runtime/composables/useWPContent.js +5 -1
- package/dist/runtime/plugins/graphqlConfig.js +2 -3
- package/dist/runtime/plugins/graphqlErrors.js +1 -1
- package/dist/runtime/queries/Viewer.gql +4 -4
- package/dist/runtime/queries/fragments/GeneralSettings.fragment.gql +0 -1
- package/dist/runtime/types/stub.d.ts +24 -0
- package/package.json +3 -3
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { defu } from 'defu';
|
|
2
|
-
import { promises, cpSync, existsSync, statSync,
|
|
2
|
+
import { promises, cpSync, existsSync, statSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
4
|
import { join } from 'node:path';
|
|
4
5
|
import { useLogger, createResolver, resolveFiles, defineNuxtModule, addPlugin, addImports, addComponentsDir, addTemplate, addTypeTemplate, hasNuxtModule, installModule } from '@nuxt/kit';
|
|
5
6
|
import { upperFirst } from 'scule';
|
|
6
7
|
import { ref } from 'vue';
|
|
7
8
|
import { parse, GraphQLError } from 'graphql';
|
|
9
|
+
import { execSync } from 'node:child_process';
|
|
8
10
|
|
|
9
11
|
function randHashGenerator(length = 12) {
|
|
10
12
|
return Math.random().toString(36).substring(2, 2 + length).toUpperCase().padEnd(length, "0");
|
|
@@ -63,16 +65,18 @@ const _parseDoc = async (doc) => {
|
|
|
63
65
|
throw error;
|
|
64
66
|
}
|
|
65
67
|
};
|
|
66
|
-
function processSelections(selections, level, query) {
|
|
68
|
+
function processSelections(selections, level, query, canExtract = true) {
|
|
67
69
|
if (!selections || selections.length === 0) return;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
const firstSelection = selections[0];
|
|
71
|
+
const hasSingleField = selections.length === 1 && firstSelection?.kind === "Field";
|
|
72
|
+
if (hasSingleField && canExtract && firstSelection.kind === "Field") {
|
|
73
|
+
query.nodes?.push(firstSelection.name.value.trim());
|
|
70
74
|
}
|
|
71
75
|
selections.forEach((s) => {
|
|
72
76
|
if (s.kind === "FragmentSpread") {
|
|
73
77
|
query.fragments?.push(s.name.value.trim());
|
|
74
78
|
} else if (s.selectionSet?.selections) {
|
|
75
|
-
processSelections(s.selectionSet.selections, level + 1, query);
|
|
79
|
+
processSelections(s.selectionSet.selections, level + 1, query, canExtract && hasSingleField);
|
|
76
80
|
}
|
|
77
81
|
});
|
|
78
82
|
}
|
|
@@ -109,13 +113,9 @@ async function prepareContext(ctx) {
|
|
|
109
113
|
const fragmentSuffix = q.fragments?.length && q.nodes?.includes("nodes") ? "[]" : "";
|
|
110
114
|
return q.fragments?.length ? q.fragments.map((f) => `${f}Fragment${fragmentSuffix}`).join(" | ") : "any";
|
|
111
115
|
};
|
|
112
|
-
const queryFnExp = (q, typed = false
|
|
113
|
-
const
|
|
114
|
-
const functionName = lazy ? `useLazy${q.name}` : baseName;
|
|
116
|
+
const queryFnExp = (q, typed = false) => {
|
|
117
|
+
const functionName = fnName(q.name);
|
|
115
118
|
if (!typed) {
|
|
116
|
-
if (lazy) {
|
|
117
|
-
return `export const ${functionName} = (params, options) => useWPContent('${q.name}', [${formatNodes(q.nodes)}], false, params, { ...options, lazy: true })`;
|
|
118
|
-
}
|
|
119
119
|
return `export const ${functionName} = (params, options) => useWPContent('${q.name}', [${formatNodes(q.nodes)}], false, params, options)`;
|
|
120
120
|
}
|
|
121
121
|
return ` export const ${functionName}: (params?: ${q.name}QueryVariables, options?: WPContentOptions) => WPContentResult<${getFragmentType(q)}>`;
|
|
@@ -130,8 +130,7 @@ async function prepareContext(ctx) {
|
|
|
130
130
|
ctx.generateImports = () => {
|
|
131
131
|
const imports = [];
|
|
132
132
|
queries.forEach((f) => {
|
|
133
|
-
imports.push(queryFnExp(f, false
|
|
134
|
-
imports.push(queryFnExp(f, false, true));
|
|
133
|
+
imports.push(queryFnExp(f, false));
|
|
135
134
|
});
|
|
136
135
|
mutations.forEach((m) => {
|
|
137
136
|
imports.push(mutationFnExp(m, false));
|
|
@@ -191,8 +190,7 @@ async function prepareContext(ctx) {
|
|
|
191
190
|
"declare module '#wpnuxt' {"
|
|
192
191
|
];
|
|
193
192
|
queries.forEach((f) => {
|
|
194
|
-
declarations.push(queryFnExp(f, true
|
|
195
|
-
declarations.push(queryFnExp(f, true, true));
|
|
193
|
+
declarations.push(queryFnExp(f, true));
|
|
196
194
|
});
|
|
197
195
|
mutations.forEach((m) => {
|
|
198
196
|
declarations.push(mutationFnExp(m, true));
|
|
@@ -201,18 +199,14 @@ async function prepareContext(ctx) {
|
|
|
201
199
|
return declarations.join("\n");
|
|
202
200
|
};
|
|
203
201
|
ctx.fnImports = [
|
|
204
|
-
// Auto-import query composables
|
|
205
|
-
...queries.
|
|
206
|
-
{ from: "#wpnuxt", name: fnName(fn.name) },
|
|
207
|
-
{ from: "#wpnuxt", name: `useLazy${fn.name}` }
|
|
208
|
-
]),
|
|
202
|
+
// Auto-import query composables
|
|
203
|
+
...queries.map((fn) => ({ from: "#wpnuxt", name: fnName(fn.name) })),
|
|
209
204
|
// Auto-import mutation composables
|
|
210
205
|
...mutations.map((m) => ({ from: "#wpnuxt", name: mutationFnName(m.name) }))
|
|
211
206
|
];
|
|
212
207
|
logger.debug("generated WPNuxt composables: ");
|
|
213
208
|
queries.forEach((f) => {
|
|
214
209
|
logger.debug(` ${fnName(f.name)}()`);
|
|
215
|
-
logger.debug(` useLazy${f.name}()`);
|
|
216
210
|
});
|
|
217
211
|
mutations.forEach((m) => {
|
|
218
212
|
logger.debug(` ${mutationFnName(m.name)}()`);
|
|
@@ -234,6 +228,149 @@ async function prepareFunctions(ctx) {
|
|
|
234
228
|
});
|
|
235
229
|
}
|
|
236
230
|
|
|
231
|
+
async function validateWordPressEndpoint(wordpressUrl, graphqlEndpoint = "/graphql", options = {}) {
|
|
232
|
+
const fullUrl = `${wordpressUrl}${graphqlEndpoint}`;
|
|
233
|
+
try {
|
|
234
|
+
const controller = new AbortController();
|
|
235
|
+
const timeout = setTimeout(() => controller.abort(), 1e4);
|
|
236
|
+
const response = await fetch(fullUrl, {
|
|
237
|
+
method: "POST",
|
|
238
|
+
headers: {
|
|
239
|
+
"Content-Type": "application/json"
|
|
240
|
+
},
|
|
241
|
+
body: JSON.stringify({
|
|
242
|
+
query: "{ __typename }"
|
|
243
|
+
}),
|
|
244
|
+
signal: controller.signal
|
|
245
|
+
});
|
|
246
|
+
clearTimeout(timeout);
|
|
247
|
+
if (!response.ok) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`[WPNuxt] WordPress GraphQL endpoint returned HTTP ${response.status}.
|
|
250
|
+
|
|
251
|
+
URL: ${fullUrl}
|
|
252
|
+
|
|
253
|
+
Possible causes:
|
|
254
|
+
- The WordPress site is down or unreachable
|
|
255
|
+
- WPGraphQL plugin is not installed or activated
|
|
256
|
+
- The GraphQL endpoint path is incorrect (default: /graphql)
|
|
257
|
+
|
|
258
|
+
Check your wpNuxt.wordpressUrl configuration in nuxt.config.ts`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
const data = await response.json();
|
|
262
|
+
if (!data || typeof data !== "object") {
|
|
263
|
+
throw new Error(
|
|
264
|
+
`[WPNuxt] WordPress GraphQL endpoint returned invalid response.
|
|
265
|
+
|
|
266
|
+
URL: ${fullUrl}
|
|
267
|
+
|
|
268
|
+
The endpoint did not return valid JSON. Make sure WPGraphQL plugin is installed and activated.`
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
if (data.errors && !data.data) {
|
|
272
|
+
const errorMessage = data.errors[0]?.message || "Unknown error";
|
|
273
|
+
throw new Error(
|
|
274
|
+
`[WPNuxt] WordPress GraphQL endpoint returned an error: ${errorMessage}
|
|
275
|
+
|
|
276
|
+
URL: ${fullUrl}
|
|
277
|
+
|
|
278
|
+
Make sure WPGraphQL plugin is installed and activated on your WordPress site.`
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
if (options.schemaPath && !existsSync(options.schemaPath)) {
|
|
282
|
+
try {
|
|
283
|
+
execSync(`npx get-graphql-schema "${fullUrl}" > "${options.schemaPath}"`, {
|
|
284
|
+
stdio: "pipe",
|
|
285
|
+
timeout: 6e4
|
|
286
|
+
// 60 second timeout
|
|
287
|
+
});
|
|
288
|
+
patchWPGraphQLSchema(options.schemaPath);
|
|
289
|
+
} catch (err) {
|
|
290
|
+
const error = err;
|
|
291
|
+
throw new Error(
|
|
292
|
+
`[WPNuxt] Failed to download GraphQL schema.
|
|
293
|
+
|
|
294
|
+
URL: ${fullUrl}
|
|
295
|
+
Error: ${error.stderr?.toString() || error.message}
|
|
296
|
+
|
|
297
|
+
Make sure WPGraphQL plugin is installed and activated on your WordPress site.`
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
} catch (error) {
|
|
302
|
+
if (error instanceof Error && error.message.startsWith("[WPNuxt]")) {
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
const err = error;
|
|
306
|
+
const errorCode = err.code || err.cause?.code || "";
|
|
307
|
+
if (err.name === "AbortError") {
|
|
308
|
+
throw new Error(
|
|
309
|
+
`[WPNuxt] WordPress GraphQL endpoint timed out after 10 seconds.
|
|
310
|
+
|
|
311
|
+
URL: ${fullUrl}
|
|
312
|
+
|
|
313
|
+
The server did not respond in time. Check if the WordPress site is accessible.`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
if (errorCode === "ENOTFOUND" || err.message?.includes("getaddrinfo")) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`[WPNuxt] WordPress site not found - DNS lookup failed.
|
|
319
|
+
|
|
320
|
+
URL: ${fullUrl}
|
|
321
|
+
|
|
322
|
+
The domain could not be resolved. Please check:
|
|
323
|
+
- The URL is spelled correctly (no typos)
|
|
324
|
+
- The domain exists and is properly configured
|
|
325
|
+
- Your network connection is working
|
|
326
|
+
|
|
327
|
+
Check your wpNuxt.wordpressUrl configuration in nuxt.config.ts`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
if (errorCode === "ECONNREFUSED") {
|
|
331
|
+
throw new Error(
|
|
332
|
+
`[WPNuxt] Connection refused to WordPress site.
|
|
333
|
+
|
|
334
|
+
URL: ${fullUrl}
|
|
335
|
+
|
|
336
|
+
The server is not accepting connections. Check if the WordPress site is running.`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
throw new Error(
|
|
340
|
+
`[WPNuxt] Failed to connect to WordPress GraphQL endpoint.
|
|
341
|
+
|
|
342
|
+
URL: ${fullUrl}
|
|
343
|
+
Error: ${err.message || "Unknown error"}
|
|
344
|
+
|
|
345
|
+
Check your wpNuxt.wordpressUrl configuration in nuxt.config.ts`
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function patchWPGraphQLSchema(schemaPath) {
|
|
350
|
+
let schema = readFileSync(schemaPath, "utf-8");
|
|
351
|
+
const problematicInterfaces = [
|
|
352
|
+
"Connection",
|
|
353
|
+
"Edge",
|
|
354
|
+
"OneToOneConnection"
|
|
355
|
+
];
|
|
356
|
+
problematicInterfaces.push("NodeWithEditorBlocks");
|
|
357
|
+
for (const iface of problematicInterfaces) {
|
|
358
|
+
schema = schema.replace(
|
|
359
|
+
new RegExp(`implements\\s+${iface}\\s*\\{`, "g"),
|
|
360
|
+
"{"
|
|
361
|
+
);
|
|
362
|
+
schema = schema.replace(
|
|
363
|
+
new RegExp(`implements\\s+${iface}\\s+&\\s+`, "g"),
|
|
364
|
+
"implements "
|
|
365
|
+
);
|
|
366
|
+
schema = schema.replace(
|
|
367
|
+
new RegExp(`\\s*&\\s*${iface}(?=\\s*[&{])`, "g"),
|
|
368
|
+
""
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
writeFileSync(schemaPath, schema);
|
|
372
|
+
}
|
|
373
|
+
|
|
237
374
|
const module$1 = defineNuxtModule({
|
|
238
375
|
meta: {
|
|
239
376
|
name: "wpnuxt",
|
|
@@ -265,8 +402,18 @@ const module$1 = defineNuxtModule({
|
|
|
265
402
|
addPlugin(resolver.resolve("./runtime/plugins/graphqlConfig"));
|
|
266
403
|
addPlugin(resolver.resolve("./runtime/plugins/graphqlErrors"));
|
|
267
404
|
const mergedQueriesFolder = await mergeQueries(nuxt, wpNuxtConfig, resolver);
|
|
268
|
-
setupServerOptions(nuxt, resolver, logger);
|
|
269
|
-
setupClientOptions(nuxt, resolver, logger);
|
|
405
|
+
await setupServerOptions(nuxt, resolver, logger);
|
|
406
|
+
await setupClientOptions(nuxt, resolver, logger);
|
|
407
|
+
if (wpNuxtConfig.downloadSchema) {
|
|
408
|
+
const schemaPath = join(nuxt.options.rootDir, "schema.graphql");
|
|
409
|
+
logger.debug(`Validating WordPress endpoint: ${wpNuxtConfig.wordpressUrl}${wpNuxtConfig.graphqlEndpoint}`);
|
|
410
|
+
await validateWordPressEndpoint(
|
|
411
|
+
wpNuxtConfig.wordpressUrl,
|
|
412
|
+
wpNuxtConfig.graphqlEndpoint,
|
|
413
|
+
{ schemaPath }
|
|
414
|
+
);
|
|
415
|
+
logger.debug("WordPress endpoint validation passed");
|
|
416
|
+
}
|
|
270
417
|
await registerModules(nuxt, resolver, wpNuxtConfig, mergedQueriesFolder);
|
|
271
418
|
nuxt.hook("devtools:customTabs", (tabs) => {
|
|
272
419
|
const middlewareTab = tabs.find((tab) => tab.name === "nuxt-graphql-middleware");
|
|
@@ -289,7 +436,9 @@ const module$1 = defineNuxtModule({
|
|
|
289
436
|
configureVercelSettings(nuxt, logger);
|
|
290
437
|
addImports([
|
|
291
438
|
{ name: "useWPContent", as: "useWPContent", from: resolver.resolve("./runtime/composables/useWPContent") },
|
|
292
|
-
{ name: "useAsyncWPContent", as: "useAsyncWPContent", from: resolver.resolve("./runtime/composables/useWPContent") }
|
|
439
|
+
{ name: "useAsyncWPContent", as: "useAsyncWPContent", from: resolver.resolve("./runtime/composables/useWPContent") },
|
|
440
|
+
{ name: "getRelativeImagePath", as: "getRelativeImagePath", from: resolver.resolve("./runtime/util/images") },
|
|
441
|
+
{ name: "usePrevNextPost", as: "usePrevNextPost", from: resolver.resolve("./runtime/composables/usePrevNextPost") }
|
|
293
442
|
// Note: useGraphqlMutation is auto-imported via nuxt-graphql-middleware with includeComposables: true
|
|
294
443
|
]);
|
|
295
444
|
addComponentsDir({
|
|
@@ -333,9 +482,13 @@ function loadConfig(options, nuxt) {
|
|
|
333
482
|
const config = defu({
|
|
334
483
|
wordpressUrl: process.env.WPNUXT_WORDPRESS_URL,
|
|
335
484
|
graphqlEndpoint: process.env.WPNUXT_GRAPHQL_ENDPOINT,
|
|
336
|
-
downloadSchema
|
|
485
|
+
// Only override downloadSchema if env var is explicitly set
|
|
486
|
+
downloadSchema: process.env.WPNUXT_DOWNLOAD_SCHEMA !== void 0 ? process.env.WPNUXT_DOWNLOAD_SCHEMA === "true" : void 0,
|
|
337
487
|
debug: process.env.WPNUXT_DEBUG ? process.env.WPNUXT_DEBUG === "true" : void 0
|
|
338
488
|
}, options);
|
|
489
|
+
if (config.downloadSchema === void 0) {
|
|
490
|
+
config.downloadSchema = true;
|
|
491
|
+
}
|
|
339
492
|
nuxt.options.runtimeConfig.public.wordpressUrl = config.wordpressUrl;
|
|
340
493
|
nuxt.options.runtimeConfig.public.wpNuxt = {
|
|
341
494
|
wordpressUrl: config.wordpressUrl,
|
|
@@ -355,7 +508,8 @@ function loadConfig(options, nuxt) {
|
|
|
355
508
|
return config;
|
|
356
509
|
}
|
|
357
510
|
const SERVER_OPTIONS_TEMPLATE = `import { defineGraphqlServerOptions } from '@wpnuxt/core/server-options'
|
|
358
|
-
import { getHeader } from 'h3'
|
|
511
|
+
import { getHeader, getCookie } from 'h3'
|
|
512
|
+
import { useRuntimeConfig } from '#imports'
|
|
359
513
|
|
|
360
514
|
/**
|
|
361
515
|
* WPNuxt default server options for nuxt-graphql-middleware.
|
|
@@ -363,18 +517,32 @@ import { getHeader } from 'h3'
|
|
|
363
517
|
* This enables:
|
|
364
518
|
* - Cookie forwarding for WordPress preview mode
|
|
365
519
|
* - Authorization header forwarding for authenticated requests
|
|
520
|
+
* - Auth token from cookie for @wpnuxt/auth
|
|
366
521
|
* - Consistent error logging
|
|
367
522
|
*
|
|
368
523
|
* Users can customize by creating their own server/graphqlMiddleware.serverOptions.ts
|
|
369
524
|
*/
|
|
370
525
|
export default defineGraphqlServerOptions({
|
|
371
526
|
async serverFetchOptions(event, _operation, _operationName, _context) {
|
|
527
|
+
// Get auth token from Authorization header or from cookie
|
|
528
|
+
let authorization = getHeader(event, 'authorization') || ''
|
|
529
|
+
|
|
530
|
+
// If no Authorization header, check for auth token in cookie (@wpnuxt/auth)
|
|
531
|
+
if (!authorization) {
|
|
532
|
+
const config = useRuntimeConfig().public.wpNuxtAuth as { cookieName?: string } | undefined
|
|
533
|
+
const cookieName = config?.cookieName || 'wpnuxt-auth-token'
|
|
534
|
+
const authToken = getCookie(event, cookieName)
|
|
535
|
+
if (authToken) {
|
|
536
|
+
authorization = \`Bearer \${authToken}\`
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
372
540
|
return {
|
|
373
541
|
headers: {
|
|
374
542
|
// Forward WordPress auth cookies for previews
|
|
375
543
|
Cookie: getHeader(event, 'cookie') || '',
|
|
376
|
-
// Forward authorization header
|
|
377
|
-
Authorization:
|
|
544
|
+
// Forward authorization header or token from cookie
|
|
545
|
+
Authorization: authorization
|
|
378
546
|
}
|
|
379
547
|
}
|
|
380
548
|
},
|
|
@@ -385,7 +553,7 @@ export default defineGraphqlServerOptions({
|
|
|
385
553
|
}
|
|
386
554
|
})
|
|
387
555
|
`;
|
|
388
|
-
function setupServerOptions(nuxt, _resolver, logger) {
|
|
556
|
+
async function setupServerOptions(nuxt, _resolver, logger) {
|
|
389
557
|
const serverDir = nuxt.options.serverDir;
|
|
390
558
|
const targetPath = join(serverDir, "graphqlMiddleware.serverOptions.ts");
|
|
391
559
|
if (existsSync(targetPath)) {
|
|
@@ -393,9 +561,9 @@ function setupServerOptions(nuxt, _resolver, logger) {
|
|
|
393
561
|
return;
|
|
394
562
|
}
|
|
395
563
|
if (!existsSync(serverDir)) {
|
|
396
|
-
|
|
564
|
+
await mkdir(serverDir, { recursive: true });
|
|
397
565
|
}
|
|
398
|
-
|
|
566
|
+
await writeFile(targetPath, SERVER_OPTIONS_TEMPLATE);
|
|
399
567
|
logger.debug("Created graphqlMiddleware.serverOptions.ts with WPNuxt defaults (cookie/auth forwarding)");
|
|
400
568
|
}
|
|
401
569
|
const CLIENT_OPTIONS_TEMPLATE = `import { defineGraphqlClientOptions } from '@wpnuxt/core/client-options'
|
|
@@ -427,7 +595,7 @@ export default defineGraphqlClientOptions<{
|
|
|
427
595
|
}
|
|
428
596
|
})
|
|
429
597
|
`;
|
|
430
|
-
function setupClientOptions(nuxt, _resolver, logger) {
|
|
598
|
+
async function setupClientOptions(nuxt, _resolver, logger) {
|
|
431
599
|
const appDir = nuxt.options.dir.app;
|
|
432
600
|
const targetPath = join(appDir, "graphqlMiddleware.clientOptions.ts");
|
|
433
601
|
if (existsSync(targetPath)) {
|
|
@@ -435,9 +603,9 @@ function setupClientOptions(nuxt, _resolver, logger) {
|
|
|
435
603
|
return;
|
|
436
604
|
}
|
|
437
605
|
if (!existsSync(appDir)) {
|
|
438
|
-
|
|
606
|
+
await mkdir(appDir, { recursive: true });
|
|
439
607
|
}
|
|
440
|
-
|
|
608
|
+
await writeFile(targetPath, CLIENT_OPTIONS_TEMPLATE);
|
|
441
609
|
logger.debug("Created graphqlMiddleware.clientOptions.ts with WPNuxt defaults (preview mode support)");
|
|
442
610
|
}
|
|
443
611
|
function configureVercelSettings(nuxt, logger) {
|
|
@@ -471,7 +639,7 @@ async function registerModules(nuxt, resolver, wpNuxtConfig, mergedQueriesFolder
|
|
|
471
639
|
graphqlEndpoint: `${wpNuxtConfig.wordpressUrl}${wpNuxtConfig.graphqlEndpoint}`,
|
|
472
640
|
autoImportPatterns: [mergedQueriesFolder],
|
|
473
641
|
includeComposables: true,
|
|
474
|
-
downloadSchema: wpNuxtConfig.downloadSchema,
|
|
642
|
+
downloadSchema: wpNuxtConfig.downloadSchema ?? true,
|
|
475
643
|
enableFileUploads: true,
|
|
476
644
|
// Use WPNuxt-branded API route prefix
|
|
477
645
|
serverApiPrefix: "/api/wpnuxt",
|
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useWPContent } from "./useWPContent.js";
|
|
2
|
+
export async function usePrevNextPost(currentSlug) {
|
|
3
|
+
const { data, execute } = useWPContent("Posts", ["posts", "nodes"], false, { limit: 100 });
|
|
4
|
+
await execute();
|
|
5
|
+
const allPosts = data.value || [];
|
|
6
|
+
if (!allPosts.length) {
|
|
7
|
+
return { prev: null, next: null };
|
|
8
|
+
}
|
|
9
|
+
const cleanSlug = currentSlug.replace(/^\/|\/$/g, "");
|
|
10
|
+
const currentIndex = allPosts.findIndex(
|
|
11
|
+
(post) => post.slug === cleanSlug
|
|
12
|
+
);
|
|
13
|
+
if (currentIndex === -1) {
|
|
14
|
+
return { prev: null, next: null };
|
|
15
|
+
}
|
|
16
|
+
const prevPost = currentIndex < allPosts.length - 1 ? allPosts[currentIndex + 1] : null;
|
|
17
|
+
const nextPost = currentIndex > 0 ? allPosts[currentIndex - 1] : null;
|
|
18
|
+
return {
|
|
19
|
+
prev: prevPost ?? null,
|
|
20
|
+
next: nextPost ?? null
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { getRelativeImagePath } from "../util/images.js";
|
|
2
2
|
import { computed, useAsyncGraphqlQuery } from "#imports";
|
|
3
3
|
export const useWPContent = (queryName, nodes, fixImagePaths, params, options) => {
|
|
4
|
+
const mergedOptions = {
|
|
5
|
+
graphqlCaching: { client: true },
|
|
6
|
+
...options
|
|
7
|
+
};
|
|
4
8
|
const { data, pending, refresh, execute, clear, error, status } = useAsyncGraphqlQuery(
|
|
5
9
|
queryName,
|
|
6
10
|
params ?? {},
|
|
7
|
-
|
|
11
|
+
mergedOptions
|
|
8
12
|
);
|
|
9
13
|
const transformedData = computed(() => {
|
|
10
14
|
const queryResult = data.value && typeof data.value === "object" && data.value !== null && "data" in data.value ? data.value.data : void 0;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { defineNuxtPlugin } from "
|
|
2
|
-
|
|
3
|
-
export default defineNuxtPlugin((_NuxtApp) => {
|
|
1
|
+
import { defineNuxtPlugin, useGraphqlState, useRuntimeConfig } from "#imports";
|
|
2
|
+
export default defineNuxtPlugin(() => {
|
|
4
3
|
const state = useGraphqlState();
|
|
5
4
|
if (!state) {
|
|
6
5
|
return;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type stubs for Nuxt-generated imports.
|
|
3
|
+
* These are used during module development when .nuxt folder doesn't exist.
|
|
4
|
+
* Actual types are generated at runtime in consuming applications.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
+
|
|
9
|
+
// Stub for #imports
|
|
10
|
+
export const computed: any
|
|
11
|
+
export const defineNuxtPlugin: any
|
|
12
|
+
export const useAsyncGraphqlQuery: any
|
|
13
|
+
export const useGraphqlState: any
|
|
14
|
+
export const useNuxtApp: any
|
|
15
|
+
export const useRuntimeConfig: any
|
|
16
|
+
export const useRoute: any
|
|
17
|
+
|
|
18
|
+
// Stub for #nuxt-graphql-middleware/operation-types
|
|
19
|
+
export type Query = Record<string, any>
|
|
20
|
+
|
|
21
|
+
// Stub for #build/graphql-operations
|
|
22
|
+
export type PostFragment = any
|
|
23
|
+
export type PageFragment = any
|
|
24
|
+
export type MenuItemFragment = any
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wpnuxt/core",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.3",
|
|
4
4
|
"description": "Nuxt module for WordPress integration via GraphQL (WPGraphQL)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nuxt",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@radya/nuxt-dompurify": "^1.0.5",
|
|
48
48
|
"defu": "^6.1.4",
|
|
49
49
|
"graphql": "^16.12.0",
|
|
50
|
-
"nuxt-graphql-middleware": "5.3.
|
|
50
|
+
"nuxt-graphql-middleware": "5.3.1",
|
|
51
51
|
"scule": "^1.3.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
67
|
"build": "nuxt-module-build build",
|
|
68
|
-
"dev:prepare": "nuxt-module-build build --stub
|
|
68
|
+
"dev:prepare": "nuxt-module-build build --stub",
|
|
69
69
|
"lint": "eslint src",
|
|
70
70
|
"test": "vitest run",
|
|
71
71
|
"test:watch": "vitest watch",
|