@goast/core 0.7.0 → 0.7.1

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 (85) hide show
  1. package/dist/src/ast/node.d.ts +43 -0
  2. package/dist/src/ast/node.js +29 -0
  3. package/dist/src/codegen/config.d.ts +9 -0
  4. package/dist/src/codegen/config.js +4 -0
  5. package/dist/src/codegen/endpoints-generator.d.ts +32 -0
  6. package/dist/src/codegen/endpoints-generator.js +19 -0
  7. package/dist/src/codegen/generator.d.ts +108 -0
  8. package/dist/src/codegen/generator.js +103 -0
  9. package/dist/src/codegen/internal-utils.d.ts +8 -0
  10. package/dist/src/codegen/internal-utils.js +8 -0
  11. package/dist/src/codegen/schemas-generator.d.ts +33 -0
  12. package/dist/src/codegen/schemas-generator.js +20 -0
  13. package/dist/src/codegen/services-generator.d.ts +24 -0
  14. package/dist/src/codegen/services-generator.js +11 -0
  15. package/dist/src/codegen/types.d.ts +56 -0
  16. package/dist/src/collect/collector.d.ts +8 -0
  17. package/dist/src/collect/collector.js +8 -0
  18. package/dist/src/collect/helpers.d.ts +16 -0
  19. package/dist/src/collect/helpers.js +16 -0
  20. package/dist/src/collect/types.d.ts +28 -0
  21. package/dist/src/internal-utils.d.ts +7 -0
  22. package/dist/src/internal-utils.js +7 -0
  23. package/dist/src/parse/deref-proxy.d.ts +19 -0
  24. package/dist/src/parse/deref-proxy.js +19 -0
  25. package/dist/src/parse/openapi-types.d.ts +168 -0
  26. package/dist/src/parse/parser.d.ts +38 -0
  27. package/dist/src/parse/parser.js +38 -0
  28. package/dist/src/parse/types.d.ts +37 -0
  29. package/dist/src/parse/types.js +4 -0
  30. package/dist/src/transform/api-types.d.ts +408 -0
  31. package/dist/src/transform/helpers.d.ts +73 -0
  32. package/dist/src/transform/helpers.js +73 -0
  33. package/dist/src/transform/transform-document.d.ts +7 -0
  34. package/dist/src/transform/transform-document.js +7 -0
  35. package/dist/src/transform/transform-endpoint.d.ts +8 -0
  36. package/dist/src/transform/transform-endpoint.js +8 -0
  37. package/dist/src/transform/transform-schema.d.ts +10 -0
  38. package/dist/src/transform/transform-schema.js +10 -0
  39. package/dist/src/transform/transformer.d.ts +7 -0
  40. package/dist/src/transform/transformer.js +7 -0
  41. package/dist/src/transform/types.d.ts +61 -0
  42. package/dist/src/transform/types.js +4 -0
  43. package/dist/src/utils/action-provider.d.ts +38 -0
  44. package/dist/src/utils/action-provider.js +38 -0
  45. package/dist/src/utils/api-data.utils.d.ts +9 -0
  46. package/dist/src/utils/api-data.utils.js +9 -0
  47. package/dist/src/utils/array.utils.d.ts +7 -0
  48. package/dist/src/utils/array.utils.js +7 -0
  49. package/dist/src/utils/asset-manager.d.ts +22 -0
  50. package/dist/src/utils/asset-manager.js +22 -0
  51. package/dist/src/utils/class.utils.d.ts +22 -0
  52. package/dist/src/utils/class.utils.js +22 -0
  53. package/dist/src/utils/common.utils.d.ts +81 -0
  54. package/dist/src/utils/common.utils.js +81 -0
  55. package/dist/src/utils/condition.d.ts +22 -0
  56. package/dist/src/utils/endpoint.utils.d.ts +12 -0
  57. package/dist/src/utils/endpoint.utils.js +12 -0
  58. package/dist/src/utils/error.utils.d.ts +6 -0
  59. package/dist/src/utils/error.utils.js +6 -0
  60. package/dist/src/utils/factory.d.ts +45 -0
  61. package/dist/src/utils/factory.js +45 -0
  62. package/dist/src/utils/file-system.utils.d.ts +37 -0
  63. package/dist/src/utils/file-system.utils.js +20 -0
  64. package/dist/src/utils/generator.utils.d.ts +7 -0
  65. package/dist/src/utils/generator.utils.js +7 -0
  66. package/dist/src/utils/object.utils.d.ts +16 -0
  67. package/dist/src/utils/object.utils.js +16 -0
  68. package/dist/src/utils/schema-factory.d.ts +197 -0
  69. package/dist/src/utils/schema-factory.js +146 -0
  70. package/dist/src/utils/schema.utils.d.ts +15 -0
  71. package/dist/src/utils/schema.utils.js +15 -0
  72. package/dist/src/utils/source-builder.d.ts +103 -0
  73. package/dist/src/utils/source-builder.js +58 -0
  74. package/dist/src/utils/string-builder/string-builder.d.ts +24 -0
  75. package/dist/src/utils/string-builder/string-builder.js +11 -0
  76. package/dist/src/utils/string-builder/utils.d.ts +68 -0
  77. package/dist/src/utils/string-builder/utils.js +33 -0
  78. package/dist/src/utils/string.utils.d.ts +78 -0
  79. package/dist/src/utils/string.utils.js +64 -0
  80. package/dist/src/utils/string.utils.types.d.ts +34 -0
  81. package/dist/src/utils/type.utils.d.ts +132 -0
  82. package/dist/src/utils/type.utils.js +19 -0
  83. package/dist/src/utils/yaml-info.d.ts +35 -0
  84. package/dist/src/utils/yaml-info.js +25 -0
  85. package/package.json +1 -1
@@ -8,6 +8,15 @@ export declare function asTypeArray(type: string | string[] | undefined | null):
8
8
  export declare function withoutNullType(types: (string | null)[]): string[];
9
9
  /** Whether the `type` keyword declares `null`, either as a scalar type or as a member of a 3.1 type array. */
10
10
  export declare function hasNullType(type: string | string[] | undefined | null): boolean;
11
+ /**
12
+ * Determines the `ApiSchemaKind` of a schema from its keywords. `oneOf` is checked first, then `allOf`/`anyOf`
13
+ * (unless the schema is typed `object`), then type arrays, then the scalar `type`. A schema with `allOf`/`anyOf`
14
+ * is `object` if it also declares properties and `combined` otherwise.
15
+ * @template T The schema-like object type that provides the keywords the kind is derived from.
16
+ * @param ctx The transformer context, whose `unknownTypeBehavior` decides schemas without a recognised type.
17
+ * @param schema The schema to classify.
18
+ * @returns The kind of the schema.
19
+ */
11
20
  export declare function determineSchemaKind<T extends {
12
21
  oneOf?: unknown;
13
22
  allOf?: unknown;
@@ -16,6 +25,13 @@ export declare function determineSchemaKind<T extends {
16
25
  properties?: Record<string, unknown>;
17
26
  additionalProperties?: unknown;
18
27
  }>(ctx: OpenApiTransformerContext, schema: T): ApiSchemaKind;
28
+ /**
29
+ * Derives the name of a schema. A `title` takes precedence. Otherwise the name comes from the schema's place in the
30
+ * document: its component or `definitions` key, a parameter, response or request body, or its parent property.
31
+ * @param schema The schema with its `$src` location and optional `title`.
32
+ * @param id The generated identifier, used as the name when nothing better can be derived.
33
+ * @returns The name, and whether it was generated rather than taken from the document.
34
+ */
19
35
  export declare function determineSchemaName(schema: {
20
36
  title?: string;
21
37
  $src: DerefSource<unknown>;
@@ -23,10 +39,22 @@ export declare function determineSchemaName(schema: {
23
39
  name: string;
24
40
  isGenerated: boolean;
25
41
  };
42
+ /**
43
+ * Maps the `readOnly` and `writeOnly` flags of a schema to its accessibility. Setting both flags yields `none`.
44
+ * @param schema The schema whose flags are read.
45
+ * @returns The accessibility of the schema.
46
+ */
26
47
  export declare function determineSchemaAccessibility(schema: {
27
48
  readOnly?: boolean;
28
49
  writeOnly?: boolean;
29
50
  }): ApiSchemaAccessibility;
51
+ /**
52
+ * Narrows an accessibility that is already known by the flags of another schema, such as one reached through `allOf`.
53
+ * A flag that contradicts the current accessibility yields `none`, and `all` takes the flags of the schema.
54
+ * @param accessibility The accessibility determined so far.
55
+ * @param schema The schema whose flags are merged in.
56
+ * @returns The combined accessibility.
57
+ */
30
58
  export declare function updateSchemaAccessibility(accessibility: ApiSchemaAccessibility, schema: {
31
59
  readOnly?: boolean;
32
60
  writeOnly?: boolean;
@@ -34,7 +62,20 @@ export declare function updateSchemaAccessibility(accessibility: ApiSchemaAccess
34
62
  type CustomFields<T extends Record<string, unknown>> = {
35
63
  [K in keyof T as K extends `x-${string}` ? K : never]: T[K];
36
64
  };
65
+ /**
66
+ * Collects the `x-` extension fields of an OpenAPI object. The prefix is removed from each key, so `x-foo`
67
+ * becomes `foo`.
68
+ * @template T The type of the OpenAPI object.
69
+ * @param schema The OpenAPI object to read the extensions from.
70
+ * @returns The extensions, keyed by their name without the `x-` prefix.
71
+ */
37
72
  export declare function getCustomFields<T extends Record<string, unknown>>(schema: T): CustomFields<T>;
73
+ /**
74
+ * Determines the name of an endpoint. The `operationId` is used if present. Otherwise the name is built from the method
75
+ * and the path, with `{param}` turned into `:param` and slashes into underscores, such as `get_pets_:id`.
76
+ * @param endpointInfo The method, path and operation of the endpoint.
77
+ * @returns The name of the endpoint.
78
+ */
38
79
  export declare function determineEndpointName(endpointInfo: {
39
80
  method: string;
40
81
  path: string;
@@ -42,16 +83,48 @@ export declare function determineEndpointName(endpointInfo: {
42
83
  operationId?: string;
43
84
  };
44
85
  }): string;
86
+ /**
87
+ * Transforms the `additionalProperties` keyword. A boolean is returned as written and a schema is transformed.
88
+ * @template TAdditionalProperties The raw type of the `additionalProperties` keyword.
89
+ * @param context The transformer context.
90
+ * @param schema The schema that may declare `additionalProperties`.
91
+ * @param transformSchema The function that transforms a schema object.
92
+ * @returns `undefined` if the keyword is absent, the boolean as written, or the transformed schema.
93
+ */
45
94
  export declare function transformAdditionalProperties<TAdditionalProperties>(context: OpenApiTransformerContext, schema: {
46
95
  additionalProperties?: TAdditionalProperties;
47
96
  }, transformSchema: (context: OpenApiTransformerContext, schema: Exclude<TAdditionalProperties, undefined | boolean>) => ApiSchema): boolean | ApiSchema | undefined;
97
+ /**
98
+ * Transforms the `properties` of a schema into a map keyed by property name. Keys starting with `$` or `x-`
99
+ * are skipped.
100
+ * @template TProperties The raw type of a property schema.
101
+ * @param context The transformer context.
102
+ * @param schema The schema that may declare `properties`.
103
+ * @param transformSchema The function that transforms a property schema.
104
+ * @returns The transformed properties, or an empty map if the schema declares none.
105
+ */
48
106
  export declare function transformSchemaProperties<TProperties>(context: OpenApiTransformerContext, schema: {
49
107
  properties?: Record<string, TProperties>;
50
108
  required?: string[];
51
109
  }, transformSchema: (context: OpenApiTransformerContext, schema: TProperties) => ApiSchema): Map<string, ApiSchemaProperty>;
110
+ /**
111
+ * Generates identifiers of the form `<name>-<n>`. The counter is kept per name, so identifiers are unique within one
112
+ * transform run.
113
+ */
52
114
  export declare class IdGenerator {
53
115
  private readonly _idMap;
116
+ /**
117
+ * Generates the next identifier for the given name.
118
+ * @param name The prefix of the identifier, such as `schema` or `endpoint`.
119
+ * @returns The next identifier, such as `schema-1` and then `schema-2`.
120
+ */
54
121
  generateId(name: string): string;
55
122
  }
123
+ /**
124
+ * Builds the key that identifies an OpenAPI object by its source file and JSON path. The transformer uses it to reuse
125
+ * the entries it has already created for the same object.
126
+ * @param obj The OpenAPI object to identify.
127
+ * @returns The key, in the form `file#path`.
128
+ */
56
129
  export declare function getOpenApiObjectIdentifier(obj: Deref<OpenApiObject<string>>): string;
57
130
  export {};
@@ -21,6 +21,15 @@ export function withoutNullType(types) {
21
21
  export function hasNullType(type) {
22
22
  return asTypeArray(type).some(isNullType);
23
23
  }
24
+ /**
25
+ * Determines the `ApiSchemaKind` of a schema from its keywords. `oneOf` is checked first, then `allOf`/`anyOf`
26
+ * (unless the schema is typed `object`), then type arrays, then the scalar `type`. A schema with `allOf`/`anyOf`
27
+ * is `object` if it also declares properties and `combined` otherwise.
28
+ * @template T The schema-like object type that provides the keywords the kind is derived from.
29
+ * @param ctx The transformer context, whose `unknownTypeBehavior` decides schemas without a recognised type.
30
+ * @param schema The schema to classify.
31
+ * @returns The kind of the schema.
32
+ */
24
33
  export function determineSchemaKind(ctx, schema) {
25
34
  // An OpenAPI 3.1 type array that reduces to at most one non-null type is normalised away by
26
35
  // `transformSchema`, which replaces `type` with the narrowed scalar (or `'null'`) and then asks this
@@ -58,6 +67,13 @@ export function determineSchemaKind(ctx, schema) {
58
67
  Object.keys(schema.properties).length > 0);
59
68
  return treadAsObject ? 'object' : 'unknown';
60
69
  }
70
+ /**
71
+ * Derives the name of a schema. A `title` takes precedence. Otherwise the name comes from the schema's place in the
72
+ * document: its component or `definitions` key, a parameter, response or request body, or its parent property.
73
+ * @param schema The schema with its `$src` location and optional `title`.
74
+ * @param id The generated identifier, used as the name when nothing better can be derived.
75
+ * @returns The name, and whether it was generated rather than taken from the document.
76
+ */
61
77
  export function determineSchemaName(schema, id) {
62
78
  if (schema.title)
63
79
  return { name: schema.title, isGenerated: false };
@@ -119,6 +135,11 @@ export function determineSchemaName(schema, id) {
119
135
  }
120
136
  return { name: id, isGenerated: true };
121
137
  }
138
+ /**
139
+ * Maps the `readOnly` and `writeOnly` flags of a schema to its accessibility. Setting both flags yields `none`.
140
+ * @param schema The schema whose flags are read.
141
+ * @returns The accessibility of the schema.
142
+ */
122
143
  export function determineSchemaAccessibility(schema) {
123
144
  if (schema.readOnly === true) {
124
145
  return schema.writeOnly === true ? 'none' : 'readOnly';
@@ -127,6 +148,13 @@ export function determineSchemaAccessibility(schema) {
127
148
  return schema.writeOnly === true ? 'writeOnly' : 'all';
128
149
  }
129
150
  }
151
+ /**
152
+ * Narrows an accessibility that is already known by the flags of another schema, such as one reached through `allOf`.
153
+ * A flag that contradicts the current accessibility yields `none`, and `all` takes the flags of the schema.
154
+ * @param accessibility The accessibility determined so far.
155
+ * @param schema The schema whose flags are merged in.
156
+ * @returns The combined accessibility.
157
+ */
130
158
  export function updateSchemaAccessibility(accessibility, schema) {
131
159
  if (accessibility === 'none')
132
160
  return 'none';
@@ -136,6 +164,13 @@ export function updateSchemaAccessibility(accessibility, schema) {
136
164
  return schema.readOnly === true ? 'none' : 'writeOnly';
137
165
  return determineSchemaAccessibility(schema);
138
166
  }
167
+ /**
168
+ * Collects the `x-` extension fields of an OpenAPI object. The prefix is removed from each key, so `x-foo`
169
+ * becomes `foo`.
170
+ * @template T The type of the OpenAPI object.
171
+ * @param schema The OpenAPI object to read the extensions from.
172
+ * @returns The extensions, keyed by their name without the `x-` prefix.
173
+ */
139
174
  export function getCustomFields(schema) {
140
175
  const result = {};
141
176
  for (const key in schema) {
@@ -146,11 +181,25 @@ export function getCustomFields(schema) {
146
181
  }
147
182
  return result;
148
183
  }
184
+ /**
185
+ * Determines the name of an endpoint. The `operationId` is used if present. Otherwise the name is built from the method
186
+ * and the path, with `{param}` turned into `:param` and slashes into underscores, such as `get_pets_:id`.
187
+ * @param endpointInfo The method, path and operation of the endpoint.
188
+ * @returns The name of the endpoint.
189
+ */
149
190
  export function determineEndpointName(endpointInfo) {
150
191
  if (endpointInfo.operation.operationId)
151
192
  return endpointInfo.operation.operationId;
152
193
  return endpointInfo.method + endpointInfo.path.replace(/\{([^}]+)\}/g, ':$1').replace(/\//g, '_');
153
194
  }
195
+ /**
196
+ * Transforms the `additionalProperties` keyword. A boolean is returned as written and a schema is transformed.
197
+ * @template TAdditionalProperties The raw type of the `additionalProperties` keyword.
198
+ * @param context The transformer context.
199
+ * @param schema The schema that may declare `additionalProperties`.
200
+ * @param transformSchema The function that transforms a schema object.
201
+ * @returns `undefined` if the keyword is absent, the boolean as written, or the transformed schema.
202
+ */
154
203
  export function transformAdditionalProperties(context, schema, transformSchema) {
155
204
  if (isNullish(schema.additionalProperties))
156
205
  return undefined;
@@ -158,6 +207,15 @@ export function transformAdditionalProperties(context, schema, transformSchema)
158
207
  return schema.additionalProperties;
159
208
  return transformSchema(context, schema.additionalProperties);
160
209
  }
210
+ /**
211
+ * Transforms the `properties` of a schema into a map keyed by property name. Keys starting with `$` or `x-`
212
+ * are skipped.
213
+ * @template TProperties The raw type of a property schema.
214
+ * @param context The transformer context.
215
+ * @param schema The schema that may declare `properties`.
216
+ * @param transformSchema The function that transforms a property schema.
217
+ * @returns The transformed properties, or an empty map if the schema declares none.
218
+ */
161
219
  export function transformSchemaProperties(context, schema, transformSchema) {
162
220
  const result = new Map();
163
221
  if (!schema.properties)
@@ -172,14 +230,29 @@ export function transformSchemaProperties(context, schema, transformSchema) {
172
230
  }
173
231
  return result;
174
232
  }
233
+ /**
234
+ * Generates identifiers of the form `<name>-<n>`. The counter is kept per name, so identifiers are unique within one
235
+ * transform run.
236
+ */
175
237
  export class IdGenerator {
176
238
  _idMap = new Map();
239
+ /**
240
+ * Generates the next identifier for the given name.
241
+ * @param name The prefix of the identifier, such as `schema` or `endpoint`.
242
+ * @returns The next identifier, such as `schema-1` and then `schema-2`.
243
+ */
177
244
  generateId(name) {
178
245
  const id = this._idMap.get(name) ?? 1;
179
246
  this._idMap.set(name, id + 1);
180
247
  return `${name}-${id}`;
181
248
  }
182
249
  }
250
+ /**
251
+ * Builds the key that identifies an OpenAPI object by its source file and JSON path. The transformer uses it to reuse
252
+ * the entries it has already created for the same object.
253
+ * @param obj The OpenAPI object to identify.
254
+ * @returns The key, in the form `file#path`.
255
+ */
183
256
  export function getOpenApiObjectIdentifier(obj) {
184
257
  return obj.$src.file + '#' + obj.$src.path;
185
258
  }
@@ -1,4 +1,11 @@
1
1
  import type { OpenApiDocument } from '../parse/openapi-types.ts';
2
2
  import type { Deref } from '../parse/types.ts';
3
3
  import type { OpenApiTransformerContext } from './types.ts';
4
+ /**
5
+ * Transforms the top-level `tags` of an OpenAPI document into services. Each tag becomes a service named after it,
6
+ * keeping the tag's `description`. Operations are attached to these services later, by tag name, in
7
+ * `transformEndpoint`.
8
+ * @param context The transformer context that collects the services.
9
+ * @param document The document whose tags are transformed.
10
+ */
4
11
  export declare function transformDocument(context: OpenApiTransformerContext, document: Deref<OpenApiDocument>): void;
@@ -1,4 +1,11 @@
1
1
  import { getOpenApiObjectIdentifier } from './helpers.js';
2
+ /**
3
+ * Transforms the top-level `tags` of an OpenAPI document into services. Each tag becomes a service named after it,
4
+ * keeping the tag's `description`. Operations are attached to these services later, by tag name, in
5
+ * `transformEndpoint`.
6
+ * @param context The transformer context that collects the services.
7
+ * @param document The document whose tags are transformed.
8
+ */
2
9
  export function transformDocument(context, document) {
3
10
  for (const tag of document.tags ?? []) {
4
11
  transformTag(context, tag);
@@ -1,4 +1,12 @@
1
1
  import type { OpenApiCollectorEndpointInfo } from '../collect/types.ts';
2
2
  import type { ApiEndpoint } from './api-types.ts';
3
3
  import type { OpenApiTransformerContext } from './types.ts';
4
+ /**
5
+ * Transforms one operation into an `ApiEndpoint` and registers it with its path and with the service of each of its
6
+ * tags. Operations without tags go to a service with a generated name. Tags that no document declares create implicit
7
+ * services, which have no `$src`.
8
+ * @param context The transformer context.
9
+ * @param endpointInfo The collected path, method, path item and operation.
10
+ * @returns The transformed endpoint.
11
+ */
4
12
  export declare function transformEndpoint(context: OpenApiTransformerContext, endpointInfo: OpenApiCollectorEndpointInfo): ApiEndpoint;
@@ -1,6 +1,14 @@
1
1
  import { isOpenApiObjectProperty } from '../internal-utils.js';
2
2
  import { determineEndpointName, getCustomFields, getOpenApiObjectIdentifier } from './helpers.js';
3
3
  import { transformSchema } from './transform-schema.js';
4
+ /**
5
+ * Transforms one operation into an `ApiEndpoint` and registers it with its path and with the service of each of its
6
+ * tags. Operations without tags go to a service with a generated name. Tags that no document declares create implicit
7
+ * services, which have no `$src`.
8
+ * @param context The transformer context.
9
+ * @param endpointInfo The collected path, method, path item and operation.
10
+ * @returns The transformed endpoint.
11
+ */
4
12
  export function transformEndpoint(context, endpointInfo) {
5
13
  const apiPath = transformApiPath(context, endpointInfo.path, endpointInfo.pathItem);
6
14
  const endpoint = {
@@ -2,4 +2,14 @@ import type { OpenApiSchema } from '../parse/openapi-types.ts';
2
2
  import type { Deref } from '../parse/types.ts';
3
3
  import type { ApiSchema } from './api-types.ts';
4
4
  import type { OpenApiTransformerContext } from './types.ts';
5
+ /**
6
+ * Transforms an OpenAPI schema into an `ApiSchema`, together with every schema it references through `$ref`, `items`,
7
+ * `properties`, `allOf` and the other keywords. Each source schema is transformed once and then reused. A type array
8
+ * that narrows to one non-null type is normalized to that type, and its `null` member sets `nullable` instead.
9
+ * @template T The type of the source schema object.
10
+ * @param ctx The transformer context.
11
+ * @param schema The schema to transform.
12
+ * @returns The transformed schema.
13
+ * @throws Error if no schema is given.
14
+ */
5
15
  export declare function transformSchema<T extends Deref<OpenApiSchema>>(ctx: OpenApiTransformerContext, schema: T): ApiSchema;
@@ -1,5 +1,15 @@
1
1
  import { createOverwriteProxy } from '../utils/object.utils.js';
2
2
  import { asTypeArray, determineSchemaAccessibility, determineSchemaKind, determineSchemaName, getCustomFields, getOpenApiObjectIdentifier, hasNullType, transformAdditionalProperties, transformSchemaProperties, withoutNullType, } from './helpers.js';
3
+ /**
4
+ * Transforms an OpenAPI schema into an `ApiSchema`, together with every schema it references through `$ref`, `items`,
5
+ * `properties`, `allOf` and the other keywords. Each source schema is transformed once and then reused. A type array
6
+ * that narrows to one non-null type is normalized to that type, and its `null` member sets `nullable` instead.
7
+ * @template T The type of the source schema object.
8
+ * @param ctx The transformer context.
9
+ * @param schema The schema to transform.
10
+ * @returns The transformed schema.
11
+ * @throws Error if no schema is given.
12
+ */
3
13
  export function transformSchema(ctx, schema) {
4
14
  if (!schema) {
5
15
  throw new Error('Schema is required.');
@@ -1,4 +1,11 @@
1
1
  import type { OpenApiCollectorData } from '../collect/types.ts';
2
2
  import type { ApiData } from './api-types.ts';
3
3
  import { type OpenApiTransformerOptions } from './types.ts';
4
+ /**
5
+ * Transforms collected OpenAPI documents into the normalized `ApiData` model that the generators consume.
6
+ * Documents are processed first, so their tags become services, then all collected schemas, then all endpoints.
7
+ * @param data The documents, schemas and endpoints gathered by the collector.
8
+ * @param options Transformer options. Values that are not given fall back to the defaults.
9
+ * @returns The transformed documents, services, endpoints and schemas.
10
+ */
4
11
  export declare function transformOpenApi(data: OpenApiCollectorData, options?: Partial<OpenApiTransformerOptions>): ApiData;
@@ -3,6 +3,13 @@ import { transformDocument } from './transform-document.js';
3
3
  import { transformEndpoint } from './transform-endpoint.js';
4
4
  import { transformSchema } from './transform-schema.js';
5
5
  import { defaultOpenApiTransformerOptions, } from './types.js';
6
+ /**
7
+ * Transforms collected OpenAPI documents into the normalized `ApiData` model that the generators consume.
8
+ * Documents are processed first, so their tags become services, then all collected schemas, then all endpoints.
9
+ * @param data The documents, schemas and endpoints gathered by the collector.
10
+ * @param options Transformer options. Values that are not given fall back to the defaults.
11
+ * @returns The transformed documents, services, endpoints and schemas.
12
+ */
6
13
  export function transformOpenApi(data, options) {
7
14
  const context = {
8
15
  config: { ...defaultOpenApiTransformerOptions, ...options },
@@ -2,6 +2,10 @@ import type { OpenApiCollectorData } from '../collect/types.ts';
2
2
  import type { ArrayItem } from '../utils/type.utils.ts';
3
3
  import type { ApiContent, ApiData, ApiHeader, ApiParameter, ApiPath, ApiRequestBody, ApiResponse, ApiSchema, ApiSchemaExtensions, ApiSchemaKind, ApiService } from './api-types.ts';
4
4
  import type { IdGenerator } from './helpers.ts';
5
+ /**
6
+ * Options that control how OpenAPI documents are transformed into the `ApiData` model.
7
+ * @see transformOpenApi
8
+ */
5
9
  export type OpenApiTransformerOptions = {
6
10
  /**
7
11
  * Determines how schemas without a `type` property should be treated.
@@ -12,22 +16,79 @@ export type OpenApiTransformerOptions = {
12
16
  */
13
17
  unknownTypeBehavior: 'keep-unknown' | 'object-if-properties' | 'always-object';
14
18
  };
19
+ /**
20
+ * The default transformer options, used for every option that the caller does not set.
21
+ * @see OpenApiTransformerOptions
22
+ */
15
23
  export declare const defaultOpenApiTransformerOptions: OpenApiTransformerOptions;
24
+ /**
25
+ * A schema whose kind-specific fields are not filled in yet. It is registered while the schema's own sub-schemas are
26
+ * being transformed, so that circular references resolve to the same instance.
27
+ * @see ApiSchema
28
+ */
16
29
  export type IncompleteApiSchema = Omit<ApiSchema, Exclude<keyof ApiSchemaExtensions<ApiSchemaKind>, 'kind'>>;
30
+ /**
31
+ * The mutable state that all transform steps of one `transformOpenApi` call share.
32
+ * @see transformOpenApi
33
+ */
17
34
  export type OpenApiTransformerContext = {
35
+ /**
36
+ * The transformer options in effect for this run.
37
+ */
18
38
  config: OpenApiTransformerOptions;
39
+ /**
40
+ * Generates the identifiers of the entries created during this run.
41
+ */
19
42
  idGenerator: IdGenerator;
43
+ /**
44
+ * The collected documents, schemas and endpoints that are being transformed.
45
+ */
20
46
  input: OpenApiCollectorData;
47
+ /**
48
+ * Schemas that are currently being transformed, keyed by `file:path`. A circular reference finds its entry here
49
+ * instead of recursing forever.
50
+ */
21
51
  incompleteSchemas: Map<string, IncompleteApiSchema>;
52
+ /**
53
+ * The path entries created so far, keyed by path template. All operations of one path share one entry.
54
+ */
22
55
  paths: Map<string, ApiPath>;
56
+ /**
57
+ * Entries already created for each source object, keyed by `getOpenApiObjectIdentifier`, so that every object is
58
+ * transformed only once.
59
+ */
23
60
  transformed: {
61
+ /**
62
+ * The services created from tags, keyed by source object identifier.
63
+ */
24
64
  services: Map<string, ApiService>;
65
+ /**
66
+ * The path entries created from path items, keyed by source object identifier.
67
+ */
25
68
  paths: Map<string, ApiPath>;
69
+ /**
70
+ * The parameters, keyed by source object identifier.
71
+ */
26
72
  parameters: Map<string, ApiParameter>;
73
+ /**
74
+ * The request bodies, keyed by source object identifier.
75
+ */
27
76
  requestBodies: Map<string, ApiRequestBody>;
77
+ /**
78
+ * The responses, keyed by source object identifier.
79
+ */
28
80
  responses: Map<string, ApiResponse>;
81
+ /**
82
+ * The media type entries, keyed by source object identifier.
83
+ */
29
84
  content: Map<string, ApiContent>;
85
+ /**
86
+ * The headers, keyed by source object identifier.
87
+ */
30
88
  headers: Map<string, ApiHeader>;
89
+ /**
90
+ * The schemas, keyed by source object identifier.
91
+ */
31
92
  schemas: Map<string, ApiSchema>;
32
93
  };
33
94
  } & {
@@ -1,3 +1,7 @@
1
+ /**
2
+ * The default transformer options, used for every option that the caller does not set.
3
+ * @see OpenApiTransformerOptions
4
+ */
1
5
  export const defaultOpenApiTransformerOptions = {
2
6
  unknownTypeBehavior: 'object-if-properties',
3
7
  };
@@ -17,12 +17,50 @@ type ValueActionRunner<T, TAction> = {
17
17
  functionName: MatchingFunctionNames<T, TAction>;
18
18
  };
19
19
  type ActionRunner<TAction> = CtorActionRunner<any, TAction> | FnActionRunner<TAction> | ValueActionRunner<any, TAction>;
20
+ /**
21
+ * Provides an action that can be run later. The action is either a method of a class that is instantiated for each run,
22
+ * a plain function, or a method of an object that is shared across runs.
23
+ * @template TAction The signature of the action.
24
+ */
20
25
  export declare class ActionProvider<TAction extends (...args: any[]) => any> {
21
26
  private readonly _runner;
27
+ /**
28
+ * Initializes a new instance of the ActionProvider class. Prefer the static `fromType`, `fromFn` and `fromValue`
29
+ * methods.
30
+ * @param runner The runner that determines how the action is invoked.
31
+ */
22
32
  constructor(runner: ActionRunner<TAction>);
33
+ /**
34
+ * Runs the action with the specified arguments. For class-based providers, a new instance is created on each run.
35
+ * @param args The arguments to pass to the action.
36
+ * @returns The result of the action.
37
+ */
23
38
  run(...args: Parameters<TAction>): ReturnType<TAction>;
39
+ /**
40
+ * Creates an action provider that calls a method on a new instance of the specified class each time it runs.
41
+ * @template T The type of the class instance.
42
+ * @template TName The name of the method to call.
43
+ * @param type The class to instantiate. Its constructor must not require arguments.
44
+ * @param functionName The name of the method to call on the instance.
45
+ * @returns A new action provider.
46
+ */
24
47
  static fromType<T, TName extends FunctionNames<T>>(type: EmptyConstructor<T>, functionName: TName): ActionProvider<T[TName] extends (...args: any[]) => any ? T[TName] : never>;
48
+ /**
49
+ * Creates an action provider that calls the specified function each time it runs.
50
+ * @template TAction The signature of the function.
51
+ * @param fn The function to call.
52
+ * @returns A new action provider.
53
+ */
25
54
  static fromFn<TAction extends (...args: any[]) => any>(fn: TAction): ActionProvider<TAction>;
55
+ /**
56
+ * Creates an action provider that calls a method on the specified object each time it runs. The object is shared
57
+ * across runs.
58
+ * @template T The type of the object.
59
+ * @template TName The name of the method to call.
60
+ * @param value The object that contains the method.
61
+ * @param functionName The name of the method to call on the object.
62
+ * @returns A new action provider.
63
+ */
26
64
  static fromValue<T, TName extends FunctionNames<T>>(value: T, functionName: TName): ActionProvider<T[TName] extends (...args: any[]) => any ? T[TName] : never>;
27
65
  }
28
66
  export {};
@@ -1,8 +1,23 @@
1
+ /**
2
+ * Provides an action that can be run later. The action is either a method of a class that is instantiated for each run,
3
+ * a plain function, or a method of an object that is shared across runs.
4
+ * @template TAction The signature of the action.
5
+ */
1
6
  export class ActionProvider {
2
7
  _runner;
8
+ /**
9
+ * Initializes a new instance of the ActionProvider class. Prefer the static `fromType`, `fromFn` and `fromValue`
10
+ * methods.
11
+ * @param runner The runner that determines how the action is invoked.
12
+ */
3
13
  constructor(runner) {
4
14
  this._runner = runner;
5
15
  }
16
+ /**
17
+ * Runs the action with the specified arguments. For class-based providers, a new instance is created on each run.
18
+ * @param args The arguments to pass to the action.
19
+ * @returns The result of the action.
20
+ */
6
21
  run(...args) {
7
22
  switch (this._runner.kind) {
8
23
  case 'ctor':
@@ -13,12 +28,35 @@ export class ActionProvider {
13
28
  return this._runner.value[this._runner.functionName](...args);
14
29
  }
15
30
  }
31
+ /**
32
+ * Creates an action provider that calls a method on a new instance of the specified class each time it runs.
33
+ * @template T The type of the class instance.
34
+ * @template TName The name of the method to call.
35
+ * @param type The class to instantiate. Its constructor must not require arguments.
36
+ * @param functionName The name of the method to call on the instance.
37
+ * @returns A new action provider.
38
+ */
16
39
  static fromType(type, functionName) {
17
40
  return new ActionProvider({ kind: 'ctor', factory: type, functionName: functionName });
18
41
  }
42
+ /**
43
+ * Creates an action provider that calls the specified function each time it runs.
44
+ * @template TAction The signature of the function.
45
+ * @param fn The function to call.
46
+ * @returns A new action provider.
47
+ */
19
48
  static fromFn(fn) {
20
49
  return new ActionProvider({ kind: 'fn', action: fn });
21
50
  }
51
+ /**
52
+ * Creates an action provider that calls a method on the specified object each time it runs. The object is shared
53
+ * across runs.
54
+ * @template T The type of the object.
55
+ * @template TName The name of the method to call.
56
+ * @param value The object that contains the method.
57
+ * @param functionName The name of the method to call on the object.
58
+ * @returns A new action provider.
59
+ */
22
60
  static fromValue(value, functionName) {
23
61
  return new ActionProvider({ kind: 'value', value, functionName: functionName });
24
62
  }
@@ -1,2 +1,11 @@
1
1
  import type { ApiComponent, ApiData } from '../transform/api-types.ts';
2
+ /**
3
+ * Gets a display name for the source location of a component in the form `<relative file path>#<source path>`. The
4
+ * file path is relative to the common base path of all documents in `data`, which is computed on first use and cached
5
+ * on `data`.
6
+ * @template T The OpenAPI type of the component.
7
+ * @param data The API data that contains the documents of the component.
8
+ * @param component The component to get the display name for.
9
+ * @returns The display name of the component's source location.
10
+ */
2
11
  export declare function getSourceDisplayName<T>(data: ApiData, component: ApiComponent<T>): string;
@@ -1,6 +1,15 @@
1
1
  import { dirname, relative } from 'node:path';
2
2
  import process from 'node:process';
3
3
  const DOCUMENTS_BASE_PATH = Symbol('documentsBasePath');
4
+ /**
5
+ * Gets a display name for the source location of a component in the form `<relative file path>#<source path>`. The
6
+ * file path is relative to the common base path of all documents in `data`, which is computed on first use and cached
7
+ * on `data`.
8
+ * @template T The OpenAPI type of the component.
9
+ * @param data The API data that contains the documents of the component.
10
+ * @param component The component to get the display name for.
11
+ * @returns The display name of the component's source location.
12
+ */
4
13
  export function getSourceDisplayName(data, component) {
5
14
  let basePath = data[DOCUMENTS_BASE_PATH];
6
15
  if (basePath === undefined) {
@@ -1 +1,8 @@
1
+ /**
2
+ * Removes all items that match a predicate from an array. The array is modified in place.
3
+ * @template T The type of the items.
4
+ * @param array The array to remove items from.
5
+ * @param predicate The function that returns `true` for items to remove. It is called once per item, from the last item
6
+ * to the first.
7
+ */
1
8
  export declare function removeItems<T>(array: T[], predicate: (item: T) => boolean): void;
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Removes all items that match a predicate from an array. The array is modified in place.
3
+ * @template T The type of the items.
4
+ * @param array The array to remove items from.
5
+ * @param predicate The function that returns `true` for items to remove. It is called once per item, from the last item
6
+ * to the first.
7
+ */
1
8
  export function removeItems(array, predicate) {
2
9
  for (let i = array.length - 1; i >= 0; i--) {
3
10
  if (predicate(array[i])) {