kirby-types 0.7.3 → 1.1.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
@@ -1,8 +1,10 @@
1
1
  # kirby-types
2
2
 
3
- [![NPM version](https://img.shields.io/npm/v/kirby-types?color=a1b858&label=)](https://www.npmjs.com/package/kirby-types)
3
+ A collection of TypeScript types for [Kirby CMS](https://getkirby.com), covering:
4
4
 
5
- A collection of TypeScript types to work with [Kirby CMS](https://getkirby.com), mainly in the context of the Kirby Query Language and [headless Kirby usage](https://github.com/johannschopplich/kirby-headless).
5
+ - 🪟 **Panel types** for Kirby Panel plugin development
6
+ - 🔌 **Backend API types** for headless Kirby usage and KQL
7
+ - 🧱 **Block and layout types** for page builder content
6
8
 
7
9
  ## Setup
8
10
 
@@ -17,50 +19,291 @@ npm i -D kirby-types
17
19
  yarn add -D kirby-types
18
20
  ```
19
21
 
22
+ ## Features
23
+
24
+ ### Panel Types
25
+
26
+ - **Panel API**: Complete API client method types.
27
+ - **State Management**: State, Feature, and Modal hierarchies.
28
+ - **Features**: Views, dialogs, drawers, notifications, uploads, and more.
29
+ - **Helpers**: Array, string, object, URL, and other utility types.
30
+ - **Libraries**: Color manipulation, dayjs, and autosize types.
31
+ - **Writer**: ProseMirror-based rich text editor utilities.
32
+
33
+ ### Backend Types
34
+
35
+ - **API Response**: Type-safe API responses.
36
+ - **Blocks**: All 11 default block types with content structures.
37
+ - **Layouts**: Layout and column types with width unions.
38
+ - **KQL**: Query Language request/response types.
39
+ - **Query Parsing**: Parse query strings into structured objects at the type level.
40
+
20
41
  ## Basic Usage
21
42
 
43
+ ### Panel Types
44
+
45
+ Type the global `window.panel` object for Panel plugin development:
46
+
22
47
  ```ts
23
- import type { KirbyQuery } from "kirby-types";
48
+ import type { Panel } from "kirby-types";
24
49
 
25
- // Strictly typed query
26
- const query: KirbyQuery = 'page.children.filterBy("featured", true)';
50
+ // Augment the global Window interface
51
+ declare global {
52
+ interface Window {
53
+ panel: Panel;
54
+ }
55
+ }
27
56
 
28
- // Invalid queries will throw a type error
29
- let invalidQuery: KirbyQuery;
30
- invalidQuery = "unknown"; // Not a valid model
31
- invalidQuery = 'site("'; // Empty parentheses
32
- invalidQuery = 'site("value"'; // Missing closing parenthesis
57
+ // Now window.panel is fully typed
58
+ window.panel.notification.success("Page saved!");
59
+ window.panel.dialog.open("pages/create");
33
60
  ```
34
61
 
35
- ## API
62
+ Common Panel operations with full type safety:
36
63
 
37
- By clicking on a type name, you will be redirected to the corresponding TypeScript definition file.
64
+ ```ts
65
+ // Notifications
66
+ window.panel.notification.success("Changes saved");
67
+ window.panel.notification.error("Something went wrong");
38
68
 
39
- ### API
69
+ // Theme
70
+ window.panel.theme.set("dark");
71
+ const currentTheme = window.panel.theme.current; // "light" | "dark"
40
72
 
41
- - [`KirbyApiResponse`](./src/api.d.ts) - Matches the response of a [Kirby API request](https://getkirby.com/docs/reference/api).
73
+ // Navigation
74
+ await window.panel.view.open("/pages/blog");
75
+ await window.panel.dialog.open("/dialogs/pages/create");
42
76
 
43
- ### Query
77
+ // API calls
78
+ const page = await window.panel.api.pages.read("blog");
79
+ await window.panel.api.pages.update("blog", { title: "New Title" });
80
+
81
+ // Content state
82
+ const hasChanges = window.panel.content.hasChanges;
83
+ await window.panel.content.save();
84
+
85
+ // User info
86
+ const user = window.panel.user;
87
+ console.log(user.email, user.role);
88
+ ```
89
+
90
+ ### Writer Extensions
91
+
92
+ For ProseMirror-based Writer extensions (requires optional ProseMirror peer dependencies):
93
+
94
+ ```ts
95
+ import type { WriterMarkContext } from "kirby-types";
96
+
97
+ // In a Writer mark extension
98
+ class Bold {
99
+ commands({ type, utils }: WriterMarkContext) {
100
+ return () => utils.toggleMark(type);
101
+ }
44
102
 
45
- - [`KirbyQueryModel`](./src/query.d.ts) - Matches any supported KirbyQL model.
46
- - [`KirbyQuery`](./src/query.d.ts) - Matches a KirbyQL [`query`](https://getkirby.com/docs/guide/blueprints/query-language).
103
+ inputRules({ type, utils }: WriterMarkContext) {
104
+ return [utils.markInputRule(/\*\*([^*]+)\*\*$/, type)];
105
+ }
106
+ }
107
+ ```
108
+
109
+ ### API Responses
110
+
111
+ ```ts
112
+ import type { KirbyApiResponse } from "kirby-types";
113
+
114
+ interface PageData {
115
+ id: string;
116
+ title: string;
117
+ url: string;
118
+ }
119
+
120
+ const response: KirbyApiResponse<PageData> = {
121
+ code: 200,
122
+ status: "ok",
123
+ result: {
124
+ id: "home",
125
+ title: "Home",
126
+ url: "/",
127
+ },
128
+ };
129
+ ```
47
130
 
48
131
  ### Blocks
49
132
 
50
- - [`KirbyBlock`](./src/blocks.d.ts) - Matches a [Kirby block](https://getkirby.com/docs/guide/page-builder).
51
- - [`KirbyDefaultBlockType`](./src/blocks.d.ts) - Matches any [Kirby default block type](https://getkirby.com/docs/reference/panel/blocks).
52
- - [`KirbyDefaultBlocks`](./src/blocks.d.ts) - Maps each of [Kirby's default block type](https://getkirby.com/docs/reference/panel/blocks) to its corresponding block content.
133
+ ```ts
134
+ import type { KirbyBlock, KirbyDefaultBlockType } from "kirby-types";
135
+
136
+ // Using a default block type
137
+ const textBlock: KirbyBlock<"text"> = {
138
+ id: "abc123",
139
+ type: "text",
140
+ isHidden: false,
141
+ content: { text: "Hello world" },
142
+ };
53
143
 
54
- ### Layout
144
+ // Using a custom block type
145
+ interface HeroContent {
146
+ title: string;
147
+ image: string;
148
+ cta: string;
149
+ }
55
150
 
56
- - [`KirbyLayout`](./src/layout.d.ts) - Matches a [Kirby layout](https://getkirby.com/docs/reference/panel/fields/layout).
57
- - [`KirbyLayoutColumn`](./src/layout.d.ts) - Matches any [supported layout width](https://getkirby.com/docs/reference/panel/fields/layout#defining-your-own-layouts__available-widths).
151
+ const heroBlock: KirbyBlock<"hero", HeroContent> = {
152
+ id: "def456",
153
+ type: "hero",
154
+ isHidden: false,
155
+ content: {
156
+ title: "Welcome",
157
+ image: "hero.jpg",
158
+ cta: "Learn more",
159
+ },
160
+ };
161
+ ```
162
+
163
+ ### Layouts
164
+
165
+ ```ts
166
+ import type { KirbyLayout, KirbyLayoutColumn } from "kirby-types";
167
+
168
+ const layout: KirbyLayout = {
169
+ id: "layout-xyz789",
170
+ attrs: { class: "highlight" },
171
+ columns: [
172
+ { id: "col-1", width: "1/3", blocks: [] },
173
+ { id: "col-2", width: "2/3", blocks: [] },
174
+ ],
175
+ };
176
+ ```
177
+
178
+ ### KQL Queries
179
+
180
+ ```ts
181
+ import type {
182
+ KirbyQuery,
183
+ KirbyQueryRequest,
184
+ KirbyQueryResponse,
185
+ } from "kirby-types";
186
+
187
+ // KQL request with pagination
188
+ const request: KirbyQueryRequest = {
189
+ query: 'page("blog").children.listed',
190
+ select: {
191
+ title: "page.title",
192
+ date: "page.date.toDate",
193
+ },
194
+ pagination: { limit: 10 },
195
+ };
196
+
197
+ // Typed response
198
+ interface BlogPost {
199
+ title: string;
200
+ date: string;
201
+ }
202
+
203
+ const response: KirbyQueryResponse<BlogPost[], true> = {
204
+ code: 200,
205
+ status: "ok",
206
+ result: {
207
+ data: [{ title: "Post 1", date: "2024-01-01" }],
208
+ pagination: { page: 1, pages: 5, offset: 0, limit: 10, total: 50 },
209
+ },
210
+ };
211
+ ```
212
+
213
+ ### Query Parsing
214
+
215
+ Parse query strings into structured objects at the type level:
216
+
217
+ ```ts
218
+ import type { ParseKirbyQuery } from "kirby-types";
219
+
220
+ type Parsed = ParseKirbyQuery<'page.children.filterBy("status", "published")'>;
221
+ // Result: {
222
+ // model: "page";
223
+ // chain: [
224
+ // { type: "property"; name: "children" },
225
+ // { type: "method"; name: "filterBy"; params: '"status", "published"' }
226
+ // ]
227
+ // }
228
+ ```
229
+
230
+ ## API Reference
231
+
232
+ ### Panel
233
+
234
+ | Type | Description |
235
+ | -------------------------------------------- | ----------------------------------- |
236
+ | [`Panel`](./src/panel/index.d.ts) | Main Panel interface |
237
+ | [`PanelApi`](./src/panel/api.d.ts) | API client methods |
238
+ | [`PanelState`](./src/panel/base.d.ts) | Base state interface |
239
+ | [`PanelFeature`](./src/panel/base.d.ts) | Feature with loading states |
240
+ | [`PanelModal`](./src/panel/base.d.ts) | Modal (dialog/drawer) interface |
241
+ | [`PanelHelpers`](./src/panel/helpers.d.ts) | Utility functions |
242
+ | [`PanelLibrary`](./src/panel/libraries.d.ts) | Libraries (colors, dayjs, autosize) |
243
+
244
+ ### Writer
245
+
246
+ | Type | Description |
247
+ | --------------------------------------------------- | ---------------------------------- |
248
+ | [`WriterUtils`](./src/panel/writer.d.ts) | ProseMirror commands and utilities |
249
+ | [`WriterMarkContext`](./src/panel/writer.d.ts) | Context for mark extensions |
250
+ | [`WriterNodeContext`](./src/panel/writer.d.ts) | Context for node extensions |
251
+ | [`WriterExtensionContext`](./src/panel/writer.d.ts) | Context for generic extensions |
252
+
253
+ ### API
254
+
255
+ | Type | Description |
256
+ | --------------------------------------- | ------------------------------------- |
257
+ | [`KirbyApiResponse<T>`](./src/api.d.ts) | Standard Kirby API response structure |
258
+
259
+ ### Blocks
260
+
261
+ | Type | Description |
262
+ | -------------------------------------------- | --------------------------------- |
263
+ | [`KirbyBlock<T, U>`](./src/blocks.d.ts) | Block with type and content |
264
+ | [`KirbyCodeLanguage`](./src/blocks.d.ts) | Valid code block languages |
265
+ | [`KirbyDefaultBlocks`](./src/blocks.d.ts) | Default block content types |
266
+ | [`KirbyDefaultBlockType`](./src/blocks.d.ts) | Union of default block type names |
267
+
268
+ ### Layouts
269
+
270
+ | Type | Description |
271
+ | --------------------------------------------- | ---------------------------- |
272
+ | [`KirbyLayout`](./src/layout.d.ts) | Layout row with columns |
273
+ | [`KirbyLayoutColumn`](./src/layout.d.ts) | Column with width and blocks |
274
+ | [`KirbyLayoutColumnWidth`](./src/layout.d.ts) | Valid column width fractions |
58
275
 
59
276
  ### KQL
60
277
 
61
- - [`KirbyQuerySchema`](./src/kql.d.ts) - Matches a [KQL query schema](https://github.com/getkirby/kql).
62
- - [`KirbyQueryRequest`](./src/kql.d.ts) - Matches any [KQL request](https://github.com/getkirby/kql).
63
- - [`KirbyQueryResponse`](./src/kql.d.ts) - Matches any [KQL response](https://github.com/getkirby/kql).
278
+ | Type | Description |
279
+ | -------------------------------------------- | ------------------------------------- |
280
+ | [`KirbyQuerySchema`](./src/kql.d.ts) | KQL query schema structure |
281
+ | [`KirbyQueryRequest`](./src/kql.d.ts) | KQL request with pagination |
282
+ | [`KirbyQueryResponse<T, P>`](./src/kql.d.ts) | KQL response with optional pagination |
283
+
284
+ ### Query
285
+
286
+ | Type | Description |
287
+ | ---------------------------------------- | ------------------------------------- |
288
+ | [`KirbyQuery<M>`](./src/query.d.ts) | Valid KQL query string |
289
+ | [`KirbyQueryModel<M>`](./src/query.d.ts) | Supported query models |
290
+ | [`KirbyQueryChain<M>`](./src/query.d.ts) | Query chain patterns |
291
+ | [`ParseKirbyQuery<T>`](./src/query.d.ts) | Parse query string to structured type |
292
+
293
+ ## Optional Dependencies
294
+
295
+ The Panel types include Writer types that require ProseMirror packages. These are optional peer dependencies:
296
+
297
+ ```bash
298
+ # Only needed if using Writer extension types
299
+ pnpm add -D prosemirror-commands prosemirror-inputrules prosemirror-model prosemirror-schema-list prosemirror-state
300
+ ```
301
+
302
+ Vue is also an optional peer dependency for Panel types:
303
+
304
+ ```bash
305
+ pnpm add -D vue@^2.7.0
306
+ ```
64
307
 
65
308
  ## License
66
309
 
package/index.d.ts CHANGED
@@ -1,13 +1,35 @@
1
+ // API types
1
2
  export type { KirbyApiResponse } from "./src/api";
3
+
4
+ // Block types
2
5
  export type {
3
6
  KirbyBlock,
7
+ KirbyCodeLanguage,
4
8
  KirbyDefaultBlocks,
5
9
  KirbyDefaultBlockType,
6
10
  } from "./src/blocks";
11
+
12
+ // KQL types
7
13
  export type {
8
14
  KirbyQueryRequest,
9
15
  KirbyQueryResponse,
10
16
  KirbyQuerySchema,
11
17
  } from "./src/kql";
12
- export type { KirbyLayout, KirbyLayoutColumn } from "./src/layout";
13
- export type { KirbyQuery, KirbyQueryChain, KirbyQueryModel } from "./src/query";
18
+
19
+ // Layout types
20
+ export type {
21
+ KirbyLayout,
22
+ KirbyLayoutColumn,
23
+ KirbyLayoutColumnWidth,
24
+ } from "./src/layout";
25
+
26
+ // Panel types
27
+ export * from "./src/panel/index";
28
+
29
+ // Query types
30
+ export type {
31
+ KirbyQuery,
32
+ KirbyQueryChain,
33
+ KirbyQueryModel,
34
+ ParseKirbyQuery,
35
+ } from "./src/query";
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "kirby-types",
3
3
  "type": "module",
4
- "version": "0.7.3",
5
- "packageManager": "pnpm@9.11.0",
4
+ "version": "1.1.0",
5
+ "packageManager": "pnpm@10.27.0",
6
6
  "description": "A collection of TypeScript types for the Kirby CMS",
7
7
  "author": "Johann Schopplich <hello@johannschopplich.com>",
8
8
  "license": "MIT",
@@ -16,6 +16,7 @@
16
16
  "getkirby",
17
17
  "kirby-cms",
18
18
  "kirby",
19
+ "kirby-panel",
19
20
  "kql",
20
21
  "query-language",
21
22
  "query",
@@ -25,7 +26,7 @@
25
26
  "exports": {
26
27
  ".": {
27
28
  "types": "./index.d.ts",
28
- "import": "./index.mjs"
29
+ "default": "./index.js"
29
30
  }
30
31
  },
31
32
  "types": "./index.d.ts",
@@ -41,12 +42,40 @@
41
42
  "release": "bumpp --commit --push --tag",
42
43
  "test": "tsd -f \"test/**/*.test-d.ts\""
43
44
  },
45
+ "peerDependencies": {
46
+ "prosemirror-commands": "^1.0.0",
47
+ "prosemirror-inputrules": "^1.0.0",
48
+ "prosemirror-model": "^1.0.0",
49
+ "prosemirror-schema-list": "^1.0.0",
50
+ "prosemirror-state": "^1.0.0",
51
+ "vue": "^2.7.0"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "prosemirror-commands": {
55
+ "optional": true
56
+ },
57
+ "prosemirror-inputrules": {
58
+ "optional": true
59
+ },
60
+ "prosemirror-model": {
61
+ "optional": true
62
+ },
63
+ "prosemirror-schema-list": {
64
+ "optional": true
65
+ },
66
+ "prosemirror-state": {
67
+ "optional": true
68
+ },
69
+ "vue": {
70
+ "optional": true
71
+ }
72
+ },
44
73
  "devDependencies": {
45
- "@antfu/eslint-config": "^3.7.1",
46
- "bumpp": "^9.5.2",
47
- "eslint": "^9.10.0",
48
- "prettier": "^3.3.3",
49
- "tsd": "^0.31.2",
50
- "typescript": "^5.5.4"
74
+ "@antfu/eslint-config": "^6.7.3",
75
+ "bumpp": "^10.3.2",
76
+ "eslint": "^9.39.2",
77
+ "prettier": "^3.7.4",
78
+ "tsd": "^0.33.0",
79
+ "typescript": "^5.9.3"
51
80
  }
52
81
  }
package/src/api.d.ts CHANGED
@@ -1,5 +1,45 @@
1
+ /**
2
+ * Represents the standard response structure from the Kirby API.
3
+ *
4
+ * @typeParam T - The type of the result data. Defaults to `any`.
5
+ *
6
+ * @see https://getkirby.com/docs/reference/api
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // Typed API response for a page
11
+ * interface PageData {
12
+ * id: string;
13
+ * title: string;
14
+ * url: string;
15
+ * }
16
+ *
17
+ * const response: KirbyApiResponse<PageData> = {
18
+ * code: 200,
19
+ * status: "ok",
20
+ * result: {
21
+ * id: "home",
22
+ * title: "Home",
23
+ * url: "/"
24
+ * }
25
+ * };
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * // Error response
31
+ * const errorResponse: KirbyApiResponse = {
32
+ * code: 404,
33
+ * status: "error"
34
+ * // result is undefined for errors
35
+ * };
36
+ * ```
37
+ */
1
38
  export interface KirbyApiResponse<T = any> {
39
+ /** HTTP status code of the response. */
2
40
  code: number;
41
+ /** Status string, typically `"ok"` for success or `"error"` for failures. */
3
42
  status: string;
43
+ /** The response data. Only present for successful responses. */
4
44
  result?: T;
5
45
  }