@type-crafter/mcp 0.6.0 → 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 +203 -221
- package/dist/docs/RULES_COMPOSITION.md +343 -0
- package/dist/docs/RULES_NULLABLE.md +293 -0
- package/dist/docs/RULES_PATTERNS.md +516 -0
- package/dist/docs/RULES_REFERENCES.md +302 -0
- package/dist/docs/RULES_STRUCTURE.md +248 -0
- package/dist/docs/RULES_TYPES.md +291 -0
- package/dist/docs/WRITING_GUIDE.md +179 -0
- package/dist/index.js +321 -345
- package/package.json +3 -5
- package/src/GUIDE.md +0 -459
- package/src/SPEC_RULES.md +0 -1755
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# Types Rules
|
|
2
|
+
|
|
3
|
+
## Primitive Types
|
|
4
|
+
|
|
5
|
+
| YAML Type | TypeScript | Notes |
|
|
6
|
+
| ------------------------- | ---------- | ------------------- |
|
|
7
|
+
| `string` | `string` | Basic string |
|
|
8
|
+
| `string` + `format: date` | `Date` | Date object |
|
|
9
|
+
| `number` | `number` | Floating point |
|
|
10
|
+
| `integer` | `number` | Also becomes number |
|
|
11
|
+
| `boolean` | `boolean` | True/false |
|
|
12
|
+
| `unknown` | `unknown` | Dynamic/any type |
|
|
13
|
+
|
|
14
|
+
```yaml
|
|
15
|
+
properties:
|
|
16
|
+
name: { type: string }
|
|
17
|
+
age: { type: number }
|
|
18
|
+
isActive: { type: boolean }
|
|
19
|
+
birthDate: { type: string, format: date }
|
|
20
|
+
metadata: { type: unknown }
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Object Types
|
|
26
|
+
|
|
27
|
+
```yaml
|
|
28
|
+
User:
|
|
29
|
+
type: object
|
|
30
|
+
description: 'User account' # Optional - becomes JSDoc
|
|
31
|
+
example: "{ id: '123' }" # Optional - becomes JSDoc
|
|
32
|
+
required:
|
|
33
|
+
- id
|
|
34
|
+
- email
|
|
35
|
+
properties:
|
|
36
|
+
id:
|
|
37
|
+
type: string
|
|
38
|
+
description: 'Unique ID' # Optional
|
|
39
|
+
example: 'user-123' # Optional
|
|
40
|
+
email:
|
|
41
|
+
type: string
|
|
42
|
+
name:
|
|
43
|
+
type: string # Not in required = nullable
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**TypeScript:**
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
/**
|
|
50
|
+
* @description User account
|
|
51
|
+
*/
|
|
52
|
+
export type User = {
|
|
53
|
+
/** @description Unique ID */
|
|
54
|
+
id: string;
|
|
55
|
+
email: string;
|
|
56
|
+
name: string | null;
|
|
57
|
+
};
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Enum Types
|
|
63
|
+
|
|
64
|
+
### String Enum (Top-Level)
|
|
65
|
+
|
|
66
|
+
```yaml
|
|
67
|
+
Status:
|
|
68
|
+
type: string
|
|
69
|
+
enum:
|
|
70
|
+
- active
|
|
71
|
+
- inactive
|
|
72
|
+
- pending
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**TypeScript:** `export type Status = 'active' | 'inactive' | 'pending';`
|
|
76
|
+
|
|
77
|
+
### Number Enum (Top-Level)
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
Priority:
|
|
81
|
+
type: number
|
|
82
|
+
enum:
|
|
83
|
+
- 1
|
|
84
|
+
- 2
|
|
85
|
+
- 3
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**TypeScript:** `export type Priority = 1 | 2 | 3;`
|
|
89
|
+
|
|
90
|
+
### Inline Enum (Property)
|
|
91
|
+
|
|
92
|
+
```yaml
|
|
93
|
+
User:
|
|
94
|
+
type: object
|
|
95
|
+
properties:
|
|
96
|
+
role:
|
|
97
|
+
type: string
|
|
98
|
+
enum:
|
|
99
|
+
- admin
|
|
100
|
+
- user
|
|
101
|
+
- guest
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**TypeScript:**
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
export type User = {
|
|
108
|
+
role: ('admin' | 'user' | 'guest') | null;
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Array Types
|
|
115
|
+
|
|
116
|
+
### CRITICAL: Arrays Cannot Be Top-Level Types
|
|
117
|
+
|
|
118
|
+
```yaml
|
|
119
|
+
# WRONG - This will NOT work
|
|
120
|
+
Tags:
|
|
121
|
+
type: array
|
|
122
|
+
items:
|
|
123
|
+
type: string
|
|
124
|
+
|
|
125
|
+
# CORRECT - Arrays must be properties
|
|
126
|
+
Post:
|
|
127
|
+
type: object
|
|
128
|
+
properties:
|
|
129
|
+
tags:
|
|
130
|
+
type: array
|
|
131
|
+
items:
|
|
132
|
+
type: string
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Array of Primitives
|
|
136
|
+
|
|
137
|
+
```yaml
|
|
138
|
+
properties:
|
|
139
|
+
tags:
|
|
140
|
+
type: array
|
|
141
|
+
items:
|
|
142
|
+
type: string
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**TypeScript:** `tags: string[] | null`
|
|
146
|
+
|
|
147
|
+
### Array of Objects
|
|
148
|
+
|
|
149
|
+
```yaml
|
|
150
|
+
properties:
|
|
151
|
+
comments:
|
|
152
|
+
type: array
|
|
153
|
+
items:
|
|
154
|
+
type: object
|
|
155
|
+
required: [text]
|
|
156
|
+
properties:
|
|
157
|
+
text: { type: string }
|
|
158
|
+
author: { type: string }
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**TypeScript:** `comments: { text: string; author: string | null }[] | null`
|
|
162
|
+
|
|
163
|
+
### Array of References
|
|
164
|
+
|
|
165
|
+
```yaml
|
|
166
|
+
properties:
|
|
167
|
+
posts:
|
|
168
|
+
type: array
|
|
169
|
+
items:
|
|
170
|
+
$ref: '#/types/Post'
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**TypeScript:** `posts: Post[] | null`
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## Nested Objects
|
|
178
|
+
|
|
179
|
+
```yaml
|
|
180
|
+
Company:
|
|
181
|
+
type: object
|
|
182
|
+
required:
|
|
183
|
+
- id
|
|
184
|
+
- address
|
|
185
|
+
properties:
|
|
186
|
+
id:
|
|
187
|
+
type: string
|
|
188
|
+
address:
|
|
189
|
+
type: object
|
|
190
|
+
required:
|
|
191
|
+
- street
|
|
192
|
+
properties:
|
|
193
|
+
street:
|
|
194
|
+
type: string
|
|
195
|
+
city:
|
|
196
|
+
type: string
|
|
197
|
+
zip:
|
|
198
|
+
type: string
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**TypeScript:**
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
export type Company = {
|
|
205
|
+
id: string;
|
|
206
|
+
address: {
|
|
207
|
+
street: string;
|
|
208
|
+
city: string | null;
|
|
209
|
+
zip: string | null;
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## Additional Properties (Hashmaps/Dictionaries)
|
|
217
|
+
|
|
218
|
+
### Simple Hashmap (any values)
|
|
219
|
+
|
|
220
|
+
```yaml
|
|
221
|
+
Metadata:
|
|
222
|
+
type: object
|
|
223
|
+
additionalProperties: true
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
**TypeScript:** `{ [keys: string]: unknown }`
|
|
227
|
+
|
|
228
|
+
### Typed Hashmap
|
|
229
|
+
|
|
230
|
+
```yaml
|
|
231
|
+
StringMap:
|
|
232
|
+
type: object
|
|
233
|
+
additionalProperties:
|
|
234
|
+
keyType: string
|
|
235
|
+
valueType:
|
|
236
|
+
type: string
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
**TypeScript:** `{ [keys: string]: string }`
|
|
240
|
+
|
|
241
|
+
### Number Keys
|
|
242
|
+
|
|
243
|
+
```yaml
|
|
244
|
+
IdMap:
|
|
245
|
+
type: object
|
|
246
|
+
additionalProperties:
|
|
247
|
+
keyType: number
|
|
248
|
+
valueType:
|
|
249
|
+
$ref: '#/types/User'
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
**TypeScript:** `{ [keys: number]: User }`
|
|
253
|
+
|
|
254
|
+
### Mixed: Properties + Additional
|
|
255
|
+
|
|
256
|
+
```yaml
|
|
257
|
+
UserWithMeta:
|
|
258
|
+
type: object
|
|
259
|
+
required:
|
|
260
|
+
- id
|
|
261
|
+
properties:
|
|
262
|
+
id:
|
|
263
|
+
type: string
|
|
264
|
+
additionalProperties:
|
|
265
|
+
keyType: string
|
|
266
|
+
valueType:
|
|
267
|
+
type: string
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
**TypeScript:**
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
export type UserWithMeta = {
|
|
274
|
+
id: string;
|
|
275
|
+
[keys: string]: string;
|
|
276
|
+
};
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## Type Definition Summary
|
|
282
|
+
|
|
283
|
+
| Type | Structure | Top-Level? |
|
|
284
|
+
| ------------ | ------------------------------ | ---------- |
|
|
285
|
+
| Object | `type: object` + `properties` | Yes |
|
|
286
|
+
| Enum | `type: string/number` + `enum` | Yes |
|
|
287
|
+
| Primitive | `type: string/number/boolean` | Yes |
|
|
288
|
+
| Array | `type: array` + `items` | **NO** |
|
|
289
|
+
| Union | `oneOf: [...]` | Yes |
|
|
290
|
+
| Intersection | `allOf: [...]` | Yes |
|
|
291
|
+
| Reference | `$ref: '...'` | Yes |
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# Type Crafter YAML Specification Guide
|
|
2
|
+
|
|
3
|
+
Type Crafter generates TypeScript types from YAML specifications. This guide teaches you how to write valid specs.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Minimal Valid Spec
|
|
8
|
+
|
|
9
|
+
```yaml
|
|
10
|
+
info:
|
|
11
|
+
version: '1.0.0'
|
|
12
|
+
title: 'My API Types'
|
|
13
|
+
|
|
14
|
+
types:
|
|
15
|
+
User:
|
|
16
|
+
type: object
|
|
17
|
+
required:
|
|
18
|
+
- id
|
|
19
|
+
- email
|
|
20
|
+
properties:
|
|
21
|
+
id:
|
|
22
|
+
type: string
|
|
23
|
+
email:
|
|
24
|
+
type: string
|
|
25
|
+
name:
|
|
26
|
+
type: string # Not in required = nullable (string | null)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Generated TypeScript:**
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
export type User = {
|
|
33
|
+
id: string;
|
|
34
|
+
email: string;
|
|
35
|
+
name: string | null;
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Common Mistakes - DO NOT DO THESE
|
|
42
|
+
|
|
43
|
+
### 1. Using `nullable: true` - DOES NOT EXIST
|
|
44
|
+
|
|
45
|
+
```yaml
|
|
46
|
+
# WRONG
|
|
47
|
+
name:
|
|
48
|
+
type: string
|
|
49
|
+
nullable: true # THIS PROPERTY DOES NOT EXIST
|
|
50
|
+
|
|
51
|
+
# CORRECT - Omit from required array
|
|
52
|
+
required:
|
|
53
|
+
- id # id is required
|
|
54
|
+
# name is NOT here, so it becomes nullable
|
|
55
|
+
properties:
|
|
56
|
+
id: { type: string }
|
|
57
|
+
name: { type: string } # Generates: string | null
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. Using `optional: true` - DOES NOT EXIST
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
# WRONG
|
|
64
|
+
name:
|
|
65
|
+
type: string
|
|
66
|
+
optional: true # THIS PROPERTY DOES NOT EXIST
|
|
67
|
+
|
|
68
|
+
# CORRECT - Use required array
|
|
69
|
+
required: [id] # Only id is required
|
|
70
|
+
properties:
|
|
71
|
+
id: { type: string }
|
|
72
|
+
name: { type: string } # Not in required = nullable
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Using `?` suffix - NOT SUPPORTED
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
# WRONG
|
|
79
|
+
properties:
|
|
80
|
+
name?: # INVALID SYNTAX
|
|
81
|
+
type: string
|
|
82
|
+
|
|
83
|
+
# CORRECT
|
|
84
|
+
required: [id]
|
|
85
|
+
properties:
|
|
86
|
+
name: { type: string }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 4. Top-level array types - NOT ALLOWED
|
|
90
|
+
|
|
91
|
+
```yaml
|
|
92
|
+
# WRONG - Arrays cannot be top-level types
|
|
93
|
+
Tags:
|
|
94
|
+
type: array
|
|
95
|
+
items:
|
|
96
|
+
type: string
|
|
97
|
+
|
|
98
|
+
# CORRECT - Arrays must be properties within objects
|
|
99
|
+
Post:
|
|
100
|
+
type: object
|
|
101
|
+
properties:
|
|
102
|
+
tags:
|
|
103
|
+
type: array
|
|
104
|
+
items:
|
|
105
|
+
type: string
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 5. Using `../` in paths - WRONG
|
|
109
|
+
|
|
110
|
+
```yaml
|
|
111
|
+
# WRONG - Relative paths from current file
|
|
112
|
+
$ref: '../common/types.yaml#/User'
|
|
113
|
+
|
|
114
|
+
# CORRECT - Paths from project root (where CLI runs)
|
|
115
|
+
$ref: './src/common/types.yaml#/User'
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 6. Using `#/` references in non-top files
|
|
119
|
+
|
|
120
|
+
```yaml
|
|
121
|
+
# In a file WITHOUT info section (non-top file)
|
|
122
|
+
# WRONG
|
|
123
|
+
$ref: '#/Cart/CartItem'
|
|
124
|
+
|
|
125
|
+
# CORRECT - Must use full path
|
|
126
|
+
$ref: './docs/cart.yaml#/Cart/CartItem'
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Quick Reference
|
|
132
|
+
|
|
133
|
+
| Concept | YAML | TypeScript |
|
|
134
|
+
| ----------------- | ---------------------------- | -------------------- |
|
|
135
|
+
| Required property | In `required` array | `prop: Type` |
|
|
136
|
+
| Nullable property | NOT in `required` | `prop: Type \| null` |
|
|
137
|
+
| String | `type: string` | `string` |
|
|
138
|
+
| Number | `type: number` | `number` |
|
|
139
|
+
| Boolean | `type: boolean` | `boolean` |
|
|
140
|
+
| Date | `type: string, format: date` | `Date` |
|
|
141
|
+
| Array | `type: array, items: {...}` | `Type[]` |
|
|
142
|
+
| Enum | `type: string, enum: [...]` | `'a' \| 'b'` |
|
|
143
|
+
| Union | `oneOf: [...]` | `A \| B` |
|
|
144
|
+
| Intersection | `allOf: [...]` | `A & B` |
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Available Detail Sections
|
|
149
|
+
|
|
150
|
+
Call `get-rules-section` with these topics for deep dives:
|
|
151
|
+
|
|
152
|
+
| Section | What You'll Learn |
|
|
153
|
+
| ------------- | --------------------------------------------------- |
|
|
154
|
+
| `structure` | Root structure, info section, types vs groupedTypes |
|
|
155
|
+
| `types` | Objects, enums, primitives, arrays, nested objects |
|
|
156
|
+
| `nullable` | How `required` array controls nullability |
|
|
157
|
+
| `references` | $ref syntax, top-file vs non-top-file rules |
|
|
158
|
+
| `composition` | oneOf (unions), allOf (intersections) |
|
|
159
|
+
| `patterns` | Common patterns with full examples |
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Workflow
|
|
164
|
+
|
|
165
|
+
1. Read this guide (you just did)
|
|
166
|
+
2. Write your YAML spec
|
|
167
|
+
3. Call `validate-spec` to check for errors
|
|
168
|
+
4. Fix any issues
|
|
169
|
+
5. Run `type-crafter generate` CLI in your project
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Key Rules Summary
|
|
174
|
+
|
|
175
|
+
1. **Nullability = `required` array** - Nothing else controls this
|
|
176
|
+
2. **Arrays = properties only** - Never top-level types
|
|
177
|
+
3. **Paths = from project root** - Where you run the CLI
|
|
178
|
+
4. **Top file = has `info` section** - Can use `#/` references
|
|
179
|
+
5. **Non-top file = no `info`** - Must use full file paths for ALL references
|