@goast/typescript 0.9.0 → 0.9.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 (143) hide show
  1. package/dist/src/assets.d.ts +18 -0
  2. package/dist/src/assets.js +18 -0
  3. package/dist/src/ast/common.d.ts +6 -0
  4. package/dist/src/ast/node.d.ts +15 -0
  5. package/dist/src/ast/node.js +15 -0
  6. package/dist/src/ast/nodes/argument.d.ts +31 -0
  7. package/dist/src/ast/nodes/argument.js +31 -0
  8. package/dist/src/ast/nodes/array-type.d.ts +32 -0
  9. package/dist/src/ast/nodes/array-type.js +32 -0
  10. package/dist/src/ast/nodes/arrow-function.d.ts +56 -0
  11. package/dist/src/ast/nodes/arrow-function.js +56 -0
  12. package/dist/src/ast/nodes/call.d.ts +34 -0
  13. package/dist/src/ast/nodes/call.js +34 -0
  14. package/dist/src/ast/nodes/class.d.ts +73 -0
  15. package/dist/src/ast/nodes/class.js +73 -0
  16. package/dist/src/ast/nodes/constructor-parameter.d.ts +37 -0
  17. package/dist/src/ast/nodes/constructor-parameter.js +37 -0
  18. package/dist/src/ast/nodes/constructor.d.ts +44 -0
  19. package/dist/src/ast/nodes/constructor.js +44 -0
  20. package/dist/src/ast/nodes/decorator.d.ts +35 -0
  21. package/dist/src/ast/nodes/decorator.js +35 -0
  22. package/dist/src/ast/nodes/doc-tag.d.ts +39 -0
  23. package/dist/src/ast/nodes/doc-tag.js +39 -0
  24. package/dist/src/ast/nodes/doc.d.ts +38 -0
  25. package/dist/src/ast/nodes/doc.js +38 -0
  26. package/dist/src/ast/nodes/enum-value.d.ts +37 -0
  27. package/dist/src/ast/nodes/enum-value.js +37 -0
  28. package/dist/src/ast/nodes/enum.d.ts +47 -0
  29. package/dist/src/ast/nodes/enum.js +47 -0
  30. package/dist/src/ast/nodes/export.d.ts +51 -0
  31. package/dist/src/ast/nodes/export.js +51 -0
  32. package/dist/src/ast/nodes/function-type.d.ts +35 -0
  33. package/dist/src/ast/nodes/function-type.js +35 -0
  34. package/dist/src/ast/nodes/function.d.ts +61 -0
  35. package/dist/src/ast/nodes/function.js +61 -0
  36. package/dist/src/ast/nodes/generic-parameter.d.ts +44 -0
  37. package/dist/src/ast/nodes/generic-parameter.js +44 -0
  38. package/dist/src/ast/nodes/indexer.d.ts +41 -0
  39. package/dist/src/ast/nodes/indexer.js +41 -0
  40. package/dist/src/ast/nodes/interface.d.ts +57 -0
  41. package/dist/src/ast/nodes/interface.js +57 -0
  42. package/dist/src/ast/nodes/intersection-type.d.ts +32 -0
  43. package/dist/src/ast/nodes/intersection-type.js +32 -0
  44. package/dist/src/ast/nodes/lookup-type.d.ts +34 -0
  45. package/dist/src/ast/nodes/lookup-type.js +34 -0
  46. package/dist/src/ast/nodes/method.d.ts +90 -0
  47. package/dist/src/ast/nodes/method.js +90 -0
  48. package/dist/src/ast/nodes/object-type.d.ts +34 -0
  49. package/dist/src/ast/nodes/object-type.js +34 -0
  50. package/dist/src/ast/nodes/object.d.ts +34 -0
  51. package/dist/src/ast/nodes/object.js +34 -0
  52. package/dist/src/ast/nodes/parameter.d.ts +51 -0
  53. package/dist/src/ast/nodes/parameter.js +51 -0
  54. package/dist/src/ast/nodes/property-accessor.d.ts +94 -0
  55. package/dist/src/ast/nodes/property-accessor.js +94 -0
  56. package/dist/src/ast/nodes/property.d.ts +113 -0
  57. package/dist/src/ast/nodes/property.js +113 -0
  58. package/dist/src/ast/nodes/reference.d.ts +90 -0
  59. package/dist/src/ast/nodes/reference.js +66 -0
  60. package/dist/src/ast/nodes/string.d.ts +35 -0
  61. package/dist/src/ast/nodes/string.js +35 -0
  62. package/dist/src/ast/nodes/tuple.d.ts +33 -0
  63. package/dist/src/ast/nodes/tuple.js +33 -0
  64. package/dist/src/ast/nodes/type-alias.d.ts +44 -0
  65. package/dist/src/ast/nodes/type-alias.js +44 -0
  66. package/dist/src/ast/nodes/typeof.d.ts +41 -0
  67. package/dist/src/ast/nodes/typeof.js +41 -0
  68. package/dist/src/ast/nodes/types.d.ts +12 -0
  69. package/dist/src/ast/nodes/union-type.d.ts +31 -0
  70. package/dist/src/ast/nodes/union-type.js +31 -0
  71. package/dist/src/ast/nodes/variable.d.ts +46 -0
  72. package/dist/src/ast/nodes/variable.js +46 -0
  73. package/dist/src/ast/references/angular.d.ts +7 -0
  74. package/dist/src/ast/references/angular.js +7 -0
  75. package/dist/src/ast/references/easy-network-stub.d.ts +4 -0
  76. package/dist/src/ast/references/easy-network-stub.js +4 -0
  77. package/dist/src/ast/references/k6.d.ts +3 -0
  78. package/dist/src/ast/references/k6.js +3 -0
  79. package/dist/src/ast/references/rxjs.d.ts +5 -0
  80. package/dist/src/ast/references/rxjs.js +5 -0
  81. package/dist/src/ast/references/typescript.d.ts +30 -0
  82. package/dist/src/ast/references/typescript.js +30 -0
  83. package/dist/src/ast/utils/get-type-script-builder-options.d.ts +8 -0
  84. package/dist/src/ast/utils/get-type-script-builder-options.js +8 -0
  85. package/dist/src/ast/utils/to-ts-node.d.ts +28 -0
  86. package/dist/src/ast/utils/write-ts-generic-parameters.d.ts +9 -0
  87. package/dist/src/ast/utils/write-ts-generic-parameters.js +9 -0
  88. package/dist/src/ast/utils/write-ts-members.d.ts +12 -0
  89. package/dist/src/ast/utils/write-ts-members.js +12 -0
  90. package/dist/src/ast/utils/write-ts-nodes.d.ts +16 -0
  91. package/dist/src/ast/utils/write-ts-nodes.js +16 -0
  92. package/dist/src/ast/utils/write-ts-parameters.d.ts +9 -0
  93. package/dist/src/ast/utils/write-ts-parameters.js +9 -0
  94. package/dist/src/common-results.d.ts +65 -0
  95. package/dist/src/config.d.ts +32 -0
  96. package/dist/src/config.js +5 -0
  97. package/dist/src/file-builder.d.ts +104 -0
  98. package/dist/src/file-builder.js +93 -0
  99. package/dist/src/generators/file-generator.d.ts +14 -0
  100. package/dist/src/generators/file-generator.js +9 -0
  101. package/dist/src/generators/models/model-generator.d.ts +24 -0
  102. package/dist/src/generators/models/model-generator.js +15 -0
  103. package/dist/src/generators/models/models-generator.d.ts +16 -0
  104. package/dist/src/generators/models/models-generator.js +16 -0
  105. package/dist/src/generators/models/models.d.ts +53 -0
  106. package/dist/src/generators/models/models.js +8 -0
  107. package/dist/src/generators/services/angular-services/angular-service-generator.d.ts +27 -0
  108. package/dist/src/generators/services/angular-services/angular-service-generator.js +17 -0
  109. package/dist/src/generators/services/angular-services/angular-services-generator.d.ts +21 -0
  110. package/dist/src/generators/services/angular-services/angular-services-generator.js +21 -0
  111. package/dist/src/generators/services/angular-services/models.d.ts +70 -1
  112. package/dist/src/generators/services/angular-services/models.js +5 -0
  113. package/dist/src/generators/services/angular-services/refs.d.ts +11 -0
  114. package/dist/src/generators/services/angular-services/refs.js +11 -0
  115. package/dist/src/generators/services/easy-network-stub/easy-network-stub-generator.d.ts +22 -0
  116. package/dist/src/generators/services/easy-network-stub/easy-network-stub-generator.js +12 -0
  117. package/dist/src/generators/services/easy-network-stub/easy-network-stubs-generator.d.ts +16 -0
  118. package/dist/src/generators/services/easy-network-stub/easy-network-stubs-generator.js +16 -0
  119. package/dist/src/generators/services/easy-network-stub/models.d.ts +61 -0
  120. package/dist/src/generators/services/easy-network-stub/models.js +5 -0
  121. package/dist/src/generators/services/easy-network-stub/refs.d.ts +7 -0
  122. package/dist/src/generators/services/easy-network-stub/refs.js +7 -0
  123. package/dist/src/generators/services/fetch-clients/fetch-client-generator.d.ts +22 -0
  124. package/dist/src/generators/services/fetch-clients/fetch-client-generator.js +12 -0
  125. package/dist/src/generators/services/fetch-clients/fetch-clients-generator.d.ts +17 -0
  126. package/dist/src/generators/services/fetch-clients/fetch-clients-generator.js +17 -0
  127. package/dist/src/generators/services/fetch-clients/models.d.ts +64 -1
  128. package/dist/src/generators/services/fetch-clients/models.js +5 -0
  129. package/dist/src/generators/services/fetch-clients/refs.d.ts +7 -0
  130. package/dist/src/generators/services/fetch-clients/refs.js +7 -0
  131. package/dist/src/generators/services/k6-clients/k6-client-generator.d.ts +21 -0
  132. package/dist/src/generators/services/k6-clients/k6-client-generator.js +11 -0
  133. package/dist/src/generators/services/k6-clients/k6-clients-generator.d.ts +18 -0
  134. package/dist/src/generators/services/k6-clients/k6-clients-generator.js +18 -0
  135. package/dist/src/generators/services/k6-clients/models.d.ts +65 -1
  136. package/dist/src/generators/services/k6-clients/models.js +5 -0
  137. package/dist/src/generators/services/k6-clients/refs.d.ts +8 -0
  138. package/dist/src/generators/services/k6-clients/refs.js +8 -0
  139. package/dist/src/import-collection.d.ts +81 -0
  140. package/dist/src/import-collection.js +58 -0
  141. package/dist/src/utils.d.ts +62 -0
  142. package/dist/src/utils.js +47 -0
  143. package/package.json +2 -2
@@ -1,5 +1,12 @@
1
1
  import { notNullish, toArray, } from '@goast/core';
2
2
  import { TsNode } from '../node.js';
3
+ /**
4
+ * Writes a single node or value to a builder.
5
+ * A `TsNode` writes itself. Any other value is passed to the builder's `append` method.
6
+ * @template TBuilder The type of the source builder the node is written to.
7
+ * @param builder The builder to write to.
8
+ * @param node The node or value to write.
9
+ */
3
10
  export function writeTsNode(builder, node) {
4
11
  if (node instanceof TsNode) {
5
12
  node.write(builder);
@@ -8,6 +15,15 @@ export function writeTsNode(builder, node) {
8
15
  builder.append(node);
9
16
  }
10
17
  }
18
+ /**
19
+ * Writes one or more nodes or values to a builder, one after another.
20
+ * Nullish entries are skipped, and nothing is written when no entries remain. Without a separator, the entries are
21
+ * written back to back, so any line break has to come from the nodes themselves.
22
+ * @template TBuilder The type of the source builder the nodes are written to.
23
+ * @param builder The builder to write to.
24
+ * @param nodes The nodes or values to write. A single entry is also accepted.
25
+ * @param options Options for writing. `separator` is written between each pair of written entries.
26
+ */
11
27
  export function writeTsNodes(builder, nodes, options) {
12
28
  const filteredNodes = toArray(nodes).filter(notNullish);
13
29
  if (filteredNodes.length === 1) {
@@ -1,3 +1,12 @@
1
1
  import { type AppendValue, type Nullable, type SingleOrMultiple, type SourceBuilder } from '@goast/core';
2
2
  import type { TsNode } from '../node.ts';
3
+ /**
4
+ * Writes a parameter list in parentheses, such as `(a: string, b: number)`.
5
+ * The parameters are written on one line, separated by `, `, unless there are more than two of them or any of them has
6
+ * decorators. Otherwise each parameter is written on its own line, separated by `,` and a line break, and the closing
7
+ * parenthesis is written on its own line. Nullish entries are skipped.
8
+ * @template TBuilder The type of the source builder the parameters are written to.
9
+ * @param builder The builder to write to.
10
+ * @param nodes The parameters to write. A single parameter is also accepted.
11
+ */
3
12
  export declare function writeTsParameters<TBuilder extends SourceBuilder>(builder: TBuilder, nodes: SingleOrMultiple<Nullable<TsNode<TBuilder> | AppendValue<TBuilder>>>): void;
@@ -1,5 +1,14 @@
1
1
  import { notNullish, toArray, } from '@goast/core';
2
2
  import { writeTsNodes } from './write-ts-nodes.js';
3
+ /**
4
+ * Writes a parameter list in parentheses, such as `(a: string, b: number)`.
5
+ * The parameters are written on one line, separated by `, `, unless there are more than two of them or any of them has
6
+ * decorators. Otherwise each parameter is written on its own line, separated by `,` and a line break, and the closing
7
+ * parenthesis is written on its own line. Nullish entries are skipped.
8
+ * @template TBuilder The type of the source builder the parameters are written to.
9
+ * @param builder The builder to write to.
10
+ * @param nodes The parameters to write. A single parameter is also accepted.
11
+ */
3
12
  export function writeTsParameters(builder, nodes) {
4
13
  const filteredNodes = toArray(nodes).filter(notNullish);
5
14
  const multiline = filteredNodes.length > 2 ||
@@ -1,24 +1,89 @@
1
1
  import type { TypeScriptExportOptions } from './import-collection.ts';
2
+ /**
3
+ * The kind of TypeScript declaration that a generated component is. The models index file re-exports components of
4
+ * kind `type` and `interface` with `export type`.
5
+ */
2
6
  export type TypeScriptComponentOutputKind = 'interface' | 'type' | 'enum' | 'class' | 'function' | 'variable';
7
+ /**
8
+ * Describes a component (a type, interface, enum, class, ...) that a generator wrote or declared. The models generator
9
+ * publishes one of these for each schema as `typescript.models[schemaId]`, so that later generators can reference the
10
+ * component with `TypeScriptFileBuilder.appendModelUsage` without knowing how it was generated.
11
+ */
3
12
  export type TypeScriptComponentOutput = {
13
+ /**
14
+ * The name of the component. For an unnamed schema that is inlined, this is the type declaration itself.
15
+ */
4
16
  component: string;
17
+ /**
18
+ * The path of the file that the component was saved to. Omitted if the component is not saved in a file of its own.
19
+ */
5
20
  filePath?: string;
21
+ /**
22
+ * The imports that are required to use the component. For a component saved in a file, this is the import from
23
+ * that file.
24
+ */
6
25
  imports: TypeScriptImport[];
26
+ /**
27
+ * Other names that the file at `filePath` exports besides `component`. Strings are value exports, and objects can
28
+ * set the export type with `type`. The models index file re-exports them.
29
+ */
7
30
  additionalExports?: string[] | ({
8
31
  name: string;
9
32
  } & TypeScriptExportOptions)[];
33
+ /**
34
+ * The kind of declaration of the component, if known. See `TypeScriptComponentOutputKind`.
35
+ */
10
36
  kind?: TypeScriptComponentOutputKind;
11
37
  };
38
+ /**
39
+ * A component that a generator wrote to a file of its own, so the file path is always known. The `type` option of
40
+ * `TypeScriptExportOptions` sets how the component is exported.
41
+ */
12
42
  export type TypeScriptExportOutput = {
43
+ /**
44
+ * The name of the exported component.
45
+ */
13
46
  component: string;
47
+ /**
48
+ * The path of the file that the component was written to.
49
+ */
14
50
  filePath: string;
15
51
  } & TypeScriptExportOptions;
52
+ /**
53
+ * How the module path of an import is resolved. A `file` is a project file whose path ends in `.ts`, `.js` or `.json`.
54
+ * Its path is written relative to the importing file and transformed by `importModuleTransformer`. A `module` is a
55
+ * package name, such as `rxjs`, which is written as it is.
56
+ */
16
57
  export type TypeScriptImportKind = 'module' | 'file';
58
+ /**
59
+ * The form of an import statement. `import` writes `import { X } from '...';`, `type-import` writes
60
+ * `import type { X } from '...';`, and `js-doc` writes the name in an `@import` tag inside a JSDoc comment.
61
+ */
17
62
  export type TypeScriptImportType = 'import' | 'type-import' | 'js-doc';
63
+ /**
64
+ * The form of a re-export statement. `export` writes `export { X } from '...';`, and `type-export` writes
65
+ * `export type { X } from '...';`.
66
+ */
18
67
  export type TypeScriptExportType = 'export' | 'type-export';
68
+ /**
69
+ * A single named import that a component needs. The imports are collected by `ImportExportCollection` and written as
70
+ * import statements at the top of the file by `TypeScriptFileBuilder`.
71
+ */
19
72
  export type TypeScriptImport = {
73
+ /**
74
+ * Whether `modulePath` is a project file or a package. See `TypeScriptImportKind`.
75
+ */
20
76
  kind: TypeScriptImportKind;
77
+ /**
78
+ * The form of the import statement. See `TypeScriptImportType`.
79
+ */
21
80
  type: TypeScriptImportType;
81
+ /**
82
+ * The path of the file that exports the name for a `file` import, or the package name for a `module` import.
83
+ */
22
84
  modulePath: string;
85
+ /**
86
+ * The name that is imported.
87
+ */
23
88
  name: string;
24
89
  };
@@ -1,13 +1,32 @@
1
1
  import type { DefaultGenerationProviderConfig, ExtendedStringCasing, OpenApiGeneratorConfig } from '@goast/core';
2
2
  import type { ImportModuleTransformer } from './utils.ts';
3
+ /**
4
+ * The configuration that is shared by all TypeScript generators, on top of the `OpenApiGeneratorConfig` options.
5
+ * It controls the casing of generated names, how import module paths are written, and some output style settings.
6
+ * @see defaultTypeScriptGeneratorConfig
7
+ */
3
8
  export type TypeScriptGeneratorConfig = OpenApiGeneratorConfig & {
4
9
  /**
5
10
  * The transformer to use for transforming import module paths.
11
+ * It only applies to imports of project files (paths ending in `.ts`, `.js` or `.json`) into a file with a known
12
+ * path.
13
+ * A function receives the relative path, such as `../models/user.ts`, and returns the path to write.
14
+ * @example
15
+ * ```ts
16
+ * // Importing `src/models/user.ts` from `src/services/api.ts` produces:
17
+ * // 'omit-extension': '../models/user'
18
+ * // 'with-extension': '../models/user.ts'
19
+ * // 'js-extension': '../models/user.js'
20
+ * ```
6
21
  * @default 'omit-extension'
7
22
  */
8
23
  importModuleTransformer: ImportModuleTransformer;
9
24
  /**
10
25
  * The casing to use for file names.
26
+ * @example
27
+ * ```ts
28
+ * // With 'kebab', a model named `UserProfile` is written to `user-profile.ts`.
29
+ * ```
11
30
  * @default 'kebab'
12
31
  */
13
32
  fileNameCasing: ExtendedStringCasing;
@@ -33,11 +52,19 @@ export type TypeScriptGeneratorConfig = OpenApiGeneratorConfig & {
33
52
  enumValueNameCasing: ExtendedStringCasing;
34
53
  /**
35
54
  * The casing to use for constant names.
55
+ * @example
56
+ * ```ts
57
+ * // With the default, `fooBar` is written as `FOO_BAR`.
58
+ * ```
36
59
  * @default { casing: 'snake', wordCasing: 'all-upper' }
37
60
  */
38
61
  constantNameCasing: ExtendedStringCasing;
39
62
  /**
40
63
  * The casing to use for generic parameter names.
64
+ * @example
65
+ * ```ts
66
+ * // With the default, the discriminator property `kind` gets the generic parameter `TKind`.
67
+ * ```
41
68
  * @default { casing: 'pascal', prefix: 'T' }
42
69
  */
43
70
  genericParamNameCasing: ExtendedStringCasing;
@@ -62,4 +89,9 @@ export type TypeScriptGeneratorConfig = OpenApiGeneratorConfig & {
62
89
  */
63
90
  includeSourceInDocs: boolean;
64
91
  };
92
+ /**
93
+ * The default values of the common TypeScript generator configuration. A generator uses these values for every
94
+ * property that is not set explicitly.
95
+ * @see TypeScriptGeneratorConfig
96
+ */
65
97
  export declare const defaultTypeScriptGeneratorConfig: DefaultGenerationProviderConfig<TypeScriptGeneratorConfig>;
@@ -1,3 +1,8 @@
1
+ /**
2
+ * The default values of the common TypeScript generator configuration. A generator uses these values for every
3
+ * property that is not set explicitly.
4
+ * @see TypeScriptGeneratorConfig
5
+ */
1
6
  export const defaultTypeScriptGeneratorConfig = {
2
7
  indent: { type: 'spaces', count: 2 },
3
8
  importModuleTransformer: 'omit-extension',
@@ -3,27 +3,131 @@ import { TsNode } from './ast/node.ts';
3
3
  import { type TypeScriptGeneratorConfig } from './config.ts';
4
4
  import type { TypeScriptModelGeneratorOutput } from './generators/index.ts';
5
5
  import { ImportExportCollection, type TypeScriptExportOptions, type TypeScriptImportOptions } from './import-collection.ts';
6
+ /**
7
+ * A value that can be appended to a `TypeScriptFileBuilder`: a TypeScript node, which is written together with its
8
+ * imports, or one of the additional values.
9
+ * @template TAdditionalAppends The additional value types that the builder accepts.
10
+ */
6
11
  export type TypeScriptAppends<TAdditionalAppends> = TsNode<TypeScriptFileBuilder> | TAdditionalAppends;
12
+ /**
13
+ * The parameter type of the `append` methods of a `TypeScriptFileBuilder`: a single value, an array of values, or a
14
+ * function that appends content to the builder.
15
+ * @template TBuilder The type of the builder that the value is appended to.
16
+ * @template TAdditionalAppends The additional value types that the builder accepts.
17
+ */
7
18
  export type TypeScriptAppendParam<TBuilder extends TypeScriptFileBuilder, TAdditionalAppends> = AppendParam<TBuilder, TypeScriptAppends<TAdditionalAppends>>;
19
+ /**
20
+ * Checks whether a value can be appended to a TypeScript file builder. This is the case for TypeScript nodes and for
21
+ * the values that a `SourceBuilder` accepts.
22
+ * @template TBuilder The type of the builder that the value is appended to.
23
+ * @param value The value to check.
24
+ * @returns `true` if the value can be appended, otherwise `false`.
25
+ */
8
26
  export declare function isTypeScriptAppendValue<TBuilder extends TypeScriptFileBuilder = TypeScriptFileBuilder>(value: unknown): value is AppendValue<TBuilder>;
27
+ /**
28
+ * Builds a TypeScript source file. Content is appended like with any `SourceBuilder`, and the imports and exports that
29
+ * the content needs are collected next to it.
30
+ *
31
+ * Imports are rarely added by hand. A `TsReference` node that has a module name or a file path, such as
32
+ * `tsReference('User', '/out/models/user.ts')`, adds its import to the builder when the node is written, because
33
+ * `appendSingle` calls `write` on every TypeScript node. The collected imports are kept in `imports`, and `toString`
34
+ * writes them as import statements above the content. The statements therefore always end up at the top of the file,
35
+ * whatever the order of the appends was.
36
+ *
37
+ * File paths are turned into relative module paths based on the `filePath` of this builder, and the
38
+ * `importModuleTransformer` option decides how their extensions are written. See `ImportExportCollection`.
39
+ *
40
+ * @template TAdditionalAppends The additional value types that the builder accepts besides TypeScript nodes.
41
+ * @example
42
+ * ```ts
43
+ * const builder = new TypeScriptFileBuilder('/out/services/user.ts', defaultTypeScriptGeneratorConfig);
44
+ * builder.append(tsReference('User', '/out/models/user.ts'));
45
+ * builder.toString(); // => "import { User } from '../models/user';\n\nUser\n"
46
+ * ```
47
+ */
9
48
  export declare class TypeScriptFileBuilder<TAdditionalAppends = never> extends SourceBuilder<TypeScriptAppends<TAdditionalAppends>> {
49
+ /**
50
+ * The path of the file that this builder writes to, or `undefined` if the path is not known. Relative module paths of
51
+ * the imports are calculated from it.
52
+ */
10
53
  readonly filePath: string | undefined;
54
+ /**
55
+ * The imports and re-exports that the content of this builder needs. `toString` writes them above the content.
56
+ */
11
57
  readonly imports: ImportExportCollection;
58
+ /**
59
+ * Gets the TypeScript generator options that this builder uses.
60
+ */
12
61
  get options(): TypeScriptGeneratorConfig;
62
+ /**
63
+ * Initializes a new instance of the TypeScriptFileBuilder class.
64
+ * @param filePath The path of the file that the content is written to. Used by `writeToFile` and for relative
65
+ * imports.
66
+ * @param options The generator options. Defaults to `defaultTypeScriptGeneratorConfig`.
67
+ */
13
68
  constructor(filePath?: string, options?: TypeScriptGeneratorConfig);
69
+ /**
70
+ * Adds a named import to the file. The import is written above the content by `toString`. Adding the same name from
71
+ * the same module more than once writes it only once.
72
+ * @param name The name to import.
73
+ * @param moduleNameOrfilePath The module name, such as `rxjs`, or the path of the file that exports the name.
74
+ * @param options The options of the import, such as the import type. Defaults to a value import.
75
+ * @returns This builder, for chaining.
76
+ */
14
77
  addImport(name: string, moduleNameOrfilePath: string, options?: TypeScriptImportOptions): this;
78
+ /**
79
+ * Adds a re-export of a named export from a file, such as `export { User } from '../models/user';`. The re-export is
80
+ * written above the content by `toString`, after the imports.
81
+ * @param name The name to re-export.
82
+ * @param filePath The path of the file that exports the name.
83
+ * @param options The options of the export, such as the export type. Defaults to a value export.
84
+ * @returns This builder, for chaining.
85
+ */
15
86
  addExport(name: string, filePath: string, options?: TypeScriptExportOptions): this;
87
+ /**
88
+ * Removes all appended content, imports and re-exports from the builder.
89
+ */
16
90
  clear(): void;
91
+ /**
92
+ * Renders the complete file: the imports and re-exports first, then the appended content. A `// @ts-check` comment is
93
+ * added at the top for files with a `.js`, `.cjs` or `.mjs` extension.
94
+ * @param addPadding Whether to separate the imports from the content with a blank line, and to end the output with a
95
+ * line break. Defaults to `true`.
96
+ * @returns The source code of the file.
97
+ */
17
98
  toString(addPadding?: boolean): string;
18
99
  protected appendSingle(value: TypeScriptAppendParam<this, TAdditionalAppends>): void;
100
+ /**
101
+ * Appends the component of a model that another generator published, and adds the imports that the component needs.
102
+ * Use it to reference a model from `typescript.models`. With the `js-doc` import type, the JSDoc comments are removed
103
+ * from the component text and the imports are written as `@import` tags instead.
104
+ * @param type The published output of the model.
105
+ * @param options The import options, such as the import type, that apply to the imports of the model.
106
+ * @returns This builder, for chaining.
107
+ */
19
108
  appendModelUsage(type: TypeScriptModelGeneratorOutput, options?: TypeScriptImportOptions): this;
109
+ /**
110
+ * Writes the rendered file to disk with the generator options. Throws if no path is given and this builder has none.
111
+ * @param filePath The path of the file to write to. Defaults to the `filePath` of this builder.
112
+ */
20
113
  writeToFile(filePath?: string): void;
114
+ /**
115
+ * Creates a file builder for the file path, lets the generator callback fill it, and writes the file. Nothing happens
116
+ * if the path is empty. See `tryGenerate` for the details.
117
+ * @param options The file path, the optional log name, the generator options, and the callback that appends content.
118
+ */
21
119
  static generate(options: {
22
120
  filePath: string;
23
121
  logName?: Nullable<string>;
24
122
  options?: TypeScriptGeneratorConfig;
25
123
  generator: (builder: TypeScriptFileBuilder) => void;
26
124
  }): void;
125
+ /**
126
+ * Creates a file builder for the file path, lets the generator callback fill it, and writes the file. Unlike
127
+ * `generate`, a nullish path is accepted. In that case nothing is generated and the callback is not called.
128
+ * @param options The file path, the generator callback, the optional log name, and the generator options. The log
129
+ * name is logged as `Generating <log name> to <file path>...` before the file is generated.
130
+ */
27
131
  static tryGenerate({ filePath, generator, options, logName, }: {
28
132
  filePath: Nullable<string>;
29
133
  logName?: Nullable<string>;
@@ -2,15 +2,59 @@ import { isAppendValue, SourceBuilder, writeGeneratedFile, } from '@goast/core';
2
2
  import { TsNode } from './ast/node.js';
3
3
  import { defaultTypeScriptGeneratorConfig } from './config.js';
4
4
  import { ImportExportCollection, } from './import-collection.js';
5
+ /**
6
+ * Checks whether a value can be appended to a TypeScript file builder. This is the case for TypeScript nodes and for
7
+ * the values that a `SourceBuilder` accepts.
8
+ * @template TBuilder The type of the builder that the value is appended to.
9
+ * @param value The value to check.
10
+ * @returns `true` if the value can be appended, otherwise `false`.
11
+ */
5
12
  export function isTypeScriptAppendValue(value) {
6
13
  return isAppendValue(value) || value instanceof TsNode;
7
14
  }
15
+ /**
16
+ * Builds a TypeScript source file. Content is appended like with any `SourceBuilder`, and the imports and exports that
17
+ * the content needs are collected next to it.
18
+ *
19
+ * Imports are rarely added by hand. A `TsReference` node that has a module name or a file path, such as
20
+ * `tsReference('User', '/out/models/user.ts')`, adds its import to the builder when the node is written, because
21
+ * `appendSingle` calls `write` on every TypeScript node. The collected imports are kept in `imports`, and `toString`
22
+ * writes them as import statements above the content. The statements therefore always end up at the top of the file,
23
+ * whatever the order of the appends was.
24
+ *
25
+ * File paths are turned into relative module paths based on the `filePath` of this builder, and the
26
+ * `importModuleTransformer` option decides how their extensions are written. See `ImportExportCollection`.
27
+ *
28
+ * @template TAdditionalAppends The additional value types that the builder accepts besides TypeScript nodes.
29
+ * @example
30
+ * ```ts
31
+ * const builder = new TypeScriptFileBuilder('/out/services/user.ts', defaultTypeScriptGeneratorConfig);
32
+ * builder.append(tsReference('User', '/out/models/user.ts'));
33
+ * builder.toString(); // => "import { User } from '../models/user';\n\nUser\n"
34
+ * ```
35
+ */
8
36
  export class TypeScriptFileBuilder extends SourceBuilder {
37
+ /**
38
+ * The path of the file that this builder writes to, or `undefined` if the path is not known. Relative module paths of
39
+ * the imports are calculated from it.
40
+ */
9
41
  filePath;
42
+ /**
43
+ * The imports and re-exports that the content of this builder needs. `toString` writes them above the content.
44
+ */
10
45
  imports;
46
+ /**
47
+ * Gets the TypeScript generator options that this builder uses.
48
+ */
11
49
  get options() {
12
50
  return super.options;
13
51
  }
52
+ /**
53
+ * Initializes a new instance of the TypeScriptFileBuilder class.
54
+ * @param filePath The path of the file that the content is written to. Used by `writeToFile` and for relative
55
+ * imports.
56
+ * @param options The generator options. Defaults to `defaultTypeScriptGeneratorConfig`.
57
+ */
14
58
  constructor(filePath, options) {
15
59
  super(options ?? defaultTypeScriptGeneratorConfig);
16
60
  this.filePath = filePath;
@@ -20,18 +64,44 @@ export class TypeScriptFileBuilder extends SourceBuilder {
20
64
  useSingleQuotes: this.options.useSingleQuotes,
21
65
  });
22
66
  }
67
+ /**
68
+ * Adds a named import to the file. The import is written above the content by `toString`. Adding the same name from
69
+ * the same module more than once writes it only once.
70
+ * @param name The name to import.
71
+ * @param moduleNameOrfilePath The module name, such as `rxjs`, or the path of the file that exports the name.
72
+ * @param options The options of the import, such as the import type. Defaults to a value import.
73
+ * @returns This builder, for chaining.
74
+ */
23
75
  addImport(name, moduleNameOrfilePath, options) {
24
76
  this.imports.addImport(name, moduleNameOrfilePath, options);
25
77
  return this;
26
78
  }
79
+ /**
80
+ * Adds a re-export of a named export from a file, such as `export { User } from '../models/user';`. The re-export is
81
+ * written above the content by `toString`, after the imports.
82
+ * @param name The name to re-export.
83
+ * @param filePath The path of the file that exports the name.
84
+ * @param options The options of the export, such as the export type. Defaults to a value export.
85
+ * @returns This builder, for chaining.
86
+ */
27
87
  addExport(name, filePath, options) {
28
88
  this.imports.addExport(name, filePath, options);
29
89
  return this;
30
90
  }
91
+ /**
92
+ * Removes all appended content, imports and re-exports from the builder.
93
+ */
31
94
  clear() {
32
95
  super.clear();
33
96
  this.imports.clear();
34
97
  }
98
+ /**
99
+ * Renders the complete file: the imports and re-exports first, then the appended content. A `// @ts-check` comment is
100
+ * added at the top for files with a `.js`, `.cjs` or `.mjs` extension.
101
+ * @param addPadding Whether to separate the imports from the content with a blank line, and to end the output with a
102
+ * line break. Defaults to `true`.
103
+ * @returns The source code of the file.
104
+ */
35
105
  toString(addPadding = true) {
36
106
  const builder = new SourceBuilder(this.options);
37
107
  if (this.filePath?.match(/\.[cm]?js$/)) {
@@ -55,6 +125,14 @@ export class TypeScriptFileBuilder extends SourceBuilder {
55
125
  value.write(this);
56
126
  }
57
127
  }
128
+ /**
129
+ * Appends the component of a model that another generator published, and adds the imports that the component needs.
130
+ * Use it to reference a model from `typescript.models`. With the `js-doc` import type, the JSDoc comments are removed
131
+ * from the component text and the imports are written as `@import` tags instead.
132
+ * @param type The published output of the model.
133
+ * @param options The import options, such as the import type, that apply to the imports of the model.
134
+ * @returns This builder, for chaining.
135
+ */
58
136
  appendModelUsage(type, options) {
59
137
  this.append(options?.type === 'js-doc' ? type.component.replace(/\/\*\*.*\*\/\s*/gs, '') : type.component);
60
138
  for (const i of type.imports) {
@@ -62,6 +140,10 @@ export class TypeScriptFileBuilder extends SourceBuilder {
62
140
  }
63
141
  return this;
64
142
  }
143
+ /**
144
+ * Writes the rendered file to disk with the generator options. Throws if no path is given and this builder has none.
145
+ * @param filePath The path of the file to write to. Defaults to the `filePath` of this builder.
146
+ */
65
147
  writeToFile(filePath) {
66
148
  if (!filePath)
67
149
  filePath = this.filePath;
@@ -69,9 +151,20 @@ export class TypeScriptFileBuilder extends SourceBuilder {
69
151
  throw new Error('File path is required');
70
152
  writeGeneratedFile(this.options, filePath, this.toString());
71
153
  }
154
+ /**
155
+ * Creates a file builder for the file path, lets the generator callback fill it, and writes the file. Nothing happens
156
+ * if the path is empty. See `tryGenerate` for the details.
157
+ * @param options The file path, the optional log name, the generator options, and the callback that appends content.
158
+ */
72
159
  static generate(options) {
73
160
  TypeScriptFileBuilder.tryGenerate(options);
74
161
  }
162
+ /**
163
+ * Creates a file builder for the file path, lets the generator callback fill it, and writes the file. Unlike
164
+ * `generate`, a nullish path is accepted. In that case nothing is generated and the callback is not called.
165
+ * @param options The file path, the generator callback, the optional log name, and the generator options. The log
166
+ * name is logged as `Generating <log name> to <file path>...` before the file is generated.
167
+ */
75
168
  static tryGenerate({ filePath, generator, options, logName, }) {
76
169
  if (!filePath)
77
170
  return;
@@ -1,7 +1,21 @@
1
1
  import type { MaybePromise, OpenApiGenerationProviderContext, OpenApiGeneratorInput, SourceBuilder } from '@goast/core';
2
2
  import { ts } from '../ast/index.ts';
3
3
  import type { TypeScriptGeneratorConfig } from '../config.ts';
4
+ /**
5
+ * Base class for the TypeScript generators that turn an OpenAPI generation context into TypeScript files.
6
+ *
7
+ * It only provides shared helpers. Subclasses such as the model and Angular service generators write their own files
8
+ * with `TypeScriptFileBuilder` and return the information that later generators need to reference them.
9
+ *
10
+ * @template TContext The type of the generation context, including the TypeScript generator configuration.
11
+ * @template TOutput The type of the output that `generate` returns.
12
+ */
4
13
  export declare abstract class TypeScriptFileGenerator<TContext extends OpenApiGenerationProviderContext<OpenApiGeneratorInput, TypeScriptGeneratorConfig>, TOutput> {
14
+ /**
15
+ * Generates the TypeScript files for the given context and returns the information about them.
16
+ * @param context The generation context, including the TypeScript generator configuration.
17
+ * @returns The output of the generator, or a promise that resolves to it.
18
+ */
5
19
  abstract generate(context: TContext): MaybePromise<TOutput>;
6
20
  protected getAnyType(context: TContext): ts.Reference<SourceBuilder>;
7
21
  }
@@ -1,4 +1,13 @@
1
1
  import { ts } from '../ast/index.js';
2
+ /**
3
+ * Base class for the TypeScript generators that turn an OpenAPI generation context into TypeScript files.
4
+ *
5
+ * It only provides shared helpers. Subclasses such as the model and Angular service generators write their own files
6
+ * with `TypeScriptFileBuilder` and return the information that later generators need to reference them.
7
+ *
8
+ * @template TContext The type of the generation context, including the TypeScript generator configuration.
9
+ * @template TOutput The type of the output that `generate` returns.
10
+ */
2
11
  export class TypeScriptFileGenerator {
3
12
  getAnyType(context) {
4
13
  return context.config.preferUnknown ? ts.refs.unknown() : ts.refs.any();
@@ -7,10 +7,34 @@ import type { TypeScriptModelGeneratorContext, TypeScriptModelGeneratorOutput }
7
7
  type Context = TypeScriptModelGeneratorContext;
8
8
  type Output = TypeScriptModelGeneratorOutput;
9
9
  type Builder = TypeScriptFileBuilder;
10
+ /**
11
+ * Generates the model of a single schema. Implement it to replace the default model generation.
12
+ * @template TOutput The type of the result that is returned for the schema.
13
+ */
10
14
  export interface TypeScriptModelGenerator<TOutput extends Output = Output> {
15
+ /**
16
+ * Generates the model of the schema in the context.
17
+ * @param ctx The context of the schema to generate, including its configuration.
18
+ * @returns The result of the model, or a promise that resolves to it.
19
+ */
11
20
  generate(ctx: Context): MaybePromise<TOutput>;
12
21
  }
22
+ /**
23
+ * The default model generator. It generates a schema as a type declaration (`type` or `interface`) or as an enum,
24
+ * depending on the `enumGeneration` and `typeDeclaration` options. Discriminator schemas also get a base type named
25
+ * with `discriminatorBaseTypeCasing`.
26
+ *
27
+ * A schema that is declared as its own type is written to `{outputDir}/{modelsDir}/{name}.ts`, with the file name
28
+ * cased by `modelFileNameCasing` (or `fileNameCasing`). Other schemas are only rendered as a type expression that is
29
+ * inlined where it is used, and no file is written for them.
30
+ */
13
31
  export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator<Context, Output> implements TypeScriptModelGenerator {
32
+ /**
33
+ * Generates the model of the schema in the context and writes its file, if it has one.
34
+ * @param ctx The context of the schema to generate, including its configuration.
35
+ * @returns The component name, the imports and, if a file was written, its path. For an inline schema, the component
36
+ * is the type expression.
37
+ */
14
38
  generate(ctx: Context): MaybePromise<Output>;
15
39
  protected getNodeKind(node: unknown): TypeScriptComponentOutputKind | undefined;
16
40
  protected getFileContent(ctx: Context): AppendValueGroup<Builder>;
@@ -4,7 +4,22 @@ import { ts } from '../../ast/index.js';
4
4
  import { TypeScriptFileBuilder } from '../../file-builder.js';
5
5
  import { getSourceDocLine } from '../../utils.js';
6
6
  import { TypeScriptFileGenerator } from '../file-generator.js';
7
+ /**
8
+ * The default model generator. It generates a schema as a type declaration (`type` or `interface`) or as an enum,
9
+ * depending on the `enumGeneration` and `typeDeclaration` options. Discriminator schemas also get a base type named
10
+ * with `discriminatorBaseTypeCasing`.
11
+ *
12
+ * A schema that is declared as its own type is written to `{outputDir}/{modelsDir}/{name}.ts`, with the file name
13
+ * cased by `modelFileNameCasing` (or `fileNameCasing`). Other schemas are only rendered as a type expression that is
14
+ * inlined where it is used, and no file is written for them.
15
+ */
7
16
  export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
17
+ /**
18
+ * Generates the model of the schema in the context and writes its file, if it has one.
19
+ * @param ctx The context of the schema to generate, including its configuration.
20
+ * @returns The component name, the imports and, if a file was written, its path. For an inline schema, the component
21
+ * is the type expression.
22
+ */
8
23
  generate(ctx) {
9
24
  if (this.shouldGenerateTypeDeclaration(ctx, ctx.schema)) {
10
25
  const name = this.getDeclarationTypeName(ctx, ctx.schema);
@@ -7,8 +7,24 @@ type Output = TypeScriptModelsGeneratorOutput;
7
7
  type Config = TypeScriptModelsGeneratorConfig;
8
8
  type SchemaOutput = TypeScriptModelGeneratorOutput;
9
9
  type Context = TypeScriptModelsGeneratorContext;
10
+ /**
11
+ * Generates the models for all OpenAPI schemas.
12
+ *
13
+ * Each schema is generated by a `TypeScriptModelGenerator`, by default a `DefaultTypeScriptModelGenerator`. A schema
14
+ * that is declared as its own type is written to `{outputDir}/{modelsDir}/{name}.ts`. An index file that re-exports
15
+ * all models is written to `{outputDir}/{modelsIndexFile}`, unless `modelsIndexFile` is nullish.
16
+ *
17
+ * It publishes the following data for later generators:
18
+ * - `typescript.models`: The result of each schema, keyed by the schema ID (`ApiSchema['id']`).
19
+ * - `typescript.indexFiles.models`: The path of the index file, or `null` if no index file was generated.
20
+ */
10
21
  export declare class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderBase<Input, Output, Config, SchemaOutput, Context> {
11
22
  private readonly _modelGeneratorFactory;
23
+ /**
24
+ * Initializes a new instance of the TypeScriptModelsGenerator class.
25
+ * @param modelGeneratorFactory The factory that creates the model generator used for each schema. If omitted, a
26
+ * `DefaultTypeScriptModelGenerator` is used.
27
+ */
12
28
  constructor(modelGeneratorFactory?: Factory<TypeScriptModelGenerator, []>);
13
29
  protected initResult(): Output;
14
30
  protected buildContext(context: OpenApiGeneratorContext<TypeScriptModelsGeneratorInput>, config?: Partial<Config> | undefined): Context;
@@ -4,8 +4,24 @@ import { ts } from '../../ast/index.js';
4
4
  import { TypeScriptFileBuilder } from '../../file-builder.js';
5
5
  import { DefaultTypeScriptModelGenerator } from './model-generator.js';
6
6
  import { defaultTypeScriptModelsGeneratorConfig, } from './models.js';
7
+ /**
8
+ * Generates the models for all OpenAPI schemas.
9
+ *
10
+ * Each schema is generated by a `TypeScriptModelGenerator`, by default a `DefaultTypeScriptModelGenerator`. A schema
11
+ * that is declared as its own type is written to `{outputDir}/{modelsDir}/{name}.ts`. An index file that re-exports
12
+ * all models is written to `{outputDir}/{modelsIndexFile}`, unless `modelsIndexFile` is nullish.
13
+ *
14
+ * It publishes the following data for later generators:
15
+ * - `typescript.models`: The result of each schema, keyed by the schema ID (`ApiSchema['id']`).
16
+ * - `typescript.indexFiles.models`: The path of the index file, or `null` if no index file was generated.
17
+ */
7
18
  export class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderBase {
8
19
  _modelGeneratorFactory;
20
+ /**
21
+ * Initializes a new instance of the TypeScriptModelsGenerator class.
22
+ * @param modelGeneratorFactory The factory that creates the model generator used for each schema. If omitted, a
23
+ * `DefaultTypeScriptModelGenerator` is used.
24
+ */
9
25
  constructor(modelGeneratorFactory) {
10
26
  super();
11
27
  this._modelGeneratorFactory = modelGeneratorFactory ?? Factory.fromValue(new DefaultTypeScriptModelGenerator());