@nemmtor/ts-databuilders 0.0.1-alpha.5 → 0.0.1-alpha.6

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.
Files changed (2) hide show
  1. package/README.md +330 -96
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,97 +1,9 @@
1
1
  # 🧱 TS DataBuilders
2
- A CLI tool that automatically generates builder classes from annotated TypeScript types.
2
+ Automatically generate type-safe builder classes from your TypeScript types to write cleaner, more focused tests.
3
3
 
4
- ## Usage
5
- Install the package:
6
- ```bash
7
- pnpm add -D @nemmtor/ts-databuilders
8
- ```
9
-
10
- Annotate the types you want to build with JsDoc tag:
11
- ```ts
12
- /**
13
- * @DataBuilder
14
- */
15
- type Example = {
16
- bar: string;
17
- baz: string;
18
- }
19
- ```
20
-
21
- Run the command:
22
- ```bash
23
- pnpm ts-databuilders
24
- ```
25
- By default it tries to find annotated types in `src/**/*.ts{,x}` and outputs builders in `generated/builders`.
26
- Above and much more is configurable - check out configuration section.
27
-
28
- Above example will result in output:
29
- ```ts
30
- import type { Example } from "../../src/example";
31
- import { DataBuilder } from "./data-builder";
32
-
33
- export class ExampleBuilder extends DataBuilder<Example> {
34
- constructor() {
35
- super({
36
- bar: "",
37
- baz: ""
38
- });
39
- }
40
-
41
- withBar(bar: Example['bar']) {
42
- return this.with({ bar: bar });
43
- }
44
-
45
- withBaz(baz: Example['baz']) {
46
- return this.with({ baz: baz });
47
- }
48
- }
49
- ```
50
-
51
- ## Configuration
52
- > [!NOTE]
53
- > Name column is equal to field names in ts-databuilders.json file.
54
-
55
- | Name | Flag | Description | Default | Type |
56
- |---------------|-----------------|----------------------------------------------------------------|--------------------------------------------------|------------|
57
- | jsdocTag | --jsdoc-tag | JSDoc tag used to mark types for data building generation. | DataBuilder | string |
58
- | outputDir | --output-dir -o | Output directory for generated builders. | generated/builders | string |
59
- | include | --include -i | Glob pattern for files included while searching for jsdoc tag. | src/**/*.ts{,x} | string |
60
- | fileSuffix | --file-suffix | File suffix for created builder files. | .builder | string |
61
- | builderSuffix | --builder-suffix | Suffix for generated classes. | Builder | string |
62
- | defaults | --defaults | Default values to be used in data builder constructor. | { string: '', number: 0, boolean: false, } | object/map |
63
-
64
- All of the above flags are optional and are having sensible defaults which should be good enough for most cases.
65
- You can configure these via cli flags or by creating `ts-databuilders.json` file in the root of your repository.
66
- Example of config file:
67
- ```json
68
- {
69
- "$schema": "https://raw.githubusercontent.com/nemmtor/ts-databuilders/refs/heads/main/schema.json",
70
- "include": "example-data/**",
71
- "builderSuffix": "GeneratedBuilder",
72
- "fileSuffix": ".generated-builder",
73
- "jsdocTag": "GenerateBuilder",
74
- "outputDir": "generated-builders/",
75
- "defaults": {
76
- "boolean": true,
77
- "number": 2000,
78
- "string": "foo"
79
- }
80
- }
81
- ```
82
-
83
- Priority when resolving config values is:
84
- 1. Cli flags
85
- 2. Config file values
86
- 3. Hardcoded defaults
87
-
88
- ## Motivation
89
- When writing tests you often want to test some scenario that is happening when
90
- one of the input values is in a specific shape.
91
- Often times this value is only one of many options provided.
92
-
93
- Imagine testing a case where document aggregate should emit an event when it successfully
94
- update it's content:
4
+ ## Why?
5
+ Tests often become cluttered with boilerplate when you need to create complex objects just to test one specific field. DataBuilders let you focus on what matters:
6
+ Imagine testing a case where document aggregate should emit an event when it successfully update it's content:
95
7
  ```ts
96
8
  it('should emit a ContentUpdatedEvent', () => {
97
9
  const aggregate = DocumentAggregate.create({
@@ -165,8 +77,330 @@ it('should show validation error when email is invalid', async () => {
165
77
 
166
78
  This not only makes the test code less verbose but also highlights what is really being tested.
167
79
 
168
- ## Nested builders
169
- TODO
80
+ ## Installation
81
+ Install the package:
82
+ ```bash
83
+ # npm
84
+ npm install -D @nemmtor/ts-databuilders
85
+
86
+ # pnpm
87
+ pnpm add -D @nemmtor/ts-databuilders
88
+
89
+ # yarn
90
+ yarn add -D @nemmtor/ts-databuilders
91
+ ```
92
+
93
+ ## Quick Start
94
+ **1. Annotate your types with JSDoc:**
95
+ ```ts
96
+ /**
97
+ * @DataBuilder
98
+ */
99
+ type User = {
100
+ id: string;
101
+ email: string;
102
+ name: string;
103
+ isActive: boolean;
104
+ }
105
+ ```
106
+ **2. Generate builders:**
107
+ ```bash
108
+ pnpm ts-databuilders
109
+ ```
110
+ **3. Use in your tests:**
111
+ ```ts
112
+ import { UserBuilder } from '...';
113
+
114
+ const testUser = new UserBuilder()
115
+ .withEmail('test@example.com')
116
+ .withIsActive(false)
117
+ .build();
118
+ ```
119
+ ## Generated Output
120
+
121
+ For the `User` type above, you'll get:
122
+ ```ts
123
+ import type { User } from "...";
124
+ import { DataBuilder } from "./data-builder";
125
+
126
+ export class UserBuilder extends DataBuilder<User> {
127
+ constructor() {
128
+ super({
129
+ id: "",
130
+ email: "",
131
+ name: "",
132
+ isActive: false
133
+ });
134
+ }
135
+
136
+ withId(id: User['id']) {
137
+ return this.with({ id });
138
+ }
139
+
140
+ withEmail(email: User['email']) {
141
+ return this.with({ email });
142
+ }
143
+
144
+ withName(name: User['name']) {
145
+ return this.with({ name });
146
+ }
147
+
148
+ withIsActive(isActive: User['isActive']) {
149
+ return this.with({ isActive });
150
+ }
151
+ }
152
+ ```
153
+ ## Configuration
154
+ All configuration is optional with sensible defaults.
155
+ ### Via CLI flags:
156
+ ```bash
157
+ pnpm ts-databuilders --output-dir="src/__generated__" --jsdoc-tag=MyBuilder
158
+ ```
159
+ ### Via config file (`ts-databuilders.json`):
160
+ ```json
161
+ {
162
+ "$schema": "https://raw.githubusercontent.com/nemmtor/ts-databuilders/refs/heads/main/schema.json",
163
+ "include": "src/**/*.ts",
164
+ "outputDir": "src/__generated__/builders",
165
+ "jsdocTag": "DataBuilder",
166
+ "fileSuffix": ".builder",
167
+ "builderSuffix": "Builder",
168
+ "defaults": {
169
+ "string": "",
170
+ "number": 0,
171
+ "boolean": false
172
+ }
173
+ }
174
+ ```
175
+
176
+ ### Options Reference
177
+
178
+ | Name | Flag | Description | Default |
179
+ |---------------|-------------------|--------------------------------------------------|-----------------------------|
180
+ | jsdocTag | `--jsdoc-tag` | JSDoc tag to mark types for generation | `DataBuilder` |
181
+ | outputDir | `--output-dir -o` | Output directory for generated builders | `generated/builders` |
182
+ | include | `--include -i` | Glob pattern for source files | `src/**/*.ts{,x}` |
183
+ | fileSuffix | `--file-suffix` | File suffix for builder files | `.builder` |
184
+ | builderSuffix | `--builder-suffix`| Class name suffix | `Builder` |
185
+ | defaults | `--defaults` | Default values for primitives | See example above |
186
+
187
+ **Priority:** CLI flags > Config file > Built-in defaults
188
+
189
+ ## Nested Builders
190
+ When your types contain complex nested objects, you can annotate their type definitions and TS DataBuilders will automatically generate nested builders, allowing you to compose them fluently.
191
+ ### Example
192
+
193
+ **Input types:**
194
+ ```ts
195
+ /**
196
+ * @DataBuilder
197
+ */
198
+ export type User = {
199
+ name: string;
200
+ address: Address;
201
+ };
202
+
203
+ /**
204
+ * @DataBuilder
205
+ */
206
+ export type Address = {
207
+ street: string;
208
+ city: string;
209
+ country: string;
210
+ };
211
+ ```
212
+ **Generated builders:**
213
+ ```ts
214
+ export class UserBuilder extends DataBuilder<User> {
215
+ constructor() {
216
+ super({
217
+ name: "",
218
+ address: new AddressBuilder().build();
219
+ });
220
+ }
221
+
222
+ withName(name: User['name']) {
223
+ return this.with({ name });
224
+ }
225
+
226
+ withAddress(address: DataBuilder<User['address']>) {
227
+ return this.with({ address: address.build() });
228
+ }
229
+ }
230
+
231
+ export class AddressBuilder extends DataBuilder<Address> {
232
+ constructor() {
233
+ super({
234
+ street: "",
235
+ city: "",
236
+ country: ""
237
+ });
238
+ }
239
+
240
+ withStreet(street: Address['street']) {
241
+ return this.with({ street });
242
+ }
243
+
244
+ withCity(city: Address['city']) {
245
+ return this.with({ city });
246
+ }
247
+
248
+ withCountry(country: Address['country']) {
249
+ return this.with({ country });
250
+ }
251
+ }
252
+ ```
253
+ **Usage:**
254
+ ```ts
255
+ // ✅ Compose builders fluently
256
+ const user = new UserBuilder()
257
+ .withName('John Doe')
258
+ .withAddress(
259
+ new AddressBuilder()
260
+ .withStreet('123 Main St')
261
+ .withCity('New York')
262
+ )
263
+ .build();
264
+ // {..., address: { street: "123 Main st", city: "New York", country: "" } }
265
+
266
+ // ✅ Use default values
267
+ const userWithDefaultAddress = new UserBuilder().build();
268
+ // {..., address: { street: "", city: "", country: "" } }
269
+
270
+ // ✅ Override just one nested field
271
+ const userWithCity = new UserBuilder()
272
+ .withAddress(
273
+ new AddressBuilder()
274
+ .withCity('San Francisco')
275
+ )
276
+ .build();
277
+ // {..., address: { street: "", city: "San Francisco", country: "" } }
278
+ ```
279
+
280
+ ### Supported Types
281
+
282
+ The library supports a wide range of TypeScript type features:
283
+
284
+ ✅ **Primitives & Built-ins**
285
+ - `string`, `number`, `boolean`, `Date`
286
+ - Literal types: `'active' | 'inactive'`, `1 | 2 | 3`
287
+
288
+ ✅ **Complex Structures**
289
+ - Objects and nested objects
290
+ - Arrays: `string[]`, `Array<number>`
291
+ - Tuples: `[string, number]`
292
+ - Records: `Record<string, string>` `Record<'foo' | 'bar', string>`
293
+
294
+ ✅ **Type Operations**
295
+ - Unions: `string | number | true | false`
296
+ - Intersections: `A & B`
297
+ - Utility types: `Pick<T, K>`, `Omit<T, K>`, `Partial<T>`, `Required<T>`, `Readonly<T>`, `Extract<T, U>`, `NonNullable<T>`
298
+ - Branded types: `type UserId = string & { __brand: 'UserId' }`
299
+
300
+ ✅ **References**
301
+ - Type references from the same file
302
+ - Type references from other files
303
+ - External library types (e.g., `z.infer<typeof schema>`)
304
+
305
+ **For a comprehensive example** of supported types, check out the [example-data/bar.ts](https://github.com/nemmtor/ts-databuilders/blob/main/example-data/bar.ts) file in the repository. This file is used during development and demonstrates complex real-world type scenarios.
306
+
307
+ ## Important Rules & Limitations
308
+
309
+ ### Unique Builder Names
310
+ Each type annotated with the JSDoc tag must have a **unique name** across your codebase:
311
+ ```ts
312
+ // ❌ Error: Duplicate builder names
313
+ // In file-a.ts
314
+ /** @DataBuilder */
315
+ export type User = { name: string };
316
+
317
+ // In file-b.ts
318
+ /** @DataBuilder */
319
+ export type User = { email: string }; // 💥 Duplicate!
320
+ ```
321
+
322
+ ### Exported Types Only
323
+ Types must be **exported** to generate builders:
324
+ ```ts
325
+ // ❌ Won't work
326
+ /** @DataBuilder */
327
+ type User = { name: string };
328
+
329
+ // ✅ Works
330
+ /** @DataBuilder */
331
+ export type User = { name: string };
332
+ ```
333
+
334
+ ### Type Aliases Only
335
+ Currently, only **type aliases** are supported as root builder types. Interfaces, classes, and enums are not supported:
336
+ ```ts
337
+ // ❌ Not supported
338
+ /** @DataBuilder */
339
+ export interface User {
340
+ name: string;
341
+ }
342
+
343
+ // ❌ Not supported
344
+ /** @DataBuilder */
345
+ export class User {
346
+ name: string;
347
+ }
348
+
349
+ // ✅ Supported
350
+ /** @DataBuilder */
351
+ export type User = {
352
+ name: string;
353
+ };
354
+ ```
355
+
356
+ ### Unsupported Type Features
357
+
358
+ Some TypeScript features are not yet supported and will cause generation errors:
359
+
360
+ - **Recursive types**: Types that reference themselves
361
+ ```ts
362
+ // ❌ Not supported
363
+ type TreeNode = {
364
+ value: string;
365
+ children: TreeNode[]; // Self-reference
366
+ };
367
+ ```
368
+
369
+ - **Function types**: Properties that are functions
370
+ ```ts
371
+ // ❌ Not supported
372
+ type WithCallback = {
373
+ onSave: (data: string) => void;
374
+ };
375
+ ```
376
+
377
+ - typeof, keyof, any, unknown, bigint, symbol
378
+
379
+ If you encounter an unsupported type, you'll see an error like:
380
+ ```
381
+ Unsupported syntax kind of id: XXX with raw type: YYY
382
+ ```
383
+ Support for above might be added in future.
384
+
385
+ ### Alpha Stage
386
+ ⚠️ **This library is in active development (v0.x.x)**
387
+
388
+ - Breaking changes may occur between minor versions
389
+ - Not all edge cases are covered yet
390
+ - Test thoroughly before using in production
391
+
392
+ **Found an issue?** Please [report it on GitHub](https://github.com/nemmtor/ts-databuilders/issues) with:
393
+ - The type definition causing the issue
394
+ - The error message received
395
+ - Your `ts-databuilders.json` config (if applicable)
396
+
397
+ Your feedback helps improve the library for everyone! 🙏
398
+
399
+
400
+ ## Contributing
401
+
402
+ Contributions welcome! Please open an issue or PR on [GitHub](https://github.com/nemmtor/ts-databuilders).
403
+
404
+ ## License
170
405
 
171
- ## Supported types
172
- TODO
406
+ MIT © [nemmtor](https://github.com/nemmtor)
package/package.json CHANGED
@@ -58,5 +58,5 @@
58
58
  "glob": "^11.0.3",
59
59
  "ts-morph": "^27.0.2"
60
60
  },
61
- "version": "0.0.1-alpha.5"
61
+ "version": "0.0.1-alpha.6"
62
62
  }