nest-hex 0.2.0 → 0.2.2
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 +183 -4
- package/dist/src/cli/bin.js +2630 -0
- package/dist/src/cli/commands/index.d.ts +23 -0
- package/dist/src/cli/commands/index.js +2595 -0
- package/dist/src/cli/config/defaults.d.ts +74 -0
- package/dist/src/cli/config/defaults.js +76 -0
- package/dist/src/cli/config/define-config.d.ts +74 -0
- package/dist/src/cli/config/define-config.js +62 -0
- package/dist/src/cli/config/loader.d.ts +74 -0
- package/dist/src/cli/config/loader.js +106 -0
- package/dist/src/cli/config/validator.d.ts +81 -0
- package/dist/src/cli/config/validator.js +108 -0
- package/dist/src/cli/generators/adapter.generator.d.ts +235 -0
- package/dist/src/cli/generators/adapter.generator.js +377 -0
- package/dist/src/cli/generators/base.generator.d.ts +190 -0
- package/dist/src/cli/generators/base.generator.js +312 -0
- package/dist/src/cli/generators/index.d.ts +264 -0
- package/dist/src/cli/generators/index.js +467 -0
- package/dist/src/cli/generators/port.generator.d.ts +211 -0
- package/dist/src/cli/generators/port.generator.js +364 -0
- package/dist/src/cli/generators/service.generator.d.ts +208 -0
- package/dist/src/cli/generators/service.generator.js +340 -0
- package/dist/src/cli/index.d.ts +74 -0
- package/dist/src/cli/index.js +69 -0
- package/dist/src/cli/types/config.types.d.ts +73 -0
- package/dist/src/cli/types/config.types.js +56 -0
- package/dist/src/cli/types/generator.types.d.ts +46 -0
- package/dist/src/cli/types/generator.types.js +56 -0
- package/dist/src/cli/types/index.d.ts +121 -0
- package/dist/src/cli/types/index.js +56 -0
- package/dist/src/cli/types/template.types.d.ts +28 -0
- package/dist/src/cli/types/template.types.js +56 -0
- package/dist/src/cli/ui/components/index.d.ts +97 -0
- package/dist/src/cli/ui/components/index.js +1493 -0
- package/dist/src/cli/utils/file-writer.d.ts +17 -0
- package/dist/src/cli/utils/file-writer.js +100 -0
- package/dist/src/cli/utils/linter-detector.d.ts +12 -0
- package/dist/src/cli/utils/linter-detector.js +128 -0
- package/dist/src/cli/utils/linter-runner.d.ts +17 -0
- package/dist/src/cli/utils/linter-runner.js +101 -0
- package/dist/src/cli/utils/name-transformer.d.ts +18 -0
- package/dist/src/cli/utils/name-transformer.js +90 -0
- package/dist/src/cli/utils/path-resolver.d.ts +5 -0
- package/dist/src/cli/utils/path-resolver.js +78 -0
- package/dist/src/cli/utils/port-scanner.d.ts +93 -0
- package/dist/src/cli/utils/port-scanner.js +104 -0
- package/dist/src/cli/utils/template-renderer.d.ts +30 -0
- package/dist/src/cli/utils/template-renderer.js +95 -0
- package/dist/{index.js → src/index.js} +14 -0
- package/package.json +10 -10
- /package/dist/{index.d.ts → src/index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -4,7 +4,30 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
|
|
7
|
-
## Why?
|
|
7
|
+
## Why Hexagonal Architecture?
|
|
8
|
+
|
|
9
|
+
**Hexagonal Architecture (Ports & Adapters)** is a powerful pattern for building maintainable, testable, and adaptable applications. Here's why it matters:
|
|
10
|
+
|
|
11
|
+
### 🎯 Keep Domain Logic Pure
|
|
12
|
+
Your business logic should never depend on infrastructure details like databases, APIs, or file systems. By defining **ports** (interfaces), your domain layer stays clean and focused on what matters: solving business problems.
|
|
13
|
+
|
|
14
|
+
### 🔌 Pluggable Infrastructure
|
|
15
|
+
Need to switch from AWS S3 to Google Cloud Storage? Replace MongoDB with PostgreSQL? Change from REST to GraphQL? With hexagonal architecture, you just swap the **adapter** – your domain logic never changes.
|
|
16
|
+
|
|
17
|
+
### 🧪 Effortless Testing
|
|
18
|
+
Mock external services by creating test adapters. No complex setup, no database connections, no API calls. Just simple, fast unit tests that focus on business logic.
|
|
19
|
+
|
|
20
|
+
### 🌍 Environment Flexibility
|
|
21
|
+
- **Development**: Use filesystem storage
|
|
22
|
+
- **Testing**: Use in-memory mocks
|
|
23
|
+
- **Production**: Use AWS S3
|
|
24
|
+
|
|
25
|
+
Same domain code, different adapters. Configure once, swap anywhere.
|
|
26
|
+
|
|
27
|
+
### 📦 Independent Deployment
|
|
28
|
+
Infrastructure changes don't require redeploying your entire application. Update an adapter independently without touching core business logic.
|
|
29
|
+
|
|
30
|
+
## Why This Library?
|
|
8
31
|
|
|
9
32
|
Building NestJS applications with the Ports & Adapters pattern involves repetitive boilerplate:
|
|
10
33
|
|
|
@@ -14,7 +37,7 @@ Building NestJS applications with the Ports & Adapters pattern involves repetiti
|
|
|
14
37
|
- Supporting both `register()` and `registerAsync()` patterns
|
|
15
38
|
- Keeping the app responsible for configuration (no `process.env` in libraries)
|
|
16
39
|
|
|
17
|
-
|
|
40
|
+
**nest-hex eliminates this boilerplate** while maintaining compile-time type safety and providing a delightful developer experience through both decorators and a powerful CLI.
|
|
18
41
|
|
|
19
42
|
## Features
|
|
20
43
|
|
|
@@ -24,6 +47,126 @@ This library provides base classes and decorators to eliminate this boilerplate
|
|
|
24
47
|
- ⚡ **Zero runtime overhead**: Uses TypeScript decorators and metadata, minimal abstraction
|
|
25
48
|
- 📦 **Tiny**: Core library is under 1KB minified
|
|
26
49
|
- 🧪 **Testable**: Easily mock adapters for testing
|
|
50
|
+
- 🛠️ **Powerful CLI**: Generate ports, adapters, and services with a single command
|
|
51
|
+
|
|
52
|
+
## CLI
|
|
53
|
+
|
|
54
|
+
**nest-hex** includes a powerful CLI to scaffold ports, adapters, and services instantly. No more manual file creation!
|
|
55
|
+
|
|
56
|
+
### Quick Start
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Initialize configuration
|
|
60
|
+
npx nest-hex init
|
|
61
|
+
|
|
62
|
+
# Generate a port (domain interface)
|
|
63
|
+
npx nest-hex generate port ObjectStorage
|
|
64
|
+
|
|
65
|
+
# Generate an adapter for the port
|
|
66
|
+
npx nest-hex generate adapter S3 --port ObjectStorage
|
|
67
|
+
|
|
68
|
+
# Generate both port and adapter together
|
|
69
|
+
npx nest-hex generate full ObjectStorage S3
|
|
70
|
+
|
|
71
|
+
# Generate a service that uses a port
|
|
72
|
+
npx nest-hex generate service FileUpload
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Available Commands
|
|
76
|
+
|
|
77
|
+
#### `init`
|
|
78
|
+
Create a `nest-hex.config.ts` configuration file in your project.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npx nest-hex init
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### `generate` (or `g`)
|
|
85
|
+
Generate ports, adapters, services, or complete modules.
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Generate a port
|
|
89
|
+
npx nest-hex generate port <name>
|
|
90
|
+
npx nest-hex g port PaymentGateway
|
|
91
|
+
|
|
92
|
+
# Generate an adapter
|
|
93
|
+
npx nest-hex generate adapter <name> --port <portName>
|
|
94
|
+
npx nest-hex g adapter Stripe --port PaymentGateway
|
|
95
|
+
|
|
96
|
+
# Generate both port and adapter
|
|
97
|
+
npx nest-hex generate full <portName> <adapterName>
|
|
98
|
+
npx nest-hex g full EmailService SendGrid
|
|
99
|
+
|
|
100
|
+
# Generate a service
|
|
101
|
+
npx nest-hex generate service <name>
|
|
102
|
+
npx nest-hex g service UserRegistration
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Interactive Mode
|
|
106
|
+
|
|
107
|
+
Run commands without arguments for interactive prompts:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
npx nest-hex generate
|
|
111
|
+
# → Select type: port, adapter, service, or full
|
|
112
|
+
# → Enter name(s)
|
|
113
|
+
# → Files generated!
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Configuration
|
|
117
|
+
|
|
118
|
+
The CLI uses `nest-hex.config.ts` to customize output paths and naming conventions:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// nest-hex.config.ts
|
|
122
|
+
import { defineConfig } from 'nest-hex/cli';
|
|
123
|
+
|
|
124
|
+
export default defineConfig({
|
|
125
|
+
output: {
|
|
126
|
+
portsDir: 'src/domain/ports', // Where to generate ports
|
|
127
|
+
adaptersDir: 'src/infrastructure', // Where to generate adapters
|
|
128
|
+
servicesDir: 'src/application', // Where to generate services
|
|
129
|
+
},
|
|
130
|
+
naming: {
|
|
131
|
+
portSuffix: 'Port', // ObjectStoragePort
|
|
132
|
+
tokenSuffix: '_PORT', // OBJECT_STORAGE_PORT
|
|
133
|
+
adapterSuffix: 'Adapter', // S3Adapter
|
|
134
|
+
serviceSuffix: 'Service', // FileUploadService
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### What Gets Generated
|
|
140
|
+
|
|
141
|
+
#### Port Generation
|
|
142
|
+
Creates a complete port with:
|
|
143
|
+
- Token definition (`OBJECT_STORAGE_PORT`)
|
|
144
|
+
- TypeScript interface with example methods
|
|
145
|
+
- Service implementation with `@InjectPort`
|
|
146
|
+
- Module that accepts adapters
|
|
147
|
+
- Barrel exports (`index.ts`)
|
|
148
|
+
|
|
149
|
+
#### Adapter Generation
|
|
150
|
+
Creates a production-ready adapter with:
|
|
151
|
+
- Implementation service class
|
|
152
|
+
- Options interface
|
|
153
|
+
- Adapter class with `@Port` decorator
|
|
154
|
+
- Complete TypeScript types
|
|
155
|
+
- Barrel exports
|
|
156
|
+
|
|
157
|
+
#### Service Generation
|
|
158
|
+
Creates a domain service with:
|
|
159
|
+
- Service class with `@InjectPort` usage
|
|
160
|
+
- Type-safe port injection
|
|
161
|
+
- Example business logic methods
|
|
162
|
+
|
|
163
|
+
### CLI Benefits
|
|
164
|
+
|
|
165
|
+
✅ **Instant scaffolding** - Generate complete, type-safe modules in seconds
|
|
166
|
+
✅ **Consistent structure** - All team members follow the same patterns
|
|
167
|
+
✅ **Best practices built-in** - Generated code follows hexagonal architecture principles
|
|
168
|
+
✅ **Customizable** - Configure paths and naming to match your project
|
|
169
|
+
✅ **Interactive** - Friendly prompts guide you through generation
|
|
27
170
|
|
|
28
171
|
## Installation
|
|
29
172
|
|
|
@@ -239,9 +382,11 @@ class AxiosAdapterClass extends Adapter<AxiosOptions> {
|
|
|
239
382
|
}
|
|
240
383
|
```
|
|
241
384
|
|
|
242
|
-
### Swapping Adapters
|
|
385
|
+
### Swapping Adapters - The Power of Pluggability
|
|
386
|
+
|
|
387
|
+
**This is the core benefit of hexagonal architecture**: swap infrastructure without touching business logic.
|
|
243
388
|
|
|
244
|
-
|
|
389
|
+
#### Environment-Based Swapping
|
|
245
390
|
|
|
246
391
|
```typescript
|
|
247
392
|
// Development: Use filesystem storage
|
|
@@ -262,6 +407,40 @@ const adapter = process.env.NODE_ENV === 'production'
|
|
|
262
407
|
export class AppModule {}
|
|
263
408
|
```
|
|
264
409
|
|
|
410
|
+
#### Multi-Cloud Strategy
|
|
411
|
+
|
|
412
|
+
```typescript
|
|
413
|
+
// Easily switch cloud providers without changing domain code
|
|
414
|
+
const storageAdapter = process.env.CLOUD_PROVIDER === 'aws'
|
|
415
|
+
? S3Adapter.register({ bucket: 'my-bucket', region: 'us-east-1' })
|
|
416
|
+
: process.env.CLOUD_PROVIDER === 'gcp'
|
|
417
|
+
? GCSAdapter.register({ bucket: 'my-bucket' })
|
|
418
|
+
: AzureBlobAdapter.register({ containerName: 'my-container' });
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
#### Feature Flags
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
// Gradually migrate to new infrastructure
|
|
425
|
+
const emailAdapter = featureFlags.useNewEmailProvider
|
|
426
|
+
? SendGridAdapter.register({ apiKey: process.env.SENDGRID_KEY })
|
|
427
|
+
: SESAdapter.register({ region: 'us-east-1' });
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
#### Testing with Mocks
|
|
431
|
+
|
|
432
|
+
```typescript
|
|
433
|
+
// Test module: in-memory mock
|
|
434
|
+
const testAdapter = MockStorageAdapter.register();
|
|
435
|
+
|
|
436
|
+
// Production module: real infrastructure
|
|
437
|
+
const prodAdapter = S3Adapter.register({ bucket: 'prod' });
|
|
438
|
+
|
|
439
|
+
// Same domain code, different runtime behavior
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
**Key Point**: Your `StorageService` business logic **never changes**. Only the infrastructure adapter changes. This is the essence of maintainable architecture.
|
|
443
|
+
|
|
265
444
|
### Testing with Mock Adapters
|
|
266
445
|
|
|
267
446
|
```typescript
|