@better-i18n/sdk 0.2.0 → 0.3.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 ADDED
@@ -0,0 +1,128 @@
1
+ # @better-i18n/sdk
2
+
3
+ Content SDK for [Better i18n](https://better-i18n.com). A lightweight, typed client for fetching content models and entries from the headless CMS.
4
+
5
+ ## Features
6
+
7
+ - 📦 **Zero Dependencies** — Runs anywhere with `fetch()`
8
+ - 🔒 **Type-Safe** — Full TypeScript types with generic custom fields
9
+ - 🌍 **Language-Aware** — Fetch localized content by language code
10
+ - 📄 **Pagination Built-in** — Paginated listing with total count and `hasMore`
11
+ - 🔍 **Filtering & Sorting** — Filter by status, sort by date or title
12
+ - ⚡ **Lightweight** — Thin wrapper over REST API
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @better-i18n/sdk
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ```typescript
23
+ import { createClient } from "@better-i18n/sdk";
24
+
25
+ const client = createClient({
26
+ org: "acme",
27
+ project: "web-app",
28
+ apiKey: process.env.BETTER_I18N_API_KEY!,
29
+ });
30
+
31
+ // List content models
32
+ const models = await client.getModels();
33
+
34
+ // List published blog posts
35
+ const { items, total, hasMore } = await client.getEntries("blog-posts", {
36
+ status: "published",
37
+ sort: "publishedAt",
38
+ order: "desc",
39
+ language: "en",
40
+ limit: 10,
41
+ });
42
+
43
+ // Get a single entry with localized content
44
+ const post = await client.getEntry("blog-posts", "hello-world", {
45
+ language: "fr",
46
+ });
47
+ console.log(post.title, post.bodyMarkdown);
48
+ ```
49
+
50
+ ## API
51
+
52
+ | Method | Description |
53
+ | --- | --- |
54
+ | `getModels()` | List all content models with entry counts |
55
+ | `getEntries(modelSlug, options?)` | Paginated list of entries for a model |
56
+ | `getEntry(modelSlug, entrySlug, options?)` | Full content entry with all fields |
57
+
58
+ ### `getEntries` Options
59
+
60
+ | Option | Type | Default | Description |
61
+ | --- | --- | --- | --- |
62
+ | `language` | `string` | source language | Language code for localized content |
63
+ | `status` | `"draft" \| "published" \| "archived"` | all | Filter by entry status |
64
+ | `sort` | `"publishedAt" \| "createdAt" \| "updatedAt" \| "title"` | `"updatedAt"` | Sort field |
65
+ | `order` | `"asc" \| "desc"` | `"desc"` | Sort direction |
66
+ | `page` | `number` | `1` | Page number (1-based) |
67
+ | `limit` | `number` | `50` | Entries per page (1-100) |
68
+
69
+ ### `getEntry` Options
70
+
71
+ | Option | Type | Default | Description |
72
+ | --- | --- | --- | --- |
73
+ | `language` | `string` | source language | Language code for localized content |
74
+
75
+ ### Response Types
76
+
77
+ **`getEntries` returns `PaginatedResponse<ContentEntryListItem>`:**
78
+
79
+ ```typescript
80
+ {
81
+ items: ContentEntryListItem[]; // slug, title, excerpt, publishedAt, tags, author
82
+ total: number; // total matching entries
83
+ hasMore: boolean; // more pages available
84
+ }
85
+ ```
86
+
87
+ **`getEntry` returns `ContentEntry<CF>`:**
88
+
89
+ ```typescript
90
+ {
91
+ id, slug, status, publishedAt, sourceLanguage, availableLanguages,
92
+ featuredImage, tags, author, customFields,
93
+ title, excerpt, body, bodyHtml, bodyMarkdown,
94
+ metaTitle, metaDescription
95
+ }
96
+ ```
97
+
98
+ ## Typed Custom Fields
99
+
100
+ Use the generic type parameter for type-safe custom fields:
101
+
102
+ ```typescript
103
+ interface BlogFields {
104
+ readingTime: string | null;
105
+ category: string | null;
106
+ }
107
+
108
+ const post = await client.getEntry<BlogFields>("blog-posts", "hello-world");
109
+ post.customFields.readingTime; // string | null (typed!)
110
+ post.customFields.category; // string | null (typed!)
111
+ ```
112
+
113
+ ## Configuration
114
+
115
+ | Option | Required | Description |
116
+ | --- | --- | --- |
117
+ | `org` | Yes | Organization slug |
118
+ | `project` | Yes | Project slug |
119
+ | `apiKey` | Yes | API key from [dashboard](https://dash.better-i18n.com) |
120
+ | `apiBase` | No | API base URL (default: `https://api.better-i18n.com`) |
121
+
122
+ ## Documentation
123
+
124
+ Full documentation at [docs.better-i18n.com/sdk](https://docs.better-i18n.com/sdk)
125
+
126
+ ## License
127
+
128
+ MIT © [Better i18n](https://better-i18n.com)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-i18n/sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Content SDK for Better i18n - headless CMS client for fetching content models and entries",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -5,6 +5,7 @@ import type {
5
5
  ContentModel,
6
6
  ListEntriesOptions,
7
7
  GetEntryOptions,
8
+ PaginatedResponse,
8
9
  } from "./types";
9
10
 
10
11
  /**
@@ -42,9 +43,12 @@ export function createContentAPIClient(
42
43
  async getEntries(
43
44
  modelSlug: string,
44
45
  options?: ListEntriesOptions,
45
- ): Promise<ContentEntryListItem[]> {
46
+ ): Promise<PaginatedResponse<ContentEntryListItem>> {
46
47
  const params = new URLSearchParams();
47
48
  if (options?.language) params.set("language", options.language);
49
+ if (options?.status) params.set("status", options.status);
50
+ if (options?.sort) params.set("sort", options.sort);
51
+ if (options?.order) params.set("order", options.order);
48
52
  if (options?.page) params.set("page", String(options.page));
49
53
  if (options?.limit) params.set("limit", String(options.limit));
50
54
  const qs = params.toString() ? `?${params}` : "";
@@ -58,16 +62,19 @@ export function createContentAPIClient(
58
62
  `API error fetching entries for ${modelSlug}: ${res.status}`,
59
63
  );
60
64
  }
61
- const data: { items?: ContentEntryListItem[] } | ContentEntryListItem[] =
62
- await res.json();
63
- return Array.isArray(data) ? data : (data.items ?? []);
65
+ const data = await res.json() as { items: ContentEntryListItem[]; total: number; hasMore: boolean };
66
+ return {
67
+ items: data.items,
68
+ total: data.total,
69
+ hasMore: data.hasMore,
70
+ };
64
71
  },
65
72
 
66
- async getEntry(
73
+ async getEntry<CF extends Record<string, string | null> = Record<string, string | null>>(
67
74
  modelSlug: string,
68
75
  entrySlug: string,
69
76
  options?: GetEntryOptions,
70
- ): Promise<ContentEntry> {
77
+ ): Promise<ContentEntry<CF>> {
71
78
  const params = new URLSearchParams();
72
79
  if (options?.language) params.set("language", options.language);
73
80
  const qs = params.toString() ? `?${params}` : "";
package/src/index.ts CHANGED
@@ -5,7 +5,10 @@ export type {
5
5
  ContentClient,
6
6
  ContentEntry,
7
7
  ContentEntryListItem,
8
+ ContentEntryStatus,
9
+ ContentEntrySortField,
8
10
  ContentModel,
9
11
  ListEntriesOptions,
10
12
  GetEntryOptions,
13
+ PaginatedResponse,
11
14
  } from "./types";
package/src/types.ts CHANGED
@@ -14,18 +14,30 @@ export interface ClientConfig {
14
14
 
15
15
  // ─── Content Types ───────────────────────────────────────────────────
16
16
 
17
- /** A full content entry with all localized fields. */
18
- export interface ContentEntry {
17
+ /**
18
+ * A full content entry with all localized fields.
19
+ *
20
+ * @typeParam CF - Custom fields shape. Defaults to `Record<string, string | null>`.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * // Typed custom fields
25
+ * interface BlogFields { readingTime: string | null; category: string | null }
26
+ * const post = await client.getEntry<BlogFields>("blog", "hello-world");
27
+ * post.customFields.readingTime; // string | null (typed!)
28
+ * ```
29
+ */
30
+ export interface ContentEntry<CF extends Record<string, string | null> = Record<string, string | null>> {
19
31
  id: string;
20
32
  slug: string;
21
- status: string;
33
+ status: "draft" | "published" | "archived";
22
34
  publishedAt: string | null;
23
35
  sourceLanguage: string;
24
36
  availableLanguages: string[];
25
37
  featuredImage: string | null;
26
38
  tags: string[];
27
39
  author: { name: string; image: string | null } | null;
28
- customFields: Record<string, string | null>;
40
+ customFields: CF;
29
41
  // Localized content
30
42
  title: string;
31
43
  excerpt: string | null;
@@ -36,6 +48,9 @@ export interface ContentEntry {
36
48
  metaDescription: string | null;
37
49
  }
38
50
 
51
+ /** Entry status filter values. */
52
+ export type ContentEntryStatus = "draft" | "published" | "archived";
53
+
39
54
  /** A summary item for content entry lists. */
40
55
  export interface ContentEntryListItem {
41
56
  slug: string;
@@ -47,6 +62,13 @@ export interface ContentEntryListItem {
47
62
  author: { name: string; image: string | null } | null;
48
63
  }
49
64
 
65
+ /** Paginated response wrapper. */
66
+ export interface PaginatedResponse<T> {
67
+ items: T[];
68
+ total: number;
69
+ hasMore: boolean;
70
+ }
71
+
50
72
  /** A content model definition. */
51
73
  export interface ContentModel {
52
74
  slug: string;
@@ -58,13 +80,22 @@ export interface ContentModel {
58
80
 
59
81
  // ─── Client Interface ───────────────────────────────────────────────
60
82
 
83
+ /** Sortable fields for content entries. */
84
+ export type ContentEntrySortField = "publishedAt" | "createdAt" | "updatedAt" | "title";
85
+
61
86
  /** Options for listing content entries. */
62
87
  export interface ListEntriesOptions {
63
88
  /** Language code for localized content. Defaults to source language. */
64
89
  language?: string;
90
+ /** Filter by entry status. */
91
+ status?: ContentEntryStatus;
92
+ /** Field to sort by. Defaults to `"updatedAt"`. */
93
+ sort?: ContentEntrySortField;
94
+ /** Sort direction. Defaults to `"desc"`. */
95
+ order?: "asc" | "desc";
65
96
  /** Page number (1-based). */
66
97
  page?: number;
67
- /** Max entries per page. */
98
+ /** Max entries per page (1-100). Defaults to 50. */
68
99
  limit?: number;
69
100
  }
70
101
 
@@ -78,15 +109,15 @@ export interface GetEntryOptions {
78
109
  export interface ContentClient {
79
110
  /** List all content models in the project. */
80
111
  getModels(): Promise<ContentModel[]>;
81
- /** List entries for a content model. */
112
+ /** List entries for a content model with pagination. */
82
113
  getEntries(
83
114
  modelSlug: string,
84
115
  options?: ListEntriesOptions,
85
- ): Promise<ContentEntryListItem[]>;
116
+ ): Promise<PaginatedResponse<ContentEntryListItem>>;
86
117
  /** Fetch a single content entry by slug. */
87
- getEntry(
118
+ getEntry<CF extends Record<string, string | null> = Record<string, string | null>>(
88
119
  modelSlug: string,
89
120
  entrySlug: string,
90
121
  options?: GetEntryOptions,
91
- ): Promise<ContentEntry>;
122
+ ): Promise<ContentEntry<CF>>;
92
123
  }