fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 348 lines 9.9 kB view raw
1import type { NameConflictResolver, Symbol } from '@hey-api/codegen-core'; 2import type { MaybeArray } from '@hey-api/types'; 3 4import type { Plugin } from '../plugins/types'; 5import type { Logs } from '../types/logs'; 6import type { Casing, NameTransformer } from '../utils/naming/types'; 7import type { Input, UserInput, UserWatch } from './input/types'; 8import type { PostProcessor } from './output/postprocess'; 9import type { SourceConfig, UserSourceConfig } from './output/source/types'; 10import type { OutputHeader } from './output/types'; 11import type { Parser, UserParser } from './parser/types'; 12 13export type FeatureToggle = { 14 /** 15 * Whether this feature is enabled. 16 */ 17 enabled: boolean; 18}; 19 20export type UserIndexExportOption = { 21 /** 22 * Whether exports should be re-exported from the entry file. 23 * 24 * - `true` — include all exports 25 * - `false` — exclude all exports 26 * - `(symbol) => boolean` — include exports matching the predicate 27 * 28 * @default false 29 * @deprecated use `includeInEntry` instead 30 */ 31 exportFromIndex?: boolean | ((symbol: Symbol) => boolean); 32 /** 33 * Whether exports should be re-exported from the entry file. 34 * 35 * - `true` — include all exports 36 * - `false` — exclude all exports 37 * - `(symbol) => boolean` — include exports matching the predicate 38 * 39 * @default false 40 */ 41 includeInEntry?: boolean | ((symbol: Symbol) => boolean); 42}; 43export type IndexExportOption = { 44 /** 45 * Whether exports should be re-exported from the entry file. 46 * 47 * @deprecated use `includeInEntry` instead 48 */ 49 exportFromIndex: boolean | ((symbol: Symbol) => boolean); 50 /** 51 * Whether exports should be re-exported from the entry file. 52 */ 53 includeInEntry: boolean | ((symbol: Symbol) => boolean); 54}; 55 56export type UserCommentsOption = { 57 /** 58 * Whether to add comments to the generated code. 59 * 60 * @default true 61 */ 62 comments?: boolean; 63}; 64export type CommentsOption = { 65 /** 66 * Whether to add comments to the generated code. 67 */ 68 comments: boolean; 69}; 70 71export type NamingOptions = { 72 /** 73 * Casing convention for generated names. 74 */ 75 case: Casing; 76 /** 77 * Naming pattern for generated names. 78 */ 79 name: NameTransformer; 80}; 81 82/** 83 * Base output shape all packages must satisfy. 84 */ 85export interface BaseUserOutput { 86 /** 87 * Defines casing of the output fields. By default, we preserve `input` 88 * values as data transforms incur a performance penalty at runtime. 89 * 90 * @default undefined 91 */ 92 case?: Casing; 93 /** 94 * Clean the `output` folder on every run? If disabled, this folder may 95 * be used to store additional files. The default option is `true` to 96 * reduce the risk of keeping outdated files around when configuration, 97 * input, or package version changes. 98 * 99 * @default true 100 */ 101 clean?: boolean; 102 /** 103 * Whether to generate an entry file that re-exports symbols for convenient imports. 104 * 105 * Plugins control their inclusion via `includeInEntry`. 106 * 107 * @default true 108 */ 109 entryFile?: boolean; 110 /** 111 * Optional function to transform file names before they are used. 112 * 113 * @param name The original file name. 114 * @returns The transformed file name. 115 * @default '{{name}}' 116 */ 117 fileName?: 118 | NameTransformer 119 | { 120 /** 121 * Casing convention for generated names. 122 * 123 * @default 'preserve' 124 */ 125 case?: Casing; 126 /** 127 * Naming pattern for generated names. 128 * 129 * @default '{{name}}' 130 */ 131 name?: NameTransformer; 132 /** 133 * Suffix to append to file names (before the extension). For example, 134 * with a suffix of `.gen`, `example.ts` becomes `example.gen.ts`. 135 * 136 * @default '.gen' 137 * @example 138 * // Given a suffix of `.gen` 139 * 'index.ts' -> 'index.ts' (index files are not renamed) 140 * 'user.ts' -> 'user.gen.ts' 141 * 'order.gen.ts' -> 'order.gen.ts' (files already containing the suffix are not renamed) 142 */ 143 suffix?: string | null; 144 }; 145 /** 146 * Text to include at the top of every generated file. 147 */ 148 header?: OutputHeader; 149 /** 150 * Whether to generate an entry file that re-exports symbols for convenient imports. 151 * 152 * Plugins control their inclusion via `includeInEntry`. 153 * 154 * @default true 155 * @deprecated use `entryFile` instead 156 */ 157 indexFile?: boolean; 158 /** 159 * Optional name conflict resolver to customize how naming conflicts 160 * are handled. 161 */ 162 nameConflictResolver?: NameConflictResolver; 163 /** 164 * The absolute path to the output folder. 165 */ 166 path: string; 167 /** 168 * Optional function to transform module specifiers. 169 * 170 * @default undefined 171 */ 172 resolveModuleName?: (moduleName: string) => string | undefined; 173 /** 174 * Configuration for generating a copy of the input source used to produce this output. 175 * 176 * Set to `false` to skip generating the source, or `true` to use defaults. 177 * 178 * You can also provide a configuration object to further customize behavior. 179 * 180 * @default false 181 */ 182 source?: boolean | UserSourceConfig; 183} 184 185/** 186 * Base output shape all packages must satisfy. 187 */ 188export interface BaseOutput { 189 /** 190 * Defines casing of the output fields. By default, we preserve `input` 191 * values as data transforms incur a performance penalty at runtime. 192 */ 193 case: Casing | undefined; 194 /** 195 * Clean the `output` folder on every run? If disabled, this folder may 196 * be used to store additional files. The default option is `true` to 197 * reduce the risk of keeping outdated files around when configuration, 198 * input, or package version changes. 199 */ 200 clean: boolean; 201 /** 202 * Whether to generate an entry file that re-exports symbols for convenient imports. 203 */ 204 entryFile: boolean; 205 /** 206 * Optional function to transform file names before they are used. 207 * 208 * @param name The original file name. 209 * @returns The transformed file name. 210 */ 211 fileName: NamingOptions & { 212 /** 213 * Suffix to append to file names (before the extension). For example, 214 * with a suffix of `.gen`, `example.ts` becomes `example.gen.ts`. 215 * 216 * @example 217 * // Given a suffix of `.gen` 218 * 'index.ts' -> 'index.ts' (index files are not renamed) 219 * 'user.ts' -> 'user.gen.ts' 220 * 'order.gen.ts' -> 'order.gen.ts' (files already containing the suffix are not renamed) 221 */ 222 suffix: string | null; 223 }; 224 /** 225 * Text to include at the top of every generated file. 226 */ 227 header: OutputHeader; 228 /** 229 * Whether to generate an entry file that re-exports symbols for convenient imports. 230 * 231 * @deprecated use `entryFile` instead 232 */ 233 indexFile: boolean; 234 /** 235 * Optional name conflict resolver to customize how naming conflicts 236 * are handled. 237 */ 238 nameConflictResolver: NameConflictResolver | undefined; 239 /** 240 * The absolute path to the output folder. 241 */ 242 path: string; 243 /** 244 * Post-processing commands to run on the output folder, executed in order. 245 */ 246 postProcess: ReadonlyArray<PostProcessor>; 247 /** 248 * Optional function to transform module specifiers. 249 */ 250 resolveModuleName: ((moduleName: string) => string | undefined) | undefined; 251 /** 252 * Configuration for generating a copy of the input source used to produce this output. 253 */ 254 source: SourceConfig; 255} 256 257/** 258 * Core configuration shared across all packages. 259 */ 260export type BaseUserConfig<TOutput extends BaseUserOutput> = { 261 /** 262 * Path to the config file. Set this value if you don't use the default 263 * config file name, or it's not located in the project root. 264 */ 265 configFile?: string; 266 /** 267 * Skip writing files to disk? 268 * 269 * @default false 270 */ 271 dryRun?: boolean; 272 /** 273 * Path to the OpenAPI specification. This can be: 274 * - path 275 * - URL 276 * - API registry shorthand 277 * 278 * Both JSON and YAML file formats are supported. You can also pass the parsed 279 * object directly if you're fetching the file yourself. 280 * 281 * Alternatively, you can define a configuration object with more options. 282 * 283 * If you define an array, we will generate a single output from multiple 284 * inputs. If you define an array of outputs with the same length, we will 285 * generate multiple outputs, one for each input. 286 */ 287 input: MaybeArray<UserInput | Required<UserInput>['path']>; 288 /** 289 * Show an interactive error reporting tool when the program crashes? You 290 * generally want to keep this disabled (default). 291 * 292 * @default false 293 */ 294 interactive?: boolean; 295 /** 296 * The relative location of the logs folder. 297 * 298 * @default process.cwd() 299 */ 300 logs?: string | Logs; 301 /** 302 * Path to the output folder. 303 * 304 * If you define an array of outputs with the same length as inputs, we will 305 * generate multiple outputs, one for each input. 306 */ 307 output: MaybeArray<string | TOutput>; 308 /** 309 * Customize how the input is parsed and transformed before it's passed to 310 * plugins. 311 */ 312 parser?: UserParser; 313 /** 314 * @deprecated use `input.watch` instead 315 */ 316 watch?: UserWatch; 317}; 318 319/** 320 * Core configuration shared across all packages. 321 */ 322export type BaseConfig<TUserConfig extends object, TOutput extends BaseOutput> = Omit< 323 Required<TUserConfig>, 324 'input' | 'logs' | 'output' | 'parser' | 'plugins' | 'watch' 325> & { 326 /** 327 * Path to the input specification. 328 */ 329 input: ReadonlyArray<Input>; 330 logs: Logs; 331 /** 332 * Path to the output folder. 333 */ 334 output: TOutput; 335 /** 336 * Customize how the input is parsed and transformed before it's passed to 337 * plugins. 338 */ 339 parser: Parser; 340 // Loose types - packages override via intersection 341 pluginOrder: ReadonlyArray<string>; 342 plugins: Record<string, Plugin.Config<Plugin.Types> | undefined>; 343}; 344 345/** 346 * For shared utilities that operate on any config. 347 */ 348export type AnyConfig = BaseConfig<Record<string, unknown>, BaseOutput>;