@foxnose/sdk 0.3.0 → 0.5.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 CHANGED
@@ -48,12 +48,12 @@ const client = new ManagementClient({
48
48
  auth,
49
49
  });
50
50
 
51
- // List folders
52
- const folders = await client.listFolders();
53
- console.log(folders.results);
51
+ // List collections
52
+ const collections = await client.listCollections();
53
+ console.log(collections.results);
54
54
 
55
55
  // Create a resource
56
- const resource = await client.createResource('my-folder-key', {
56
+ const resource = await client.createResource('my-collection-key', {
57
57
  data: { title: 'Hello World' },
58
58
  });
59
59
  console.log(resource.key);
@@ -62,6 +62,62 @@ console.log(resource.key);
62
62
  client.close();
63
63
  ```
64
64
 
65
+ > **Note (0.4.0):** Folder-named methods (`listFolders`, `createFolder`,
66
+ > `addApiFolder`, `listFolderVersions`, `listFolderFields`, etc.) remain as
67
+ > `@deprecated` aliases that emit a one-shot `console.warn` on first use per
68
+ > process. They keep their original wire behaviour (hitting the legacy
69
+ > `/folders/...` URL alias on the server) and will be removed in **1.0**.
70
+ > Prefer the `*Collection*` names in new code.
71
+
72
+ ### Components on Collections
73
+
74
+ Collections can embed Components as nested fields with explicit pin
75
+ semantics (`component`, `component_version`, `auto_update`). The
76
+ `nestedFieldMeta` helper builds the `meta` block with camelCase
77
+ ergonomics, and `syncCollectionComponent` advances pinned fields to a
78
+ target Component version on demand.
79
+
80
+ ```typescript
81
+ import { ManagementClient, JWTAuth, nestedFieldMeta } from '@foxnose/sdk';
82
+
83
+ const client = new ManagementClient({
84
+ config: { baseUrl: 'https://api.foxnose.com' },
85
+ environmentKey: 'prod',
86
+ auth: new JWTAuth('ACCESS_TOKEN'),
87
+ });
88
+
89
+ // Embed a Component as a pinned nested field on a Collection draft.
90
+ await client.createCollectionField('articles', 'v2-draft', {
91
+ key: 'seo',
92
+ name: 'SEO',
93
+ type: 'nested',
94
+ required: true,
95
+ meta: nestedFieldMeta({
96
+ component: 'cmp-seo-metadata',
97
+ componentVersion: 'ver-abc12345',
98
+ autoUpdate: false, // default — pin until explicit sync
99
+ }),
100
+ });
101
+
102
+ // Later, advance every pinned nested field to its Component's
103
+ // current version (empty body = sync all pinned).
104
+ const result = await client.syncCollectionComponent('articles');
105
+ console.log(result.synced_paths, result.schema_version);
106
+
107
+ // Advance specific paths to a chosen Component version.
108
+ await client.syncCollectionComponent('articles', {
109
+ fieldPaths: ['seo'],
110
+ toVersions: { seo: 'ver-def67890' },
111
+ });
112
+ ```
113
+
114
+ `syncCollectionComponent` returns a `SyncComponentResponse` with
115
+ `synced_paths`, `skipped` (per-path reasons), and `schema_version`
116
+ (UID of the newly published Collection schema version, or `null` if no
117
+ field needed advancing). On compatibility conflict the server returns
118
+ 409 `component_sync_conflict`; quota exhaustion returns 422
119
+ `too_many_versions`. Both surface as `FoxnoseAPIError`.
120
+
65
121
  ### Flux Client
66
122
 
67
123
  ```typescript
@@ -97,6 +153,11 @@ console.log(router.routes.length);
97
153
  const schema = await client.getSchema('articles');
98
154
  console.log(schema.searchable_fields);
99
155
 
156
+ // Writes require a write-capable key. Create publishes immediately; `key` is an
157
+ // optional external id used to deduplicate. updateResource is a full replace.
158
+ const created = await client.createResource('articles', { title: 'Hello' }, { key: 'my-id' });
159
+ await client.updateResource('articles', created.resource_key, { title: 'Hello (edited)' });
160
+
100
161
  client.close();
101
162
  ```
102
163
 
@@ -233,7 +294,7 @@ All API errors are thrown as typed exceptions:
233
294
  import { FoxnoseAPIError, FoxnoseTransportError } from '@foxnose/sdk';
234
295
 
235
296
  try {
236
- await client.getResource('folder', 'nonexistent-key');
297
+ await client.getResource('collection', 'nonexistent-key');
237
298
  } catch (err) {
238
299
  if (err instanceof FoxnoseAPIError) {
239
300
  console.error(err.statusCode); // 404
@@ -245,6 +306,79 @@ try {
245
306
  }
246
307
  ```
247
308
 
309
+ ### Billing errors
310
+
311
+ Billing-related responses are thrown as typed subclasses of `FoxnoseAPIError`,
312
+ so an existing `catch (err) { if (err instanceof FoxnoseAPIError) ... }` keeps
313
+ working. Narrow to a subclass to read its typed fields:
314
+
315
+ ```typescript
316
+ import {
317
+ FoxnoseAPIError,
318
+ SpendCapExceededError,
319
+ PlanExhaustedError,
320
+ PlanLimitExceededError,
321
+ RateLimitExceededError,
322
+ } from '@foxnose/sdk';
323
+
324
+ try {
325
+ await client.createResource('collection', payload);
326
+ } catch (err) {
327
+ if (err instanceof SpendCapExceededError) {
328
+ // HTTP 402
329
+ console.error(err.capUsd); // Spend cap in USD (or null)
330
+ console.error(err.cycleResetsAt); // ISO timestamp
331
+ console.error(err.raiseCapUrl); // Where to raise the cap
332
+ } else if (err instanceof PlanExhaustedError) {
333
+ // HTTP 402
334
+ console.error(err.axis); // e.g. "retrievals", "writes"
335
+ console.error(err.windowResetsAt); // ISO timestamp
336
+ console.error(err.upgradeUrl);
337
+ } else if (err instanceof PlanLimitExceededError) {
338
+ // HTTP 403
339
+ console.error(err.entity); // e.g. "collections"
340
+ console.error(err.current, err.limit);
341
+ console.error(err.upgradeUrl); // May be undefined
342
+ } else if (err instanceof RateLimitExceededError) {
343
+ // HTTP 429
344
+ console.error(err.retryAfter); // Seconds to wait (from Retry-After)
345
+ } else if (err instanceof FoxnoseAPIError) {
346
+ console.error(err.statusCode, err.errorCode);
347
+ }
348
+ }
349
+ ```
350
+
351
+ ### Write errors
352
+
353
+ Resource writes (`createResource` / `updateResource`) can throw these typed
354
+ subclasses of `FoxnoseAPIError`:
355
+
356
+ ```typescript
357
+ import {
358
+ CollectionNotWritableError,
359
+ ExternalIdConflictError,
360
+ ContentValidationFailedError,
361
+ UpstreamError,
362
+ } from '@foxnose/sdk';
363
+
364
+ try {
365
+ await client.createResource('articles', { title: 'Hi' }, { key: 'ext-1' });
366
+ } catch (err) {
367
+ if (err instanceof ExternalIdConflictError) {
368
+ // HTTP 409 — a resource with that key already exists
369
+ } else if (err instanceof ContentValidationFailedError) {
370
+ // HTTP 422 — err.errors[] each carry a json_path; err.errorsTruncated
371
+ console.error(err.errors, err.errorsTruncated);
372
+ } else if (err instanceof CollectionNotWritableError) {
373
+ // HTTP 403 — this key cannot write to that collection
374
+ } else if (err instanceof UpstreamError) {
375
+ // HTTP 502 — outcome unknown; re-read with a GET before retrying
376
+ }
377
+ }
378
+ ```
379
+
380
+ Writes are never retried automatically (they are not idempotent).
381
+
248
382
  ## Batch Operations
249
383
 
250
384
  Efficiently upsert multiple resources with concurrency control: