@ng-openapi/http-resource 0.0.34-pr-85-testing-fd2f80a.0 → 0.0.34-pr-103-refactored-d262edf.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.
Files changed (4) hide show
  1. package/index.cjs +440 -486
  2. package/index.d.ts +157 -29
  3. package/index.js +440 -486
  4. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,12 +1,58 @@
1
- import { ScriptTarget, ModuleKind, Project } from 'ts-morph';
2
- import { Info, ExternalDocs, Path, ParameterType, XML, BodyParameter, QueryParameter, Security, Tag } from 'swagger-schema-official';
1
+ import { Project, ScriptTarget, ModuleKind } from 'ts-morph';
2
+ import { ParameterType, XML, ExternalDocs, Info, Path, BodyParameter, QueryParameter, Security, Tag } from 'swagger-schema-official';
3
3
 
4
+ /**
5
+ * An operation parameter. OpenAPI 3.x carries the type in `schema`;
6
+ * Swagger 2.0 puts `type`/`format` directly on the parameter — consumers
7
+ * must handle both (or use the normalized model, which precomputes them).
8
+ */
9
+ interface Parameter {
10
+ name: string;
11
+ in: "query" | "path" | "header" | "cookie";
12
+ required?: boolean;
13
+ schema?: SwaggerDefinition;
14
+ type?: string;
15
+ format?: string;
16
+ description?: string;
17
+ }
18
+ /**
19
+ * One operation of the spec, flattened to (path, method). Raw-ish view —
20
+ * generators consume its precomputed extension NormalizedOperation instead.
21
+ */
22
+ interface PathInfo {
23
+ path: string;
24
+ method: string;
25
+ operationId?: string;
26
+ summary?: string;
27
+ description?: string;
28
+ tags?: string[];
29
+ parameters?: Parameter[];
30
+ requestBody?: RequestBody;
31
+ responses?: Record<string, SwaggerResponse>;
32
+ }
33
+ /**
34
+ * Operation request body, keyed by content type — the OpenAPI 3.x shape.
35
+ * Swagger 2.0 `in: "body"` parameters are NOT yet lifted into this shape;
36
+ * 2.0 operations currently generate without a body parameter.
37
+ */
38
+ interface RequestBody {
39
+ required?: boolean;
40
+ content?: Record<string, {
41
+ schema?: SwaggerDefinition;
42
+ }>;
43
+ }
44
+ /**
45
+ * A single response entry, keyed by content type — the OpenAPI 3.x shape.
46
+ * A Swagger 2.0 response's direct `schema` is NOT yet mapped into `content`;
47
+ * 2.0 responses currently type as `any`.
48
+ */
4
49
  interface SwaggerResponse {
5
50
  description?: string;
6
51
  content?: Record<string, {
7
- schema?: any;
52
+ schema?: SwaggerDefinition;
8
53
  }>;
9
54
  }
55
+ /** OpenAPI 3.x security scheme (subset of the spec's fields). */
10
56
  interface OpenApiSecurityScheme {
11
57
  type?: "apiKey" | "http" | "oauth2" | "openIdConnect";
12
58
  description?: string;
@@ -14,15 +60,20 @@ interface OpenApiSecurityScheme {
14
60
  in?: "query" | "header" | "cookie";
15
61
  scheme?: string;
16
62
  bearerFormat?: string;
17
- flows?: Record<string, any>;
63
+ flows?: Record<string, unknown>;
18
64
  openIdConnectUrl?: string;
19
65
  }
66
+ /**
67
+ * A JSON-Schema-ish definition as it appears in the spec, shared by
68
+ * Swagger 2.0 and OpenAPI 3.x (3.0's `nullable` and 3.1's type arrays
69
+ * both flow through `type`/`nullable`). May contain unresolved `$ref`s.
70
+ */
20
71
  interface SwaggerDefinition {
21
72
  type?: ParameterType | undefined;
22
73
  format?: string | undefined;
23
74
  title?: string | undefined;
24
75
  description?: string | undefined;
25
- default?: any;
76
+ default?: unknown;
26
77
  multipleOf?: number | undefined;
27
78
  maximum?: number | undefined;
28
79
  exclusiveMaximum?: boolean | undefined;
@@ -36,7 +87,9 @@ interface SwaggerDefinition {
36
87
  uniqueItems?: boolean | undefined;
37
88
  maxProperties?: number | undefined;
38
89
  minProperties?: number | undefined;
39
- enum?: any[] | undefined;
90
+ enum?: Array<string | number> | undefined;
91
+ /** OpenAPI 3.1 (JSON Schema); normalizeSchema folds it into a single-value `enum`. */
92
+ const?: unknown;
40
93
  items?: SwaggerDefinition | SwaggerDefinition[] | undefined;
41
94
  $ref?: string | undefined;
42
95
  allOf?: SwaggerDefinition[] | undefined;
@@ -49,11 +102,16 @@ interface SwaggerDefinition {
49
102
  nullable?: boolean | undefined;
50
103
  xml?: XML | undefined;
51
104
  externalDocs?: ExternalDocs | undefined;
52
- example?: any;
105
+ example?: unknown;
53
106
  required?: string[] | undefined;
54
107
  oneOf?: SwaggerDefinition[];
55
108
  anyOf?: SwaggerDefinition[];
56
109
  }
110
+ /**
111
+ * The raw parsed spec document. Exactly one of `swagger` ("2.x") or
112
+ * `openapi` ("3.x") identifies the version; everything version-specific
113
+ * is resolved once by normalizeSpec.
114
+ */
57
115
  interface SwaggerSpec {
58
116
  openapi: string;
59
117
  swagger: string;
@@ -89,74 +147,144 @@ interface SwaggerSpec {
89
147
  };
90
148
  }
91
149
 
92
- declare class SwaggerParser {
93
- private readonly spec;
94
- private constructor();
95
- static create(swaggerPathOrUrl: string, config: GeneratorConfig): Promise<SwaggerParser>;
96
- private static loadContent;
97
- private static fetchUrlContent;
98
- private static parseSpecContent;
99
- private static detectFormat;
100
- getDefinitions(): Record<string, SwaggerDefinition>;
101
- getDefinition(name: string): SwaggerDefinition | undefined;
150
+ type ResponseKind = "json" | "blob" | "arraybuffer" | "text";
151
+ /**
152
+ * A spec operation with everything the generators need precomputed once at
153
+ * normalization time. Generators must not re-derive any of these fields or
154
+ * resolve $refs during emission — that is exactly the duplication this model
155
+ * exists to remove (REFACTORING_PLAN.md phase 2.2).
156
+ */
157
+ interface NormalizedOperation extends PathInfo {
158
+ /** parameters with in === "path" */
159
+ pathParams: Parameter[];
160
+ /** parameters with in === "query" */
161
+ queryParams: Parameter[];
162
+ hasBody: boolean;
163
+ isMultipart: boolean;
164
+ /** urlencoded body without a JSON alternative */
165
+ isUrlEncoded: boolean;
166
+ /** ref-resolved multipart body schema (set only when isMultipart) */
167
+ formDataSchema?: SwaggerDefinition;
168
+ /** field names of formDataSchema's properties */
169
+ formDataFields: string[];
170
+ /** ref-resolved urlencoded body schema (set only when isUrlEncoded) */
171
+ urlEncodedSchema?: SwaggerDefinition;
172
+ /** field names of urlEncodedSchema's properties */
173
+ urlEncodedFields: string[];
174
+ /** derived from the first success response (200/201/202/204/206) */
175
+ responseType: ResponseKind;
176
+ }
177
+
178
+ /** Detected spec flavor and its literal version string (e.g. openapi "3.0.3"). */
179
+ interface SpecVersion {
180
+ type: "swagger" | "openapi";
181
+ version: string;
182
+ }
183
+ /**
184
+ * Version-free view of a parsed spec: Swagger 2.0 vs OpenAPI 3.x differences
185
+ * are resolved once at normalization time. Generators consume this instead of
186
+ * touching the raw SwaggerSpec.
187
+ */
188
+ interface NormalizedSpec {
189
+ version: SpecVersion | null;
190
+ /** 2.0 `definitions` or 3.x `components.schemas`, whichever the spec has */
191
+ definitions: Record<string, SwaggerDefinition>;
192
+ operations: NormalizedOperation[];
193
+ /** Lookup for "#/definitions/X" and "#/components/schemas/X" style refs */
102
194
  resolveReference(ref: string): SwaggerDefinition | undefined;
103
- getAllDefinitionNames(): string[];
104
- getSpec(): SwaggerSpec;
105
- getPaths(): Record<string, any>;
106
- isValidSpec(): boolean;
107
- getSpecVersion(): {
108
- type: "swagger" | "openapi";
109
- version: string;
110
- } | null;
111
195
  }
112
196
 
197
+ /**
198
+ * Everything a plugin generator receives from the orchestrator.
199
+ *
200
+ * Plugins consume the version-free NormalizedSpec — never the raw spec or the
201
+ * SwaggerParser — and emit through the shared ts-morph Project so the
202
+ * orchestrator can track written files.
203
+ */
204
+ interface PluginGeneratorContext {
205
+ /** Version-free spec model; $refs resolved, per-operation fields precomputed. */
206
+ spec: NormalizedSpec;
207
+ /** Shared ts-morph project all generators emit through. */
208
+ project: Project;
209
+ /** Full user-facing config; plugins should read only the slice they need. */
210
+ config: GeneratorConfig;
211
+ /** Sink for non-fatal diagnostics — plugins must never log directly. */
212
+ onWarning?: (message: string) => void;
213
+ }
214
+ /**
215
+ * Constructor contract for plugin generator classes (what GeneratorConfig.plugins accepts).
216
+ */
217
+ interface IPluginGeneratorClass {
218
+ new (context: PluginGeneratorContext): IPluginGenerator;
219
+ }
113
220
  /**
114
221
  * Interface for generator instances
115
222
  */
116
223
  interface IPluginGenerator {
117
224
  /**
118
- * Generate code files
225
+ * Generate code files under the given output root.
119
226
  */
120
227
  generate(outputRoot: string): Promise<void>;
121
228
  }
122
229
 
230
+ /**
231
+ * The user-facing configuration (config file or programmatic call).
232
+ * Validated at the boundary by validateGeneratorConfig, which throws
233
+ * ConfigValidationError listing every problem at once — see
234
+ * https://ng-openapi.dev for full option documentation.
235
+ */
123
236
  interface GeneratorConfig {
237
+ /** Path or http(s) URL of the OpenAPI 3.x / Swagger 2.x spec (.json/.yaml/.yml). */
124
238
  input: string;
239
+ /** Output directory; created if missing. */
125
240
  output: string;
241
+ /** Distinguishes tokens/providers when several clients coexist in one app. */
126
242
  clientName?: string;
243
+ /** Custom acceptance check run on the parsed spec; returning false aborts generation. */
127
244
  validateInput?: (spec: SwaggerSpec) => boolean;
128
245
  options: {
246
+ /** How date/date-time formats are typed in generated models. */
129
247
  dateType: "string" | "Date";
248
+ /** Emit TS enums or literal-union types for spec enums. */
130
249
  enumStyle: "enum" | "union";
131
250
  validation?: {
251
+ /** Adds a `parse` hook to RequestOptions for response validation. */
132
252
  response?: boolean;
133
253
  };
254
+ /** Set false to generate models only. Default: true. */
134
255
  generateServices?: boolean;
256
+ /** Read enum member names from JSON-encoded descriptions (see EnumValueObject). */
135
257
  generateEnumBasedOnDescription?: boolean;
258
+ /** Default headers added to every request when not already present. */
136
259
  customHeaders?: Record<string, string>;
260
+ /** Pin the Angular responseType per response content type. */
137
261
  responseTypeMapping?: {
138
262
  [contentType: string]: "json" | "blob" | "arraybuffer" | "text";
139
263
  };
264
+ /** Derive method names from operationIds; throws when an operation has none. */
140
265
  customizeMethodName?: (operationId: string) => string;
266
+ /** Collapse each method's parameters into a single request object. */
141
267
  useSingleRequestParameter?: boolean;
142
268
  };
269
+ /** Overrides for the ts-morph compiler settings used during generation. */
143
270
  compilerOptions?: {
144
271
  declaration?: boolean;
145
272
  target?: ScriptTarget;
146
273
  module?: ModuleKind;
147
274
  strict?: boolean;
148
275
  };
149
- plugins?: (new (...args: any) => IPluginGenerator)[];
276
+ /** Plugin generator classes, run after core generation (see PluginGeneratorContext). */
277
+ plugins?: IPluginGeneratorClass[];
150
278
  }
151
279
 
152
280
  declare class HttpResourceGenerator implements IPluginGenerator {
153
281
  private project;
154
- private parser;
155
282
  private spec;
156
283
  private config;
157
284
  private methodGenerator;
158
285
  private indexGenerator;
159
- constructor(parser: SwaggerParser, project: Project, config: GeneratorConfig);
286
+ private readonly onWarning?;
287
+ constructor(context: PluginGeneratorContext);
160
288
  generate(outputRoot: string): Promise<void>;
161
289
  private groupPathsByController;
162
290
  private generateServiceFile;