@bettercms-ai/sdk 1.6.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 +126 -0
- package/dist/index.cjs +1616 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1072 -0
- package/dist/index.d.ts +1072 -0
- package/dist/index.js +1507 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @bettercms-ai/sdk
|
|
2
|
+
|
|
3
|
+
Typed JavaScript/TypeScript client for the BetterCMS Delivery API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @bettercms-ai/sdk
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @bettercms-ai/sdk
|
|
11
|
+
# or
|
|
12
|
+
yarn add @bettercms-ai/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { BetterCMS } from "@bettercms-ai/sdk";
|
|
19
|
+
|
|
20
|
+
const client = BetterCMS.site({ workspace: "my-workspace" });
|
|
21
|
+
|
|
22
|
+
// Fetch a single page
|
|
23
|
+
const page = await client.getContent("home");
|
|
24
|
+
console.log(page.entry.title);
|
|
25
|
+
|
|
26
|
+
// List pages (paginated)
|
|
27
|
+
const result = await client.listContent({ page: 1, perPage: 20 });
|
|
28
|
+
console.log(result.items);
|
|
29
|
+
console.log(`Next page? ${result.hasNextPage}`);
|
|
30
|
+
|
|
31
|
+
// Iterate all pages automatically
|
|
32
|
+
for await (const item of client.listContentAll()) {
|
|
33
|
+
console.log(item.entry.title);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Collect with a limit
|
|
37
|
+
const top = await client.listContentAll().all({ limit: 500 });
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## With an API key
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
const client = BetterCMS.site({
|
|
44
|
+
workspace: "my-workspace",
|
|
45
|
+
apiKey: process.env.BETTERCMS_API_KEY,
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## TypeScript
|
|
50
|
+
|
|
51
|
+
This package is written in TypeScript and ships its own type declarations. No separate `@types/` package needed. All types from `@bettercms-ai/types` are re-exported.
|
|
52
|
+
|
|
53
|
+
## Error handling
|
|
54
|
+
|
|
55
|
+
The SDK throws `BetterCMSError` on failures. Each error has a semantic `code`:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { BetterCMSError, ErrorCodes } from "@bettercms-ai/sdk";
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const page = await client.getContent("does-not-exist");
|
|
62
|
+
} catch (err) {
|
|
63
|
+
if (err instanceof BetterCMSError) {
|
|
64
|
+
switch (err.code) {
|
|
65
|
+
case ErrorCodes.CONTENT_NOT_FOUND:
|
|
66
|
+
// 404
|
|
67
|
+
break;
|
|
68
|
+
case ErrorCodes.UNAUTHORIZED:
|
|
69
|
+
// 401
|
|
70
|
+
break;
|
|
71
|
+
case ErrorCodes.FORBIDDEN:
|
|
72
|
+
// 403
|
|
73
|
+
break;
|
|
74
|
+
case ErrorCodes.RATE_LIMITED:
|
|
75
|
+
// 429
|
|
76
|
+
break;
|
|
77
|
+
case ErrorCodes.INTERNAL_ERROR:
|
|
78
|
+
// 5xx
|
|
79
|
+
break;
|
|
80
|
+
case ErrorCodes.NETWORK_ERROR:
|
|
81
|
+
// Network failure
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
### `BetterCMS.site(options)`
|
|
91
|
+
|
|
92
|
+
| Option | Type | Required | Default |
|
|
93
|
+
| --------- | ------ | -------- | -------------------------- |
|
|
94
|
+
| `workspace` | `string` | Yes | — |
|
|
95
|
+
| `apiKey` | `string` | No | — |
|
|
96
|
+
| `baseUrl` | `string` | No | `https://api.bettercms.ai/v1` |
|
|
97
|
+
|
|
98
|
+
Returns a `BetterCMSDeliveryClient` instance.
|
|
99
|
+
|
|
100
|
+
### `client.getContent(slug)`
|
|
101
|
+
|
|
102
|
+
Fetches a single published page by slug.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const content = await client.getContent("about-us");
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `client.listContent(options?)`
|
|
109
|
+
|
|
110
|
+
Returns a paginated result object.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const result = await client.listContent({ page: 1, perPage: 20 });
|
|
114
|
+
// result.items, result.hasNextPage, result.totalPages, ...
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### `client.listContentAll(options?)`
|
|
118
|
+
|
|
119
|
+
Returns an async iterator that transparently follows pagination.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
for await (const item of client.listContentAll()) { ... }
|
|
123
|
+
|
|
124
|
+
// With a limit:
|
|
125
|
+
const items = await client.listContentAll().all({ limit: 100 });
|
|
126
|
+
```
|