@conloca/content-api-client 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +190 -105
- package/dist/client.d.ts +92 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +435 -0
- 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 +142 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +761 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/content-api-client.d.ts +2 -0
- package/dist/lib/content-api-client.d.ts.map +1 -0
- package/dist/lib/content-api-client.js +4 -0
- package/dist/lib/content-api-client.js.map +1 -0
- package/dist/test-helpers.d.ts +89 -0
- package/dist/test-helpers.d.ts.map +1 -0
- package/dist/test-helpers.js +236 -0
- package/dist/test-helpers.js.map +1 -0
- package/dist/testing.d.ts +8 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +8 -0
- package/dist/testing.js.map +1 -0
- package/package.json +64 -19
- 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 -240
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -1105
- package/dist/index.mjs.map +0 -1
- package/dist/test-helpers-BJxZXOMi.d.mts +0 -94
- package/dist/test-helpers-BJxZXOMi.d.mts.map +0 -1
- package/dist/test-helpers-CRvDHjs2.mjs +0 -220
- package/dist/test-helpers-CRvDHjs2.mjs.map +0 -1
- package/dist/testing.d.mts +0 -2
- package/dist/testing.mjs +0 -3
package/README.md
CHANGED
|
@@ -8,10 +8,10 @@ content operations.
|
|
|
8
8
|
This package provides:
|
|
9
9
|
|
|
10
10
|
- `ContentAPIClient` - HTTP client for content operations
|
|
11
|
-
- React hooks (`useContent`, `
|
|
11
|
+
- React hooks (`useContent`, `useLocalizedContent`, `useUpdateLocalized`, etc.) built on TanStack Query
|
|
12
12
|
- Full TypeScript support with comprehensive types
|
|
13
|
-
- Dual
|
|
14
|
-
- Test helpers
|
|
13
|
+
- Dual ETags that refuse a write made against an outdated version
|
|
14
|
+
- Test helpers (`@conloca/content-api-client/testing`) that mock `fetch` in unit tests
|
|
15
15
|
|
|
16
16
|
## Installation
|
|
17
17
|
|
|
@@ -19,6 +19,10 @@ This package provides:
|
|
|
19
19
|
bun add @conloca/content-api-client
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
The React hooks need React 19 (a peer dependency) and a `QueryClientProvider` from `@tanstack/react-query` above the
|
|
23
|
+
components that use them. Add `@tanstack/react-query` to your app in a version that satisfies the range in this
|
|
24
|
+
package's `dependencies`, so your provider and the hooks use the same copy.
|
|
25
|
+
|
|
22
26
|
## Usage
|
|
23
27
|
|
|
24
28
|
### Basic Client Usage
|
|
@@ -29,12 +33,13 @@ import { ContentAPIClient } from '@conloca/content-api-client';
|
|
|
29
33
|
// Create client instance
|
|
30
34
|
const client = new ContentAPIClient({
|
|
31
35
|
baseUrl: '/__conloca/api', // optional, defaults to '/__conloca/api'
|
|
36
|
+
// fetch: myFetch, // optional, defaults to the global fetch at the time the client is created
|
|
32
37
|
});
|
|
33
38
|
|
|
34
|
-
// Get content by ID
|
|
39
|
+
// Get content by ID (null when there is no such entry)
|
|
35
40
|
const content = await client.getContent('page-id');
|
|
36
41
|
|
|
37
|
-
// Get localized content
|
|
42
|
+
// Get localized content (null when the entry or the locale does not exist)
|
|
38
43
|
const localizedContent = await client.getLocalized('page-id', 'en');
|
|
39
44
|
|
|
40
45
|
// Create new content
|
|
@@ -53,45 +58,69 @@ const result = await client.createContent({
|
|
|
53
58
|
});
|
|
54
59
|
|
|
55
60
|
// Update content with ETag validation
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
if (localizedContent) {
|
|
62
|
+
const updateResult = await client.updateLocalized({
|
|
63
|
+
id: 'page-id',
|
|
64
|
+
locale: 'en',
|
|
65
|
+
data: {
|
|
66
|
+
meta: { title: 'Updated Title' },
|
|
67
|
+
},
|
|
68
|
+
etag: localizedContent.localized.etag,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
64
71
|
```
|
|
65
72
|
|
|
73
|
+
The dev server of an `@conloca/astro-cms` site serves the API under its editor route, at `/__cms/api` unless you changed
|
|
74
|
+
`route`; pass that as `baseUrl` there.
|
|
75
|
+
|
|
66
76
|
### React Hooks Usage
|
|
67
77
|
|
|
68
|
-
|
|
69
|
-
|
|
78
|
+
The hooks share one client. It calls `/__conloca/api` until you replace it, once before the first render, with
|
|
79
|
+
`setContentAPIClient(new ContentAPIClient({ baseUrl: '/__cms/api' }))`.
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
83
|
+
import { StaleWriteError, useLocalizedContent, useUpdateLocalized } from '@conloca/content-api-client';
|
|
84
|
+
|
|
85
|
+
const queryClient = new QueryClient();
|
|
70
86
|
|
|
71
|
-
function
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
export function App() {
|
|
88
|
+
return (
|
|
89
|
+
<QueryClientProvider client={queryClient}>
|
|
90
|
+
<TitleEditor contentId="page-id" />
|
|
91
|
+
</QueryClientProvider>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function TitleEditor({ contentId }: { contentId: string }) {
|
|
96
|
+
// Fetch one locale of the entry, with its ETag
|
|
97
|
+
const { data: entry, isLoading } = useLocalizedContent(contentId, 'en');
|
|
74
98
|
|
|
75
99
|
// Update mutation
|
|
76
|
-
const
|
|
100
|
+
const updateLocalized = useUpdateLocalized();
|
|
77
101
|
|
|
78
102
|
const handleUpdate = async () => {
|
|
103
|
+
if (!entry) return;
|
|
79
104
|
try {
|
|
80
|
-
await
|
|
105
|
+
await updateLocalized.mutateAsync({
|
|
81
106
|
id: contentId,
|
|
82
107
|
locale: 'en',
|
|
83
108
|
data: { meta: { title: 'New Title' } },
|
|
84
|
-
etag:
|
|
109
|
+
etag: entry.localized.etag,
|
|
85
110
|
});
|
|
86
111
|
} catch (error) {
|
|
87
112
|
if (error instanceof StaleWriteError) {
|
|
88
|
-
// Handle conflict - error.
|
|
113
|
+
// Handle conflict - error.currentEtag is the ETag of the version on the server
|
|
89
114
|
}
|
|
90
115
|
}
|
|
91
116
|
};
|
|
92
117
|
|
|
118
|
+
if (isLoading) return <p>Loading…</p>;
|
|
119
|
+
|
|
93
120
|
return (
|
|
94
|
-
|
|
121
|
+
<button type="button" onClick={handleUpdate} disabled={updateLocalized.isPending}>
|
|
122
|
+
Rename “{entry?.localized.meta.title}”
|
|
123
|
+
</button>
|
|
95
124
|
);
|
|
96
125
|
}
|
|
97
126
|
```
|
|
@@ -118,11 +147,19 @@ and error cases.
|
|
|
118
147
|
|
|
119
148
|
```typescript
|
|
120
149
|
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
|
121
|
-
import { ContentAPIClient } from '
|
|
122
|
-
import { setupFetchMock } from '
|
|
150
|
+
import { ContentAPIClient, StaleWriteError } from '@conloca/content-api-client';
|
|
151
|
+
import { type MockAPIResponses, setupFetchMock } from '@conloca/content-api-client/testing';
|
|
123
152
|
|
|
124
153
|
describe('ContentAPIClient', () => {
|
|
125
|
-
|
|
154
|
+
let client: ContentAPIClient;
|
|
155
|
+
let responses: MockAPIResponses;
|
|
156
|
+
|
|
157
|
+
beforeEach(() => {
|
|
158
|
+
// setupFetchMock replaces the global fetch, so create the client after it:
|
|
159
|
+
// a client keeps the fetch it found when it was created.
|
|
160
|
+
({ responses } = setupFetchMock(mock));
|
|
161
|
+
client = new ContentAPIClient();
|
|
162
|
+
});
|
|
126
163
|
|
|
127
164
|
it('should handle 404 responses', async () => {
|
|
128
165
|
responses.mockGetContent(null); // Returns 404
|
|
@@ -149,7 +186,8 @@ describe('ContentAPIClient', () => {
|
|
|
149
186
|
### 2. Integration Tests with InMemoryContentAPI (`client-integration.test.ts`)
|
|
150
187
|
|
|
151
188
|
Integration tests use the real `InMemoryContentAPI` implementation with the full middleware stack to test end-to-end
|
|
152
|
-
behavior.
|
|
189
|
+
behavior. `InMemoryContentAPI` and `createContentAPIRouter` come from `@conloca/content-api/node`; add
|
|
190
|
+
`@conloca/content-api` to your dev dependencies to import them.
|
|
153
191
|
|
|
154
192
|
**When to use InMemoryContentAPI:**
|
|
155
193
|
|
|
@@ -162,60 +200,71 @@ behavior.
|
|
|
162
200
|
**Example:**
|
|
163
201
|
|
|
164
202
|
```typescript
|
|
203
|
+
import { beforeEach, describe, expect, it } from 'bun:test';
|
|
165
204
|
import { createContentAPIRouter, InMemoryContentAPI } from '@conloca/content-api/node';
|
|
166
|
-
import { ContentAPIClient } from '
|
|
205
|
+
import { ContentAPIClient } from '@conloca/content-api-client';
|
|
167
206
|
|
|
168
207
|
describe('ContentAPIClient Integration Tests', () => {
|
|
169
|
-
let contentApi: InMemoryContentAPI;
|
|
170
208
|
let client: ContentAPIClient;
|
|
171
209
|
|
|
172
210
|
beforeEach(() => {
|
|
173
211
|
// Create in-memory API
|
|
174
|
-
contentApi = new InMemoryContentAPI({
|
|
175
|
-
sites: { default: { locales: ['en', 'nl'] } }
|
|
212
|
+
const contentApi = new InMemoryContentAPI({
|
|
213
|
+
sites: { default: { locales: ['en', 'nl'], defaultLocale: 'en' } },
|
|
214
|
+
globalLocales: ['en', 'nl'],
|
|
176
215
|
});
|
|
177
216
|
|
|
178
|
-
//
|
|
179
|
-
const
|
|
217
|
+
// The router answers the API routes from its root (`/content`, `/sites`, ...)
|
|
218
|
+
const router = createContentAPIRouter(contentApi);
|
|
180
219
|
|
|
181
|
-
//
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
client = new ContentAPIClient({ baseUrl: 'http://test/__conloca/api' });
|
|
220
|
+
// Send the client's requests straight to the router
|
|
221
|
+
client = new ContentAPIClient({
|
|
222
|
+
baseUrl: 'http://test',
|
|
223
|
+
fetch: ((input, init) => router.request(input, init)) as typeof fetch,
|
|
224
|
+
});
|
|
188
225
|
});
|
|
189
226
|
|
|
190
227
|
it('should handle complete content lifecycle', async () => {
|
|
191
228
|
// Create content
|
|
192
|
-
const createResult = await client.createContent({
|
|
229
|
+
const createResult = await client.createContent({
|
|
230
|
+
kind: 'page',
|
|
231
|
+
site: 'default',
|
|
232
|
+
collection: 'pages',
|
|
233
|
+
type: 'puck',
|
|
234
|
+
locales: { en: { pathname: '/about', meta: { title: 'About' }, content: { puckData: { root: {} } } } },
|
|
235
|
+
});
|
|
236
|
+
const id = createResult.id!;
|
|
193
237
|
|
|
194
238
|
// Read it back
|
|
195
|
-
const content = await client.getContent(
|
|
239
|
+
const content = await client.getContent(id);
|
|
240
|
+
expect(content?.locales.en.pathname).toBe('/about');
|
|
196
241
|
|
|
197
242
|
// Update it
|
|
198
|
-
await client.updateLocalized({
|
|
243
|
+
await client.updateLocalized({
|
|
244
|
+
id,
|
|
245
|
+
locale: 'en',
|
|
246
|
+
data: { meta: { title: 'About Us' } },
|
|
247
|
+
etag: content!.locales.en.etag,
|
|
248
|
+
});
|
|
199
249
|
|
|
200
|
-
// Delete it
|
|
201
|
-
await client.
|
|
250
|
+
// Delete it: deleting a whole entry takes the current ETag of any one of its locales
|
|
251
|
+
const updated = await client.getContent(id);
|
|
252
|
+
await client.deleteContent(id, updated!.locales.en.etag);
|
|
202
253
|
});
|
|
203
254
|
});
|
|
204
255
|
```
|
|
205
256
|
|
|
206
257
|
## Test Helpers
|
|
207
258
|
|
|
208
|
-
The package provides
|
|
259
|
+
The package provides test helpers in `@conloca/content-api-client/testing`:
|
|
209
260
|
|
|
210
261
|
### Mock Factories
|
|
211
262
|
|
|
263
|
+
`mockContentEntry`, `mockContentManifest`, `mockLocalizedEntry`, `mockCreateResult`, `mockUpdateResult`,
|
|
264
|
+
`mockDeleteResult` and `mockBatchResult` each return a complete value of their type; pass the fields you want to change.
|
|
265
|
+
|
|
212
266
|
```typescript
|
|
213
|
-
import {
|
|
214
|
-
mockContentEntry,
|
|
215
|
-
mockLocalizedEntry,
|
|
216
|
-
mockCreateResult,
|
|
217
|
-
mockUpdateResult,
|
|
218
|
-
} from '@conloca/content-api-client/test-helpers';
|
|
267
|
+
import { mockContentEntry } from '@conloca/content-api-client/testing';
|
|
219
268
|
|
|
220
269
|
// Create mock data with proper types
|
|
221
270
|
const content = mockContentEntry({
|
|
@@ -226,31 +275,49 @@ const content = mockContentEntry({
|
|
|
226
275
|
|
|
227
276
|
### Fetch Mock Setup
|
|
228
277
|
|
|
278
|
+
`setupFetchMock` replaces the global `fetch` with a mock (it does not restore it) and returns that mock with a
|
|
279
|
+
`MockAPIResponses` that queues one response per call: `mockGetContent`, `mockGetLocalized`, `mockListContent`,
|
|
280
|
+
`mockCreateContent`, `mockUpdateContent`, `mockDeleteContent`, `mockStaleWrite`, `mockError` and `mockNetworkError`.
|
|
281
|
+
Create the client after calling it.
|
|
282
|
+
|
|
229
283
|
```typescript
|
|
230
|
-
import { setupFetchMock } from '@conloca/content-api-client/test-helpers';
|
|
231
284
|
import { mock } from 'bun:test';
|
|
285
|
+
import { mockContentEntry, setupFetchMock } from '@conloca/content-api-client/testing';
|
|
232
286
|
|
|
233
287
|
const { fetchMock, responses } = setupFetchMock(mock);
|
|
234
288
|
|
|
235
289
|
// Chain mock responses
|
|
236
290
|
responses
|
|
237
|
-
.mockGetContent(
|
|
291
|
+
.mockGetContent(mockContentEntry())
|
|
238
292
|
.mockUpdateContent({ success: true, etag: 'new.etag' })
|
|
239
293
|
.mockStaleWrite('current.etag');
|
|
240
294
|
```
|
|
241
295
|
|
|
296
|
+
`jsonResponse(body, { status, headers })` builds a JSON `Response` for your own mocks, and `API_ROUTES` holds API route
|
|
297
|
+
paths relative to `baseUrl`.
|
|
298
|
+
|
|
242
299
|
## Dual ETag System
|
|
243
300
|
|
|
244
|
-
The content API uses a dual ETag system for optimistic
|
|
301
|
+
The content API uses a dual ETag system for optimistic concurrency control:
|
|
245
302
|
|
|
246
|
-
- **Format**: `metaHash.contentHash`
|
|
303
|
+
- **Format**: `metaHash.contentHash` for pages and fragments; a data entry has a single hash
|
|
247
304
|
- **Meta Hash**: Changes when metadata (title, description, etc.) changes
|
|
248
305
|
- **Content Hash**: Changes when actual content changes
|
|
249
306
|
|
|
250
|
-
|
|
307
|
+
The file-backed API (`FileSystemContentAPI`, which `createContentAPI` from `@conloca/content-api/node` returns) checks
|
|
308
|
+
only the half a write changes: a `meta`, `publishAt` or `unpublishAt` change needs the current meta hash, a `content`
|
|
309
|
+
change the current content hash. A metadata or content write also updates the entry's `modified` time, which is part of
|
|
310
|
+
the metadata, so a metadata write changes only the meta hash while a content write changes both. Two writes sent with
|
|
311
|
+
the same loaded ETag therefore both succeed only when the metadata write comes first:
|
|
251
312
|
|
|
252
313
|
```typescript
|
|
253
|
-
|
|
314
|
+
import { ContentAPIClient } from '@conloca/content-api-client';
|
|
315
|
+
|
|
316
|
+
const client = new ContentAPIClient();
|
|
317
|
+
|
|
318
|
+
// Both users loaded the page when its ETag was 'oldMeta.oldContent'
|
|
319
|
+
|
|
320
|
+
// User 1 updates metadata first
|
|
254
321
|
await client.updateLocalized({
|
|
255
322
|
id: 'page-id',
|
|
256
323
|
locale: 'en',
|
|
@@ -258,21 +325,24 @@ await client.updateLocalized({
|
|
|
258
325
|
etag: 'oldMeta.oldContent',
|
|
259
326
|
});
|
|
260
327
|
|
|
261
|
-
// User 2
|
|
328
|
+
// User 2 then updates content with the ETag they loaded:
|
|
329
|
+
// the metadata write left its content hash current
|
|
262
330
|
await client.updateLocalized({
|
|
263
331
|
id: 'page-id',
|
|
264
332
|
locale: 'en',
|
|
265
333
|
data: {
|
|
266
334
|
content: {
|
|
267
|
-
puckData: {
|
|
268
|
-
/* ... */
|
|
269
|
-
},
|
|
335
|
+
puckData: {/* ... */},
|
|
270
336
|
},
|
|
271
337
|
},
|
|
272
|
-
etag: '
|
|
338
|
+
etag: 'oldMeta.oldContent', // Succeeds
|
|
273
339
|
});
|
|
274
340
|
```
|
|
275
341
|
|
|
342
|
+
In the opposite order the metadata write is refused, because the content write already changed the meta hash. A second
|
|
343
|
+
metadata change with `'oldMeta.oldContent'` is refused too. `InMemoryContentAPI` compares the whole ETag, so there any
|
|
344
|
+
outdated ETag is refused.
|
|
345
|
+
|
|
276
346
|
## Best Practices
|
|
277
347
|
|
|
278
348
|
### 1. Choose the Right Testing Approach
|
|
@@ -284,58 +354,64 @@ await client.updateLocalized({
|
|
|
284
354
|
### 2. Handle Stale Writes Gracefully
|
|
285
355
|
|
|
286
356
|
```typescript
|
|
357
|
+
import { ContentAPIClient, StaleWriteError } from '@conloca/content-api-client';
|
|
358
|
+
|
|
359
|
+
const client = new ContentAPIClient();
|
|
360
|
+
const loaded = await client.getLocalized('page-id', 'en');
|
|
361
|
+
if (!loaded) throw new Error('Page not found');
|
|
362
|
+
|
|
363
|
+
const update = { id: 'page-id', locale: 'en', data: { meta: { title: 'New Title' } }, etag: loaded.localized.etag };
|
|
364
|
+
|
|
287
365
|
try {
|
|
288
|
-
await
|
|
366
|
+
await client.updateLocalized(update);
|
|
289
367
|
} catch (error) {
|
|
290
|
-
if (error instanceof StaleWriteError)
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
});
|
|
368
|
+
if (!(error instanceof StaleWriteError)) throw error;
|
|
369
|
+
|
|
370
|
+
// Fetch latest version
|
|
371
|
+
const latest = await client.getLocalized(update.id, update.locale);
|
|
372
|
+
|
|
373
|
+
// Retry with the current etag once you know your change does not undo theirs
|
|
374
|
+
if (latest) {
|
|
375
|
+
await client.updateLocalized({ ...update, etag: latest.localized.etag });
|
|
299
376
|
}
|
|
300
377
|
}
|
|
301
378
|
```
|
|
302
379
|
|
|
303
380
|
### 3. Use React Query Features
|
|
304
381
|
|
|
305
|
-
The
|
|
382
|
+
The hooks are built on `@tanstack/react-query` 5 (`useQuery` and `useMutation`), so they return its usual results:
|
|
383
|
+
`data`, `isLoading`, `error` and `refetch` from a read hook, `mutate`, `mutateAsync` and `isPending` from a write hook.
|
|
306
384
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
- Offline support
|
|
311
|
-
|
|
312
|
-
```typescript
|
|
313
|
-
const { data, isLoading, error, refetch } = useContent(id);
|
|
385
|
+
When a write succeeds, the write hooks put the server's answer into the cache, or refetch the queries the write
|
|
386
|
+
affected, so other components that show the same content update without extra code. They do not update the cache before
|
|
387
|
+
the server answers, and their query keys are not exported. To reload a query yourself, call its `refetch`:
|
|
314
388
|
|
|
315
|
-
|
|
316
|
-
|
|
389
|
+
```tsx
|
|
390
|
+
import { useContent } from '@conloca/content-api-client';
|
|
317
391
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
onMutate: async (newData) => {
|
|
321
|
-
// Cancel outgoing refetches
|
|
322
|
-
await queryClient.cancelQueries(['content', id]);
|
|
392
|
+
export function ContentStatus({ id }: { id: string }) {
|
|
393
|
+
const { data, isLoading, error, refetch } = useContent(id);
|
|
323
394
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
queryClient.setQueryData(['content', id], newData);
|
|
395
|
+
if (isLoading) return <p>Loading…</p>;
|
|
396
|
+
if (error) return <p>{error.message}</p>;
|
|
327
397
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
398
|
+
return (
|
|
399
|
+
<p>
|
|
400
|
+
{data ? `${data.kind} ${data.id}` : 'Not found'}
|
|
401
|
+
{/* Manually refetch */}
|
|
402
|
+
<button type="button" onClick={() => refetch()}>
|
|
403
|
+
Reload
|
|
404
|
+
</button>
|
|
405
|
+
</p>
|
|
406
|
+
);
|
|
407
|
+
}
|
|
335
408
|
```
|
|
336
409
|
|
|
337
410
|
## API Reference
|
|
338
411
|
|
|
412
|
+
The full list of methods and hooks is at
|
|
413
|
+
[conloca.com/packages/content-api-client](https://conloca.com/packages/content-api-client/).
|
|
414
|
+
|
|
339
415
|
### ContentAPIClient
|
|
340
416
|
|
|
341
417
|
Main client class for content operations.
|
|
@@ -343,29 +419,38 @@ Main client class for content operations.
|
|
|
343
419
|
#### Constructor
|
|
344
420
|
|
|
345
421
|
```typescript
|
|
346
|
-
new ContentAPIClient(options?: { baseUrl?: string })
|
|
422
|
+
new ContentAPIClient(options?: { baseUrl?: string; fetch?: typeof fetch })
|
|
347
423
|
```
|
|
348
424
|
|
|
349
|
-
####
|
|
425
|
+
#### Common methods
|
|
350
426
|
|
|
351
427
|
- `getContent(id: string): Promise<ContentEntry | null>`
|
|
352
428
|
- `getLocalized(id: string, locale: string): Promise<LocalizedEntry | null>`
|
|
353
429
|
- `createContent(data: CreateContentInput): Promise<CreateResult>`
|
|
354
430
|
- `updateLocalized(input: UpdateLocaleInput): Promise<UpdateResult>`
|
|
355
431
|
- `deleteContent(id: string, etag: string): Promise<DeleteResult>`
|
|
356
|
-
- `
|
|
432
|
+
- `deleteLocalized(input: { id: string; locale: string; etag: string }): Promise<DeleteResult>`
|
|
433
|
+
- `listAllContent(filters?: GlobalFilters): Promise<ContentListResult>`
|
|
357
434
|
- `batchUpdate(operations: UpdateLocaleInput[]): Promise<BatchResult>`
|
|
358
|
-
- `isPathnameAvailable(site: string, pathname: string, excludeId?: string): Promise<boolean>`
|
|
435
|
+
- `isPathnameAvailable(site: string, pathname: string, excludeId?: string, locale?: string): Promise<boolean>`
|
|
436
|
+
|
|
437
|
+
This package does not export `CreateContentInput`, `UpdateLocaleInput`, `GlobalFilters`, `ContentListResult` or
|
|
438
|
+
`BatchResult`; add `@conloca/content-api` to your dependencies and import them from there, for example
|
|
439
|
+
`import type { CreateContentInput, UpdateLocaleInput } from '@conloca/content-api';`.
|
|
440
|
+
|
|
441
|
+
Most methods throw an `APIClientError` (with `code` and `details`) when the server answers with an API error; an
|
|
442
|
+
outdated ETag throws its subclass `StaleWriteError` (with `currentEtag`).
|
|
359
443
|
|
|
360
444
|
### React Hooks
|
|
361
445
|
|
|
446
|
+
- `setContentAPIClient(client: ContentAPIClient)` / `getContentAPIClient()` - Set or read the client the hooks use
|
|
362
447
|
- `useContent(id: string)` - Fetch content by ID
|
|
363
448
|
- `useLocalizedContent(id: string, locale: string)` - Fetch localized content
|
|
364
449
|
- `useCreateContent()` - Create content mutation
|
|
365
|
-
- `
|
|
366
|
-
- `useDeleteContent()` - Delete content mutation
|
|
450
|
+
- `useUpdateLocalized()` - Update one locale mutation (takes the same input as `updateLocalized`)
|
|
451
|
+
- `useDeleteContent()` - Delete content mutation (takes `{ id, etag, locale? }`; with `locale`, deletes that locale)
|
|
367
452
|
- `useSitePages(site: string, locale?: string)` - List site pages
|
|
368
|
-
- `
|
|
453
|
+
- `useFragments(collection?: string, locale?: string)` - List fragments
|
|
369
454
|
|
|
370
455
|
## Building
|
|
371
456
|
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { AssetEntry, AssetMetadataPatch, AssetUsage, BatchResult, ContentEntry, ContentListResult, ContentManifest, CreateContentInput, CreateResult, DeleteResult, ErrorCode, FolderListing, FolderTreeNode, GitCommitResult, GitPullResult, GitPushResult, GitStatus, GlobalFilters, LocalizedEntry, MDXCompileResponse, MoveResult, SitesConfig, UpdateLocaleInput, UpdateResult } from '@conloca/content-api';
|
|
2
|
+
export interface ContentAPIClientOptions {
|
|
3
|
+
baseUrl?: string;
|
|
4
|
+
fetch?: typeof fetch;
|
|
5
|
+
}
|
|
6
|
+
export declare class ContentAPIClient {
|
|
7
|
+
private baseUrl;
|
|
8
|
+
private fetch;
|
|
9
|
+
constructor(options?: ContentAPIClientOptions);
|
|
10
|
+
private encodeAssetPath;
|
|
11
|
+
private fetchAPI;
|
|
12
|
+
getContent(id: string): Promise<ContentEntry | null>;
|
|
13
|
+
getLocalized(id: string, locale: string): Promise<LocalizedEntry | null>;
|
|
14
|
+
createContent(data: CreateContentInput): Promise<CreateResult>;
|
|
15
|
+
updateLocalized(input: UpdateLocaleInput): Promise<UpdateResult>;
|
|
16
|
+
deleteContent(id: string, etag: string): Promise<DeleteResult>;
|
|
17
|
+
deleteLocalized(input: {
|
|
18
|
+
id: string;
|
|
19
|
+
locale: string;
|
|
20
|
+
etag: string;
|
|
21
|
+
}): Promise<DeleteResult>;
|
|
22
|
+
getSitePages(site: string, locale?: string): Promise<ContentListResult>;
|
|
23
|
+
getPageByPathname(site: string, pathname: string, locale?: string): Promise<ContentManifest | null>;
|
|
24
|
+
isPathnameAvailable(site: string, pathname: string, excludeId?: string, locale?: string): Promise<boolean>;
|
|
25
|
+
movePage(site: string, id: string, pathname: string, locale: string, etag: string): Promise<MoveResult>;
|
|
26
|
+
getFragments(collection?: string, locale?: string): Promise<ContentListResult>;
|
|
27
|
+
getFragmentByName(name: string, collection?: string, locale?: string): Promise<ContentManifest | null>;
|
|
28
|
+
compileMDX(mdxContent: string): Promise<MDXCompileResponse>;
|
|
29
|
+
getData(collection?: string, locale?: string): Promise<ContentListResult>;
|
|
30
|
+
getDataByName(name: string, collection: string, locale?: string): Promise<ContentManifest | null>;
|
|
31
|
+
isDataNameAvailable(name: string, collection: string, excludeId?: string): Promise<boolean>;
|
|
32
|
+
getDataCollections(): Promise<string[]>;
|
|
33
|
+
listAllContent(filters?: GlobalFilters): Promise<ContentListResult>;
|
|
34
|
+
findUntranslatedContent(targetLocale: string, excludeSites?: string[], includeUnpublished?: boolean): Promise<ContentListResult>;
|
|
35
|
+
getSitesConfig(): Promise<SitesConfig>;
|
|
36
|
+
batchUpdate(operations: UpdateLocaleInput[]): Promise<BatchResult>;
|
|
37
|
+
getGitStatus(): Promise<GitStatus>;
|
|
38
|
+
commitChanges(message?: string): Promise<GitCommitResult>;
|
|
39
|
+
pushChanges(): Promise<GitPushResult>;
|
|
40
|
+
pullChanges(): Promise<GitPullResult>;
|
|
41
|
+
listAssets(): Promise<{
|
|
42
|
+
assets: AssetEntry[];
|
|
43
|
+
}>;
|
|
44
|
+
/** An asset by its path from the assets root: `trip/beach.png`, or `beach.png` for one at the top. */
|
|
45
|
+
getAsset(path: string): Promise<AssetEntry | null>;
|
|
46
|
+
uploadAsset(formData: FormData): Promise<AssetEntry>;
|
|
47
|
+
importAssetUrl(url: string, alt?: string, folder?: string): Promise<AssetEntry>;
|
|
48
|
+
/** Delete the asset at a path from the assets root, as `getAsset` takes it. */
|
|
49
|
+
deleteAsset(path: string): Promise<{
|
|
50
|
+
success: boolean;
|
|
51
|
+
}>;
|
|
52
|
+
listFolder(path?: string): Promise<FolderListing>;
|
|
53
|
+
createFolder(path: string): Promise<void>;
|
|
54
|
+
/** Merge alt text and tags onto the asset at a path from the assets root, as `getAsset` takes it. */
|
|
55
|
+
updateAssetMetadata(path: string, updates: AssetMetadataPatch): Promise<AssetEntry>;
|
|
56
|
+
/** The content that links to the asset at a path from the assets root, as `getAsset` takes it. */
|
|
57
|
+
getAssetUsage(path: string): Promise<AssetUsage[]>;
|
|
58
|
+
moveAssets(filenames: string[], sourceFolder: string, targetFolder: string): Promise<{
|
|
59
|
+
success: boolean;
|
|
60
|
+
moved?: number;
|
|
61
|
+
error?: string;
|
|
62
|
+
}>;
|
|
63
|
+
getFolderTree(): Promise<{
|
|
64
|
+
tree: FolderTreeNode[];
|
|
65
|
+
}>;
|
|
66
|
+
getCurrentUser(): Promise<{
|
|
67
|
+
authenticated: boolean;
|
|
68
|
+
user?: {
|
|
69
|
+
email: string;
|
|
70
|
+
sub: string;
|
|
71
|
+
};
|
|
72
|
+
}>;
|
|
73
|
+
}
|
|
74
|
+
export declare class APIClientError extends Error {
|
|
75
|
+
code: ErrorCode;
|
|
76
|
+
details?: any;
|
|
77
|
+
constructor(message: string, code: ErrorCode, details?: any);
|
|
78
|
+
}
|
|
79
|
+
export declare class StaleWriteError extends APIClientError {
|
|
80
|
+
data: {
|
|
81
|
+
code: ErrorCode;
|
|
82
|
+
message: string;
|
|
83
|
+
currentEtag?: string;
|
|
84
|
+
};
|
|
85
|
+
constructor(data: {
|
|
86
|
+
code: ErrorCode;
|
|
87
|
+
message: string;
|
|
88
|
+
currentEtag?: string;
|
|
89
|
+
});
|
|
90
|
+
get currentEtag(): string | undefined;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,UAAU,EACV,kBAAkB,EAClB,UAAU,EACV,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EAClB,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,aAAa,EACb,cAAc,EACd,eAAe,EACf,aAAa,EACb,aAAa,EACb,SAAS,EACT,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,WAAW,EACX,iBAAiB,EACjB,YAAY,EACb,MAAM,sBAAsB,CAAC;AAG9B,MAAM,WAAW,uBAAuB;IACtC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,KAAK,CAAe;IAE5B,YAAY,OAAO,GAAE,uBAA4B,EAWhD;IAED,OAAO,CAAC,eAAe;YAOT,QAAQ;IAgDhB,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAczD;IAEK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