@szymonpiatek/nextwordpress 0.0.4 → 0.0.6
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/hooks/index.cjs +371 -11
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.cts +92 -2
- package/dist/hooks/index.d.ts +92 -2
- package/dist/hooks/index.js +365 -12
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +217 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -168
- package/dist/index.d.ts +46 -168
- package/dist/index.js +215 -116
- package/dist/index.js.map +1 -1
- package/dist/{types-EjYH-eZp.d.cts → types-soq-mA1E.d.cts} +158 -1
- package/dist/{types-EjYH-eZp.d.ts → types-soq-mA1E.d.ts} +158 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,6 +7,133 @@ function resolveBaseUrl(config) {
|
|
|
7
7
|
return typeof window === "undefined" ? config.serverURL : config.clientURL;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
// src/integrations/restApi/core/client/types.ts
|
|
11
|
+
var WordPressAPIError = class extends Error {
|
|
12
|
+
constructor(message, status, endpoint) {
|
|
13
|
+
super(message);
|
|
14
|
+
__publicField(this, "status", status);
|
|
15
|
+
__publicField(this, "endpoint", endpoint);
|
|
16
|
+
this.name = "WordPressAPIError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/integrations/restApi/core/client/url.ts
|
|
21
|
+
function buildUrl(config, path, query) {
|
|
22
|
+
const base = resolveBaseUrl(config);
|
|
23
|
+
if (!query) return `${base}${path}`;
|
|
24
|
+
const params = new URLSearchParams();
|
|
25
|
+
for (const [key, value] of Object.entries(query)) {
|
|
26
|
+
if (value !== void 0 && value !== null) {
|
|
27
|
+
params.set(key, String(value));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const qs = params.toString();
|
|
31
|
+
return qs ? `${base}${path}?${qs}` : `${base}${path}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/integrations/restApi/core/client/fetcher.ts
|
|
35
|
+
var USER_AGENT = "NextWordpress Client";
|
|
36
|
+
async function doFetch(url, init = {}) {
|
|
37
|
+
const response = await fetch(url, init);
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
throw new WordPressAPIError(
|
|
40
|
+
`WordPress API request failed: ${response.statusText}`,
|
|
41
|
+
response.status,
|
|
42
|
+
url
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
function createFetcher(config) {
|
|
48
|
+
const cacheTtl = config.cacheTTL ?? 300;
|
|
49
|
+
async function wpFetch(path, query, tags = ["wordpress"]) {
|
|
50
|
+
const url = buildUrl(config, path, query);
|
|
51
|
+
const res = await doFetch(url, {
|
|
52
|
+
headers: { "User-Agent": USER_AGENT },
|
|
53
|
+
next: { tags, revalidate: cacheTtl }
|
|
54
|
+
});
|
|
55
|
+
return await res.json();
|
|
56
|
+
}
|
|
57
|
+
async function wpFetchGraceful(path, fallback, query, tags = ["wordpress"]) {
|
|
58
|
+
try {
|
|
59
|
+
return await wpFetch(path, query, tags);
|
|
60
|
+
} catch {
|
|
61
|
+
console.warn(`WordPress fetch failed for ${path}`);
|
|
62
|
+
return fallback;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function wpFetchPaginated(path, query, tags = ["wordpress"]) {
|
|
66
|
+
const url = buildUrl(config, path, query);
|
|
67
|
+
const res = await doFetch(url, {
|
|
68
|
+
headers: { "User-Agent": USER_AGENT },
|
|
69
|
+
next: { tags, revalidate: cacheTtl }
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
data: await res.json(),
|
|
73
|
+
headers: {
|
|
74
|
+
total: parseInt(res.headers.get("X-WP-Total") ?? "0", 10),
|
|
75
|
+
totalPages: parseInt(res.headers.get("X-WP-TotalPages") ?? "0", 10)
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async function wpFetchPaginatedGraceful(path, query, tags = ["wordpress"]) {
|
|
80
|
+
const empty = { data: [], headers: { total: 0, totalPages: 0 } };
|
|
81
|
+
try {
|
|
82
|
+
return await wpFetchPaginated(path, query, tags);
|
|
83
|
+
} catch {
|
|
84
|
+
console.warn(`WordPress paginated fetch failed for ${path}`);
|
|
85
|
+
return empty;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function wpMutate(path, body, method = "POST", authToken) {
|
|
89
|
+
const url = buildUrl(config, path);
|
|
90
|
+
const headers = {
|
|
91
|
+
"Content-Type": "application/json",
|
|
92
|
+
"User-Agent": USER_AGENT
|
|
93
|
+
};
|
|
94
|
+
if (authToken) {
|
|
95
|
+
headers["Authorization"] = authToken;
|
|
96
|
+
}
|
|
97
|
+
const res = await doFetch(url, {
|
|
98
|
+
method,
|
|
99
|
+
headers,
|
|
100
|
+
body: JSON.stringify(body),
|
|
101
|
+
cache: "no-store"
|
|
102
|
+
});
|
|
103
|
+
return await res.json();
|
|
104
|
+
}
|
|
105
|
+
async function wpUpload(path, file, filename, mimeType, authToken) {
|
|
106
|
+
const url = buildUrl(config, path);
|
|
107
|
+
const res = await doFetch(url, {
|
|
108
|
+
method: "POST",
|
|
109
|
+
headers: {
|
|
110
|
+
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
111
|
+
"Content-Type": mimeType,
|
|
112
|
+
"User-Agent": USER_AGENT,
|
|
113
|
+
"Authorization": authToken
|
|
114
|
+
},
|
|
115
|
+
body: file,
|
|
116
|
+
cache: "no-store"
|
|
117
|
+
});
|
|
118
|
+
return await res.json();
|
|
119
|
+
}
|
|
120
|
+
return { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful, wpMutate, wpUpload };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/integrations/restApi/core/contactForm7/queries.ts
|
|
124
|
+
function createCF7Queries(config) {
|
|
125
|
+
async function submitForm(formId, data) {
|
|
126
|
+
const url = buildUrl(config, `/wp-json/contact-form-7/v1/contact-forms/${formId}/feedback`);
|
|
127
|
+
const body = new FormData();
|
|
128
|
+
for (const [key, value] of Object.entries(data)) {
|
|
129
|
+
body.append(key, value);
|
|
130
|
+
}
|
|
131
|
+
const res = await fetch(url, { method: "POST", body, cache: "no-store" });
|
|
132
|
+
return res.json();
|
|
133
|
+
}
|
|
134
|
+
return { submitForm };
|
|
135
|
+
}
|
|
136
|
+
|
|
10
137
|
// src/integrations/restApi/core/mutations/posts/mutations.ts
|
|
11
138
|
function createPostsMutations(fetcher) {
|
|
12
139
|
const { wpMutate } = fetcher;
|
|
@@ -200,119 +327,6 @@ function createMediaMutations(fetcher) {
|
|
|
200
327
|
return { uploadMedia, updateMedia, deleteMedia };
|
|
201
328
|
}
|
|
202
329
|
|
|
203
|
-
// src/integrations/restApi/core/client/types.ts
|
|
204
|
-
var WordPressAPIError = class extends Error {
|
|
205
|
-
constructor(message, status, endpoint) {
|
|
206
|
-
super(message);
|
|
207
|
-
__publicField(this, "status", status);
|
|
208
|
-
__publicField(this, "endpoint", endpoint);
|
|
209
|
-
this.name = "WordPressAPIError";
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
// src/integrations/restApi/core/client/url.ts
|
|
214
|
-
function buildUrl(config, path, query) {
|
|
215
|
-
const base = resolveBaseUrl(config);
|
|
216
|
-
if (!query) return `${base}${path}`;
|
|
217
|
-
const params = new URLSearchParams();
|
|
218
|
-
for (const [key, value] of Object.entries(query)) {
|
|
219
|
-
if (value !== void 0 && value !== null) {
|
|
220
|
-
params.set(key, String(value));
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
const qs = params.toString();
|
|
224
|
-
return qs ? `${base}${path}?${qs}` : `${base}${path}`;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// src/integrations/restApi/core/client/fetcher.ts
|
|
228
|
-
var USER_AGENT = "NextWordpress Client";
|
|
229
|
-
async function doFetch(url, init = {}) {
|
|
230
|
-
const response = await fetch(url, init);
|
|
231
|
-
if (!response.ok) {
|
|
232
|
-
throw new WordPressAPIError(
|
|
233
|
-
`WordPress API request failed: ${response.statusText}`,
|
|
234
|
-
response.status,
|
|
235
|
-
url
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
return response;
|
|
239
|
-
}
|
|
240
|
-
function createFetcher(config) {
|
|
241
|
-
const cacheTtl = 300;
|
|
242
|
-
async function wpFetch(path, query, tags = ["wordpress"]) {
|
|
243
|
-
const url = buildUrl(config, path, query);
|
|
244
|
-
const res = await doFetch(url, {
|
|
245
|
-
headers: { "User-Agent": USER_AGENT },
|
|
246
|
-
next: { tags, revalidate: cacheTtl }
|
|
247
|
-
});
|
|
248
|
-
return await res.json();
|
|
249
|
-
}
|
|
250
|
-
async function wpFetchGraceful(path, fallback, query, tags = ["wordpress"]) {
|
|
251
|
-
try {
|
|
252
|
-
return await wpFetch(path, query, tags);
|
|
253
|
-
} catch {
|
|
254
|
-
console.warn(`WordPress fetch failed for ${path}`);
|
|
255
|
-
return fallback;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
async function wpFetchPaginated(path, query, tags = ["wordpress"]) {
|
|
259
|
-
const url = buildUrl(config, path, query);
|
|
260
|
-
const res = await doFetch(url, {
|
|
261
|
-
headers: { "User-Agent": USER_AGENT },
|
|
262
|
-
next: { tags, revalidate: cacheTtl }
|
|
263
|
-
});
|
|
264
|
-
return {
|
|
265
|
-
data: await res.json(),
|
|
266
|
-
headers: {
|
|
267
|
-
total: parseInt(res.headers.get("X-WP-Total") ?? "0", 10),
|
|
268
|
-
totalPages: parseInt(res.headers.get("X-WP-TotalPages") ?? "0", 10)
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
async function wpFetchPaginatedGraceful(path, query, tags = ["wordpress"]) {
|
|
273
|
-
const empty = { data: [], headers: { total: 0, totalPages: 0 } };
|
|
274
|
-
try {
|
|
275
|
-
return await wpFetchPaginated(path, query, tags);
|
|
276
|
-
} catch {
|
|
277
|
-
console.warn(`WordPress paginated fetch failed for ${path}`);
|
|
278
|
-
return empty;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
async function wpMutate(path, body, method = "POST", authToken) {
|
|
282
|
-
const url = buildUrl(config, path);
|
|
283
|
-
const headers = {
|
|
284
|
-
"Content-Type": "application/json",
|
|
285
|
-
"User-Agent": USER_AGENT
|
|
286
|
-
};
|
|
287
|
-
if (authToken) {
|
|
288
|
-
headers["Authorization"] = authToken;
|
|
289
|
-
}
|
|
290
|
-
const res = await doFetch(url, {
|
|
291
|
-
method,
|
|
292
|
-
headers,
|
|
293
|
-
body: JSON.stringify(body),
|
|
294
|
-
cache: "no-store"
|
|
295
|
-
});
|
|
296
|
-
return await res.json();
|
|
297
|
-
}
|
|
298
|
-
async function wpUpload(path, file, filename, mimeType, authToken) {
|
|
299
|
-
const url = buildUrl(config, path);
|
|
300
|
-
const res = await doFetch(url, {
|
|
301
|
-
method: "POST",
|
|
302
|
-
headers: {
|
|
303
|
-
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
304
|
-
"Content-Type": mimeType,
|
|
305
|
-
"User-Agent": USER_AGENT,
|
|
306
|
-
"Authorization": authToken
|
|
307
|
-
},
|
|
308
|
-
body: file,
|
|
309
|
-
cache: "no-store"
|
|
310
|
-
});
|
|
311
|
-
return await res.json();
|
|
312
|
-
}
|
|
313
|
-
return { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful, wpMutate, wpUpload };
|
|
314
|
-
}
|
|
315
|
-
|
|
316
330
|
// src/integrations/restApi/core/mutations/index.ts
|
|
317
331
|
function createWordPressMutationsClient(config) {
|
|
318
332
|
const fetcher = createFetcher(config);
|
|
@@ -1225,9 +1239,9 @@ var WPGraphQLError = class extends Error {
|
|
|
1225
1239
|
|
|
1226
1240
|
// src/integrations/wpGraphQL/client/fetcher.ts
|
|
1227
1241
|
var USER_AGENT3 = "NextWordpress WPGraphQL Client";
|
|
1228
|
-
var CACHE_TTL = 300;
|
|
1229
1242
|
function createWPGraphQLFetcher(config) {
|
|
1230
1243
|
const url = `${resolveBaseUrl(config)}/graphql`;
|
|
1244
|
+
const cacheTTL = config.cacheTTL ?? 300;
|
|
1231
1245
|
async function gqlFetch(document, variables, tags = ["wpgraphql"]) {
|
|
1232
1246
|
const response = await fetch(url, {
|
|
1233
1247
|
method: "POST",
|
|
@@ -1236,7 +1250,7 @@ function createWPGraphQLFetcher(config) {
|
|
|
1236
1250
|
"User-Agent": USER_AGENT3
|
|
1237
1251
|
},
|
|
1238
1252
|
body: JSON.stringify({ query: document, variables }),
|
|
1239
|
-
next: { tags, revalidate:
|
|
1253
|
+
next: { tags, revalidate: cacheTTL }
|
|
1240
1254
|
});
|
|
1241
1255
|
if (!response.ok) {
|
|
1242
1256
|
throw new WPGraphQLError(
|
|
@@ -3188,6 +3202,91 @@ function createRevalidationHandler(config, revalidateTag) {
|
|
|
3188
3202
|
};
|
|
3189
3203
|
}
|
|
3190
3204
|
|
|
3205
|
+
// src/nextjs/faust.ts
|
|
3206
|
+
function createFaustAuthHandler(config) {
|
|
3207
|
+
const cookieName = config.cookieName ?? "faustwp-rt";
|
|
3208
|
+
return async function handler(request, routePath) {
|
|
3209
|
+
if (routePath === "auth/token") {
|
|
3210
|
+
const url = new URL(request.url);
|
|
3211
|
+
const code = url.searchParams.get("code");
|
|
3212
|
+
const redirectUri = url.searchParams.get("redirect_uri") ?? "/";
|
|
3213
|
+
const refreshToken = parseCookie(request, cookieName);
|
|
3214
|
+
if (!code && !refreshToken) {
|
|
3215
|
+
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
3216
|
+
}
|
|
3217
|
+
const wpRes = await fetch(`${config.wpUrl}/?rest_route=/faustwp/v1/authorize`, {
|
|
3218
|
+
method: "POST",
|
|
3219
|
+
headers: {
|
|
3220
|
+
"Content-Type": "application/json",
|
|
3221
|
+
"x-faustwp-secret": config.secretKey
|
|
3222
|
+
},
|
|
3223
|
+
body: JSON.stringify({ code, refreshToken })
|
|
3224
|
+
});
|
|
3225
|
+
if (!wpRes.ok) {
|
|
3226
|
+
const body = await wpRes.text();
|
|
3227
|
+
return new Response(body, { status: wpRes.status });
|
|
3228
|
+
}
|
|
3229
|
+
const data = await wpRes.json();
|
|
3230
|
+
return new Response(null, {
|
|
3231
|
+
status: 302,
|
|
3232
|
+
headers: {
|
|
3233
|
+
Location: new URL(redirectUri, request.url).toString(),
|
|
3234
|
+
"Set-Cookie": buildSetCookie(cookieName, data.refreshToken, 2592e3)
|
|
3235
|
+
}
|
|
3236
|
+
});
|
|
3237
|
+
}
|
|
3238
|
+
if (routePath === "auth/logout") {
|
|
3239
|
+
return new Response(JSON.stringify({}), {
|
|
3240
|
+
status: 200,
|
|
3241
|
+
headers: {
|
|
3242
|
+
"Content-Type": "application/json",
|
|
3243
|
+
"Set-Cookie": `${cookieName}=; Max-Age=0; Path=/; HttpOnly`
|
|
3244
|
+
}
|
|
3245
|
+
});
|
|
3246
|
+
}
|
|
3247
|
+
return Response.json({ error: "Not Found" }, { status: 404 });
|
|
3248
|
+
};
|
|
3249
|
+
}
|
|
3250
|
+
function createPreviewHandler(config, deps) {
|
|
3251
|
+
const previewPath = config.previewPath ?? "posts";
|
|
3252
|
+
return async function handler(request) {
|
|
3253
|
+
const url = new URL(request.url);
|
|
3254
|
+
const postId = url.searchParams.get("postId");
|
|
3255
|
+
const previewPathname = url.searchParams.get("previewPathname");
|
|
3256
|
+
if (!postId) {
|
|
3257
|
+
return new Response("Missing postId", { status: 400 });
|
|
3258
|
+
}
|
|
3259
|
+
const slug = await resolvePreviewSlug(config, postId, previewPathname);
|
|
3260
|
+
if (!slug) {
|
|
3261
|
+
return new Response("Post not found", { status: 404 });
|
|
3262
|
+
}
|
|
3263
|
+
await deps.enableDraftMode();
|
|
3264
|
+
deps.redirect(`/${previewPath}/${slug}?postId=${postId}`);
|
|
3265
|
+
};
|
|
3266
|
+
}
|
|
3267
|
+
function parseCookie(request, name) {
|
|
3268
|
+
const header = request.headers.get("cookie") ?? "";
|
|
3269
|
+
const match = header.match(new RegExp(`(?:^|;\\s*)${name}=([^;]*)`));
|
|
3270
|
+
return match?.[1];
|
|
3271
|
+
}
|
|
3272
|
+
function buildSetCookie(name, value, maxAge) {
|
|
3273
|
+
const secure = process.env.NODE_ENV === "production" ? "; Secure" : "";
|
|
3274
|
+
return `${name}=${value}; HttpOnly; Path=/; SameSite=Strict; Max-Age=${maxAge}${secure}`;
|
|
3275
|
+
}
|
|
3276
|
+
async function resolvePreviewSlug(config, postId, previewPathname) {
|
|
3277
|
+
if (previewPathname && !previewPathname.includes("p=")) {
|
|
3278
|
+
const slug = previewPathname.replace(/\/$/, "").split("/").pop();
|
|
3279
|
+
if (slug) return slug;
|
|
3280
|
+
}
|
|
3281
|
+
const res = await fetch(`${config.wpUrl}/wp-json/wp/v2/posts/${postId}?_fields=slug`, {
|
|
3282
|
+
headers: { "x-faustwp-secret": config.secretKey },
|
|
3283
|
+
cache: "no-store"
|
|
3284
|
+
});
|
|
3285
|
+
if (!res.ok) return null;
|
|
3286
|
+
const data = await res.json();
|
|
3287
|
+
return data.slug ?? `preview-${postId}`;
|
|
3288
|
+
}
|
|
3289
|
+
|
|
3191
3290
|
// src/auth/applicationPassword.ts
|
|
3192
3291
|
function createApplicationPasswordToken(username, appPassword) {
|
|
3193
3292
|
const encoded = Buffer.from(`${username}:${appPassword}`).toString("base64");
|
|
@@ -3230,6 +3329,6 @@ async function validateJwtToken(config, token) {
|
|
|
3230
3329
|
return response.ok;
|
|
3231
3330
|
}
|
|
3232
3331
|
|
|
3233
|
-
export { AuthenticationError, WPGraphQLError, authenticateJwt, buildACFFragment, buildPageWithACFQuery, buildPostWithACFQuery, buildPostsWithACFQuery, createApplicationPasswordToken, createAuthorsMutations, createCPTQueries, createCategoriesMutations, createCommentsMutations, createMediaMutations, createMenusMutations, createPagesMutations, createPostsMutations, createRevalidationHandler, createTagsMutations, createWPGraphQLCoreClient as createWPGraphQLClient, createWPGraphQLCoreClient, createWPGraphQLFetcher, createWPGraphQLMutationsClient, createWPGraphQLWooCommerceClient, createWooCommerceClient, createWooCommerceFetcher, createWordPressClient, createWordPressMutationsClient, createYoastQueries, extractOmnibusData, resolveBaseUrl, validateJwtToken, withOmnibus, withOmnibusVariation };
|
|
3332
|
+
export { AuthenticationError, WPGraphQLError, authenticateJwt, buildACFFragment, buildPageWithACFQuery, buildPostWithACFQuery, buildPostsWithACFQuery, createApplicationPasswordToken, createAuthorsMutations, createCF7Queries, createCPTQueries, createCategoriesMutations, createCommentsMutations, createFaustAuthHandler, createMediaMutations, createMenusMutations, createPagesMutations, createPostsMutations, createPreviewHandler, createRevalidationHandler, createTagsMutations, createWPGraphQLCoreClient as createWPGraphQLClient, createWPGraphQLCoreClient, createWPGraphQLFetcher, createWPGraphQLMutationsClient, createWPGraphQLWooCommerceClient, createWooCommerceClient, createWooCommerceFetcher, createWordPressClient, createWordPressMutationsClient, createYoastQueries, extractOmnibusData, resolveBaseUrl, validateJwtToken, withOmnibus, withOmnibusVariation };
|
|
3234
3333
|
//# sourceMappingURL=index.js.map
|
|
3235
3334
|
//# sourceMappingURL=index.js.map
|