@goast/core 0.6.0-beta.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/_dnt.shims.ts +69 -0
- package/src/mod.ts +35 -0
- package/src/src/ast/node.ts +46 -0
- package/src/src/codegen/config.ts +50 -0
- package/src/src/codegen/endpoints-generator.ts +72 -0
- package/src/src/codegen/generator.ts +192 -0
- package/src/src/codegen/internal-utils.ts +6 -0
- package/src/src/codegen/schemas-generator.ts +87 -0
- package/src/src/codegen/services-generator.ts +77 -0
- package/src/src/codegen/types.ts +37 -0
- package/src/src/collect/collector.ts +142 -0
- package/src/src/collect/helpers.ts +36 -0
- package/src/src/collect/types.ts +21 -0
- package/src/src/internal-utils.ts +5 -0
- package/src/src/parse/deref-proxy.ts +70 -0
- package/src/src/parse/openapi-types.ts +953 -0
- package/src/src/parse/parser.ts +206 -0
- package/src/src/parse/types.ts +28 -0
- package/src/src/transform/api-types.ts +259 -0
- package/src/src/transform/helpers.ts +251 -0
- package/src/src/transform/transform-document.ts +35 -0
- package/src/src/transform/transform-endpoint.ts +332 -0
- package/src/src/transform/transform-schema.ts +232 -0
- package/src/src/transform/transformer.ts +53 -0
- package/src/src/transform/types.ts +52 -0
- package/src/src/utils/action-provider.ts +58 -0
- package/src/src/utils/api-data.utils.ts +48 -0
- package/src/src/utils/array.utils.ts +7 -0
- package/src/src/utils/asset-manager.ts +51 -0
- package/src/src/utils/class.utils.ts +40 -0
- package/src/src/utils/common.utils.ts +86 -0
- package/src/src/utils/condition.ts +7 -0
- package/src/src/utils/endpoint.utils.ts +10 -0
- package/src/src/utils/error.utils.ts +3 -0
- package/src/src/utils/factory.ts +42 -0
- package/src/src/utils/file-system.utils.ts +97 -0
- package/src/src/utils/generator.utils.ts +6 -0
- package/src/src/utils/object.utils.ts +32 -0
- package/src/src/utils/schema.utils.ts +240 -0
- package/src/src/utils/source-builder.ts +597 -0
- package/src/src/utils/string-builder/options.ts +19 -0
- package/src/src/utils/string-builder/string-builder.ts +165 -0
- package/src/src/utils/string-builder/utils.ts +69 -0
- package/src/src/utils/string.utils.ts +394 -0
- package/src/src/utils/string.utils.types.ts +179 -0
- package/src/src/utils/type.utils.ts +93 -0
- package/src/src/utils/yaml-info.ts +48 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goast/core",
|
|
3
|
-
"version": "0.6.0
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A tool designed to transform OpenAPI specifications into various forms with flexibility and extensibility at its core.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Marc Schmidt (MaSch0212)",
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { fetch, File, FormData, Headers, Request, Response } from "undici";
|
|
2
|
+
export { fetch, File, FormData, Headers, Request, Response, type BodyInit, type HeadersInit, type ReferrerPolicy, type RequestInit, type RequestCache, type RequestMode, type RequestRedirect, type ResponseInit } from "undici";
|
|
3
|
+
|
|
4
|
+
const dntGlobals = {
|
|
5
|
+
fetch,
|
|
6
|
+
File,
|
|
7
|
+
FormData,
|
|
8
|
+
Headers,
|
|
9
|
+
Request,
|
|
10
|
+
Response,
|
|
11
|
+
};
|
|
12
|
+
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
13
|
+
|
|
14
|
+
function createMergeProxy<T extends object, U extends object>(
|
|
15
|
+
baseObj: T,
|
|
16
|
+
extObj: U,
|
|
17
|
+
): Omit<T, keyof U> & U {
|
|
18
|
+
return new Proxy(baseObj, {
|
|
19
|
+
get(_target, prop, _receiver) {
|
|
20
|
+
if (prop in extObj) {
|
|
21
|
+
return (extObj as any)[prop];
|
|
22
|
+
} else {
|
|
23
|
+
return (baseObj as any)[prop];
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
set(_target, prop, value) {
|
|
27
|
+
if (prop in extObj) {
|
|
28
|
+
delete (extObj as any)[prop];
|
|
29
|
+
}
|
|
30
|
+
(baseObj as any)[prop] = value;
|
|
31
|
+
return true;
|
|
32
|
+
},
|
|
33
|
+
deleteProperty(_target, prop) {
|
|
34
|
+
let success = false;
|
|
35
|
+
if (prop in extObj) {
|
|
36
|
+
delete (extObj as any)[prop];
|
|
37
|
+
success = true;
|
|
38
|
+
}
|
|
39
|
+
if (prop in baseObj) {
|
|
40
|
+
delete (baseObj as any)[prop];
|
|
41
|
+
success = true;
|
|
42
|
+
}
|
|
43
|
+
return success;
|
|
44
|
+
},
|
|
45
|
+
ownKeys(_target) {
|
|
46
|
+
const baseKeys = Reflect.ownKeys(baseObj);
|
|
47
|
+
const extKeys = Reflect.ownKeys(extObj);
|
|
48
|
+
const extKeysSet = new Set(extKeys);
|
|
49
|
+
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
50
|
+
},
|
|
51
|
+
defineProperty(_target, prop, desc) {
|
|
52
|
+
if (prop in extObj) {
|
|
53
|
+
delete (extObj as any)[prop];
|
|
54
|
+
}
|
|
55
|
+
Reflect.defineProperty(baseObj, prop, desc);
|
|
56
|
+
return true;
|
|
57
|
+
},
|
|
58
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
59
|
+
if (prop in extObj) {
|
|
60
|
+
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
61
|
+
} else {
|
|
62
|
+
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
has(_target, prop) {
|
|
66
|
+
return prop in extObj || prop in baseObj;
|
|
67
|
+
},
|
|
68
|
+
}) as any;
|
|
69
|
+
}
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export * from './src/ast/node.js';
|
|
2
|
+
|
|
3
|
+
export * from './src/codegen/config.js';
|
|
4
|
+
export * from './src/codegen/endpoints-generator.js';
|
|
5
|
+
export * from './src/codegen/generator.js';
|
|
6
|
+
export * from './src/codegen/schemas-generator.js';
|
|
7
|
+
export * from './src/codegen/services-generator.js';
|
|
8
|
+
export * from './src/codegen/types.js';
|
|
9
|
+
|
|
10
|
+
export * from './src/parse/openapi-types.js';
|
|
11
|
+
export * from './src/parse/parser.js';
|
|
12
|
+
export * from './src/parse/types.js';
|
|
13
|
+
|
|
14
|
+
export * from './src/transform/api-types.js';
|
|
15
|
+
export * from './src/transform/transformer.js';
|
|
16
|
+
|
|
17
|
+
export * from './src/utils/api-data.utils.js';
|
|
18
|
+
export * from './src/utils/array.utils.js';
|
|
19
|
+
export * from './src/utils/asset-manager.js';
|
|
20
|
+
export * from './src/utils/class.utils.js';
|
|
21
|
+
export * from './src/utils/common.utils.js';
|
|
22
|
+
export * from './src/utils/endpoint.utils.js';
|
|
23
|
+
export * from './src/utils/error.utils.js';
|
|
24
|
+
export * from './src/utils/factory.js';
|
|
25
|
+
export * from './src/utils/file-system.utils.js';
|
|
26
|
+
export * from './src/utils/generator.utils.js';
|
|
27
|
+
export * from './src/utils/object.utils.js';
|
|
28
|
+
export * from './src/utils/schema.utils.js';
|
|
29
|
+
export * from './src/utils/source-builder.js';
|
|
30
|
+
export * from './src/utils/string-builder/options.js';
|
|
31
|
+
export { type AppendParam, StringBuilder } from './src/utils/string-builder/string-builder.js';
|
|
32
|
+
export * from './src/utils/string-builder/utils.js';
|
|
33
|
+
export * from './src/utils/string.utils.js';
|
|
34
|
+
export * from './src/utils/string.utils.types.js';
|
|
35
|
+
export * from './src/utils/type.utils.js';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { StringBuilder } from '../utils/string-builder/string-builder.js';
|
|
2
|
+
import type { AppendValue } from '../utils/string-builder/utils.js';
|
|
3
|
+
import type { Prettify } from '../utils/type.utils.js';
|
|
4
|
+
|
|
5
|
+
const NODE_HISTORY_SYMBOL = Symbol('node-history');
|
|
6
|
+
type BuilderWithHistory<T extends StringBuilder = StringBuilder> = T & { [NODE_HISTORY_SYMBOL]?: AstNode[] };
|
|
7
|
+
|
|
8
|
+
export type AstNodeInject<TBuilder extends StringBuilder, TInjects extends string> = {
|
|
9
|
+
[K in `${'after' | 'before'}${Capitalize<TInjects>}`]?: AppendValue<TBuilder>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type AstNodeOptions<
|
|
13
|
+
// deno-lint-ignore no-explicit-any
|
|
14
|
+
TBase extends abstract new (...args: any) => any,
|
|
15
|
+
// deno-lint-ignore ban-types
|
|
16
|
+
TExtensions extends object = {},
|
|
17
|
+
> = Prettify<TExtensions & ConstructorParameters<TBase>[0]>;
|
|
18
|
+
|
|
19
|
+
export abstract class AstNode<TBuilder extends StringBuilder = StringBuilder, TInjects extends string = never> {
|
|
20
|
+
public inject: AstNodeInject<TBuilder, TInjects | ''>;
|
|
21
|
+
|
|
22
|
+
constructor(options: { inject?: AstNode<TBuilder, TInjects>['inject'] }) {
|
|
23
|
+
this.inject = options.inject ?? {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public write(builder: TBuilder): void {
|
|
27
|
+
let history = (builder as BuilderWithHistory)[NODE_HISTORY_SYMBOL];
|
|
28
|
+
if (!history) {
|
|
29
|
+
history = [];
|
|
30
|
+
(builder as BuilderWithHistory)[NODE_HISTORY_SYMBOL] = history;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
history.push(this);
|
|
34
|
+
builder.append(this.inject.before);
|
|
35
|
+
this.onWrite(builder);
|
|
36
|
+
builder.append(this.inject.after);
|
|
37
|
+
history.pop();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected getParentNode(builder: TBuilder): AstNode | undefined {
|
|
41
|
+
const history = (builder as BuilderWithHistory)[NODE_HISTORY_SYMBOL];
|
|
42
|
+
return history?.at(-2);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected abstract onWrite(builder: TBuilder): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { OpenApiParserOptions } from '../parse/types.js';
|
|
2
|
+
import { defaultOpenApiTransformerOptions } from '../transform/types.js';
|
|
3
|
+
import { defaultSourceBuilderOptions, type SourceBuilderOptions } from '../utils/source-builder.js';
|
|
4
|
+
|
|
5
|
+
export type OpenApiGeneratorConfig =
|
|
6
|
+
& SourceBuilderOptions
|
|
7
|
+
& OpenApiParserOptions
|
|
8
|
+
& {
|
|
9
|
+
/**
|
|
10
|
+
* The directory where the generated files should be saved.
|
|
11
|
+
* @default 'generated'
|
|
12
|
+
*/
|
|
13
|
+
outputDir: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether to clear the output directory before generating the files.
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
19
|
+
clearOutputDir: boolean;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How to handle existing files in the output directory.
|
|
23
|
+
* - 'override': overwrite existing files.
|
|
24
|
+
* - 'skip': skip generating files that already exist.
|
|
25
|
+
* - 'error': throw an error if a file already exists.
|
|
26
|
+
* - 'count': write to a new path derived by inserting `_1` before the file extension (`_2`, `_3`, ... if that
|
|
27
|
+
* path is also taken). A counted file is written under a name no other generated file references. The
|
|
28
|
+
* generators compute a schema's file path from its name and use that same path when they emit an import or
|
|
29
|
+
* type reference, so the reference still points at the first file written. `'count'` makes a collision
|
|
30
|
+
* inspectable instead of fatal; it does not make the output correct. It also has a second failure mode
|
|
31
|
+
* unrelated to collisions: with `clearOutputDir: false`, every regeneration finds the previous run's file
|
|
32
|
+
* already on disk and counts up again, so re-running the generator against the same output directory silently
|
|
33
|
+
* accumulates another `_N` file per schema on every run, forever, with no error to signal it.
|
|
34
|
+
*
|
|
35
|
+
* Is is recommended to use `'error'` to prevent issues of two schemas generating the same file and overwriting each other.
|
|
36
|
+
* If `clearOutputDir` is set to `false`, it is recommended to use `'override'` instead of `'count'` to ensure
|
|
37
|
+
* that the generated files are up to date without accumulating stale counted copies on every run.
|
|
38
|
+
* @default 'error'
|
|
39
|
+
*/
|
|
40
|
+
existingFileBehavior: 'override' | 'skip' | 'error' | 'count';
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const defaultOpenApiGeneratorConfig: OpenApiGeneratorConfig = {
|
|
44
|
+
...defaultSourceBuilderOptions,
|
|
45
|
+
...defaultOpenApiTransformerOptions,
|
|
46
|
+
|
|
47
|
+
outputDir: 'generated',
|
|
48
|
+
clearOutputDir: true,
|
|
49
|
+
existingFileBehavior: 'error',
|
|
50
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ApiEndpoint } from '../transform/api-types.js';
|
|
2
|
+
import type { MaybePromise } from '../utils/type.utils.js';
|
|
3
|
+
import { type DefaultGenerationProviderConfig, OpenApiGenerationProviderBase } from './generator.js';
|
|
4
|
+
import { addSourceIfTest } from './internal-utils.js';
|
|
5
|
+
import type {
|
|
6
|
+
AnyConfig,
|
|
7
|
+
OpenApiGenerationProviderContext,
|
|
8
|
+
OpenApiGeneratorContext,
|
|
9
|
+
OpenApiGeneratorInput,
|
|
10
|
+
OpenApiGeneratorOutput,
|
|
11
|
+
} from './types.js';
|
|
12
|
+
|
|
13
|
+
export type OpenApiEndpointsGenerationProviderContext<
|
|
14
|
+
TInput extends OpenApiGeneratorInput,
|
|
15
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
16
|
+
TConfig extends AnyConfig,
|
|
17
|
+
TEndpointOutput extends OpenApiGeneratorOutput,
|
|
18
|
+
> = OpenApiGenerationProviderContext<TInput, TConfig> & {
|
|
19
|
+
existingEndpointResults: Map<string, TEndpointOutput>;
|
|
20
|
+
output: TOutput;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export abstract class OpenApiEndpointsGenerationProviderBase<
|
|
24
|
+
TInput extends OpenApiGeneratorInput,
|
|
25
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
26
|
+
TConfig extends AnyConfig,
|
|
27
|
+
TEndpointOutput extends OpenApiGeneratorOutput,
|
|
28
|
+
TContext extends OpenApiEndpointsGenerationProviderContext<TInput, TOutput, TConfig, TEndpointOutput>,
|
|
29
|
+
> extends OpenApiGenerationProviderBase<TInput, TOutput, TConfig, TContext> {
|
|
30
|
+
protected override getProviderContext(
|
|
31
|
+
context: OpenApiGeneratorContext<TInput>,
|
|
32
|
+
config: Partial<TConfig> | undefined,
|
|
33
|
+
defaultConfig: DefaultGenerationProviderConfig<TConfig>,
|
|
34
|
+
): OpenApiEndpointsGenerationProviderContext<TInput, TOutput, TConfig, TEndpointOutput> {
|
|
35
|
+
const ctx = super.getProviderContext(context, config, defaultConfig);
|
|
36
|
+
return Object.assign(ctx, {
|
|
37
|
+
existingEndpointResults: new Map<string, TEndpointOutput>(),
|
|
38
|
+
output: this.initResult(ctx),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected override async onGenerate(ctx: TContext): Promise<TOutput> {
|
|
43
|
+
for (const endpoint of ctx.data.endpoints) {
|
|
44
|
+
await this.getEndpointResult(ctx, endpoint);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await this.generateAdditionalFiles(ctx);
|
|
48
|
+
|
|
49
|
+
return ctx.output;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public async getEndpointResult(ctx: TContext, endpoint: ApiEndpoint): Promise<TEndpointOutput> {
|
|
53
|
+
const existingResult = ctx.existingEndpointResults.get(endpoint.id);
|
|
54
|
+
if (existingResult) return existingResult;
|
|
55
|
+
|
|
56
|
+
const result = await this.generateEndpoint(ctx, endpoint);
|
|
57
|
+
addSourceIfTest(ctx.config, result, () => `${endpoint.$src.file}#${endpoint.$src.path}`);
|
|
58
|
+
this.addEndpointResult(ctx, endpoint, result);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected abstract initResult(ctx: OpenApiGenerationProviderContext<TInput, TConfig>): TOutput;
|
|
63
|
+
|
|
64
|
+
protected abstract generateEndpoint(ctx: TContext, endpoint: ApiEndpoint): Promise<TEndpointOutput>;
|
|
65
|
+
|
|
66
|
+
protected abstract addEndpointResult(ctx: TContext, endpoint: ApiEndpoint, result: TEndpointOutput): void;
|
|
67
|
+
|
|
68
|
+
// deno-lint-ignore no-unused-vars
|
|
69
|
+
protected generateAdditionalFiles(ctx: TContext): MaybePromise<void> {
|
|
70
|
+
// Override if needed
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
|
|
6
|
+
import { OpenApiParser } from '../parse/parser.js';
|
|
7
|
+
import type { ApiData } from '../transform/api-types.js';
|
|
8
|
+
import { ActionProvider } from '../utils/action-provider.js';
|
|
9
|
+
import { type DirectoryScanOptions, getFiles } from '../utils/file-system.utils.js';
|
|
10
|
+
import type { EmptyConstructor, Merge } from '../utils/type.utils.js';
|
|
11
|
+
import { defaultOpenApiGeneratorConfig, type OpenApiGeneratorConfig } from './config.js';
|
|
12
|
+
import type {
|
|
13
|
+
AnyConfig,
|
|
14
|
+
OpenApiGenerationProvider,
|
|
15
|
+
OpenApiGenerationProviderContext,
|
|
16
|
+
OpenApiGenerationProviderFn,
|
|
17
|
+
OpenApiGeneratorContext,
|
|
18
|
+
OpenApiGeneratorInput,
|
|
19
|
+
OpenApiGeneratorOutput,
|
|
20
|
+
} from './types.js';
|
|
21
|
+
|
|
22
|
+
type OpenApiGenerationProviders = {
|
|
23
|
+
provider: ActionProvider<OpenApiGenerationProviderFn>;
|
|
24
|
+
config: AnyConfig | undefined;
|
|
25
|
+
}[];
|
|
26
|
+
|
|
27
|
+
type VInput<TActual, TExpected> = TExpected extends TActual ? TActual
|
|
28
|
+
: { __error: 'The current output of the generator does not satisfy the input of this generator.' };
|
|
29
|
+
|
|
30
|
+
class _OpenApiGenerator<TOutput extends OpenApiGeneratorInput> {
|
|
31
|
+
private readonly _providers: OpenApiGenerationProviders;
|
|
32
|
+
private readonly _config: OpenApiGeneratorConfig;
|
|
33
|
+
private readonly _parser: OpenApiParser;
|
|
34
|
+
|
|
35
|
+
constructor(config: OpenApiGeneratorConfig, providers: OpenApiGenerationProviders, parser: OpenApiParser) {
|
|
36
|
+
this._providers = providers;
|
|
37
|
+
this._config = config;
|
|
38
|
+
this._parser = parser;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public use<PInput extends OpenApiGeneratorInput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(
|
|
42
|
+
provider: ActionProvider<OpenApiGenerationProviderFn<VInput<PInput, TOutput>, POutput, PConfig>>,
|
|
43
|
+
config?: Partial<PConfig>,
|
|
44
|
+
): _OpenApiGenerator<Merge<[TOutput, POutput]>> {
|
|
45
|
+
// deno-lint-ignore no-explicit-any
|
|
46
|
+
this._providers.push({ provider: provider as any, config });
|
|
47
|
+
return new _OpenApiGenerator<Merge<[TOutput, POutput]>>(this._config, [...this._providers], this._parser);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public useType<
|
|
51
|
+
PInput extends OpenApiGeneratorInput,
|
|
52
|
+
POutput extends OpenApiGeneratorOutput,
|
|
53
|
+
PConfig extends AnyConfig,
|
|
54
|
+
>(
|
|
55
|
+
type: EmptyConstructor<OpenApiGenerationProvider<VInput<PInput, TOutput>, POutput, PConfig>>,
|
|
56
|
+
config?: Partial<PConfig>,
|
|
57
|
+
): _OpenApiGenerator<Merge<[TOutput, POutput]>> {
|
|
58
|
+
return this.use(ActionProvider.fromType(type, 'generate'), config);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public useFn<PInput extends TOutput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(
|
|
62
|
+
fn: OpenApiGenerationProviderFn<VInput<PInput, TOutput>, POutput, PConfig>,
|
|
63
|
+
config?: Partial<PConfig>,
|
|
64
|
+
): _OpenApiGenerator<Merge<[TOutput, POutput]>> {
|
|
65
|
+
return this.use(ActionProvider.fromFn(fn), config);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public useValue<PInput extends TOutput, POutput extends OpenApiGeneratorOutput, PConfig extends AnyConfig>(
|
|
69
|
+
value: OpenApiGenerationProvider<VInput<PInput, TOutput>, POutput, PConfig>,
|
|
70
|
+
config?: Partial<PConfig>,
|
|
71
|
+
): _OpenApiGenerator<Merge<[TOutput, POutput]>> {
|
|
72
|
+
return this.use(ActionProvider.fromValue(value, 'generate'), config);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public async generate<T extends ApiData>(data: T): Promise<TOutput> {
|
|
76
|
+
const absOutputPath = resolve(this._config.outputDir);
|
|
77
|
+
if (this._config.clearOutputDir) {
|
|
78
|
+
await fs.emptyDir(absOutputPath);
|
|
79
|
+
} else {
|
|
80
|
+
await fs.ensureDir(absOutputPath);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this._providers.forEach(({ config }) => {
|
|
84
|
+
if (config && 'outputDir' in config && config.outputDir && typeof config.outputDir === 'string') {
|
|
85
|
+
const providerOutputDir = resolve(config.outputDir);
|
|
86
|
+
if (this._config.clearOutputDir) {
|
|
87
|
+
fs.emptyDirSync(providerOutputDir);
|
|
88
|
+
} else {
|
|
89
|
+
fs.ensureDirSync(providerOutputDir);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
let input = {} as TOutput;
|
|
95
|
+
for (const generator of this._providers) {
|
|
96
|
+
const context = {
|
|
97
|
+
data,
|
|
98
|
+
input,
|
|
99
|
+
config: this._config,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const result = await generator.provider.run(context, generator.config);
|
|
103
|
+
if (result) {
|
|
104
|
+
input = mergeDeep(input, result);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return input;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public async parseAndGenerate(...fileNames: (string | string[])[]): Promise<TOutput> {
|
|
112
|
+
const data = await this._parser.parseApisAndTransform(...fileNames);
|
|
113
|
+
return await this.generate(data);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public async parseAndGenerateFromDir(dir: string, options?: Partial<DirectoryScanOptions>): Promise<TOutput> {
|
|
117
|
+
const filter = (file: string) =>
|
|
118
|
+
(file.endsWith('.yml') || file.endsWith('.yaml') || file.endsWith('.json')) && (options?.filter?.(file) ?? true);
|
|
119
|
+
return this.parseAndGenerate(...(await getFiles(dir, { ...options, filter })));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// deno-lint-ignore ban-types
|
|
124
|
+
export class OpenApiGenerator extends _OpenApiGenerator<{}> {
|
|
125
|
+
constructor(config?: Partial<OpenApiGeneratorConfig>) {
|
|
126
|
+
super(
|
|
127
|
+
{ ...defaultOpenApiGeneratorConfig, ...config },
|
|
128
|
+
[],
|
|
129
|
+
new OpenApiParser({ ...defaultOpenApiGeneratorConfig, ...config }),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function mergeDeep<T extends Record<string, unknown>, U extends Record<string, unknown>[]>(
|
|
135
|
+
target: T,
|
|
136
|
+
...sources: U
|
|
137
|
+
): T & U[number] {
|
|
138
|
+
if (!sources.length) return target;
|
|
139
|
+
const source = sources.shift();
|
|
140
|
+
|
|
141
|
+
for (const key in source) {
|
|
142
|
+
const value = source[key];
|
|
143
|
+
if (value && typeof value === 'object') {
|
|
144
|
+
if (!target[key]) Object.assign(target, { [key]: value });
|
|
145
|
+
else mergeDeep(target[key] as Record<string, unknown>, value as Record<string, unknown>);
|
|
146
|
+
} else if (Array.isArray(value)) {
|
|
147
|
+
if (!target[key]) Object.assign(target, { [key]: value });
|
|
148
|
+
else Object.assign(target, { [key]: [...(target[key] as Iterable<unknown>), ...value] });
|
|
149
|
+
} else {
|
|
150
|
+
Object.assign(target, { [key]: value });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return mergeDeep(target, ...sources);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type DefaultGenerationProviderConfig<T extends AnyConfig> =
|
|
158
|
+
& Omit<T, keyof OpenApiGeneratorConfig>
|
|
159
|
+
& Partial<Pick<T, keyof OpenApiGeneratorConfig>>;
|
|
160
|
+
|
|
161
|
+
export abstract class OpenApiGenerationProviderBase<
|
|
162
|
+
TInput extends OpenApiGeneratorInput,
|
|
163
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
164
|
+
TConfig extends AnyConfig,
|
|
165
|
+
TContext extends OpenApiGenerationProviderContext<TInput, TConfig>,
|
|
166
|
+
> implements OpenApiGenerationProvider<TInput, TOutput, TConfig> {
|
|
167
|
+
public async generate(
|
|
168
|
+
context: OpenApiGeneratorContext<TInput>,
|
|
169
|
+
config?: Partial<TConfig> | undefined,
|
|
170
|
+
): Promise<TOutput> {
|
|
171
|
+
const ctx = await this.buildContext(context, config);
|
|
172
|
+
return await this.onGenerate(ctx);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
protected getProviderContext(
|
|
176
|
+
context: OpenApiGeneratorContext<TInput>,
|
|
177
|
+
config: Partial<TConfig> | undefined,
|
|
178
|
+
defaultConfig: DefaultGenerationProviderConfig<TConfig>,
|
|
179
|
+
): OpenApiGenerationProviderContext<TInput, TConfig> {
|
|
180
|
+
const c = { ...context.config, ...defaultConfig, ...config } as unknown as OpenApiGeneratorConfig & TConfig;
|
|
181
|
+
return Object.assign(context, {
|
|
182
|
+
config: c,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
protected abstract buildContext(
|
|
187
|
+
context: OpenApiGeneratorContext<TInput>,
|
|
188
|
+
config?: Partial<TConfig> | undefined,
|
|
189
|
+
): TContext | Promise<TContext>;
|
|
190
|
+
|
|
191
|
+
protected abstract onGenerate(ctx: TContext): TOutput | Promise<TOutput>;
|
|
192
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ApiSchema } from '../transform/api-types.js';
|
|
2
|
+
import { getSchemaReference } from '../utils/schema.utils.js';
|
|
3
|
+
import type { MaybePromise } from '../utils/type.utils.js';
|
|
4
|
+
import { type DefaultGenerationProviderConfig, OpenApiGenerationProviderBase } from './generator.js';
|
|
5
|
+
import { addSourceIfTest } from './internal-utils.js';
|
|
6
|
+
import type {
|
|
7
|
+
AnyConfig,
|
|
8
|
+
OpenApiGenerationProviderContext,
|
|
9
|
+
OpenApiGeneratorContext,
|
|
10
|
+
OpenApiGeneratorInput,
|
|
11
|
+
OpenApiGeneratorOutput,
|
|
12
|
+
} from './types.js';
|
|
13
|
+
|
|
14
|
+
export type OpenApiSchemasGenerationProviderContext<
|
|
15
|
+
TInput extends OpenApiGeneratorInput,
|
|
16
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
17
|
+
TConfig extends AnyConfig,
|
|
18
|
+
TSchemaOutput extends OpenApiGeneratorOutput,
|
|
19
|
+
> = OpenApiGenerationProviderContext<TInput, TConfig> & {
|
|
20
|
+
existingSchemaResults: Map<string, TSchemaOutput>;
|
|
21
|
+
output: TOutput;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export abstract class OpenApiSchemasGenerationProviderBase<
|
|
25
|
+
TInput extends OpenApiGeneratorInput,
|
|
26
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
27
|
+
TConfig extends AnyConfig,
|
|
28
|
+
TSchemaOutput extends OpenApiGeneratorOutput,
|
|
29
|
+
TContext extends OpenApiSchemasGenerationProviderContext<TInput, TOutput, TConfig, TSchemaOutput>,
|
|
30
|
+
> extends OpenApiGenerationProviderBase<TInput, TOutput, TConfig, TContext> {
|
|
31
|
+
protected override getProviderContext(
|
|
32
|
+
context: OpenApiGeneratorContext<TInput>,
|
|
33
|
+
config: Partial<TConfig> | undefined,
|
|
34
|
+
defaultConfig: DefaultGenerationProviderConfig<TConfig>,
|
|
35
|
+
): OpenApiSchemasGenerationProviderContext<TInput, TOutput, TConfig, TSchemaOutput> {
|
|
36
|
+
const ctx = super.getProviderContext(context, config, defaultConfig);
|
|
37
|
+
return Object.assign(ctx, {
|
|
38
|
+
existingSchemaResults: new Map<string, TSchemaOutput>(),
|
|
39
|
+
output: this.initResult(ctx),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected override async onGenerate(ctx: TContext): Promise<TOutput> {
|
|
44
|
+
for (const schema of ctx.data.schemas) {
|
|
45
|
+
await this.getSchemaResult(ctx, schema);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
await this.generateAdditionalFiles(ctx);
|
|
49
|
+
|
|
50
|
+
return ctx.output;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public async getSchemaResult(ctx: TContext, schema: ApiSchema): Promise<TSchemaOutput> {
|
|
54
|
+
const existingResult = ctx.existingSchemaResults.get(schema.id);
|
|
55
|
+
if (existingResult) return existingResult;
|
|
56
|
+
|
|
57
|
+
let result: TSchemaOutput;
|
|
58
|
+
const reference = this.getSchemaReference(ctx, schema);
|
|
59
|
+
if (reference) {
|
|
60
|
+
result = await this.getSchemaResult(ctx, reference);
|
|
61
|
+
} else {
|
|
62
|
+
result = await this.generateSchema(ctx, schema);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
addSourceIfTest(ctx.config, result, () => `${schema.$src.file}#${schema.$src.path}`);
|
|
66
|
+
ctx.existingSchemaResults.set(schema.id, result);
|
|
67
|
+
this.addSchemaResult(ctx, schema, result);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// deno-lint-ignore no-unused-vars
|
|
72
|
+
protected getSchemaReference(ctx: TContext, schema: ApiSchema): ApiSchema | undefined {
|
|
73
|
+
const ref = getSchemaReference(schema, ['description']);
|
|
74
|
+
return ref.id !== schema.id ? ref : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected abstract initResult(ctx: OpenApiGenerationProviderContext<TInput, TConfig>): TOutput;
|
|
78
|
+
|
|
79
|
+
protected abstract generateSchema(ctx: TContext, schema: ApiSchema): MaybePromise<TSchemaOutput>;
|
|
80
|
+
|
|
81
|
+
protected abstract addSchemaResult(ctx: TContext, schema: ApiSchema, result: TSchemaOutput): void;
|
|
82
|
+
|
|
83
|
+
// deno-lint-ignore no-unused-vars
|
|
84
|
+
protected generateAdditionalFiles(ctx: TContext): MaybePromise<void> {
|
|
85
|
+
// Override if needed
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ApiService } from '../transform/api-types.js';
|
|
2
|
+
import type { MaybePromise } from '../utils/type.utils.js';
|
|
3
|
+
import { type DefaultGenerationProviderConfig, OpenApiGenerationProviderBase } from './generator.js';
|
|
4
|
+
import { addSourceIfTest } from './internal-utils.js';
|
|
5
|
+
import type {
|
|
6
|
+
AnyConfig,
|
|
7
|
+
OpenApiGenerationProviderContext,
|
|
8
|
+
OpenApiGeneratorContext,
|
|
9
|
+
OpenApiGeneratorInput,
|
|
10
|
+
OpenApiGeneratorOutput,
|
|
11
|
+
} from './types.js';
|
|
12
|
+
|
|
13
|
+
export type OpenApiServicesGenerationProviderContext<
|
|
14
|
+
TInput extends OpenApiGeneratorInput,
|
|
15
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
16
|
+
TConfig extends AnyConfig,
|
|
17
|
+
TServiceOutput extends OpenApiGeneratorOutput,
|
|
18
|
+
> = OpenApiGenerationProviderContext<TInput, TConfig> & {
|
|
19
|
+
existingServiceResults: Map<string, TServiceOutput>;
|
|
20
|
+
output: TOutput;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export abstract class OpenApiServicesGenerationProviderBase<
|
|
24
|
+
TInput extends OpenApiGeneratorInput,
|
|
25
|
+
TOutput extends OpenApiGeneratorOutput,
|
|
26
|
+
TConfig extends AnyConfig,
|
|
27
|
+
TServiceOutput extends OpenApiGeneratorOutput,
|
|
28
|
+
TContext extends OpenApiServicesGenerationProviderContext<TInput, TOutput, TConfig, TServiceOutput>,
|
|
29
|
+
> extends OpenApiGenerationProviderBase<TInput, TOutput, TConfig, TContext> {
|
|
30
|
+
protected override getProviderContext(
|
|
31
|
+
context: OpenApiGeneratorContext<TInput>,
|
|
32
|
+
config: Partial<TConfig> | undefined,
|
|
33
|
+
defaultConfig: DefaultGenerationProviderConfig<TConfig>,
|
|
34
|
+
): OpenApiServicesGenerationProviderContext<TInput, TOutput, TConfig, TServiceOutput> {
|
|
35
|
+
const ctx = super.getProviderContext(context, config, defaultConfig);
|
|
36
|
+
return Object.assign(ctx, {
|
|
37
|
+
existingServiceResults: new Map<string, TServiceOutput>(),
|
|
38
|
+
output: this.initResult(ctx),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected override async onGenerate(ctx: TContext): Promise<TOutput> {
|
|
43
|
+
for (const service of ctx.data.services) {
|
|
44
|
+
await this.getServiceResult(ctx, service);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await this.generateAdditionalFiles(ctx);
|
|
48
|
+
|
|
49
|
+
return ctx.output;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected async getServiceResult(ctx: TContext, service: ApiService): Promise<TServiceOutput> {
|
|
53
|
+
const existingResult = ctx.existingServiceResults.get(service.id);
|
|
54
|
+
if (existingResult) return existingResult;
|
|
55
|
+
|
|
56
|
+
const result = await this.generateService(ctx, service);
|
|
57
|
+
|
|
58
|
+
addSourceIfTest(
|
|
59
|
+
ctx.config,
|
|
60
|
+
result,
|
|
61
|
+
() => service.$src ? `${service.$src.file}#${service.$src.path}` : `tag:${service.name}`,
|
|
62
|
+
);
|
|
63
|
+
this.addServiceResult(ctx, service, result);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected abstract initResult(ctx: OpenApiGenerationProviderContext<TInput, TConfig>): TOutput;
|
|
68
|
+
|
|
69
|
+
protected abstract generateService(ctx: TContext, service: ApiService): MaybePromise<TServiceOutput>;
|
|
70
|
+
|
|
71
|
+
protected abstract addServiceResult(ctx: TContext, service: ApiService, result: TServiceOutput): void;
|
|
72
|
+
|
|
73
|
+
// deno-lint-ignore no-unused-vars
|
|
74
|
+
protected generateAdditionalFiles(ctx: TContext): MaybePromise<void> {
|
|
75
|
+
// Override if needed
|
|
76
|
+
}
|
|
77
|
+
}
|