@conloca/content-api-client 0.1.16 → 0.2.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 +190 -105
- package/dist/client.d.ts +12 -14
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +26 -25
- package/dist/client.js.map +1 -0
- package/dist/data-bindings.d.ts +13 -0
- package/dist/data-bindings.d.ts.map +1 -0
- package/dist/data-bindings.js +71 -0
- package/dist/data-bindings.js.map +1 -0
- package/dist/hooks.d.ts +42 -39
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +93 -47
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -0
- package/dist/lib/content-api-client.js +1 -0
- package/dist/lib/content-api-client.js.map +1 -0
- package/dist/test-helpers.d.ts +1 -2
- package/dist/test-helpers.d.ts.map +1 -1
- package/dist/test-helpers.js +2 -2
- package/dist/test-helpers.js.map +1 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +2 -1
- package/dist/testing.js.map +1 -0
- package/package.json +44 -44
- package/src/client.ts +513 -0
- package/src/data-bindings.ts +104 -0
- package/src/hooks.tsx +959 -0
- package/src/index.ts +50 -0
- package/src/lib/content-api-client.ts +3 -0
- package/src/test-helpers.ts +282 -0
- package/src/testing.ts +8 -0
- package/dist/index.d.mts +0 -234
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -1106
- package/dist/index.mjs.map +0 -1
- package/dist/test-helpers-BFyjmKzv.mjs +0 -221
- package/dist/test-helpers-BFyjmKzv.mjs.map +0 -1
- package/dist/test-helpers-B_YQhpE2.d.mts +0 -93
- package/dist/test-helpers-B_YQhpE2.d.mts.map +0 -1
- package/dist/testing.d.mts +0 -2
- package/dist/testing.mjs +0 -2
package/dist/index.mjs
DELETED
|
@@ -1,1106 +0,0 @@
|
|
|
1
|
-
import { t as API_ROUTES } from "./test-helpers-BFyjmKzv.mjs";
|
|
2
|
-
import { ErrorCodes, ErrorCodes as ErrorCodes$1, blockEditableSchema, dataEditableSchema, formatFileSize, localesOf, normalizeAndValidatePathname } from "@conloca/content-api";
|
|
3
|
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
4
|
-
//#region src/client.ts
|
|
5
|
-
var ContentAPIClient = class {
|
|
6
|
-
baseUrl;
|
|
7
|
-
fetch;
|
|
8
|
-
constructor(options = {}) {
|
|
9
|
-
if (options.fetch) this.fetch = options.fetch;
|
|
10
|
-
else if (typeof globalThis !== "undefined" && globalThis.fetch) this.fetch = globalThis.fetch.bind(globalThis);
|
|
11
|
-
else this.fetch = fetch;
|
|
12
|
-
this.baseUrl = options.baseUrl || "/__conloca/api";
|
|
13
|
-
}
|
|
14
|
-
encodeAssetPath(filename) {
|
|
15
|
-
return filename.split("/").map((s) => encodeURIComponent(s)).join("/");
|
|
16
|
-
}
|
|
17
|
-
async fetchAPI(url, options) {
|
|
18
|
-
const response = await this.fetch(url, {
|
|
19
|
-
...options,
|
|
20
|
-
headers: {
|
|
21
|
-
"Content-Type": "application/json",
|
|
22
|
-
...options?.headers
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
if (!response.ok) {
|
|
26
|
-
let errorData;
|
|
27
|
-
try {
|
|
28
|
-
errorData = await response.json();
|
|
29
|
-
} catch {
|
|
30
|
-
throw new Error(`API error: ${response.status} ${response.statusText}`);
|
|
31
|
-
}
|
|
32
|
-
if (errorData?.error?.code) {
|
|
33
|
-
const apiError = errorData;
|
|
34
|
-
if (apiError.error.code === ErrorCodes$1.STALE_WRITE) throw new StaleWriteError({
|
|
35
|
-
code: apiError.error.code,
|
|
36
|
-
message: apiError.error.message,
|
|
37
|
-
currentEtag: apiError.error.details?.currentEtag
|
|
38
|
-
});
|
|
39
|
-
throw new APIClientError(apiError.error.message, apiError.error.code, apiError.error.details);
|
|
40
|
-
}
|
|
41
|
-
throw new Error(`API error: ${response.status} ${response.statusText}`);
|
|
42
|
-
}
|
|
43
|
-
return await response.json();
|
|
44
|
-
}
|
|
45
|
-
async getContent(id) {
|
|
46
|
-
try {
|
|
47
|
-
const response = await this.fetch(`${this.baseUrl}/content/${id}`);
|
|
48
|
-
if (response.status === 404) return null;
|
|
49
|
-
if (!response.ok) throw new Error(`Failed to fetch content: ${response.statusText}`);
|
|
50
|
-
return await response.json();
|
|
51
|
-
} catch (error) {
|
|
52
|
-
console.error("Error fetching content:", error);
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async getLocalized(id, locale) {
|
|
57
|
-
try {
|
|
58
|
-
const response = await this.fetch(`${this.baseUrl}/content/${id}/${locale}`);
|
|
59
|
-
if (response.status === 404) return null;
|
|
60
|
-
if (!response.ok) throw new Error(`Failed to fetch localized content: ${response.statusText}`);
|
|
61
|
-
return await response.json();
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.error("Error fetching localized content:", error);
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async createContent(data) {
|
|
68
|
-
return this.fetchAPI(`${this.baseUrl}/content`, {
|
|
69
|
-
method: "POST",
|
|
70
|
-
body: JSON.stringify(data)
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
async updateLocalized(input) {
|
|
74
|
-
const { id, locale, data, etag } = input;
|
|
75
|
-
const params = new URLSearchParams({
|
|
76
|
-
locale,
|
|
77
|
-
etag
|
|
78
|
-
});
|
|
79
|
-
return this.fetchAPI(`${this.baseUrl}/content/${id}?${params}`, {
|
|
80
|
-
method: "PUT",
|
|
81
|
-
body: JSON.stringify(data)
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
async deleteContent(id, etag) {
|
|
85
|
-
return this.fetchAPI(`${this.baseUrl}/content/${id}`, {
|
|
86
|
-
method: "DELETE",
|
|
87
|
-
headers: { "If-Match": etag }
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
async deleteLocalized(input) {
|
|
91
|
-
return this.fetchAPI(`${this.baseUrl}/content/${input.id}?locale=${input.locale}`, {
|
|
92
|
-
method: "DELETE",
|
|
93
|
-
headers: { "If-Match": input.etag }
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
async getSitePages(site, locale) {
|
|
97
|
-
const url = locale ? `${this.baseUrl}/${site}/pages?locale=${locale}` : `${this.baseUrl}/${site}/pages`;
|
|
98
|
-
return this.fetchAPI(url);
|
|
99
|
-
}
|
|
100
|
-
async getPageByPathname(site, pathname, locale) {
|
|
101
|
-
try {
|
|
102
|
-
const url = locale ? `${this.baseUrl}/sites/${site}/pathname${pathname}?locale=${locale}` : `${this.baseUrl}/sites/${site}/pathname${pathname}`;
|
|
103
|
-
const response = await this.fetch(url);
|
|
104
|
-
if (response.status === 404) return null;
|
|
105
|
-
if (!response.ok) throw new Error(`Failed to fetch page: ${response.statusText}`);
|
|
106
|
-
return await response.json();
|
|
107
|
-
} catch (error) {
|
|
108
|
-
console.error("Error fetching page by pathname:", error);
|
|
109
|
-
throw error;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
async isPathnameAvailable(site, pathname, excludeId) {
|
|
113
|
-
const params = new URLSearchParams({
|
|
114
|
-
pathname,
|
|
115
|
-
...excludeId && { excludeId }
|
|
116
|
-
});
|
|
117
|
-
const response = await this.fetch(`${this.baseUrl}/${site}/pathname-available?${params}`);
|
|
118
|
-
if (!response.ok) throw new Error(`Failed to check pathname availability: ${response.statusText}`);
|
|
119
|
-
return (await response.json()).available;
|
|
120
|
-
}
|
|
121
|
-
async movePage(site, id, pathname, locale, etag) {
|
|
122
|
-
return this.fetchAPI(`${this.baseUrl}/${site}/move`, {
|
|
123
|
-
method: "POST",
|
|
124
|
-
body: JSON.stringify({
|
|
125
|
-
id,
|
|
126
|
-
pathname,
|
|
127
|
-
locale,
|
|
128
|
-
etag
|
|
129
|
-
})
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
async getBlocks(collection, locale) {
|
|
133
|
-
const params = new URLSearchParams();
|
|
134
|
-
if (collection) params.set("collection", collection);
|
|
135
|
-
if (locale) params.set("locale", locale);
|
|
136
|
-
const url = `${this.baseUrl}/blocks${params.toString() ? `?${params}` : ""}`;
|
|
137
|
-
return this.fetchAPI(url);
|
|
138
|
-
}
|
|
139
|
-
async getBlockByName(name, collection, locale) {
|
|
140
|
-
try {
|
|
141
|
-
const params = new URLSearchParams();
|
|
142
|
-
if (collection) params.set("collection", collection);
|
|
143
|
-
if (locale) params.set("locale", locale);
|
|
144
|
-
const url = `${this.baseUrl}/blocks/${name}${params.toString() ? `?${params}` : ""}`;
|
|
145
|
-
const response = await this.fetch(url);
|
|
146
|
-
if (response.status === 404) return null;
|
|
147
|
-
if (!response.ok) throw new Error(`Failed to fetch block: ${response.statusText}`);
|
|
148
|
-
return await response.json();
|
|
149
|
-
} catch (error) {
|
|
150
|
-
console.error("Error fetching block by name:", error);
|
|
151
|
-
throw error;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
async compileMDX(mdxContent) {
|
|
155
|
-
return this.fetchAPI(`${this.baseUrl}/mdx/compile`, {
|
|
156
|
-
method: "POST",
|
|
157
|
-
body: JSON.stringify({ mdxContent })
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
async getData(collection, locale) {
|
|
161
|
-
const params = new URLSearchParams();
|
|
162
|
-
if (collection) params.set("collection", collection);
|
|
163
|
-
if (locale) params.set("locale", locale);
|
|
164
|
-
const url = `${this.baseUrl}/data${params.toString() ? `?${params}` : ""}`;
|
|
165
|
-
return this.fetchAPI(url);
|
|
166
|
-
}
|
|
167
|
-
async getDataByName(name, collection, locale) {
|
|
168
|
-
try {
|
|
169
|
-
const params = new URLSearchParams();
|
|
170
|
-
params.set("collection", collection);
|
|
171
|
-
if (locale) params.set("locale", locale);
|
|
172
|
-
const url = `${this.baseUrl}/data/${name}?${params}`;
|
|
173
|
-
const response = await this.fetch(url);
|
|
174
|
-
if (response.status === 404) return null;
|
|
175
|
-
if (!response.ok) throw new Error(`Failed to fetch data entry: ${response.statusText}`);
|
|
176
|
-
return await response.json();
|
|
177
|
-
} catch (error) {
|
|
178
|
-
console.error("Error fetching data entry by name:", error);
|
|
179
|
-
throw error;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
async isDataNameAvailable(name, collection, excludeId) {
|
|
183
|
-
const params = new URLSearchParams({
|
|
184
|
-
name,
|
|
185
|
-
collection,
|
|
186
|
-
...excludeId && { excludeId }
|
|
187
|
-
});
|
|
188
|
-
const response = await this.fetch(`${this.baseUrl}/data/name-available?${params}`);
|
|
189
|
-
if (!response.ok) throw new Error(`Failed to check data name availability: ${response.statusText}`);
|
|
190
|
-
return (await response.json()).available;
|
|
191
|
-
}
|
|
192
|
-
async getDataCollections() {
|
|
193
|
-
return (await this.fetchAPI(`${this.baseUrl}/data/collections`)).collections;
|
|
194
|
-
}
|
|
195
|
-
async getDataContext(pageId) {
|
|
196
|
-
return this.fetchAPI(`${this.baseUrl}/data-context?pageId=${pageId}`);
|
|
197
|
-
}
|
|
198
|
-
async listAllContent(filters) {
|
|
199
|
-
const params = new URLSearchParams();
|
|
200
|
-
if (filters?.site) params.set("site", filters.site);
|
|
201
|
-
if (filters?.collection) params.set("collection", filters.collection);
|
|
202
|
-
if (filters?.locales?.length) params.set("locales", filters.locales.join(","));
|
|
203
|
-
if (filters?.type) params.set("type", filters.type);
|
|
204
|
-
if (filters?.published !== void 0) params.set("published", String(filters.published));
|
|
205
|
-
if (filters?.kind) params.set("kind", filters.kind);
|
|
206
|
-
if (filters?.localization) params.set("localization", filters.localization);
|
|
207
|
-
if (filters?.missingLocales?.length) params.set("missingLocales", filters.missingLocales.join(","));
|
|
208
|
-
const url = `${this.baseUrl}/content${params.toString() ? `?${params}` : ""}`;
|
|
209
|
-
return this.fetchAPI(url);
|
|
210
|
-
}
|
|
211
|
-
async findUntranslatedContent(targetLocale, excludeSites, includeUnpublished) {
|
|
212
|
-
const params = new URLSearchParams();
|
|
213
|
-
if (excludeSites?.length) params.set("excludeSites", excludeSites.join(","));
|
|
214
|
-
if (includeUnpublished) params.set("includeUnpublished", "true");
|
|
215
|
-
const url = `${this.baseUrl}/untranslated/${targetLocale}${params.toString() ? `?${params}` : ""}`;
|
|
216
|
-
return this.fetchAPI(url);
|
|
217
|
-
}
|
|
218
|
-
async getSitesConfig() {
|
|
219
|
-
return this.fetchAPI(`${this.baseUrl}/sites`);
|
|
220
|
-
}
|
|
221
|
-
async batchUpdate(operations) {
|
|
222
|
-
return this.fetchAPI(`${this.baseUrl}/content/batch`, {
|
|
223
|
-
method: "POST",
|
|
224
|
-
body: JSON.stringify({ operations })
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
async getGitStatus() {
|
|
228
|
-
return this.fetchAPI(`${this.baseUrl}/git/status`);
|
|
229
|
-
}
|
|
230
|
-
async commitChanges(message) {
|
|
231
|
-
return this.fetchAPI(`${this.baseUrl}/git/commit`, {
|
|
232
|
-
method: "POST",
|
|
233
|
-
body: JSON.stringify(message ? { message } : {})
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
async pushChanges() {
|
|
237
|
-
return this.fetchAPI(`${this.baseUrl}/git/push`, {
|
|
238
|
-
method: "POST",
|
|
239
|
-
body: JSON.stringify({})
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
async pullChanges() {
|
|
243
|
-
return this.fetchAPI(`${this.baseUrl}/git/pull`, {
|
|
244
|
-
method: "POST",
|
|
245
|
-
body: JSON.stringify({})
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
async listAssets() {
|
|
249
|
-
return this.fetchAPI(`${this.baseUrl}/assets`);
|
|
250
|
-
}
|
|
251
|
-
async getAsset(filename) {
|
|
252
|
-
try {
|
|
253
|
-
const response = await this.fetch(`${this.baseUrl}/assets/meta/${this.encodeAssetPath(filename)}`);
|
|
254
|
-
if (response.status === 404) return null;
|
|
255
|
-
if (!response.ok) throw new Error(`Failed to fetch asset: ${response.statusText}`);
|
|
256
|
-
return await response.json();
|
|
257
|
-
} catch (error) {
|
|
258
|
-
console.error("Error fetching asset:", error);
|
|
259
|
-
throw error;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
async uploadAsset(formData) {
|
|
263
|
-
const response = await this.fetch(`${this.baseUrl}/assets/upload`, {
|
|
264
|
-
method: "POST",
|
|
265
|
-
body: formData
|
|
266
|
-
});
|
|
267
|
-
if (!response.ok) {
|
|
268
|
-
let errorData;
|
|
269
|
-
try {
|
|
270
|
-
errorData = await response.json();
|
|
271
|
-
} catch {}
|
|
272
|
-
throw new Error(errorData?.error?.message || `Upload failed: ${response.status} ${response.statusText}`);
|
|
273
|
-
}
|
|
274
|
-
return await response.json();
|
|
275
|
-
}
|
|
276
|
-
async importAssetUrl(url, alt, folder) {
|
|
277
|
-
return this.fetchAPI(`${this.baseUrl}/assets/import-url`, {
|
|
278
|
-
method: "POST",
|
|
279
|
-
body: JSON.stringify({
|
|
280
|
-
url,
|
|
281
|
-
alt,
|
|
282
|
-
folder
|
|
283
|
-
})
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
async deleteAsset(filename) {
|
|
287
|
-
return this.fetchAPI(`${this.baseUrl}/assets/meta/${this.encodeAssetPath(filename)}`, { method: "DELETE" });
|
|
288
|
-
}
|
|
289
|
-
async listFolder(path = "/") {
|
|
290
|
-
const params = new URLSearchParams({ path });
|
|
291
|
-
return this.fetchAPI(`${this.baseUrl}/assets/folders?${params}`);
|
|
292
|
-
}
|
|
293
|
-
async createFolder(path) {
|
|
294
|
-
await this.fetch(`${this.baseUrl}/assets/folders`, {
|
|
295
|
-
method: "POST",
|
|
296
|
-
headers: { "Content-Type": "application/json" },
|
|
297
|
-
body: JSON.stringify({ path })
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
async updateAssetMetadata(filename, updates) {
|
|
301
|
-
return this.fetchAPI(`${this.baseUrl}/assets/meta/${this.encodeAssetPath(filename)}`, {
|
|
302
|
-
method: "PATCH",
|
|
303
|
-
body: JSON.stringify(updates)
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
async getAssetUsage(filename) {
|
|
307
|
-
return this.fetchAPI(`${this.baseUrl}/assets/usage/${this.encodeAssetPath(filename)}`);
|
|
308
|
-
}
|
|
309
|
-
async moveAssets(filenames, sourceFolder, targetFolder) {
|
|
310
|
-
return this.fetchAPI(`${this.baseUrl}/assets/move`, {
|
|
311
|
-
method: "POST",
|
|
312
|
-
body: JSON.stringify({
|
|
313
|
-
filenames,
|
|
314
|
-
sourceFolder,
|
|
315
|
-
targetFolder
|
|
316
|
-
})
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
async getFolderTree() {
|
|
320
|
-
return this.fetchAPI(`${this.baseUrl}/assets/folder-tree`);
|
|
321
|
-
}
|
|
322
|
-
async getCurrentUser() {
|
|
323
|
-
const response = await this.fetch(`${this.baseUrl}/auth/user`);
|
|
324
|
-
if (!response.ok) return { authenticated: false };
|
|
325
|
-
return response.json();
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
var APIClientError = class extends Error {
|
|
329
|
-
code;
|
|
330
|
-
details;
|
|
331
|
-
constructor(message, code, details) {
|
|
332
|
-
super(message);
|
|
333
|
-
this.code = code;
|
|
334
|
-
this.details = details;
|
|
335
|
-
this.name = "APIClientError";
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
var StaleWriteError = class extends APIClientError {
|
|
339
|
-
data;
|
|
340
|
-
constructor(data) {
|
|
341
|
-
super(data.message, data.code, { currentEtag: data.currentEtag });
|
|
342
|
-
this.data = data;
|
|
343
|
-
this.name = "StaleWriteError";
|
|
344
|
-
}
|
|
345
|
-
get currentEtag() {
|
|
346
|
-
return this.details?.currentEtag;
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
//#endregion
|
|
350
|
-
//#region src/hooks.tsx
|
|
351
|
-
let globalClient = null;
|
|
352
|
-
function getContentAPIClient() {
|
|
353
|
-
if (!globalClient) globalClient = new ContentAPIClient();
|
|
354
|
-
return globalClient;
|
|
355
|
-
}
|
|
356
|
-
function setContentAPIClient(client) {
|
|
357
|
-
globalClient = client;
|
|
358
|
-
}
|
|
359
|
-
function isContentListResult(value) {
|
|
360
|
-
return typeof value === "object" && value !== null && "items" in value && "total" in value && Array.isArray(value.items) && typeof value.total === "number";
|
|
361
|
-
}
|
|
362
|
-
function hashString(value) {
|
|
363
|
-
let hash = 0;
|
|
364
|
-
for (let index = 0; index < value.length; index++) {
|
|
365
|
-
const char = value.charCodeAt(index);
|
|
366
|
-
hash = (hash << 5) - hash + char;
|
|
367
|
-
hash |= 0;
|
|
368
|
-
}
|
|
369
|
-
return hash.toString(36);
|
|
370
|
-
}
|
|
371
|
-
const queryKeys = {
|
|
372
|
-
currentUser: () => ["auth", "user"],
|
|
373
|
-
gitStatus: () => ["git", "status"],
|
|
374
|
-
content: (id) => ["content", id],
|
|
375
|
-
localized: (id, locale) => [
|
|
376
|
-
"content",
|
|
377
|
-
id,
|
|
378
|
-
locale
|
|
379
|
-
],
|
|
380
|
-
sitePages: (site, locale) => [
|
|
381
|
-
"sites",
|
|
382
|
-
site,
|
|
383
|
-
"pages",
|
|
384
|
-
locale
|
|
385
|
-
],
|
|
386
|
-
pageByPathname: (site, pathname, locale) => [
|
|
387
|
-
"sites",
|
|
388
|
-
site,
|
|
389
|
-
"pathname",
|
|
390
|
-
pathname,
|
|
391
|
-
locale
|
|
392
|
-
],
|
|
393
|
-
pathnameAvailability: (site, pathname, excludeId) => [
|
|
394
|
-
"sites",
|
|
395
|
-
site,
|
|
396
|
-
"pathname-availability",
|
|
397
|
-
pathname,
|
|
398
|
-
excludeId
|
|
399
|
-
],
|
|
400
|
-
blocks: (collection, locale) => [
|
|
401
|
-
"blocks",
|
|
402
|
-
collection,
|
|
403
|
-
locale
|
|
404
|
-
],
|
|
405
|
-
blockByName: (name, collection, locale) => [
|
|
406
|
-
"blocks",
|
|
407
|
-
name,
|
|
408
|
-
collection,
|
|
409
|
-
locale
|
|
410
|
-
],
|
|
411
|
-
data: (collection, locale) => [
|
|
412
|
-
"data",
|
|
413
|
-
collection,
|
|
414
|
-
locale
|
|
415
|
-
],
|
|
416
|
-
dataByName: (name, collection, locale) => [
|
|
417
|
-
"data",
|
|
418
|
-
name,
|
|
419
|
-
collection,
|
|
420
|
-
locale
|
|
421
|
-
],
|
|
422
|
-
dataNameAvailability: (name, collection, excludeId) => [
|
|
423
|
-
"data",
|
|
424
|
-
"name-availability",
|
|
425
|
-
name,
|
|
426
|
-
collection,
|
|
427
|
-
excludeId
|
|
428
|
-
],
|
|
429
|
-
dataCollections: () => ["data", "collections"],
|
|
430
|
-
dataContext: (pageId) => ["data-context", pageId],
|
|
431
|
-
allContent: (filters) => [
|
|
432
|
-
"content",
|
|
433
|
-
"all",
|
|
434
|
-
filters
|
|
435
|
-
],
|
|
436
|
-
untranslatedContent: (targetLocale, excludeSites, includeUnpublished) => [
|
|
437
|
-
"content",
|
|
438
|
-
"untranslated",
|
|
439
|
-
targetLocale,
|
|
440
|
-
excludeSites,
|
|
441
|
-
includeUnpublished
|
|
442
|
-
],
|
|
443
|
-
sitesConfig: () => ["sites", "config"],
|
|
444
|
-
assets: () => ["assets"],
|
|
445
|
-
asset: (filename) => ["assets", filename],
|
|
446
|
-
assetFolders: (path) => ["asset-folders", path],
|
|
447
|
-
assetUsage: (filename) => ["asset-usage", filename],
|
|
448
|
-
folderTree: () => ["folder-tree"],
|
|
449
|
-
mdxCompile: (cacheKey) => [
|
|
450
|
-
"mdx",
|
|
451
|
-
"compile",
|
|
452
|
-
cacheKey
|
|
453
|
-
]
|
|
454
|
-
};
|
|
455
|
-
function useContent(id) {
|
|
456
|
-
const client = getContentAPIClient();
|
|
457
|
-
return useQuery({
|
|
458
|
-
queryKey: queryKeys.content(id),
|
|
459
|
-
queryFn: () => client.getContent(id),
|
|
460
|
-
enabled: !!id
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
function useLocalizedContent(id, locale) {
|
|
464
|
-
const client = getContentAPIClient();
|
|
465
|
-
return useQuery({
|
|
466
|
-
queryKey: queryKeys.localized(id, locale),
|
|
467
|
-
queryFn: () => client.getLocalized(id, locale),
|
|
468
|
-
enabled: !!id && !!locale
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
function useCompileMDX({ mdxContent, cacheKey }) {
|
|
472
|
-
const client = getContentAPIClient();
|
|
473
|
-
const resolvedCacheKey = cacheKey || (mdxContent ? `mdx-${hashString(mdxContent)}` : "mdx-empty");
|
|
474
|
-
return useQuery({
|
|
475
|
-
queryKey: queryKeys.mdxCompile(resolvedCacheKey),
|
|
476
|
-
queryFn: () => client.compileMDX(mdxContent),
|
|
477
|
-
enabled: !!mdxContent,
|
|
478
|
-
retry: false,
|
|
479
|
-
staleTime: Number.POSITIVE_INFINITY
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
function useCreateContent() {
|
|
483
|
-
const client = getContentAPIClient();
|
|
484
|
-
const queryClient = useQueryClient();
|
|
485
|
-
return useMutation({
|
|
486
|
-
mutationFn: async (data) => {
|
|
487
|
-
const result = await client.createContent(data);
|
|
488
|
-
if (result.success && result.id) {
|
|
489
|
-
const fullContent = await client.getContent(result.id);
|
|
490
|
-
return {
|
|
491
|
-
...result,
|
|
492
|
-
fullContent
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
return result;
|
|
496
|
-
},
|
|
497
|
-
onSuccess: (result, variables) => {
|
|
498
|
-
if (result.success && result.id && "fullContent" in result && result.fullContent) {
|
|
499
|
-
queryClient.setQueryData(queryKeys.content(result.id), result.fullContent);
|
|
500
|
-
const newManifest = {
|
|
501
|
-
id: result.fullContent.id,
|
|
502
|
-
type: result.fullContent.type,
|
|
503
|
-
kind: result.fullContent.kind,
|
|
504
|
-
site: result.fullContent.site,
|
|
505
|
-
collection: result.fullContent.collection,
|
|
506
|
-
locales: Object.entries(result.fullContent.locales).reduce((acc, [locale, localeData]) => {
|
|
507
|
-
const { content, ...manifestData } = localeData;
|
|
508
|
-
acc[locale] = manifestData;
|
|
509
|
-
return acc;
|
|
510
|
-
}, {})
|
|
511
|
-
};
|
|
512
|
-
if (variables.kind === "page" && variables.site) queryClient.setQueriesData({ queryKey: queryKeys.sitePages(variables.site) }, (old) => {
|
|
513
|
-
if (!isContentListResult(old)) return old;
|
|
514
|
-
return {
|
|
515
|
-
...old,
|
|
516
|
-
items: [...old.items, newManifest],
|
|
517
|
-
total: old.total + 1
|
|
518
|
-
};
|
|
519
|
-
});
|
|
520
|
-
else if (variables.kind === "block") queryClient.invalidateQueries({ queryKey: ["blocks"] });
|
|
521
|
-
else if (variables.kind === "data") queryClient.invalidateQueries({ queryKey: ["data"] });
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
function useUpdateLocalized() {
|
|
527
|
-
const client = getContentAPIClient();
|
|
528
|
-
const queryClient = useQueryClient();
|
|
529
|
-
return useMutation({
|
|
530
|
-
mutationFn: async (input) => {
|
|
531
|
-
const result = await client.updateLocalized(input);
|
|
532
|
-
if (result.success) {
|
|
533
|
-
const updatedContent = await client.getLocalized(input.id, input.locale);
|
|
534
|
-
return {
|
|
535
|
-
...result,
|
|
536
|
-
updatedContent
|
|
537
|
-
};
|
|
538
|
-
}
|
|
539
|
-
return result;
|
|
540
|
-
},
|
|
541
|
-
onSuccess: (result, variables) => {
|
|
542
|
-
if (result.success && "updatedContent" in result && result.updatedContent) {
|
|
543
|
-
queryClient.setQueryData(queryKeys.localized(variables.id, variables.locale), result.updatedContent);
|
|
544
|
-
queryClient.setQueryData(queryKeys.content(variables.id), (old) => {
|
|
545
|
-
if (!old) return old;
|
|
546
|
-
return {
|
|
547
|
-
...old,
|
|
548
|
-
locales: {
|
|
549
|
-
...old.locales,
|
|
550
|
-
[variables.locale]: result.updatedContent.localized
|
|
551
|
-
}
|
|
552
|
-
};
|
|
553
|
-
});
|
|
554
|
-
const updateListQueries = (old) => {
|
|
555
|
-
if (!isContentListResult(old)) return old;
|
|
556
|
-
return {
|
|
557
|
-
...old,
|
|
558
|
-
items: old.items.map((item) => item.id === variables.id ? {
|
|
559
|
-
...item,
|
|
560
|
-
locales: {
|
|
561
|
-
...item.locales,
|
|
562
|
-
[variables.locale]: {
|
|
563
|
-
locale: result.updatedContent.localized.locale,
|
|
564
|
-
etag: result.updatedContent.localized.etag,
|
|
565
|
-
created: result.updatedContent.localized.created,
|
|
566
|
-
modified: result.updatedContent.localized.modified,
|
|
567
|
-
pathname: result.updatedContent.localized.pathname,
|
|
568
|
-
previousPathnames: result.updatedContent.localized.previousPathnames,
|
|
569
|
-
name: result.updatedContent.localized.name,
|
|
570
|
-
publishAt: result.updatedContent.localized.publishAt,
|
|
571
|
-
unpublishAt: result.updatedContent.localized.unpublishAt,
|
|
572
|
-
meta: result.updatedContent.localized.meta
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
} : item)
|
|
576
|
-
};
|
|
577
|
-
};
|
|
578
|
-
queryClient.setQueriesData({
|
|
579
|
-
queryKey: ["sites"],
|
|
580
|
-
exact: false
|
|
581
|
-
}, updateListQueries);
|
|
582
|
-
queryClient.setQueriesData({
|
|
583
|
-
queryKey: ["blocks"],
|
|
584
|
-
exact: false
|
|
585
|
-
}, updateListQueries);
|
|
586
|
-
queryClient.setQueriesData({
|
|
587
|
-
queryKey: ["data"],
|
|
588
|
-
exact: false
|
|
589
|
-
}, updateListQueries);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
|
-
function useDeleteContent() {
|
|
595
|
-
const client = getContentAPIClient();
|
|
596
|
-
const queryClient = useQueryClient();
|
|
597
|
-
return useMutation({
|
|
598
|
-
mutationFn: async ({ id, etag, locale }) => {
|
|
599
|
-
if (locale) return client.deleteLocalized({
|
|
600
|
-
id,
|
|
601
|
-
locale,
|
|
602
|
-
etag
|
|
603
|
-
});
|
|
604
|
-
return client.deleteContent(id, etag);
|
|
605
|
-
},
|
|
606
|
-
onSuccess: (result, variables) => {
|
|
607
|
-
if (result.success) if (variables.locale) {
|
|
608
|
-
queryClient.setQueryData(queryKeys.content(variables.id), (old) => {
|
|
609
|
-
if (!old) return old;
|
|
610
|
-
const locale = variables.locale;
|
|
611
|
-
const { [locale]: _, ...remainingLocales } = old.locales;
|
|
612
|
-
return {
|
|
613
|
-
...old,
|
|
614
|
-
locales: remainingLocales
|
|
615
|
-
};
|
|
616
|
-
});
|
|
617
|
-
const updateListQueries = (old) => {
|
|
618
|
-
if (!isContentListResult(old)) return old;
|
|
619
|
-
const locale = variables.locale;
|
|
620
|
-
return {
|
|
621
|
-
...old,
|
|
622
|
-
items: old.items.map((item) => {
|
|
623
|
-
if (item.id === variables.id) {
|
|
624
|
-
const { [locale]: _, ...remainingLocales } = item.locales;
|
|
625
|
-
return {
|
|
626
|
-
...item,
|
|
627
|
-
locales: remainingLocales
|
|
628
|
-
};
|
|
629
|
-
}
|
|
630
|
-
return item;
|
|
631
|
-
}).filter((item) => Object.keys(item.locales).length > 0)
|
|
632
|
-
};
|
|
633
|
-
};
|
|
634
|
-
queryClient.setQueriesData({
|
|
635
|
-
queryKey: ["sites"],
|
|
636
|
-
exact: false
|
|
637
|
-
}, updateListQueries);
|
|
638
|
-
queryClient.setQueriesData({
|
|
639
|
-
queryKey: ["blocks"],
|
|
640
|
-
exact: false
|
|
641
|
-
}, updateListQueries);
|
|
642
|
-
queryClient.setQueriesData({
|
|
643
|
-
queryKey: ["data"],
|
|
644
|
-
exact: false
|
|
645
|
-
}, updateListQueries);
|
|
646
|
-
queryClient.setQueriesData({
|
|
647
|
-
queryKey: ["content", "all"],
|
|
648
|
-
exact: false
|
|
649
|
-
}, updateListQueries);
|
|
650
|
-
} else {
|
|
651
|
-
queryClient.removeQueries({ queryKey: queryKeys.content(variables.id) });
|
|
652
|
-
queryClient.removeQueries({
|
|
653
|
-
queryKey: ["content", variables.id],
|
|
654
|
-
exact: false
|
|
655
|
-
});
|
|
656
|
-
queryClient.setQueriesData({
|
|
657
|
-
queryKey: ["sites"],
|
|
658
|
-
exact: false
|
|
659
|
-
}, (old) => {
|
|
660
|
-
if (!isContentListResult(old)) return old;
|
|
661
|
-
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
662
|
-
return {
|
|
663
|
-
...old,
|
|
664
|
-
items: newItems,
|
|
665
|
-
total: newItems.length < old.items.length ? old.total - 1 : old.total
|
|
666
|
-
};
|
|
667
|
-
});
|
|
668
|
-
queryClient.setQueriesData({
|
|
669
|
-
queryKey: ["blocks"],
|
|
670
|
-
exact: false
|
|
671
|
-
}, (old) => {
|
|
672
|
-
if (!isContentListResult(old)) return old;
|
|
673
|
-
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
674
|
-
return {
|
|
675
|
-
...old,
|
|
676
|
-
items: newItems,
|
|
677
|
-
total: newItems.length < old.items.length ? old.total - 1 : old.total
|
|
678
|
-
};
|
|
679
|
-
});
|
|
680
|
-
queryClient.setQueriesData({
|
|
681
|
-
queryKey: ["data"],
|
|
682
|
-
exact: false
|
|
683
|
-
}, (old) => {
|
|
684
|
-
if (!isContentListResult(old)) return old;
|
|
685
|
-
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
686
|
-
return {
|
|
687
|
-
...old,
|
|
688
|
-
items: newItems,
|
|
689
|
-
total: newItems.length < old.items.length ? old.total - 1 : old.total
|
|
690
|
-
};
|
|
691
|
-
});
|
|
692
|
-
queryClient.setQueriesData({
|
|
693
|
-
queryKey: ["content", "all"],
|
|
694
|
-
exact: false
|
|
695
|
-
}, (old) => {
|
|
696
|
-
if (!isContentListResult(old)) return old;
|
|
697
|
-
const newItems = old.items.filter((item) => item.id !== variables.id);
|
|
698
|
-
return {
|
|
699
|
-
...old,
|
|
700
|
-
items: newItems,
|
|
701
|
-
total: newItems.length < old.items.length ? old.total - 1 : old.total
|
|
702
|
-
};
|
|
703
|
-
});
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
});
|
|
707
|
-
}
|
|
708
|
-
function useDeleteLocalized() {
|
|
709
|
-
const client = getContentAPIClient();
|
|
710
|
-
const queryClient = useQueryClient();
|
|
711
|
-
return useMutation({
|
|
712
|
-
mutationFn: ({ id, locale, etag }) => client.deleteLocalized({
|
|
713
|
-
id,
|
|
714
|
-
locale,
|
|
715
|
-
etag
|
|
716
|
-
}),
|
|
717
|
-
onSuccess: (result, variables) => {
|
|
718
|
-
if (result.success) {
|
|
719
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.localized(variables.id, variables.locale) });
|
|
720
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.content(variables.id) });
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
});
|
|
724
|
-
}
|
|
725
|
-
function useSitePages(site, locale) {
|
|
726
|
-
const client = getContentAPIClient();
|
|
727
|
-
return useQuery({
|
|
728
|
-
queryKey: queryKeys.sitePages(site, locale),
|
|
729
|
-
queryFn: () => client.getSitePages(site, locale)
|
|
730
|
-
});
|
|
731
|
-
}
|
|
732
|
-
function usePageByPathname(site, pathname, locale) {
|
|
733
|
-
const client = getContentAPIClient();
|
|
734
|
-
return useQuery({
|
|
735
|
-
queryKey: queryKeys.pageByPathname(site, pathname, locale),
|
|
736
|
-
queryFn: () => client.getPageByPathname(site, pathname, locale),
|
|
737
|
-
enabled: !!pathname
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
function usePathnameAvailability(site, pathname, excludeId) {
|
|
741
|
-
const client = getContentAPIClient();
|
|
742
|
-
return useQuery({
|
|
743
|
-
queryKey: queryKeys.pathnameAvailability(site, pathname, excludeId),
|
|
744
|
-
queryFn: async () => {
|
|
745
|
-
return { available: await client.isPathnameAvailable(site, pathname, excludeId) };
|
|
746
|
-
},
|
|
747
|
-
enabled: !!site && !!pathname,
|
|
748
|
-
staleTime: 0,
|
|
749
|
-
gcTime: 0
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
|
-
function useMovePage() {
|
|
753
|
-
const client = getContentAPIClient();
|
|
754
|
-
const queryClient = useQueryClient();
|
|
755
|
-
return useMutation({
|
|
756
|
-
mutationFn: ({ site, id, pathname, locale = "en", etag }) => client.movePage(site, id, pathname, locale, etag),
|
|
757
|
-
onSuccess: (result, variables) => {
|
|
758
|
-
if (result.moved) {
|
|
759
|
-
queryClient.invalidateQueries({ queryKey: ["sites", variables.site] });
|
|
760
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.content(variables.id) });
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
});
|
|
764
|
-
}
|
|
765
|
-
function useBlocks(collection, locale) {
|
|
766
|
-
const client = getContentAPIClient();
|
|
767
|
-
return useQuery({
|
|
768
|
-
queryKey: queryKeys.blocks(collection, locale),
|
|
769
|
-
queryFn: () => client.getBlocks(collection, locale)
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
function useBlockByName(name, collection, locale) {
|
|
773
|
-
const client = getContentAPIClient();
|
|
774
|
-
return useQuery({
|
|
775
|
-
queryKey: queryKeys.blockByName(name, collection, locale),
|
|
776
|
-
queryFn: () => client.getBlockByName(name, collection, locale),
|
|
777
|
-
enabled: !!name
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
function useData(collection, locale) {
|
|
781
|
-
const client = getContentAPIClient();
|
|
782
|
-
return useQuery({
|
|
783
|
-
queryKey: queryKeys.data(collection, locale),
|
|
784
|
-
queryFn: () => client.getData(collection, locale)
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
function useDataByName(name, collection, locale) {
|
|
788
|
-
const client = getContentAPIClient();
|
|
789
|
-
return useQuery({
|
|
790
|
-
queryKey: queryKeys.dataByName(name, collection, locale),
|
|
791
|
-
queryFn: () => client.getDataByName(name, collection, locale),
|
|
792
|
-
enabled: !!name && !!collection
|
|
793
|
-
});
|
|
794
|
-
}
|
|
795
|
-
function useDataNameAvailability(name, collection, excludeId) {
|
|
796
|
-
const client = getContentAPIClient();
|
|
797
|
-
return useQuery({
|
|
798
|
-
queryKey: queryKeys.dataNameAvailability(name, collection, excludeId),
|
|
799
|
-
queryFn: async () => {
|
|
800
|
-
return { available: await client.isDataNameAvailable(name, collection, excludeId) };
|
|
801
|
-
},
|
|
802
|
-
enabled: !!name && !!collection,
|
|
803
|
-
staleTime: 0,
|
|
804
|
-
gcTime: 0
|
|
805
|
-
});
|
|
806
|
-
}
|
|
807
|
-
function useDataCollections() {
|
|
808
|
-
const client = getContentAPIClient();
|
|
809
|
-
return useQuery({
|
|
810
|
-
queryKey: queryKeys.dataCollections(),
|
|
811
|
-
queryFn: () => client.getDataCollections()
|
|
812
|
-
});
|
|
813
|
-
}
|
|
814
|
-
function useDataContext(pageId) {
|
|
815
|
-
const client = getContentAPIClient();
|
|
816
|
-
return useQuery({
|
|
817
|
-
queryKey: queryKeys.dataContext(pageId),
|
|
818
|
-
queryFn: () => client.getDataContext(pageId),
|
|
819
|
-
enabled: !!pageId
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
function useAllContent(filters) {
|
|
823
|
-
const client = getContentAPIClient();
|
|
824
|
-
return useQuery({
|
|
825
|
-
queryKey: queryKeys.allContent(filters),
|
|
826
|
-
queryFn: () => client.listAllContent(filters)
|
|
827
|
-
});
|
|
828
|
-
}
|
|
829
|
-
function useUntranslatedContent(targetLocale, excludeSites, includeUnpublished) {
|
|
830
|
-
const client = getContentAPIClient();
|
|
831
|
-
return useQuery({
|
|
832
|
-
queryKey: queryKeys.untranslatedContent(targetLocale, excludeSites, includeUnpublished),
|
|
833
|
-
queryFn: () => client.findUntranslatedContent(targetLocale, excludeSites, includeUnpublished)
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
function useSitesConfig() {
|
|
837
|
-
const client = getContentAPIClient();
|
|
838
|
-
return useQuery({
|
|
839
|
-
queryKey: queryKeys.sitesConfig(),
|
|
840
|
-
queryFn: () => client.getSitesConfig()
|
|
841
|
-
});
|
|
842
|
-
}
|
|
843
|
-
function useCurrentUser() {
|
|
844
|
-
const client = getContentAPIClient();
|
|
845
|
-
return useQuery({
|
|
846
|
-
queryKey: queryKeys.currentUser(),
|
|
847
|
-
queryFn: () => client.getCurrentUser(),
|
|
848
|
-
staleTime: 6e4
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
function useBatchUpdate() {
|
|
852
|
-
const client = getContentAPIClient();
|
|
853
|
-
const queryClient = useQueryClient();
|
|
854
|
-
return useMutation({
|
|
855
|
-
mutationFn: async (operations) => {
|
|
856
|
-
const result = await client.batchUpdate(operations);
|
|
857
|
-
if (result.operations && result.updated > 0) {
|
|
858
|
-
const updatedContents = await Promise.all(result.operations.map(async (opResult, index) => {
|
|
859
|
-
if (opResult.updated) {
|
|
860
|
-
const op = operations[index];
|
|
861
|
-
return {
|
|
862
|
-
index,
|
|
863
|
-
content: await client.getLocalized(op.id, op.locale)
|
|
864
|
-
};
|
|
865
|
-
}
|
|
866
|
-
return null;
|
|
867
|
-
}));
|
|
868
|
-
return {
|
|
869
|
-
...result,
|
|
870
|
-
updatedContents: updatedContents.filter((item) => item !== null)
|
|
871
|
-
};
|
|
872
|
-
}
|
|
873
|
-
return result;
|
|
874
|
-
},
|
|
875
|
-
onSuccess: (result, variables) => {
|
|
876
|
-
if ("updatedContents" in result && result.updatedContents) {
|
|
877
|
-
result.updatedContents.forEach(({ index, content }) => {
|
|
878
|
-
if (!content) return;
|
|
879
|
-
const operation = variables[index];
|
|
880
|
-
queryClient.setQueryData(queryKeys.localized(operation.id, operation.locale), content);
|
|
881
|
-
queryClient.setQueryData(queryKeys.content(operation.id), (old) => {
|
|
882
|
-
if (!old) return old;
|
|
883
|
-
return {
|
|
884
|
-
...old,
|
|
885
|
-
locales: {
|
|
886
|
-
...old.locales,
|
|
887
|
-
[operation.locale]: content.localized
|
|
888
|
-
}
|
|
889
|
-
};
|
|
890
|
-
});
|
|
891
|
-
});
|
|
892
|
-
const updatedMap = new Map(result.updatedContents.map(({ index, content }) => {
|
|
893
|
-
const op = variables[index];
|
|
894
|
-
return [op.id + ":" + op.locale, content];
|
|
895
|
-
}));
|
|
896
|
-
const updateListQueries = (old) => {
|
|
897
|
-
if (!isContentListResult(old)) return old;
|
|
898
|
-
return {
|
|
899
|
-
...old,
|
|
900
|
-
items: old.items.map((item) => {
|
|
901
|
-
if (Object.keys(item.locales).some((locale) => updatedMap.has(item.id + ":" + locale))) {
|
|
902
|
-
const newLocales = { ...item.locales };
|
|
903
|
-
Object.keys(newLocales).forEach((locale) => {
|
|
904
|
-
const updated = updatedMap.get(item.id + ":" + locale);
|
|
905
|
-
if (updated) {
|
|
906
|
-
const { content, ...manifestData } = updated.localized;
|
|
907
|
-
newLocales[locale] = manifestData;
|
|
908
|
-
}
|
|
909
|
-
});
|
|
910
|
-
return {
|
|
911
|
-
...item,
|
|
912
|
-
locales: newLocales
|
|
913
|
-
};
|
|
914
|
-
}
|
|
915
|
-
return item;
|
|
916
|
-
})
|
|
917
|
-
};
|
|
918
|
-
};
|
|
919
|
-
queryClient.setQueriesData({
|
|
920
|
-
queryKey: ["sites"],
|
|
921
|
-
exact: false
|
|
922
|
-
}, updateListQueries);
|
|
923
|
-
queryClient.setQueriesData({
|
|
924
|
-
queryKey: ["blocks"],
|
|
925
|
-
exact: false
|
|
926
|
-
}, updateListQueries);
|
|
927
|
-
queryClient.setQueriesData({
|
|
928
|
-
queryKey: ["data"],
|
|
929
|
-
exact: false
|
|
930
|
-
}, updateListQueries);
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
});
|
|
934
|
-
}
|
|
935
|
-
function useGitStatus() {
|
|
936
|
-
const client = getContentAPIClient();
|
|
937
|
-
return useQuery({
|
|
938
|
-
queryKey: queryKeys.gitStatus(),
|
|
939
|
-
queryFn: () => client.getGitStatus(),
|
|
940
|
-
refetchInterval: 1e4,
|
|
941
|
-
staleTime: 5e3
|
|
942
|
-
});
|
|
943
|
-
}
|
|
944
|
-
function useCommitChanges() {
|
|
945
|
-
const queryClient = useQueryClient();
|
|
946
|
-
const client = getContentAPIClient();
|
|
947
|
-
return useMutation({
|
|
948
|
-
mutationFn: (message) => client.commitChanges(message),
|
|
949
|
-
onSuccess: () => {
|
|
950
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus() });
|
|
951
|
-
}
|
|
952
|
-
});
|
|
953
|
-
}
|
|
954
|
-
function usePushChanges() {
|
|
955
|
-
const queryClient = useQueryClient();
|
|
956
|
-
const client = getContentAPIClient();
|
|
957
|
-
return useMutation({
|
|
958
|
-
mutationFn: () => client.pushChanges(),
|
|
959
|
-
onSuccess: () => {
|
|
960
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus() });
|
|
961
|
-
}
|
|
962
|
-
});
|
|
963
|
-
}
|
|
964
|
-
function usePullChanges() {
|
|
965
|
-
const queryClient = useQueryClient();
|
|
966
|
-
const client = getContentAPIClient();
|
|
967
|
-
return useMutation({
|
|
968
|
-
mutationFn: () => client.pullChanges(),
|
|
969
|
-
onSuccess: () => {
|
|
970
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.gitStatus() });
|
|
971
|
-
}
|
|
972
|
-
});
|
|
973
|
-
}
|
|
974
|
-
function useAssets() {
|
|
975
|
-
const client = getContentAPIClient();
|
|
976
|
-
return useQuery({
|
|
977
|
-
queryKey: queryKeys.assets(),
|
|
978
|
-
queryFn: () => client.listAssets()
|
|
979
|
-
});
|
|
980
|
-
}
|
|
981
|
-
function useAsset(filename) {
|
|
982
|
-
const client = getContentAPIClient();
|
|
983
|
-
return useQuery({
|
|
984
|
-
queryKey: queryKeys.asset(filename),
|
|
985
|
-
queryFn: () => client.getAsset(filename),
|
|
986
|
-
enabled: !!filename
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
function useUploadAsset() {
|
|
990
|
-
const client = getContentAPIClient();
|
|
991
|
-
const queryClient = useQueryClient();
|
|
992
|
-
return useMutation({
|
|
993
|
-
mutationFn: (formData) => client.uploadAsset(formData),
|
|
994
|
-
onSuccess: () => {
|
|
995
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
996
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
997
|
-
}
|
|
998
|
-
});
|
|
999
|
-
}
|
|
1000
|
-
function useImportAssetUrl() {
|
|
1001
|
-
const client = getContentAPIClient();
|
|
1002
|
-
const queryClient = useQueryClient();
|
|
1003
|
-
return useMutation({
|
|
1004
|
-
mutationFn: ({ url, alt, folder }) => client.importAssetUrl(url, alt, folder),
|
|
1005
|
-
onSuccess: () => {
|
|
1006
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
1007
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
1008
|
-
}
|
|
1009
|
-
});
|
|
1010
|
-
}
|
|
1011
|
-
function useDeleteAsset() {
|
|
1012
|
-
const client = getContentAPIClient();
|
|
1013
|
-
const queryClient = useQueryClient();
|
|
1014
|
-
return useMutation({
|
|
1015
|
-
mutationFn: (filename) => client.deleteAsset(filename),
|
|
1016
|
-
onSuccess: () => {
|
|
1017
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
1018
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
1019
|
-
}
|
|
1020
|
-
});
|
|
1021
|
-
}
|
|
1022
|
-
function useBulkDeleteAssets() {
|
|
1023
|
-
const client = getContentAPIClient();
|
|
1024
|
-
const queryClient = useQueryClient();
|
|
1025
|
-
return useMutation({
|
|
1026
|
-
mutationFn: async (filenames) => {
|
|
1027
|
-
let successCount = 0;
|
|
1028
|
-
let failCount = 0;
|
|
1029
|
-
for (const filename of filenames) try {
|
|
1030
|
-
await client.deleteAsset(filename);
|
|
1031
|
-
successCount++;
|
|
1032
|
-
} catch {
|
|
1033
|
-
failCount++;
|
|
1034
|
-
}
|
|
1035
|
-
return {
|
|
1036
|
-
successCount,
|
|
1037
|
-
failCount
|
|
1038
|
-
};
|
|
1039
|
-
},
|
|
1040
|
-
onSettled: () => {
|
|
1041
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
1042
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
1043
|
-
}
|
|
1044
|
-
});
|
|
1045
|
-
}
|
|
1046
|
-
function useAssetFolders(path) {
|
|
1047
|
-
const client = getContentAPIClient();
|
|
1048
|
-
return useQuery({
|
|
1049
|
-
queryKey: queryKeys.assetFolders(path),
|
|
1050
|
-
queryFn: () => client.listFolder(path)
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
function useCreateFolder() {
|
|
1054
|
-
const client = getContentAPIClient();
|
|
1055
|
-
const queryClient = useQueryClient();
|
|
1056
|
-
return useMutation({
|
|
1057
|
-
mutationFn: (path) => client.createFolder(path),
|
|
1058
|
-
onSuccess: () => {
|
|
1059
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
1060
|
-
}
|
|
1061
|
-
});
|
|
1062
|
-
}
|
|
1063
|
-
function useUpdateAssetMetadata() {
|
|
1064
|
-
const client = getContentAPIClient();
|
|
1065
|
-
const queryClient = useQueryClient();
|
|
1066
|
-
return useMutation({
|
|
1067
|
-
mutationFn: ({ filename, updates }) => client.updateAssetMetadata(filename, updates),
|
|
1068
|
-
onSuccess: () => {
|
|
1069
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
1070
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
1071
|
-
}
|
|
1072
|
-
});
|
|
1073
|
-
}
|
|
1074
|
-
function useAssetUsage(filename) {
|
|
1075
|
-
const client = getContentAPIClient();
|
|
1076
|
-
return useQuery({
|
|
1077
|
-
queryKey: queryKeys.assetUsage(filename),
|
|
1078
|
-
queryFn: () => client.getAssetUsage(filename),
|
|
1079
|
-
enabled: !!filename,
|
|
1080
|
-
staleTime: 6e4
|
|
1081
|
-
});
|
|
1082
|
-
}
|
|
1083
|
-
function useFolderTree() {
|
|
1084
|
-
const client = getContentAPIClient();
|
|
1085
|
-
return useQuery({
|
|
1086
|
-
queryKey: queryKeys.folderTree(),
|
|
1087
|
-
queryFn: () => client.getFolderTree(),
|
|
1088
|
-
staleTime: 3e4
|
|
1089
|
-
});
|
|
1090
|
-
}
|
|
1091
|
-
function useMoveAssets() {
|
|
1092
|
-
const client = getContentAPIClient();
|
|
1093
|
-
const queryClient = useQueryClient();
|
|
1094
|
-
return useMutation({
|
|
1095
|
-
mutationFn: ({ filenames, sourceFolder, targetFolder }) => client.moveAssets(filenames, sourceFolder, targetFolder),
|
|
1096
|
-
onSuccess: () => {
|
|
1097
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.assets() });
|
|
1098
|
-
queryClient.invalidateQueries({ queryKey: ["asset-folders"] });
|
|
1099
|
-
queryClient.invalidateQueries({ queryKey: queryKeys.folderTree() });
|
|
1100
|
-
}
|
|
1101
|
-
});
|
|
1102
|
-
}
|
|
1103
|
-
//#endregion
|
|
1104
|
-
export { APIClientError, API_ROUTES, ContentAPIClient, ErrorCodes, StaleWriteError, blockEditableSchema, dataEditableSchema, formatFileSize, getContentAPIClient, localesOf, normalizeAndValidatePathname, setContentAPIClient, useAllContent, useAsset, useAssetFolders, useAssetUsage, useAssets, useBatchUpdate, useBlockByName, useBlocks, useBulkDeleteAssets, useCommitChanges, useCompileMDX, useContent, useCreateContent, useCreateFolder, useCurrentUser, useData, useDataByName, useDataCollections, useDataContext, useDataNameAvailability, useDeleteAsset, useDeleteContent, useDeleteLocalized, useFolderTree, useGitStatus, useImportAssetUrl, useLocalizedContent, useMoveAssets, useMovePage, usePageByPathname, usePathnameAvailability, usePullChanges, usePushChanges, useSitePages, useSitesConfig, useUntranslatedContent, useUpdateAssetMetadata, useUpdateLocalized, useUploadAsset };
|
|
1105
|
-
|
|
1106
|
-
//# sourceMappingURL=index.mjs.map
|