@goast/core 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/ast/node.d.ts +43 -0
- package/dist/src/ast/node.js +29 -0
- package/dist/src/codegen/config.d.ts +9 -0
- package/dist/src/codegen/config.js +4 -0
- package/dist/src/codegen/endpoints-generator.d.ts +32 -0
- package/dist/src/codegen/endpoints-generator.js +19 -0
- package/dist/src/codegen/generator.d.ts +108 -0
- package/dist/src/codegen/generator.js +103 -0
- package/dist/src/codegen/internal-utils.d.ts +8 -0
- package/dist/src/codegen/internal-utils.js +8 -0
- package/dist/src/codegen/schemas-generator.d.ts +33 -0
- package/dist/src/codegen/schemas-generator.js +20 -0
- package/dist/src/codegen/services-generator.d.ts +24 -0
- package/dist/src/codegen/services-generator.js +11 -0
- package/dist/src/codegen/types.d.ts +56 -0
- package/dist/src/collect/collector.d.ts +8 -0
- package/dist/src/collect/collector.js +8 -0
- package/dist/src/collect/helpers.d.ts +16 -0
- package/dist/src/collect/helpers.js +16 -0
- package/dist/src/collect/types.d.ts +28 -0
- package/dist/src/internal-utils.d.ts +7 -0
- package/dist/src/internal-utils.js +7 -0
- package/dist/src/parse/deref-proxy.d.ts +19 -0
- package/dist/src/parse/deref-proxy.js +19 -0
- package/dist/src/parse/openapi-types.d.ts +168 -0
- package/dist/src/parse/parser.d.ts +38 -0
- package/dist/src/parse/parser.js +38 -0
- package/dist/src/parse/types.d.ts +37 -0
- package/dist/src/parse/types.js +4 -0
- package/dist/src/transform/api-types.d.ts +408 -0
- package/dist/src/transform/helpers.d.ts +73 -0
- package/dist/src/transform/helpers.js +73 -0
- package/dist/src/transform/transform-document.d.ts +7 -0
- package/dist/src/transform/transform-document.js +7 -0
- package/dist/src/transform/transform-endpoint.d.ts +8 -0
- package/dist/src/transform/transform-endpoint.js +8 -0
- package/dist/src/transform/transform-schema.d.ts +10 -0
- package/dist/src/transform/transform-schema.js +10 -0
- package/dist/src/transform/transformer.d.ts +7 -0
- package/dist/src/transform/transformer.js +7 -0
- package/dist/src/transform/types.d.ts +61 -0
- package/dist/src/transform/types.js +4 -0
- package/dist/src/utils/action-provider.d.ts +38 -0
- package/dist/src/utils/action-provider.js +38 -0
- package/dist/src/utils/api-data.utils.d.ts +9 -0
- package/dist/src/utils/api-data.utils.js +9 -0
- package/dist/src/utils/array.utils.d.ts +7 -0
- package/dist/src/utils/array.utils.js +7 -0
- package/dist/src/utils/asset-manager.d.ts +22 -0
- package/dist/src/utils/asset-manager.js +22 -0
- package/dist/src/utils/class.utils.d.ts +22 -0
- package/dist/src/utils/class.utils.js +22 -0
- package/dist/src/utils/common.utils.d.ts +81 -0
- package/dist/src/utils/common.utils.js +81 -0
- package/dist/src/utils/condition.d.ts +22 -0
- package/dist/src/utils/endpoint.utils.d.ts +12 -0
- package/dist/src/utils/endpoint.utils.js +12 -0
- package/dist/src/utils/error.utils.d.ts +6 -0
- package/dist/src/utils/error.utils.js +6 -0
- package/dist/src/utils/factory.d.ts +45 -0
- package/dist/src/utils/factory.js +45 -0
- package/dist/src/utils/file-system.utils.d.ts +37 -0
- package/dist/src/utils/file-system.utils.js +20 -0
- package/dist/src/utils/generator.utils.d.ts +7 -0
- package/dist/src/utils/generator.utils.js +7 -0
- package/dist/src/utils/object.utils.d.ts +16 -0
- package/dist/src/utils/object.utils.js +16 -0
- package/dist/src/utils/schema-factory.d.ts +197 -0
- package/dist/src/utils/schema-factory.js +146 -0
- package/dist/src/utils/schema.utils.d.ts +15 -0
- package/dist/src/utils/schema.utils.js +15 -0
- package/dist/src/utils/source-builder.d.ts +103 -0
- package/dist/src/utils/source-builder.js +58 -0
- package/dist/src/utils/string-builder/string-builder.d.ts +24 -0
- package/dist/src/utils/string-builder/string-builder.js +11 -0
- package/dist/src/utils/string-builder/utils.d.ts +68 -0
- package/dist/src/utils/string-builder/utils.js +33 -0
- package/dist/src/utils/string.utils.d.ts +78 -0
- package/dist/src/utils/string.utils.js +64 -0
- package/dist/src/utils/string.utils.types.d.ts +34 -0
- package/dist/src/utils/type.utils.d.ts +132 -0
- package/dist/src/utils/type.utils.js +19 -0
- package/dist/src/utils/yaml-info.d.ts +35 -0
- package/dist/src/utils/yaml-info.js +25 -0
- package/package.json +1 -1
|
@@ -1,8 +1,30 @@
|
|
|
1
1
|
import { URL } from 'node:url';
|
|
2
|
+
/**
|
|
3
|
+
* Reads and copies asset files, such as templates, that are shipped with a generator package. The assets base URL can
|
|
4
|
+
* be a `file:` URL, in which case files are read from the local file system, or an HTTP(S) URL, in which case files
|
|
5
|
+
* are fetched over the network. Relative paths are resolved against the base URL.
|
|
6
|
+
*/
|
|
2
7
|
export declare class AssetManager {
|
|
3
8
|
private readonly _assetsBaseUrl;
|
|
9
|
+
/**
|
|
10
|
+
* Initializes a new instance of the AssetManager class.
|
|
11
|
+
* @param assetsBaseUrl The base URL of the asset directory. Either a `file:` URL or an HTTP(S) URL.
|
|
12
|
+
*/
|
|
4
13
|
constructor(assetsBaseUrl: URL);
|
|
14
|
+
/**
|
|
15
|
+
* Gets the content of an asset file as text. The file is read from disk for `file:` base URLs and fetched for HTTP(S)
|
|
16
|
+
* base URLs. Rejects if the file cannot be read, or if the HTTP response is not successful.
|
|
17
|
+
* @param relativePath The path of the asset file, relative to the assets base URL.
|
|
18
|
+
* @returns The text content of the asset file.
|
|
19
|
+
*/
|
|
5
20
|
getAssetFileContent(relativePath: string): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Copies an asset file into the specified directory under its base name, overwriting any existing file at that path.
|
|
23
|
+
* @param relativePath The path of the asset file, relative to the assets base URL.
|
|
24
|
+
* @param destDir The directory to copy the file into.
|
|
25
|
+
* @param logName The name to show in the log message. Nothing is logged if omitted; an empty string logs the relative
|
|
26
|
+
* path instead.
|
|
27
|
+
*/
|
|
6
28
|
copyAssetFile(relativePath: string, destDir: string, logName?: string): Promise<void>;
|
|
7
29
|
private fetchAssetFile;
|
|
8
30
|
}
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import { basename, resolve } from 'node:path';
|
|
2
2
|
import { fileURLToPath, URL } from 'node:url';
|
|
3
3
|
import fs from 'fs-extra';
|
|
4
|
+
/**
|
|
5
|
+
* Reads and copies asset files, such as templates, that are shipped with a generator package. The assets base URL can
|
|
6
|
+
* be a `file:` URL, in which case files are read from the local file system, or an HTTP(S) URL, in which case files
|
|
7
|
+
* are fetched over the network. Relative paths are resolved against the base URL.
|
|
8
|
+
*/
|
|
4
9
|
export class AssetManager {
|
|
5
10
|
_assetsBaseUrl;
|
|
11
|
+
/**
|
|
12
|
+
* Initializes a new instance of the AssetManager class.
|
|
13
|
+
* @param assetsBaseUrl The base URL of the asset directory. Either a `file:` URL or an HTTP(S) URL.
|
|
14
|
+
*/
|
|
6
15
|
constructor(assetsBaseUrl) {
|
|
7
16
|
this._assetsBaseUrl = assetsBaseUrl;
|
|
8
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Gets the content of an asset file as text. The file is read from disk for `file:` base URLs and fetched for HTTP(S)
|
|
20
|
+
* base URLs. Rejects if the file cannot be read, or if the HTTP response is not successful.
|
|
21
|
+
* @param relativePath The path of the asset file, relative to the assets base URL.
|
|
22
|
+
* @returns The text content of the asset file.
|
|
23
|
+
*/
|
|
9
24
|
async getAssetFileContent(relativePath) {
|
|
10
25
|
if (this._assetsBaseUrl.protocol === 'file:') {
|
|
11
26
|
return await fs.readFile(resolve(fileURLToPath(this._assetsBaseUrl), relativePath), 'utf8');
|
|
@@ -14,6 +29,13 @@ export class AssetManager {
|
|
|
14
29
|
return await this.fetchAssetFile(relativePath);
|
|
15
30
|
}
|
|
16
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Copies an asset file into the specified directory under its base name, overwriting any existing file at that path.
|
|
34
|
+
* @param relativePath The path of the asset file, relative to the assets base URL.
|
|
35
|
+
* @param destDir The directory to copy the file into.
|
|
36
|
+
* @param logName The name to show in the log message. Nothing is logged if omitted; an empty string logs the relative
|
|
37
|
+
* path instead.
|
|
38
|
+
*/
|
|
17
39
|
async copyAssetFile(relativePath, destDir, logName) {
|
|
18
40
|
const targetPath = resolve(destDir, basename(relativePath));
|
|
19
41
|
if (logName !== undefined) {
|
|
@@ -1,7 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups the specified items by the first class in the classification that they are an instance of. Items that match
|
|
3
|
+
* none of the classes are collected in the `rest` array.
|
|
4
|
+
* @template TItem The type of the items to classify.
|
|
5
|
+
* @template TClassification The record that maps property names to the classes to test against.
|
|
6
|
+
* @param items The items to classify.
|
|
7
|
+
* @param classification An object that maps property names to classes. Each key becomes an array in the result.
|
|
8
|
+
* @returns An object with one array per classification key and a `rest` array for the unclassified items.
|
|
9
|
+
*/
|
|
1
10
|
export declare function classifyArray<TItem, TClassification extends Record<string, abstract new (...args: any[]) => any>>(items: Iterable<TItem>, classification: TClassification): {
|
|
2
11
|
[P in keyof TClassification]: InstanceType<TClassification[P]>[];
|
|
3
12
|
} & {
|
|
4
13
|
rest: Exclude<TItem, InstanceType<TClassification[string]>>[];
|
|
5
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Checks whether the specified item is an instance of the specified class.
|
|
17
|
+
* @template T The type to check for.
|
|
18
|
+
* @param item The item to check.
|
|
19
|
+
* @param type The class to check against.
|
|
20
|
+
* @returns `true` if the item is an instance of the class; otherwise, `false`.
|
|
21
|
+
*/
|
|
6
22
|
export declare function isInstanceOf<T>(item: unknown, type: abstract new (...args: any[]) => T): item is T;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a type guard that checks whether an item is an instance of the specified class.
|
|
25
|
+
* @template T The type to check for.
|
|
26
|
+
* @param type The class to create the type guard for.
|
|
27
|
+
* @returns A function that returns `true` if its argument is an instance of the class; otherwise, `false`.
|
|
28
|
+
*/
|
|
7
29
|
export declare function getIsInstanceOf<T>(type: abstract new (...args: any[]) => T): (item: unknown) => item is T;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups the specified items by the first class in the classification that they are an instance of. Items that match
|
|
3
|
+
* none of the classes are collected in the `rest` array.
|
|
4
|
+
* @template TItem The type of the items to classify.
|
|
5
|
+
* @template TClassification The record that maps property names to the classes to test against.
|
|
6
|
+
* @param items The items to classify.
|
|
7
|
+
* @param classification An object that maps property names to classes. Each key becomes an array in the result.
|
|
8
|
+
* @returns An object with one array per classification key and a `rest` array for the unclassified items.
|
|
9
|
+
*/
|
|
1
10
|
export function classifyArray(items, classification) {
|
|
2
11
|
const result = { rest: [] };
|
|
3
12
|
for (const key in classification) {
|
|
@@ -19,9 +28,22 @@ export function classifyArray(items, classification) {
|
|
|
19
28
|
}
|
|
20
29
|
return result;
|
|
21
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Checks whether the specified item is an instance of the specified class.
|
|
33
|
+
* @template T The type to check for.
|
|
34
|
+
* @param item The item to check.
|
|
35
|
+
* @param type The class to check against.
|
|
36
|
+
* @returns `true` if the item is an instance of the class; otherwise, `false`.
|
|
37
|
+
*/
|
|
22
38
|
export function isInstanceOf(item, type) {
|
|
23
39
|
return item instanceof type;
|
|
24
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates a type guard that checks whether an item is an instance of the specified class.
|
|
43
|
+
* @template T The type to check for.
|
|
44
|
+
* @param type The class to create the type guard for.
|
|
45
|
+
* @returns A function that returns `true` if its argument is an instance of the class; otherwise, `false`.
|
|
46
|
+
*/
|
|
25
47
|
export function getIsInstanceOf(type) {
|
|
26
48
|
return (item) => item instanceof type;
|
|
27
49
|
}
|
|
@@ -1,15 +1,96 @@
|
|
|
1
1
|
import type { SingleOrMultiple } from './type.utils.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether the specified value is `null` or `undefined`.
|
|
4
|
+
* @param value The value to check.
|
|
5
|
+
* @returns `true` if the value is `null` or `undefined`; otherwise, `false`.
|
|
6
|
+
*/
|
|
2
7
|
export declare function isNullish(value: unknown): value is null | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Checks whether the specified value is neither `null` nor `undefined`. Can be used as a type guard in filters.
|
|
10
|
+
* @template T The type of the value.
|
|
11
|
+
* @param value The value to check.
|
|
12
|
+
* @returns `true` if the value is neither `null` nor `undefined`; otherwise, `false`.
|
|
13
|
+
*/
|
|
3
14
|
export declare function notNullish<T>(value: T): value is NonNullable<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Concatenates one or more single or multiple values into one. A single argument is returned as-is, while multiple
|
|
17
|
+
* arguments are combined into a new array in which array arguments are flattened by one level.
|
|
18
|
+
* @template T The type of the items.
|
|
19
|
+
* @param values The single or multiple values to concatenate.
|
|
20
|
+
* @returns The single argument, or a new array containing the items of all arguments.
|
|
21
|
+
*/
|
|
4
22
|
export declare function concatSingleOrMultiple<T>(...values: SingleOrMultiple<T>[]): SingleOrMultiple<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Checks whether the specified value is a non-null object that implements the iterable protocol. Strings are not
|
|
25
|
+
* considered iterable by this check, because they are primitive values.
|
|
26
|
+
* @param value The value to check.
|
|
27
|
+
* @returns `true` if the value is an iterable object; otherwise, `false`.
|
|
28
|
+
*/
|
|
5
29
|
export declare function isIterable(value: unknown): value is Iterable<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Converts the specified value to an array. Arrays are returned as-is, other iterables are copied into a new array, and
|
|
32
|
+
* any other value (including a string) is wrapped in a single-item array.
|
|
33
|
+
* @template T The type of the items.
|
|
34
|
+
* @param value The value to convert.
|
|
35
|
+
* @returns An array containing the items of the value.
|
|
36
|
+
*/
|
|
6
37
|
export declare function toArray<T>(value: T | T[] | Iterable<T>): T[];
|
|
38
|
+
/**
|
|
39
|
+
* Invokes the callback with the specified value if it is neither `null` nor `undefined`, and returns the value.
|
|
40
|
+
* The callback can change the value in place.
|
|
41
|
+
* @template T The type of the value.
|
|
42
|
+
* @param value The value to modify.
|
|
43
|
+
* @param callback The callback that receives the value if it is not nullish.
|
|
44
|
+
* @returns The original value.
|
|
45
|
+
*/
|
|
7
46
|
export declare function modify<T>(value: T, callback: (value: NonNullable<T>) => void): T;
|
|
47
|
+
/**
|
|
48
|
+
* Invokes the callback with the specified value and returns the value. Useful for performing side effects in a chain.
|
|
49
|
+
* @template T The type of the value.
|
|
50
|
+
* @param value The value to pass to the callback.
|
|
51
|
+
* @param callback The callback to invoke with the value.
|
|
52
|
+
* @returns The original value.
|
|
53
|
+
*/
|
|
8
54
|
export declare function run<T>(value: T, callback: (value: T) => void): T;
|
|
55
|
+
/**
|
|
56
|
+
* Passes the specified value to the callback and returns the result of the callback.
|
|
57
|
+
* @template T The type of the input value.
|
|
58
|
+
* @template U The type of the result.
|
|
59
|
+
* @param value The value to transform.
|
|
60
|
+
* @param callback The callback that computes the result from the value.
|
|
61
|
+
* @returns The result of the callback.
|
|
62
|
+
*/
|
|
9
63
|
export declare function transform<T, U>(value: T, callback: (value: T) => U): U;
|
|
10
64
|
type IterableItem<T> = T extends Iterable<infer U> ? U : never;
|
|
65
|
+
/**
|
|
66
|
+
* Invokes the callback for each item of the specified iterable that is neither `null` nor `undefined`, and returns the
|
|
67
|
+
* iterable. Nullish items are skipped.
|
|
68
|
+
* @template T The type of the iterable.
|
|
69
|
+
* @param values The iterable whose non-nullish items are passed to the callback.
|
|
70
|
+
* @param callback The callback that receives each non-nullish item.
|
|
71
|
+
* @returns The original iterable.
|
|
72
|
+
*/
|
|
11
73
|
export declare function modifyEach<T extends Iterable<unknown>>(values: T, callback: (value: NonNullable<IterableItem<T>>) => void): T;
|
|
74
|
+
/**
|
|
75
|
+
* Invokes the callback for each item of the specified iterable, including nullish items, and returns the iterable.
|
|
76
|
+
* @template T The type of the iterable.
|
|
77
|
+
* @param values The iterable whose items are passed to the callback.
|
|
78
|
+
* @param callback The callback to invoke for each item.
|
|
79
|
+
* @returns The original iterable.
|
|
80
|
+
*/
|
|
12
81
|
export declare function runEach<T extends Iterable<unknown>>(values: T, callback: (value: IterableItem<T>) => void): T;
|
|
82
|
+
/**
|
|
83
|
+
* Maps each item of the specified iterable with the callback and returns the results as a new array.
|
|
84
|
+
* @template T The type of the input items.
|
|
85
|
+
* @template U The type of the mapped items.
|
|
86
|
+
* @param values The iterable whose items are transformed.
|
|
87
|
+
* @param callback The callback that maps each item to a new value.
|
|
88
|
+
* @returns A new array containing the mapped items.
|
|
89
|
+
*/
|
|
13
90
|
export declare function transformEach<T, U>(values: Iterable<T>, callback: (value: T) => U): U[];
|
|
91
|
+
/**
|
|
92
|
+
* Ignores the specified value and returns nothing. Useful for explicitly discarding the result of an expression.
|
|
93
|
+
* @param _ The value to ignore.
|
|
94
|
+
*/
|
|
14
95
|
export declare function asVoid(_: unknown): void;
|
|
15
96
|
export {};
|
|
@@ -1,9 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether the specified value is `null` or `undefined`.
|
|
3
|
+
* @param value The value to check.
|
|
4
|
+
* @returns `true` if the value is `null` or `undefined`; otherwise, `false`.
|
|
5
|
+
*/
|
|
1
6
|
export function isNullish(value) {
|
|
2
7
|
return value === null || value === undefined;
|
|
3
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Checks whether the specified value is neither `null` nor `undefined`. Can be used as a type guard in filters.
|
|
11
|
+
* @template T The type of the value.
|
|
12
|
+
* @param value The value to check.
|
|
13
|
+
* @returns `true` if the value is neither `null` nor `undefined`; otherwise, `false`.
|
|
14
|
+
*/
|
|
4
15
|
export function notNullish(value) {
|
|
5
16
|
return !isNullish(value);
|
|
6
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Concatenates one or more single or multiple values into one. A single argument is returned as-is, while multiple
|
|
20
|
+
* arguments are combined into a new array in which array arguments are flattened by one level.
|
|
21
|
+
* @template T The type of the items.
|
|
22
|
+
* @param values The single or multiple values to concatenate.
|
|
23
|
+
* @returns The single argument, or a new array containing the items of all arguments.
|
|
24
|
+
*/
|
|
7
25
|
export function concatSingleOrMultiple(...values) {
|
|
8
26
|
if (values.length === 1) {
|
|
9
27
|
return values[0];
|
|
@@ -19,9 +37,22 @@ export function concatSingleOrMultiple(...values) {
|
|
|
19
37
|
}
|
|
20
38
|
return result;
|
|
21
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Checks whether the specified value is a non-null object that implements the iterable protocol. Strings are not
|
|
42
|
+
* considered iterable by this check, because they are primitive values.
|
|
43
|
+
* @param value The value to check.
|
|
44
|
+
* @returns `true` if the value is an iterable object; otherwise, `false`.
|
|
45
|
+
*/
|
|
22
46
|
export function isIterable(value) {
|
|
23
47
|
return typeof value === 'object' && value !== null && Symbol.iterator in value;
|
|
24
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Converts the specified value to an array. Arrays are returned as-is, other iterables are copied into a new array, and
|
|
51
|
+
* any other value (including a string) is wrapped in a single-item array.
|
|
52
|
+
* @template T The type of the items.
|
|
53
|
+
* @param value The value to convert.
|
|
54
|
+
* @returns An array containing the items of the value.
|
|
55
|
+
*/
|
|
25
56
|
export function toArray(value) {
|
|
26
57
|
if (Array.isArray(value)) {
|
|
27
58
|
return value;
|
|
@@ -31,19 +62,50 @@ export function toArray(value) {
|
|
|
31
62
|
}
|
|
32
63
|
return [value];
|
|
33
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Invokes the callback with the specified value if it is neither `null` nor `undefined`, and returns the value.
|
|
67
|
+
* The callback can change the value in place.
|
|
68
|
+
* @template T The type of the value.
|
|
69
|
+
* @param value The value to modify.
|
|
70
|
+
* @param callback The callback that receives the value if it is not nullish.
|
|
71
|
+
* @returns The original value.
|
|
72
|
+
*/
|
|
34
73
|
export function modify(value, callback) {
|
|
35
74
|
if (notNullish(value)) {
|
|
36
75
|
callback(value);
|
|
37
76
|
}
|
|
38
77
|
return value;
|
|
39
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Invokes the callback with the specified value and returns the value. Useful for performing side effects in a chain.
|
|
81
|
+
* @template T The type of the value.
|
|
82
|
+
* @param value The value to pass to the callback.
|
|
83
|
+
* @param callback The callback to invoke with the value.
|
|
84
|
+
* @returns The original value.
|
|
85
|
+
*/
|
|
40
86
|
export function run(value, callback) {
|
|
41
87
|
callback(value);
|
|
42
88
|
return value;
|
|
43
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Passes the specified value to the callback and returns the result of the callback.
|
|
92
|
+
* @template T The type of the input value.
|
|
93
|
+
* @template U The type of the result.
|
|
94
|
+
* @param value The value to transform.
|
|
95
|
+
* @param callback The callback that computes the result from the value.
|
|
96
|
+
* @returns The result of the callback.
|
|
97
|
+
*/
|
|
44
98
|
export function transform(value, callback) {
|
|
45
99
|
return callback(value);
|
|
46
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Invokes the callback for each item of the specified iterable that is neither `null` nor `undefined`, and returns the
|
|
103
|
+
* iterable. Nullish items are skipped.
|
|
104
|
+
* @template T The type of the iterable.
|
|
105
|
+
* @param values The iterable whose non-nullish items are passed to the callback.
|
|
106
|
+
* @param callback The callback that receives each non-nullish item.
|
|
107
|
+
* @returns The original iterable.
|
|
108
|
+
*/
|
|
47
109
|
export function modifyEach(values, callback) {
|
|
48
110
|
for (const value of values) {
|
|
49
111
|
if (notNullish(value)) {
|
|
@@ -52,12 +114,27 @@ export function modifyEach(values, callback) {
|
|
|
52
114
|
}
|
|
53
115
|
return values;
|
|
54
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Invokes the callback for each item of the specified iterable, including nullish items, and returns the iterable.
|
|
119
|
+
* @template T The type of the iterable.
|
|
120
|
+
* @param values The iterable whose items are passed to the callback.
|
|
121
|
+
* @param callback The callback to invoke for each item.
|
|
122
|
+
* @returns The original iterable.
|
|
123
|
+
*/
|
|
55
124
|
export function runEach(values, callback) {
|
|
56
125
|
for (const value of values) {
|
|
57
126
|
callback(value);
|
|
58
127
|
}
|
|
59
128
|
return values;
|
|
60
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Maps each item of the specified iterable with the callback and returns the results as a new array.
|
|
132
|
+
* @template T The type of the input items.
|
|
133
|
+
* @template U The type of the mapped items.
|
|
134
|
+
* @param values The iterable whose items are transformed.
|
|
135
|
+
* @param callback The callback that maps each item to a new value.
|
|
136
|
+
* @returns A new array containing the mapped items.
|
|
137
|
+
*/
|
|
61
138
|
export function transformEach(values, callback) {
|
|
62
139
|
const result = [];
|
|
63
140
|
for (const value of values) {
|
|
@@ -65,6 +142,10 @@ export function transformEach(values, callback) {
|
|
|
65
142
|
}
|
|
66
143
|
return result;
|
|
67
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Ignores the specified value and returns nothing. Useful for explicitly discarding the result of an expression.
|
|
147
|
+
* @param _ The value to ignore.
|
|
148
|
+
*/
|
|
68
149
|
export function asVoid(_) {
|
|
69
150
|
// Do nothing
|
|
70
151
|
}
|
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A boolean value, or a function that returns one. If `T` is `never`, the function takes no arguments; otherwise, it
|
|
3
|
+
* receives a context value of type `T`.
|
|
4
|
+
* @template T The type of the context value passed to a condition function.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const longName: Condition<string> = (name) => name.length > 10;
|
|
8
|
+
* evalCondition(longName, 'example-name'); // true
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
1
11
|
export type Condition<T = never> = boolean | ([T] extends [never] ? () => boolean : (ctx: T) => boolean);
|
|
12
|
+
/**
|
|
13
|
+
* Evaluates the specified condition. A function condition is invoked without a context value.
|
|
14
|
+
* @param condition The boolean value or condition function to evaluate.
|
|
15
|
+
* @returns The boolean result of the condition.
|
|
16
|
+
*/
|
|
2
17
|
export declare function evalCondition(condition: Condition<never>): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Evaluates the specified condition with a context value. A function condition is invoked with the context value.
|
|
20
|
+
* @template T The type of the context value.
|
|
21
|
+
* @param condition The boolean value or condition function to evaluate.
|
|
22
|
+
* @param ctx The context value to pass to a condition function.
|
|
23
|
+
* @returns The boolean result of the condition.
|
|
24
|
+
*/
|
|
3
25
|
export declare function evalCondition<T>(condition: Condition<T>, ctx: T): boolean;
|
|
@@ -1,2 +1,14 @@
|
|
|
1
1
|
import type { ApiEndpoint } from '../transform/api-types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Builds a preview of the URL of an endpoint that shows its query parameters as templates. Required query parameters
|
|
4
|
+
* are written as `name={name}` and optional ones as `[name={name}]`. The path is kept as written in the spec, so path
|
|
5
|
+
* parameters keep their `{name}` placeholders.
|
|
6
|
+
* @param endpoint The endpoint to build the URL preview for.
|
|
7
|
+
* @returns The endpoint path, followed by `?` and the query templates joined with `&` if there are query parameters.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // For GET /pets with a required `limit` and an optional `offset` query parameter:
|
|
11
|
+
* getEndpointUrlPreview(endpoint); // '/pets?limit={limit}&[offset={offset}]'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
2
14
|
export declare function getEndpointUrlPreview(endpoint: ApiEndpoint): string;
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds a preview of the URL of an endpoint that shows its query parameters as templates. Required query parameters
|
|
3
|
+
* are written as `name={name}` and optional ones as `[name={name}]`. The path is kept as written in the spec, so path
|
|
4
|
+
* parameters keep their `{name}` placeholders.
|
|
5
|
+
* @param endpoint The endpoint to build the URL preview for.
|
|
6
|
+
* @returns The endpoint path, followed by `?` and the query templates joined with `&` if there are query parameters.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* // For GET /pets with a required `limit` and an optional `offset` query parameter:
|
|
10
|
+
* getEndpointUrlPreview(endpoint); // '/pets?limit={limit}&[offset={offset}]'
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
1
13
|
export function getEndpointUrlPreview(endpoint) {
|
|
2
14
|
let url = endpoint.path;
|
|
3
15
|
const queryParams = endpoint.parameters.filter((p) => p.target === 'query');
|
|
@@ -1 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws an `Error` with the specified message. Useful in expressions that must produce a value, such as the right-hand
|
|
3
|
+
* side of `??`.
|
|
4
|
+
* @param message The message of the error to throw.
|
|
5
|
+
* @returns This function never returns, because it always throws.
|
|
6
|
+
*/
|
|
1
7
|
export declare function throwExp(message: string): never;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws an `Error` with the specified message. Useful in expressions that must produce a value, such as the right-hand
|
|
3
|
+
* side of `??`.
|
|
4
|
+
* @param message The message of the error to throw.
|
|
5
|
+
* @returns This function never returns, because it always throws.
|
|
6
|
+
*/
|
|
1
7
|
export function throwExp(message) {
|
|
2
8
|
throw new Error(message);
|
|
3
9
|
}
|
|
@@ -9,12 +9,57 @@ type FactoryProvider<T, TArgs extends unknown[]> = {
|
|
|
9
9
|
kind: 'value';
|
|
10
10
|
factory: T;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Creates values on demand. A factory is backed by a constructor, a function or a fixed value, which lets callers
|
|
14
|
+
* supply an alternative implementation, such as a custom generator, without changing the code that uses it.
|
|
15
|
+
* @template T The type of the created values.
|
|
16
|
+
* @template TArgs The types of the arguments passed to the constructor or function.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const byFn = Factory.fromFn((name: string) => new MyGenerator(name));
|
|
20
|
+
* const byValue = Factory.fromValue(new MyGenerator('default'));
|
|
21
|
+
*
|
|
22
|
+
* const generator = byFn.create('custom'); // A new instance on every call.
|
|
23
|
+
* const same = byValue.create(); // Always the same instance.
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
12
26
|
export declare class Factory<T, TArgs extends unknown[]> {
|
|
13
27
|
private readonly _provider;
|
|
28
|
+
/**
|
|
29
|
+
* Initializes a new instance of the Factory class. Prefer the static `fromType`, `fromFn` and `fromValue` methods.
|
|
30
|
+
* @param provider The provider that determines how values are created.
|
|
31
|
+
*/
|
|
14
32
|
constructor(provider: FactoryProvider<T, TArgs>);
|
|
33
|
+
/**
|
|
34
|
+
* Creates a value. Constructors are invoked with `new` and functions are called with the arguments. Fixed values
|
|
35
|
+
* are returned as-is, so the same value is returned on every call.
|
|
36
|
+
* @param args The arguments to pass to the constructor or function. Ignored for fixed values.
|
|
37
|
+
* @returns The created value.
|
|
38
|
+
*/
|
|
15
39
|
create(...args: TArgs): T;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a factory that instantiates the specified class with `new` on every call to `create`.
|
|
42
|
+
* @template T The type of the created values.
|
|
43
|
+
* @template TArgs The types of the constructor arguments.
|
|
44
|
+
* @param type The constructor to invoke.
|
|
45
|
+
* @returns A new factory that creates values with the constructor.
|
|
46
|
+
*/
|
|
16
47
|
static fromType<T, TArgs extends unknown[]>(type: Constructor<T, TArgs>): Factory<T, TArgs>;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a factory that calls the specified function on every call to `create`.
|
|
50
|
+
* @template T The type of the created values.
|
|
51
|
+
* @template TArgs The types of the function arguments.
|
|
52
|
+
* @param fn The function that creates the values.
|
|
53
|
+
* @returns A new factory that creates values with the function.
|
|
54
|
+
*/
|
|
17
55
|
static fromFn<T, TArgs extends unknown[]>(fn: (...args: TArgs) => T): Factory<T, TArgs>;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a factory that always returns the specified value. The value is not copied, so every call to `create`
|
|
58
|
+
* returns the same value.
|
|
59
|
+
* @template T The type of the value.
|
|
60
|
+
* @param value The value to return from `create`.
|
|
61
|
+
* @returns A new factory that returns the value.
|
|
62
|
+
*/
|
|
18
63
|
static fromValue<T>(value: T): Factory<T, []>;
|
|
19
64
|
}
|
|
20
65
|
export {};
|
|
@@ -1,8 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates values on demand. A factory is backed by a constructor, a function or a fixed value, which lets callers
|
|
3
|
+
* supply an alternative implementation, such as a custom generator, without changing the code that uses it.
|
|
4
|
+
* @template T The type of the created values.
|
|
5
|
+
* @template TArgs The types of the arguments passed to the constructor or function.
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const byFn = Factory.fromFn((name: string) => new MyGenerator(name));
|
|
9
|
+
* const byValue = Factory.fromValue(new MyGenerator('default'));
|
|
10
|
+
*
|
|
11
|
+
* const generator = byFn.create('custom'); // A new instance on every call.
|
|
12
|
+
* const same = byValue.create(); // Always the same instance.
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
1
15
|
export class Factory {
|
|
2
16
|
_provider;
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of the Factory class. Prefer the static `fromType`, `fromFn` and `fromValue` methods.
|
|
19
|
+
* @param provider The provider that determines how values are created.
|
|
20
|
+
*/
|
|
3
21
|
constructor(provider) {
|
|
4
22
|
this._provider = provider;
|
|
5
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates a value. Constructors are invoked with `new` and functions are called with the arguments. Fixed values
|
|
26
|
+
* are returned as-is, so the same value is returned on every call.
|
|
27
|
+
* @param args The arguments to pass to the constructor or function. Ignored for fixed values.
|
|
28
|
+
* @returns The created value.
|
|
29
|
+
*/
|
|
6
30
|
create(...args) {
|
|
7
31
|
switch (this._provider.kind) {
|
|
8
32
|
case 'ctor':
|
|
@@ -13,12 +37,33 @@ export class Factory {
|
|
|
13
37
|
return this._provider.factory;
|
|
14
38
|
}
|
|
15
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a factory that instantiates the specified class with `new` on every call to `create`.
|
|
42
|
+
* @template T The type of the created values.
|
|
43
|
+
* @template TArgs The types of the constructor arguments.
|
|
44
|
+
* @param type The constructor to invoke.
|
|
45
|
+
* @returns A new factory that creates values with the constructor.
|
|
46
|
+
*/
|
|
16
47
|
static fromType(type) {
|
|
17
48
|
return new Factory({ kind: 'ctor', factory: type });
|
|
18
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a factory that calls the specified function on every call to `create`.
|
|
52
|
+
* @template T The type of the created values.
|
|
53
|
+
* @template TArgs The types of the function arguments.
|
|
54
|
+
* @param fn The function that creates the values.
|
|
55
|
+
* @returns A new factory that creates values with the function.
|
|
56
|
+
*/
|
|
19
57
|
static fromFn(fn) {
|
|
20
58
|
return new Factory({ kind: 'fn', factory: fn });
|
|
21
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a factory that always returns the specified value. The value is not copied, so every call to `create`
|
|
62
|
+
* returns the same value.
|
|
63
|
+
* @template T The type of the value.
|
|
64
|
+
* @param value The value to return from `create`.
|
|
65
|
+
* @returns A new factory that returns the value.
|
|
66
|
+
*/
|
|
22
67
|
static fromValue(value) {
|
|
23
68
|
return new Factory({ kind: 'value', factory: value });
|
|
24
69
|
}
|
|
@@ -1,10 +1,47 @@
|
|
|
1
1
|
import type { OpenApiGeneratorConfig } from '../codegen/config.ts';
|
|
2
2
|
import type { Nullable } from './type.utils.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Options for scanning a directory with `getFiles`.
|
|
5
|
+
* @see getFiles
|
|
6
|
+
*/
|
|
3
7
|
export type DirectoryScanOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* Whether to include the files of subdirectories.
|
|
10
|
+
* @default false
|
|
11
|
+
*/
|
|
4
12
|
recursive: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The maximum number of subdirectory levels to scan when `recursive` is `true`. `null` or `0` prevents subdirectories
|
|
15
|
+
* from being entered.
|
|
16
|
+
* @default 5
|
|
17
|
+
*/
|
|
5
18
|
maxDepth: Nullable<number>;
|
|
19
|
+
/**
|
|
20
|
+
* A predicate that receives the name of each file (without its directory) and returns whether to include it.
|
|
21
|
+
* All files are included if omitted.
|
|
22
|
+
*/
|
|
6
23
|
filter?: (file: string) => boolean;
|
|
7
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Default options for `getFiles`. Subdirectories are not scanned, and a maximum depth of 5 applies once `recursive` is
|
|
27
|
+
* enabled.
|
|
28
|
+
*/
|
|
8
29
|
export declare const defaultDirectoryScanOptions: DirectoryScanOptions;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the absolute paths of the files in the specified directory. Subdirectories are only scanned if `recursive` is
|
|
32
|
+
* enabled. Symbolic links are not followed; a link is returned like a regular file.
|
|
33
|
+
* @param dir The directory to scan.
|
|
34
|
+
* @param options The scan options. Merged with `defaultDirectoryScanOptions`, so omitted options use their defaults.
|
|
35
|
+
* @returns A promise that resolves to the absolute paths of the matching files.
|
|
36
|
+
*/
|
|
9
37
|
export declare function getFiles(dir: string, options?: Partial<DirectoryScanOptions>): Promise<string[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Writes generated content to the specified file, creating missing parent directories. If the file already exists, the
|
|
40
|
+
* `existingFileBehavior` of the configuration decides what happens.
|
|
41
|
+
* @param config The generator configuration. Its `existingFileBehavior` applies when the file already exists:
|
|
42
|
+
* `'override'` overwrites it, `'error'` throws, `'skip'` leaves it unchanged, and `'count'` writes to the next free
|
|
43
|
+
* path such as `file_1.ts`.
|
|
44
|
+
* @param filePath The path of the file to write.
|
|
45
|
+
* @param content The content to write.
|
|
46
|
+
*/
|
|
10
47
|
export declare function writeGeneratedFile(config: OpenApiGeneratorConfig, filePath: string, content: string): void;
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
|
+
/**
|
|
4
|
+
* Default options for `getFiles`. Subdirectories are not scanned, and a maximum depth of 5 applies once `recursive` is
|
|
5
|
+
* enabled.
|
|
6
|
+
*/
|
|
3
7
|
export const defaultDirectoryScanOptions = {
|
|
4
8
|
recursive: false,
|
|
5
9
|
maxDepth: 5,
|
|
6
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Gets the absolute paths of the files in the specified directory. Subdirectories are only scanned if `recursive` is
|
|
13
|
+
* enabled. Symbolic links are not followed; a link is returned like a regular file.
|
|
14
|
+
* @param dir The directory to scan.
|
|
15
|
+
* @param options The scan options. Merged with `defaultDirectoryScanOptions`, so omitted options use their defaults.
|
|
16
|
+
* @returns A promise that resolves to the absolute paths of the matching files.
|
|
17
|
+
*/
|
|
7
18
|
export async function getFiles(dir, options) {
|
|
8
19
|
const opts = { ...defaultDirectoryScanOptions, ...options };
|
|
9
20
|
const files = [];
|
|
@@ -56,6 +67,15 @@ function getCountedFilePath(filePath) {
|
|
|
56
67
|
}
|
|
57
68
|
return candidate;
|
|
58
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Writes generated content to the specified file, creating missing parent directories. If the file already exists, the
|
|
72
|
+
* `existingFileBehavior` of the configuration decides what happens.
|
|
73
|
+
* @param config The generator configuration. Its `existingFileBehavior` applies when the file already exists:
|
|
74
|
+
* `'override'` overwrites it, `'error'` throws, `'skip'` leaves it unchanged, and `'count'` writes to the next free
|
|
75
|
+
* path such as `file_1.ts`.
|
|
76
|
+
* @param filePath The path of the file to write.
|
|
77
|
+
* @param content The content to write.
|
|
78
|
+
*/
|
|
59
79
|
export function writeGeneratedFile(config, filePath, content) {
|
|
60
80
|
let targetPath = filePath;
|
|
61
81
|
if (config.existingFileBehavior !== 'override' && fs.existsSync(filePath)) {
|