Skip to content

Commit da64779

Browse files
docs: update formatting
1 parent f23d814 commit da64779

File tree

1 file changed

+123
-29
lines changed

1 file changed

+123
-29
lines changed

docs/api/types-query-request.md

Lines changed: 123 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,133 @@ Types are re-exported from the [`kirby-types`](https://github.com/johannschoppli
1313
:::
1414

1515
```ts
16-
type KirbyQueryModel<CustomModel extends string = never> =
17-
| 'collection'
18-
| 'kirby'
19-
| 'site'
20-
| 'page'
21-
| 'user'
22-
| 'file'
23-
| 'content'
24-
| 'item'
25-
| 'arrayItem'
26-
| 'structureItem'
27-
| 'block'
28-
| CustomModel
16+
/**
17+
* Represents all supported model names in Kirby Query Language.
18+
*
19+
* This type includes all built-in Kirby models that can be used as the starting point
20+
* for queries, plus any custom models you define.
21+
*
22+
* Built-in models include:
23+
* - `site` - The site object
24+
* - `page` - A page object
25+
* - `user` - A user object
26+
* - `file` - A file object
27+
* - `collection` - A collection object
28+
* - `kirby` - The Kirby instance
29+
* - `content` - Content field data
30+
* - `item` - Generic item in collections
31+
* - `arrayItem` - An item in an array
32+
* - `structureItem` - An item in a structure field
33+
* - `block` - A block in the blocks field
34+
*
35+
* @example
36+
* ```ts
37+
* // Using built-in models
38+
* const siteModel: KirbyQueryModel = "site";
39+
* const pageModel: KirbyQueryModel = "page";
40+
*
41+
* // Using with custom models
42+
* type CustomModels = "product" | "category";
43+
* const customModel: KirbyQueryModel<CustomModels> = "product";
44+
* ```
45+
*
46+
* @template CustomModel - Additional custom model names to include
47+
*/
48+
type KirbyQueryModel<CustomModel extends string = never>
49+
= | 'collection'
50+
| 'kirby'
51+
| 'site'
52+
| 'page'
53+
| 'user'
54+
| 'file'
55+
| 'content'
56+
| 'item'
57+
| 'arrayItem'
58+
| 'structureItem'
59+
| 'block'
60+
| CustomModel
2961

30-
// For simple dot-based queries like `site.title`, `page.images`, etc.
31-
type DotNotationQuery<M extends string = never> =
32-
`${KirbyQueryModel<M>}.${string}`
62+
/**
63+
* Helper type for dot notation queries (e.g., `model.property.method`).
64+
* @internal
65+
*/
66+
type DotNotationQuery<M extends string = never>
67+
= `${KirbyQueryModel<M>}.${string}`
3368

34-
// For function-based queries like `page("id")`, `user("name")`, etc.
35-
type FunctionNotationQuery<M extends string = never> =
36-
`${KirbyQueryModel<M>}(${string})${string}`
69+
/**
70+
* Helper type for function notation queries (e.g., `model(params)` or `model(params).chain`).
71+
* @internal
72+
*/
73+
type FunctionNotationQuery<M extends string = never>
74+
= | `${KirbyQueryModel<M>}(${string})`
75+
| `${KirbyQueryModel<M>}(${string})${string}`
3776

38-
// Combines the two types above to allow for either dot or function notation
39-
type KirbyQueryChain<M extends string = never> =
40-
| DotNotationQuery<M>
41-
| FunctionNotationQuery<M>
77+
/**
78+
* Represents query chains that extend beyond a simple model name.
79+
*
80+
* This type covers all valid query patterns that start with a model and include
81+
* additional property access or method calls:
82+
*
83+
* - **Dot notation**: `model.property.method()`
84+
* - **Function calls**: `model(params)`
85+
* - **Mixed chains**: `model(params).property.method()`
86+
*
87+
* @example
88+
* ```ts
89+
* // Dot notation queries
90+
* const dotQuery: KirbyQueryChain = "page.children.listed";
91+
* const methodQuery: KirbyQueryChain = "page.children.filterBy('featured', true)";
92+
*
93+
* // Function notation queries
94+
* const funcQuery: KirbyQueryChain = 'site("home")';
95+
* const mixedQuery: KirbyQueryChain = 'page("blog").children.sortBy("date")';
96+
*
97+
* // With custom models
98+
* type CustomModels = "product" | "category";
99+
* const customQuery: KirbyQueryChain<CustomModels> = "product.price";
100+
* ```
101+
*
102+
* @template M - Optional custom model names to include in validation
103+
*/
104+
type KirbyQueryChain<M extends string = never>
105+
= | DotNotationQuery<M>
106+
| FunctionNotationQuery<M>
42107

43-
type KirbyQuery<CustomModel extends string = never> =
44-
| KirbyQueryModel<CustomModel>
45-
// Ensures that it must match the pattern exactly, but not more broadly
46-
| (string extends KirbyQueryChain<CustomModel>
47-
? never
48-
: KirbyQueryChain<CustomModel>)
108+
/**
109+
* Represents any valid Kirby Query Language (KQL) string.
110+
*
111+
* This is the main type for validating KQL queries. It accepts:
112+
* - Simple model names (e.g., `"site"`, `"page"`)
113+
* - Property chains (e.g., `"page.children.listed"`)
114+
* - Method calls (e.g., `'site("home")'`, `'page.filterBy("status", "published")'`)
115+
* - Complex mixed queries (e.g., `'page("blog").children.filterBy("featured", true).sortBy("date")'`)
116+
*
117+
* Invalid queries (unknown models, malformed syntax) will be rejected at the type level.
118+
*
119+
* @example
120+
* ```ts
121+
* // Valid queries
122+
* const simpleQuery: KirbyQuery = "site";
123+
* const propertyQuery: KirbyQuery = "page.children.listed";
124+
* const methodQuery: KirbyQuery = 'page.filterBy("featured", true)';
125+
* const complexQuery: KirbyQuery = 'site("home").children.sortBy("date", "desc").limit(10)';
126+
*
127+
* // Custom models
128+
* type MyModels = "product" | "category";
129+
* const customQuery: KirbyQuery<MyModels> = "product.price";
130+
*
131+
* // Invalid queries (these will cause TypeScript errors)
132+
* // const invalid: KirbyQuery = "unknownModel"; // ❌ Unknown model
133+
* // const invalid: KirbyQuery<MyModels> = "user"; // ❌ Not in custom models
134+
* ```
135+
*
136+
* @template CustomModel - Optional custom model names to include alongside built-in models
137+
*/
138+
type KirbyQuery<CustomModel extends string = never>
139+
= | KirbyQueryModel<CustomModel>
140+
| (string extends KirbyQueryChain<CustomModel>
141+
? never
142+
: KirbyQueryChain<CustomModel>)
49143

50144
interface KirbyQuerySchema {
51145
query: KirbyQuery

0 commit comments

Comments
 (0)