ng-openapi 0.0.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.
Files changed (6) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +188 -0
  3. package/cli.cjs +1827 -0
  4. package/index.d.ts +169 -0
  5. package/index.js +1807 -0
  6. package/package.json +86 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Tareq Jami
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # ng-openapi Usage Guide
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ npm install ng-openapi --save-dev
7
+ # or
8
+ yarn add ng-openapi --dev
9
+ ```
10
+
11
+ ## CLI Usage
12
+
13
+ ### Using a Configuration File (Recommended)
14
+
15
+ Create a configuration file (e.g., `openapi.config.ts`):
16
+
17
+ ```typescript
18
+ import { GeneratorConfig } from 'ng-openapi';
19
+
20
+ const config: GeneratorConfig = {
21
+ input: './swagger.json',
22
+ output: './src/api',
23
+ options: {
24
+ dateType: 'Date',
25
+ enumStyle: 'enum',
26
+ generateEnumBasedOnDescription: true,
27
+ generateServices: true,
28
+ customHeaders: {
29
+ 'X-Requested-With': 'XMLHttpRequest',
30
+ 'Accept': 'application/json'
31
+ },
32
+ responseTypeMapping: {
33
+ 'application/pdf': 'blob',
34
+ 'application/zip': 'blob',
35
+ 'text/csv': 'text'
36
+ },
37
+ customizeMethodName: (operationId) => {
38
+ const parts = operationId.split('_');
39
+ return parts[parts.length - 1] || operationId;
40
+ }
41
+ }
42
+ };
43
+
44
+ export default config;
45
+ ```
46
+
47
+ Then run:
48
+
49
+ ```bash
50
+ # Direct command
51
+ ng-openapi -c openapi.config.ts
52
+
53
+ # Or with the generate subcommand
54
+ ng-openapi generate -c openapi.config.ts
55
+ ```
56
+
57
+ ### Using Command Line Options
58
+
59
+ ```bash
60
+ # Generate both types and services
61
+ ng-openapi -i ./swagger.json -o ./src/api
62
+
63
+ # Generate only types
64
+ ng-openapi -i ./swagger.json -o ./src/api --types-only
65
+
66
+ # Specify date type
67
+ ng-openapi -i ./swagger.json -o ./src/api --date-type string
68
+ ```
69
+
70
+ ### Command Line Options
71
+
72
+ - `-c, --config <path>` - Path to configuration file
73
+ - `-i, --input <path>` - Path to Swagger/OpenAPI specification file
74
+ - `-o, --output <path>` - Output directory (default: `./src/generated`)
75
+ - `--types-only` - Generate only TypeScript interfaces
76
+ - `--date-type <type>` - Date type to use: `string` or `Date` (default: `Date`)
77
+
78
+ ## Configuration Options
79
+
80
+ ### Required Fields
81
+
82
+ - `input` - Path to your Swagger/OpenAPI specification file
83
+ - `output` - Output directory for generated files
84
+
85
+ ### Optional Fields
86
+
87
+ - `options.dateType` - How to handle date types: `'string'` or `'Date'` (default: `'Date'`)
88
+ - `options.enumStyle` - Enum generation style: `'enum'` or `'union'` (default: `'enum'`)
89
+ - `options.generateEnumBasedOnDescription` - Parse enum values from description field (default: `true`)
90
+ - `options.generateServices` - Generate Angular services (default: `true`)
91
+ - `options.customHeaders` - Headers to add to all HTTP requests
92
+ - `options.responseTypeMapping` - Map content types to Angular HttpClient response types
93
+ - `options.customizeMethodName` - Function to customize generated method names
94
+ - `compilerOptions` - TypeScript compiler options for code generation
95
+
96
+ ## Generated Files Structure
97
+
98
+ ```
99
+ output/
100
+ ├── models/
101
+ │ └── index.ts # TypeScript interfaces/types
102
+ ├── services/
103
+ │ ├── index.ts # Service exports
104
+ │ └── *.service.ts # Angular services
105
+ ├── tokens/
106
+ │ └── index.ts # Injection tokens
107
+ └── utils/
108
+ ├── date-transformer.ts # Date transformation interceptor
109
+ └── file-download.ts # File download helpers
110
+ ```
111
+
112
+ ## Angular Integration
113
+
114
+ ### 1. Configure Base Path
115
+
116
+ ```typescript
117
+ import { BASE_PATH } from './api/tokens';
118
+
119
+ // In your app.config.ts or module
120
+ export const appConfig: ApplicationConfig = {
121
+ providers: [
122
+ { provide: BASE_PATH, useValue: 'https://api.example.com' },
123
+ // other providers...
124
+ ]
125
+ };
126
+ ```
127
+
128
+ ### 2. Add Date Interceptor (if using Date type)
129
+
130
+ ```typescript
131
+ import { DateInterceptor } from './api/utils/date-transformer';
132
+ import { HTTP_INTERCEPTORS } from '@angular/common/http';
133
+
134
+ export const appConfig: ApplicationConfig = {
135
+ providers: [
136
+ { provide: HTTP_INTERCEPTORS, useClass: DateInterceptor, multi: true },
137
+ // other providers...
138
+ ]
139
+ };
140
+ ```
141
+
142
+ ### 3. Use Generated Services
143
+
144
+ ```typescript
145
+ import { UserService } from './api/services';
146
+ import { User } from './api/models';
147
+
148
+ @Component({
149
+ selector: 'app-users',
150
+ template: `...`
151
+ })
152
+ export class UsersComponent {
153
+ users$ = this.userService.getUsers();
154
+
155
+ constructor(private userService: UserService) {}
156
+ }
157
+ ```
158
+
159
+ ## File Download Example
160
+
161
+ ```typescript
162
+ import { downloadFileOperator } from './api/utils/file-download';
163
+
164
+ export class ReportComponent {
165
+ constructor(private reportService: ReportService) {}
166
+
167
+ downloadReport() {
168
+ this.reportService.getReport('pdf', { reportId: 123 })
169
+ .pipe(
170
+ downloadFileOperator('report.pdf')
171
+ )
172
+ .subscribe();
173
+ }
174
+ }
175
+ ```
176
+
177
+ ## Package.json Scripts
178
+
179
+ Add these scripts to your `package.json`:
180
+
181
+ ```json
182
+ {
183
+ "scripts": {
184
+ "generate:api": "ng-openapi -c openapi.config.ts",
185
+ "generate:api:watch": "nodemon --watch swagger.json --exec npm run generate:api"
186
+ }
187
+ }
188
+ ```