@goast/core 0.7.0-beta.2 → 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
@@ -1,15 +1,58 @@
1
1
  import type { StringBuilder } from '../utils/string-builder/string-builder.ts';
2
2
  import type { AppendValue } from '../utils/string-builder/utils.ts';
3
3
  import type { Prettify } from '../utils/type.utils.ts';
4
+ /**
5
+ * The injection points of an AST node. For each name `X` in `TInjects`, the node accepts an optional `beforeX` and
6
+ * `afterX` value, such as `beforeName` for the name of a declaration. `before` and `after` are always available and
7
+ * wrap the whole node.
8
+ * @template TBuilder The type of the builder that the node is written to.
9
+ * @template TInjects The names of the injection points that the node supports, in addition to `before` and `after`.
10
+ * @see AstNode.write
11
+ */
4
12
  export type AstNodeInject<TBuilder extends StringBuilder, TInjects extends string> = {
5
13
  [K in `${'after' | 'before'}${Capitalize<TInjects>}`]?: AppendValue<TBuilder>;
6
14
  };
15
+ /**
16
+ * The options that are accepted by the constructor of an AST node class. They combine the options of the subclass with
17
+ * the options of its base class constructor, so that language packages can extend the options of their base nodes.
18
+ * @template TBase The class whose constructor options are included.
19
+ * @template TExtensions The additional options of the subclass.
20
+ */
7
21
  export type AstNodeOptions<TBase extends abstract new (...args: any) => any, TExtensions extends object = {}> = Prettify<TExtensions & ConstructorParameters<TBase>[0]>;
22
+ /**
23
+ * Base class for AST nodes that write themselves to a `StringBuilder`. The language packages build their nodes on it,
24
+ * for example `TsNode` in `@goast/typescript` and `KtNode` in `@goast/kotlin`.
25
+ *
26
+ * A node is written with `write`, which runs in three steps:
27
+ * 1. The node is pushed onto a history stack that is kept per builder. Nodes that are written while it is on the stack
28
+ * can find it through `getParentNode`, which allows a node to render differently depending on where it is placed.
29
+ * 2. The `before` injection is appended, followed by the node's own output, which the subclass renders in `onWrite`.
30
+ * 3. The `after` injection is appended, and the node is popped from the history stack again.
31
+ *
32
+ * Injection points let callers add custom content to a node without subclassing it. `before` and `after` wrap the whole
33
+ * node. The node-specific injections declared through `TInjects`, such as `beforeDecorators` or `afterBody`, are
34
+ * appended by the subclass around the matching part of its output. Injected values are appended like any other value,
35
+ * so they can be strings, builder functions or arrays.
36
+ * @template TBuilder The type of the builder that the node is written to.
37
+ * @template TInjects Additional names of custom injection points (`before*` and `after*`) accepted by this node.
38
+ */
8
39
  export declare abstract class AstNode<TBuilder extends StringBuilder = StringBuilder, TInjects extends string = never> {
40
+ /**
41
+ * The values that are injected before and after this node or its parts. See the class documentation for details.
42
+ */
9
43
  inject: AstNodeInject<TBuilder, TInjects | ''>;
44
+ /**
45
+ * Initializes a new instance of the AstNode class.
46
+ * @param options The options of the node. Only `inject` is read by this base class.
47
+ */
10
48
  constructor(options: {
11
49
  inject?: AstNode<TBuilder, TInjects>['inject'];
12
50
  });
51
+ /**
52
+ * Writes this node to the builder. The `before` injection wraps the output of the node, and the node is registered in
53
+ * the history of the builder while it is written.
54
+ * @param builder The builder to write the node to.
55
+ */
13
56
  write(builder: TBuilder): void;
14
57
  protected getParentNode(builder: TBuilder): AstNode | undefined;
15
58
  protected abstract onWrite(builder: TBuilder): void;
@@ -1,9 +1,38 @@
1
1
  const NODE_HISTORY_SYMBOL = Symbol('node-history');
2
+ /**
3
+ * Base class for AST nodes that write themselves to a `StringBuilder`. The language packages build their nodes on it,
4
+ * for example `TsNode` in `@goast/typescript` and `KtNode` in `@goast/kotlin`.
5
+ *
6
+ * A node is written with `write`, which runs in three steps:
7
+ * 1. The node is pushed onto a history stack that is kept per builder. Nodes that are written while it is on the stack
8
+ * can find it through `getParentNode`, which allows a node to render differently depending on where it is placed.
9
+ * 2. The `before` injection is appended, followed by the node's own output, which the subclass renders in `onWrite`.
10
+ * 3. The `after` injection is appended, and the node is popped from the history stack again.
11
+ *
12
+ * Injection points let callers add custom content to a node without subclassing it. `before` and `after` wrap the whole
13
+ * node. The node-specific injections declared through `TInjects`, such as `beforeDecorators` or `afterBody`, are
14
+ * appended by the subclass around the matching part of its output. Injected values are appended like any other value,
15
+ * so they can be strings, builder functions or arrays.
16
+ * @template TBuilder The type of the builder that the node is written to.
17
+ * @template TInjects Additional names of custom injection points (`before*` and `after*`) accepted by this node.
18
+ */
2
19
  export class AstNode {
20
+ /**
21
+ * The values that are injected before and after this node or its parts. See the class documentation for details.
22
+ */
3
23
  inject;
24
+ /**
25
+ * Initializes a new instance of the AstNode class.
26
+ * @param options The options of the node. Only `inject` is read by this base class.
27
+ */
4
28
  constructor(options) {
5
29
  this.inject = options.inject ?? {};
6
30
  }
31
+ /**
32
+ * Writes this node to the builder. The `before` injection wraps the output of the node, and the node is registered in
33
+ * the history of the builder while it is written.
34
+ * @param builder The builder to write the node to.
35
+ */
7
36
  write(builder) {
8
37
  let history = builder[NODE_HISTORY_SYMBOL];
9
38
  if (!history) {
@@ -1,5 +1,10 @@
1
1
  import type { OpenApiParserOptions } from '../parse/types.ts';
2
2
  import { type SourceBuilderOptions } from '../utils/source-builder.ts';
3
+ /**
4
+ * The configuration of the `OpenApiGenerator`. It combines the source builder options and the parser options
5
+ * with the output settings of the generator.
6
+ * @see OpenApiGenerator
7
+ */
3
8
  export type OpenApiGeneratorConfig = SourceBuilderOptions & OpenApiParserOptions & {
4
9
  /**
5
10
  * The directory where the generated files should be saved.
@@ -32,4 +37,8 @@ export type OpenApiGeneratorConfig = SourceBuilderOptions & OpenApiParserOptions
32
37
  */
33
38
  existingFileBehavior: 'override' | 'skip' | 'error' | 'count';
34
39
  };
40
+ /**
41
+ * The default configuration of the `OpenApiGenerator`. Options passed to its constructor are merged on top of these.
42
+ * @see OpenApiGeneratorConfig
43
+ */
35
44
  export declare const defaultOpenApiGeneratorConfig: OpenApiGeneratorConfig;
@@ -1,5 +1,9 @@
1
1
  import { defaultOpenApiTransformerOptions } from '../transform/types.js';
2
2
  import { defaultSourceBuilderOptions } from '../utils/source-builder.js';
3
+ /**
4
+ * The default configuration of the `OpenApiGenerator`. Options passed to its constructor are merged on top of these.
5
+ * @see OpenApiGeneratorConfig
6
+ */
3
7
  export const defaultOpenApiGeneratorConfig = {
4
8
  ...defaultSourceBuilderOptions,
5
9
  ...defaultOpenApiTransformerOptions,
@@ -2,13 +2,45 @@ import type { ApiEndpoint } from '../transform/api-types.ts';
2
2
  import type { MaybePromise } from '../utils/type.utils.ts';
3
3
  import { type DefaultGenerationProviderConfig, OpenApiGenerationProviderBase } from './generator.ts';
4
4
  import type { AnyConfig, OpenApiGenerationProviderContext, OpenApiGeneratorContext, OpenApiGeneratorInput, OpenApiGeneratorOutput } from './types.ts';
5
+ /**
6
+ * The context that is passed to the methods of `OpenApiEndpointsGenerationProviderBase`.
7
+ * @template TInput The type of the merged output of the providers that ran before.
8
+ * @template TOutput The type of the output produced by the provider.
9
+ * @template TConfig The type of the provider-specific configuration.
10
+ * @template TEndpointOutput The type of the result produced for each endpoint.
11
+ */
5
12
  export type OpenApiEndpointsGenerationProviderContext<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TEndpointOutput extends OpenApiGeneratorOutput> = OpenApiGenerationProviderContext<TInput, TConfig> & {
13
+ /**
14
+ * The results that have been generated so far, keyed by the ID of their endpoint.
15
+ */
6
16
  existingEndpointResults: Map<string, TEndpointOutput>;
17
+ /**
18
+ * The output of the provider. It is created by `initResult` and returned after all endpoints have been generated.
19
+ */
7
20
  output: TOutput;
8
21
  };
22
+ /**
23
+ * Base class for generation providers that produce one result per API endpoint.
24
+ * Results are cached per endpoint, so each endpoint is generated only once. Subclasses implement `initResult`,
25
+ * `generateEndpoint` and `addEndpointResult`, and can override `generateAdditionalFiles` to produce output after all
26
+ * endpoints have been processed.
27
+ * @template TInput The type of the merged output of the providers that ran before.
28
+ * @template TOutput The type of the output produced by the provider.
29
+ * @template TConfig The type of the provider-specific configuration.
30
+ * @template TEndpointOutput The type of the result produced for each endpoint.
31
+ * @template TContext The type of the context that is passed to the provider methods.
32
+ * @see OpenApiGenerationProviderBase
33
+ */
9
34
  export declare abstract class OpenApiEndpointsGenerationProviderBase<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TEndpointOutput extends OpenApiGeneratorOutput, TContext extends OpenApiEndpointsGenerationProviderContext<TInput, TOutput, TConfig, TEndpointOutput>> extends OpenApiGenerationProviderBase<TInput, TOutput, TConfig, TContext> {
10
35
  protected getProviderContext(context: OpenApiGeneratorContext<TInput>, config: Partial<TConfig> | undefined, defaultConfig: DefaultGenerationProviderConfig<TConfig>): OpenApiEndpointsGenerationProviderContext<TInput, TOutput, TConfig, TEndpointOutput>;
11
36
  protected onGenerate(ctx: TContext): Promise<TOutput>;
37
+ /**
38
+ * Gets the result for an endpoint. The result is generated on the first call and returned from the cache afterwards.
39
+ * In test mode, the source location of the endpoint is stored in the result.
40
+ * @param ctx The context of the provider.
41
+ * @param endpoint The endpoint to get the result for.
42
+ * @returns The result for the endpoint.
43
+ */
12
44
  getEndpointResult(ctx: TContext, endpoint: ApiEndpoint): Promise<TEndpointOutput>;
13
45
  protected abstract initResult(ctx: OpenApiGenerationProviderContext<TInput, TConfig>): TOutput;
14
46
  protected abstract generateEndpoint(ctx: TContext, endpoint: ApiEndpoint): Promise<TEndpointOutput>;
@@ -1,5 +1,17 @@
1
1
  import { OpenApiGenerationProviderBase } from './generator.js';
2
2
  import { addSourceIfTest } from './internal-utils.js';
3
+ /**
4
+ * Base class for generation providers that produce one result per API endpoint.
5
+ * Results are cached per endpoint, so each endpoint is generated only once. Subclasses implement `initResult`,
6
+ * `generateEndpoint` and `addEndpointResult`, and can override `generateAdditionalFiles` to produce output after all
7
+ * endpoints have been processed.
8
+ * @template TInput The type of the merged output of the providers that ran before.
9
+ * @template TOutput The type of the output produced by the provider.
10
+ * @template TConfig The type of the provider-specific configuration.
11
+ * @template TEndpointOutput The type of the result produced for each endpoint.
12
+ * @template TContext The type of the context that is passed to the provider methods.
13
+ * @see OpenApiGenerationProviderBase
14
+ */
3
15
  export class OpenApiEndpointsGenerationProviderBase extends OpenApiGenerationProviderBase {
4
16
  getProviderContext(context, config, defaultConfig) {
5
17
  const ctx = super.getProviderContext(context, config, defaultConfig);
@@ -15,6 +27,13 @@ export class OpenApiEndpointsGenerationProviderBase extends OpenApiGenerationPro
15
27
  await this.generateAdditionalFiles(ctx);
16
28
  return ctx.output;
17
29
  }
30
+ /**
31
+ * Gets the result for an endpoint. The result is generated on the first call and returned from the cache afterwards.
32
+ * In test mode, the source location of the endpoint is stored in the result.
33
+ * @param ctx The context of the provider.
34
+ * @param endpoint The endpoint to get the result for.
35
+ * @returns The result for the endpoint.
36
+ */
18
37
  async getEndpointResult(ctx, endpoint) {
19
38
  const existingResult = ctx.existingEndpointResults.get(endpoint.id);
20
39
  if (existingResult)
@@ -17,19 +17,127 @@ declare class _OpenApiGenerator<TOutput extends OpenApiGeneratorInput> {
17
17
  private readonly _config;
18
18
  private readonly _parser;
19
19
  constructor(config: OpenApiGeneratorConfig, providers: OpenApiGenerationProviders, parser: OpenApiParser);
20
+ /**
21
+ * Adds a generation provider to the pipeline. Providers run in the order they were added.
22
+ * @template PInput The input the provider requires. The output of the providers added so far must satisfy it.
23
+ * @template POutput The output produced by the provider.
24
+ * @template PConfig The type of the provider-specific configuration.
25
+ * @param provider The provider to add. Its output is deep-merged into the input of the following providers.
26
+ * @param config The provider-specific configuration that is passed to the provider when it runs.
27
+ * @returns A new generator whose output type includes the output of the added provider.
28
+ */
20
29
  use<PInput extends OpenApiGeneratorInput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(provider: ActionProvider<OpenApiGenerationProviderFn<VInput<PInput, TOutput>, POutput, PConfig>>, config?: Partial<PConfig>): _OpenApiGenerator<Merge<[TOutput, POutput]>>;
30
+ /**
31
+ * Adds a generation provider from a class. A new instance of the class is created when the generator runs,
32
+ * and its `generate` method is called to produce the output.
33
+ * @template PInput The input the provider requires. The output of the providers added so far must satisfy it.
34
+ * @template POutput The output produced by the provider.
35
+ * @template PConfig The type of the provider-specific configuration.
36
+ * @param type The class of the provider. It must have a constructor without parameters.
37
+ * @param config The provider-specific configuration that is passed to the provider when it runs.
38
+ * @returns A new generator whose output type includes the output of the added provider.
39
+ * @see OpenApiGenerationProvider
40
+ */
21
41
  useType<PInput extends OpenApiGeneratorInput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(type: EmptyConstructor<OpenApiGenerationProvider<VInput<PInput, TOutput>, POutput, PConfig>>, config?: Partial<PConfig>): _OpenApiGenerator<Merge<[TOutput, POutput]>>;
42
+ /**
43
+ * Adds a generation provider from a function. The function is called when the generator runs.
44
+ * @template PInput The input the function requires. The output of the providers added so far must satisfy it.
45
+ * @template POutput The output produced by the function.
46
+ * @template PConfig The type of the provider-specific configuration.
47
+ * @param fn The function that produces the output of the provider.
48
+ * @param config The provider-specific configuration that is passed to the function when it runs.
49
+ * @returns A new generator whose output type includes the output of the added provider.
50
+ * @see OpenApiGenerationProviderFn
51
+ */
22
52
  useFn<PInput extends TOutput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(fn: OpenApiGenerationProviderFn<VInput<PInput, TOutput>, POutput, PConfig>, config?: Partial<PConfig>): _OpenApiGenerator<Merge<[TOutput, POutput]>>;
53
+ /**
54
+ * Adds a generation provider from an object. Its `generate` method is called when the generator runs.
55
+ * @template PInput The input the provider requires. The output of the providers added so far must satisfy it.
56
+ * @template POutput The output produced by the provider.
57
+ * @template PConfig The type of the provider-specific configuration.
58
+ * @param value The object that produces the output of the provider.
59
+ * @param config The provider-specific configuration that is passed to the provider when it runs.
60
+ * @returns A new generator whose output type includes the output of the added provider.
61
+ * @see OpenApiGenerationProvider
62
+ */
23
63
  useValue<PInput extends TOutput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(value: OpenApiGenerationProvider<VInput<PInput, TOutput>, POutput, PConfig>, config?: Partial<PConfig>): _OpenApiGenerator<Merge<[TOutput, POutput]>>;
64
+ /**
65
+ * Runs all added generation providers in order on already parsed API data.
66
+ * The output directory and the `outputDir` of each provider that defines one are created, or emptied first if
67
+ * `clearOutputDir` is enabled. Each provider receives the merged output of the providers that ran before it.
68
+ * @template T The type of the parsed and transformed API data.
69
+ * @param data The parsed and transformed API data of the OpenAPI specifications.
70
+ * @returns The merged output of all providers.
71
+ */
24
72
  generate<T extends ApiData>(data: T): Promise<TOutput>;
73
+ /**
74
+ * Parses and transforms the given OpenAPI specification files, then runs the generation providers on the result.
75
+ * @param fileNames The paths of the specification files. Each argument can be a single path or an array of paths.
76
+ * @returns The merged output of all providers.
77
+ */
25
78
  parseAndGenerate(...fileNames: (string | string[])[]): Promise<TOutput>;
79
+ /**
80
+ * Parses and transforms all OpenAPI specification files in a directory, then runs the generation providers on the
81
+ * result. Only files with the extensions `.json`, `.yaml` and `.yml` are considered. The directory is scanned
82
+ * non-recursively unless `options.recursive` is enabled.
83
+ * @param dir The directory that contains the specification files.
84
+ * @param options Options for scanning the directory, including an optional filter for the files to consider.
85
+ * @returns The merged output of all providers.
86
+ */
26
87
  parseAndGenerateFromDir(dir: string, options?: Partial<DirectoryScanOptions>): Promise<TOutput>;
27
88
  }
89
+ /**
90
+ * Generates output from OpenAPI specifications by running a pipeline of generation providers.
91
+ * Providers are added with `useType`, `useFn` or `useValue` and run in the order they were added. Each provider can
92
+ * read the output of the providers before it from `ctx.input`.
93
+ * @example
94
+ * ```ts
95
+ * import { OpenApiGenerator } from '@goast/core';
96
+ * import { TypeScriptModelsGenerator } from '@goast/typescript';
97
+ *
98
+ * const generator = new OpenApiGenerator()
99
+ * // Add a generator using a class
100
+ * .useType(TypeScriptModelsGenerator)
101
+ * // Add a generator using a function that can read the output of the previous generator from `ctx.input`
102
+ * .useFn((ctx, cfg) => {
103
+ * // Do the generation
104
+ * return {}; // Return information about the generated files so it can be used by other generators
105
+ * });
106
+ *
107
+ * // Generate for all OpenAPI specification files in a directory
108
+ * await generator.parseAndGenerateFromDir('path/to/openapi/specs');
109
+ * ```
110
+ */
28
111
  export declare class OpenApiGenerator extends _OpenApiGenerator<{}> {
112
+ /**
113
+ * Initializes a new instance of the OpenApiGenerator class.
114
+ * @param config The configuration to use. Options that are not specified fall back to
115
+ * `defaultOpenApiGeneratorConfig`.
116
+ */
29
117
  constructor(config?: Partial<OpenApiGeneratorConfig>);
30
118
  }
119
+ /**
120
+ * The default configuration of a generation provider. The options of `OpenApiGeneratorConfig` become optional and fall
121
+ * back to the generator configuration, while the provider-specific options keep their own optionality.
122
+ * @template T The type of the provider configuration.
123
+ */
31
124
  export type DefaultGenerationProviderConfig<T extends AnyConfig> = Omit<T, keyof OpenApiGeneratorConfig> & Partial<Pick<T, keyof OpenApiGeneratorConfig>>;
125
+ /**
126
+ * Base class for generation providers. Subclasses implement `buildContext`, which creates a provider-specific context
127
+ * from the generator context and configuration, and `onGenerate`, which produces the output from that context.
128
+ * @template TInput The type of the merged output of the providers that ran before.
129
+ * @template TOutput The type of the output produced by the provider.
130
+ * @template TConfig The type of the provider-specific configuration.
131
+ * @template TContext The type of the context that is created by `buildContext` and consumed by `onGenerate`.
132
+ * @see OpenApiGenerationProvider
133
+ */
32
134
  export declare abstract class OpenApiGenerationProviderBase<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TContext extends OpenApiGenerationProviderContext<TInput, TConfig>> implements OpenApiGenerationProvider<TInput, TOutput, TConfig> {
135
+ /**
136
+ * Generates the output of this provider by building its context and then calling `onGenerate` with it.
137
+ * @param context The context of the generator, including the output of the providers that ran before.
138
+ * @param config The provider-specific configuration, if any.
139
+ * @returns The output of this provider.
140
+ */
33
141
  generate(context: OpenApiGeneratorContext<TInput>, config?: Partial<TConfig> | undefined): Promise<TOutput>;
34
142
  protected getProviderContext(context: OpenApiGeneratorContext<TInput>, config: Partial<TConfig> | undefined, defaultConfig: DefaultGenerationProviderConfig<TConfig>): OpenApiGenerationProviderContext<TInput, TConfig>;
35
143
  protected abstract buildContext(context: OpenApiGeneratorContext<TInput>, config?: Partial<TConfig> | undefined): TContext | Promise<TContext>;
@@ -13,19 +13,67 @@ class _OpenApiGenerator {
13
13
  this._config = config;
14
14
  this._parser = parser;
15
15
  }
16
+ /**
17
+ * Adds a generation provider to the pipeline. Providers run in the order they were added.
18
+ * @template PInput The input the provider requires. The output of the providers added so far must satisfy it.
19
+ * @template POutput The output produced by the provider.
20
+ * @template PConfig The type of the provider-specific configuration.
21
+ * @param provider The provider to add. Its output is deep-merged into the input of the following providers.
22
+ * @param config The provider-specific configuration that is passed to the provider when it runs.
23
+ * @returns A new generator whose output type includes the output of the added provider.
24
+ */
16
25
  use(provider, config) {
17
26
  this._providers.push({ provider: provider, config });
18
27
  return new _OpenApiGenerator(this._config, [...this._providers], this._parser);
19
28
  }
29
+ /**
30
+ * Adds a generation provider from a class. A new instance of the class is created when the generator runs,
31
+ * and its `generate` method is called to produce the output.
32
+ * @template PInput The input the provider requires. The output of the providers added so far must satisfy it.
33
+ * @template POutput The output produced by the provider.
34
+ * @template PConfig The type of the provider-specific configuration.
35
+ * @param type The class of the provider. It must have a constructor without parameters.
36
+ * @param config The provider-specific configuration that is passed to the provider when it runs.
37
+ * @returns A new generator whose output type includes the output of the added provider.
38
+ * @see OpenApiGenerationProvider
39
+ */
20
40
  useType(type, config) {
21
41
  return this.use(ActionProvider.fromType(type, 'generate'), config);
22
42
  }
43
+ /**
44
+ * Adds a generation provider from a function. The function is called when the generator runs.
45
+ * @template PInput The input the function requires. The output of the providers added so far must satisfy it.
46
+ * @template POutput The output produced by the function.
47
+ * @template PConfig The type of the provider-specific configuration.
48
+ * @param fn The function that produces the output of the provider.
49
+ * @param config The provider-specific configuration that is passed to the function when it runs.
50
+ * @returns A new generator whose output type includes the output of the added provider.
51
+ * @see OpenApiGenerationProviderFn
52
+ */
23
53
  useFn(fn, config) {
24
54
  return this.use(ActionProvider.fromFn(fn), config);
25
55
  }
56
+ /**
57
+ * Adds a generation provider from an object. Its `generate` method is called when the generator runs.
58
+ * @template PInput The input the provider requires. The output of the providers added so far must satisfy it.
59
+ * @template POutput The output produced by the provider.
60
+ * @template PConfig The type of the provider-specific configuration.
61
+ * @param value The object that produces the output of the provider.
62
+ * @param config The provider-specific configuration that is passed to the provider when it runs.
63
+ * @returns A new generator whose output type includes the output of the added provider.
64
+ * @see OpenApiGenerationProvider
65
+ */
26
66
  useValue(value, config) {
27
67
  return this.use(ActionProvider.fromValue(value, 'generate'), config);
28
68
  }
69
+ /**
70
+ * Runs all added generation providers in order on already parsed API data.
71
+ * The output directory and the `outputDir` of each provider that defines one are created, or emptied first if
72
+ * `clearOutputDir` is enabled. Each provider receives the merged output of the providers that ran before it.
73
+ * @template T The type of the parsed and transformed API data.
74
+ * @param data The parsed and transformed API data of the OpenAPI specifications.
75
+ * @returns The merged output of all providers.
76
+ */
29
77
  async generate(data) {
30
78
  const absOutputPath = resolve(this._config.outputDir);
31
79
  if (this._config.clearOutputDir) {
@@ -59,16 +107,56 @@ class _OpenApiGenerator {
59
107
  }
60
108
  return input;
61
109
  }
110
+ /**
111
+ * Parses and transforms the given OpenAPI specification files, then runs the generation providers on the result.
112
+ * @param fileNames The paths of the specification files. Each argument can be a single path or an array of paths.
113
+ * @returns The merged output of all providers.
114
+ */
62
115
  async parseAndGenerate(...fileNames) {
63
116
  const data = await this._parser.parseApisAndTransform(...fileNames);
64
117
  return await this.generate(data);
65
118
  }
119
+ /**
120
+ * Parses and transforms all OpenAPI specification files in a directory, then runs the generation providers on the
121
+ * result. Only files with the extensions `.json`, `.yaml` and `.yml` are considered. The directory is scanned
122
+ * non-recursively unless `options.recursive` is enabled.
123
+ * @param dir The directory that contains the specification files.
124
+ * @param options Options for scanning the directory, including an optional filter for the files to consider.
125
+ * @returns The merged output of all providers.
126
+ */
66
127
  async parseAndGenerateFromDir(dir, options) {
67
128
  const filter = (file) => (file.endsWith('.yml') || file.endsWith('.yaml') || file.endsWith('.json')) && (options?.filter?.(file) ?? true);
68
129
  return this.parseAndGenerate(...(await getFiles(dir, { ...options, filter })));
69
130
  }
70
131
  }
132
+ /**
133
+ * Generates output from OpenAPI specifications by running a pipeline of generation providers.
134
+ * Providers are added with `useType`, `useFn` or `useValue` and run in the order they were added. Each provider can
135
+ * read the output of the providers before it from `ctx.input`.
136
+ * @example
137
+ * ```ts
138
+ * import { OpenApiGenerator } from '@goast/core';
139
+ * import { TypeScriptModelsGenerator } from '@goast/typescript';
140
+ *
141
+ * const generator = new OpenApiGenerator()
142
+ * // Add a generator using a class
143
+ * .useType(TypeScriptModelsGenerator)
144
+ * // Add a generator using a function that can read the output of the previous generator from `ctx.input`
145
+ * .useFn((ctx, cfg) => {
146
+ * // Do the generation
147
+ * return {}; // Return information about the generated files so it can be used by other generators
148
+ * });
149
+ *
150
+ * // Generate for all OpenAPI specification files in a directory
151
+ * await generator.parseAndGenerateFromDir('path/to/openapi/specs');
152
+ * ```
153
+ */
71
154
  export class OpenApiGenerator extends _OpenApiGenerator {
155
+ /**
156
+ * Initializes a new instance of the OpenApiGenerator class.
157
+ * @param config The configuration to use. Options that are not specified fall back to
158
+ * `defaultOpenApiGeneratorConfig`.
159
+ */
72
160
  constructor(config) {
73
161
  super({ ...defaultOpenApiGeneratorConfig, ...config }, [], new OpenApiParser({ ...defaultOpenApiGeneratorConfig, ...config }));
74
162
  }
@@ -97,7 +185,22 @@ function mergeDeep(target, ...sources) {
97
185
  }
98
186
  return mergeDeep(target, ...sources);
99
187
  }
188
+ /**
189
+ * Base class for generation providers. Subclasses implement `buildContext`, which creates a provider-specific context
190
+ * from the generator context and configuration, and `onGenerate`, which produces the output from that context.
191
+ * @template TInput The type of the merged output of the providers that ran before.
192
+ * @template TOutput The type of the output produced by the provider.
193
+ * @template TConfig The type of the provider-specific configuration.
194
+ * @template TContext The type of the context that is created by `buildContext` and consumed by `onGenerate`.
195
+ * @see OpenApiGenerationProvider
196
+ */
100
197
  export class OpenApiGenerationProviderBase {
198
+ /**
199
+ * Generates the output of this provider by building its context and then calling `onGenerate` with it.
200
+ * @param context The context of the generator, including the output of the providers that ran before.
201
+ * @param config The provider-specific configuration, if any.
202
+ * @returns The output of this provider.
203
+ */
101
204
  async generate(context, config) {
102
205
  const ctx = await this.buildContext(context, config);
103
206
  return await this.onGenerate(ctx);
@@ -1 +1,9 @@
1
+ /**
2
+ * Stores the spec location of a generated result in its `__source__` property when running in test mode.
3
+ * The location is only recorded if `config.__test__` is set and the result does not have a `__source__` yet, so tests
4
+ * can trace a generated item back to the schema, endpoint or service it was created from.
5
+ * @param config The generator configuration. Only the `__test__` flag is read.
6
+ * @param result The generation result to annotate. It is mutated in place.
7
+ * @param sourceFn Returns the location to store, usually formatted as `file#path`.
8
+ */
1
9
  export declare function addSourceIfTest(config: any, result: any, sourceFn: () => any): void;
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Stores the spec location of a generated result in its `__source__` property when running in test mode.
3
+ * The location is only recorded if `config.__test__` is set and the result does not have a `__source__` yet, so tests
4
+ * can trace a generated item back to the schema, endpoint or service it was created from.
5
+ * @param config The generator configuration. Only the `__test__` flag is read.
6
+ * @param result The generation result to annotate. It is mutated in place.
7
+ * @param sourceFn Returns the location to store, usually formatted as `file#path`.
8
+ */
1
9
  export function addSourceIfTest(config, result, sourceFn) {
2
10
  if (config.__test__ && !result.__source__) {
3
11
  result.__source__ = sourceFn();
@@ -2,13 +2,46 @@ import type { ApiSchema } from '../transform/api-types.ts';
2
2
  import type { MaybePromise } from '../utils/type.utils.ts';
3
3
  import { type DefaultGenerationProviderConfig, OpenApiGenerationProviderBase } from './generator.ts';
4
4
  import type { AnyConfig, OpenApiGenerationProviderContext, OpenApiGeneratorContext, OpenApiGeneratorInput, OpenApiGeneratorOutput } from './types.ts';
5
+ /**
6
+ * The context that is passed to the methods of `OpenApiSchemasGenerationProviderBase`.
7
+ * @template TInput The type of the merged output of the providers that ran before.
8
+ * @template TOutput The type of the output produced by the provider.
9
+ * @template TConfig The type of the provider-specific configuration.
10
+ * @template TSchemaOutput The type of the result produced for each schema.
11
+ */
5
12
  export type OpenApiSchemasGenerationProviderContext<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TSchemaOutput extends OpenApiGeneratorOutput> = OpenApiGenerationProviderContext<TInput, TConfig> & {
13
+ /**
14
+ * The results that have been generated so far, keyed by the ID of their schema.
15
+ */
6
16
  existingSchemaResults: Map<string, TSchemaOutput>;
17
+ /**
18
+ * The output of the provider. It is created by `initResult` and returned after all schemas have been generated.
19
+ */
7
20
  output: TOutput;
8
21
  };
22
+ /**
23
+ * Base class for generation providers that produce one result per schema.
24
+ * Results are cached per schema. A schema that is a reference to another schema reuses the result of the referenced
25
+ * schema. Subclasses implement `initResult`, `generateSchema` and `addSchemaResult`, and can override
26
+ * `generateAdditionalFiles` to produce output after all schemas have been processed.
27
+ * @template TInput The type of the merged output of the providers that ran before.
28
+ * @template TOutput The type of the output produced by the provider.
29
+ * @template TConfig The type of the provider-specific configuration.
30
+ * @template TSchemaOutput The type of the result produced for each schema.
31
+ * @template TContext The type of the context that is passed to the provider methods.
32
+ * @see OpenApiGenerationProviderBase
33
+ */
9
34
  export declare abstract class OpenApiSchemasGenerationProviderBase<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TSchemaOutput extends OpenApiGeneratorOutput, TContext extends OpenApiSchemasGenerationProviderContext<TInput, TOutput, TConfig, TSchemaOutput>> extends OpenApiGenerationProviderBase<TInput, TOutput, TConfig, TContext> {
10
35
  protected getProviderContext(context: OpenApiGeneratorContext<TInput>, config: Partial<TConfig> | undefined, defaultConfig: DefaultGenerationProviderConfig<TConfig>): OpenApiSchemasGenerationProviderContext<TInput, TOutput, TConfig, TSchemaOutput>;
11
36
  protected onGenerate(ctx: TContext): Promise<TOutput>;
37
+ /**
38
+ * Gets the result for a schema. The result is generated on the first call and returned from the cache afterwards.
39
+ * If the schema is a reference to another schema, the result of the referenced schema is used.
40
+ * In test mode, the source location of the schema is stored in the result.
41
+ * @param ctx The context of the provider.
42
+ * @param schema The schema to get the result for.
43
+ * @returns The result for the schema.
44
+ */
12
45
  getSchemaResult(ctx: TContext, schema: ApiSchema): Promise<TSchemaOutput>;
13
46
  protected getSchemaReference(ctx: TContext, schema: ApiSchema): ApiSchema | undefined;
14
47
  protected abstract initResult(ctx: OpenApiGenerationProviderContext<TInput, TConfig>): TOutput;
@@ -1,6 +1,18 @@
1
1
  import { getSchemaReference } from '../utils/schema.utils.js';
2
2
  import { OpenApiGenerationProviderBase } from './generator.js';
3
3
  import { addSourceIfTest } from './internal-utils.js';
4
+ /**
5
+ * Base class for generation providers that produce one result per schema.
6
+ * Results are cached per schema. A schema that is a reference to another schema reuses the result of the referenced
7
+ * schema. Subclasses implement `initResult`, `generateSchema` and `addSchemaResult`, and can override
8
+ * `generateAdditionalFiles` to produce output after all schemas have been processed.
9
+ * @template TInput The type of the merged output of the providers that ran before.
10
+ * @template TOutput The type of the output produced by the provider.
11
+ * @template TConfig The type of the provider-specific configuration.
12
+ * @template TSchemaOutput The type of the result produced for each schema.
13
+ * @template TContext The type of the context that is passed to the provider methods.
14
+ * @see OpenApiGenerationProviderBase
15
+ */
4
16
  export class OpenApiSchemasGenerationProviderBase extends OpenApiGenerationProviderBase {
5
17
  getProviderContext(context, config, defaultConfig) {
6
18
  const ctx = super.getProviderContext(context, config, defaultConfig);
@@ -16,6 +28,14 @@ export class OpenApiSchemasGenerationProviderBase extends OpenApiGenerationProvi
16
28
  await this.generateAdditionalFiles(ctx);
17
29
  return ctx.output;
18
30
  }
31
+ /**
32
+ * Gets the result for a schema. The result is generated on the first call and returned from the cache afterwards.
33
+ * If the schema is a reference to another schema, the result of the referenced schema is used.
34
+ * In test mode, the source location of the schema is stored in the result.
35
+ * @param ctx The context of the provider.
36
+ * @param schema The schema to get the result for.
37
+ * @returns The result for the schema.
38
+ */
19
39
  async getSchemaResult(ctx, schema) {
20
40
  const existingResult = ctx.existingSchemaResults.get(schema.id);
21
41
  if (existingResult)
@@ -2,10 +2,34 @@ import type { ApiService } from '../transform/api-types.ts';
2
2
  import type { MaybePromise } from '../utils/type.utils.ts';
3
3
  import { type DefaultGenerationProviderConfig, OpenApiGenerationProviderBase } from './generator.ts';
4
4
  import type { AnyConfig, OpenApiGenerationProviderContext, OpenApiGeneratorContext, OpenApiGeneratorInput, OpenApiGeneratorOutput } from './types.ts';
5
+ /**
6
+ * The context that is passed to the methods of `OpenApiServicesGenerationProviderBase`.
7
+ * @template TInput The type of the merged output of the providers that ran before.
8
+ * @template TOutput The type of the output produced by the provider.
9
+ * @template TConfig The type of the provider-specific configuration.
10
+ * @template TServiceOutput The type of the result produced for each service.
11
+ */
5
12
  export type OpenApiServicesGenerationProviderContext<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TServiceOutput extends OpenApiGeneratorOutput> = OpenApiGenerationProviderContext<TInput, TConfig> & {
13
+ /**
14
+ * The results that have been generated so far, keyed by the ID of their service.
15
+ */
6
16
  existingServiceResults: Map<string, TServiceOutput>;
17
+ /**
18
+ * The output of the provider. It is created by `initResult` and returned after all services have been generated.
19
+ */
7
20
  output: TOutput;
8
21
  };
22
+ /**
23
+ * Base class for generation providers that produce one result per API service.
24
+ * Results are cached per service. Subclasses implement `initResult`, `generateService` and `addServiceResult`, and can
25
+ * override `generateAdditionalFiles` to produce output after all services have been processed.
26
+ * @template TInput The type of the merged output of the providers that ran before.
27
+ * @template TOutput The type of the output produced by the provider.
28
+ * @template TConfig The type of the provider-specific configuration.
29
+ * @template TServiceOutput The type of the result produced for each service.
30
+ * @template TContext The type of the context that is passed to the provider methods.
31
+ * @see OpenApiGenerationProviderBase
32
+ */
9
33
  export declare abstract class OpenApiServicesGenerationProviderBase<TInput extends OpenApiGeneratorInput, TOutput extends OpenApiGeneratorOutput, TConfig extends AnyConfig, TServiceOutput extends OpenApiGeneratorOutput, TContext extends OpenApiServicesGenerationProviderContext<TInput, TOutput, TConfig, TServiceOutput>> extends OpenApiGenerationProviderBase<TInput, TOutput, TConfig, TContext> {
10
34
  protected getProviderContext(context: OpenApiGeneratorContext<TInput>, config: Partial<TConfig> | undefined, defaultConfig: DefaultGenerationProviderConfig<TConfig>): OpenApiServicesGenerationProviderContext<TInput, TOutput, TConfig, TServiceOutput>;
11
35
  protected onGenerate(ctx: TContext): Promise<TOutput>;