@skmtc/core 0.0.791 → 0.0.793
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/esm/dsl/GeneratedValueList.d.ts +158 -0
- package/esm/dsl/GeneratedValueList.d.ts.map +1 -1
- package/esm/dsl/GeneratedValueList.js +158 -0
- package/esm/dsl/Stringable.d.ts +76 -0
- package/esm/dsl/Stringable.d.ts.map +1 -1
- package/esm/dsl/Stringable.js +75 -0
- package/esm/helpers/collateExamples.d.ts +150 -0
- package/esm/helpers/collateExamples.d.ts.map +1 -1
- package/esm/helpers/collateExamples.js +145 -0
- package/esm/helpers/formatNumber.d.ts +95 -0
- package/esm/helpers/formatNumber.d.ts.map +1 -1
- package/esm/helpers/formatNumber.js +95 -0
- package/esm/helpers/generationStats.d.ts +287 -1
- package/esm/helpers/generationStats.d.ts.map +1 -1
- package/esm/helpers/generationStats.js +266 -0
- package/esm/helpers/isEmpty.d.ts +62 -0
- package/esm/helpers/isEmpty.d.ts.map +1 -1
- package/esm/helpers/isEmpty.js +62 -0
- package/esm/helpers/isImported.d.ts +80 -0
- package/esm/helpers/isImported.d.ts.map +1 -1
- package/esm/helpers/isImported.js +80 -0
- package/esm/helpers/naming.d.ts +65 -1
- package/esm/helpers/naming.d.ts.map +1 -1
- package/esm/helpers/naming.js +65 -1
- package/esm/helpers/parseModuleName.d.ts +175 -0
- package/esm/helpers/parseModuleName.d.ts.map +1 -1
- package/esm/helpers/parseModuleName.js +153 -0
- package/esm/helpers/toResolvedArtifactPath.d.ts +145 -0
- package/esm/helpers/toResolvedArtifactPath.d.ts.map +1 -1
- package/esm/helpers/toResolvedArtifactPath.js +140 -0
- package/esm/oas/example/Example.d.ts +81 -1
- package/esm/oas/example/Example.d.ts.map +1 -1
- package/esm/oas/example/Example.js +74 -1
- package/esm/oas/header/Header.d.ts +217 -1
- package/esm/oas/header/Header.d.ts.map +1 -1
- package/esm/oas/header/Header.js +207 -1
- package/esm/oas/info/Info.d.ts +72 -0
- package/esm/oas/info/Info.d.ts.map +1 -1
- package/esm/oas/info/Info.js +62 -0
- package/esm/oas/mediaType/MediaType.d.ts +85 -0
- package/esm/oas/mediaType/MediaType.d.ts.map +1 -1
- package/esm/oas/mediaType/MediaType.js +77 -0
- package/esm/oas/pathItem/PathItem.d.ts +92 -0
- package/esm/oas/pathItem/PathItem.d.ts.map +1 -1
- package/esm/oas/pathItem/PathItem.js +85 -0
- package/esm/oas/requestBody/RequestBody.d.ts +264 -0
- package/esm/oas/requestBody/RequestBody.d.ts.map +1 -1
- package/esm/oas/requestBody/RequestBody.js +257 -0
- package/esm/oas/schema/Schema.d.ts +117 -0
- package/esm/oas/schema/Schema.d.ts.map +1 -1
- package/esm/typescript/identifiers.d.ts +159 -0
- package/esm/typescript/identifiers.d.ts.map +1 -1
- package/esm/typescript/identifiers.js +159 -0
- package/esm/typescript/withDescription.d.ts +119 -0
- package/esm/typescript/withDescription.d.ts.map +1 -1
- package/esm/typescript/withDescription.js +119 -0
- package/package.json +1 -1
- package/script/dsl/GeneratedValueList.d.ts +158 -0
- package/script/dsl/GeneratedValueList.d.ts.map +1 -1
- package/script/dsl/GeneratedValueList.js +158 -0
- package/script/dsl/Stringable.d.ts +76 -0
- package/script/dsl/Stringable.d.ts.map +1 -1
- package/script/dsl/Stringable.js +75 -0
- package/script/helpers/collateExamples.d.ts +150 -0
- package/script/helpers/collateExamples.d.ts.map +1 -1
- package/script/helpers/collateExamples.js +145 -0
- package/script/helpers/formatNumber.d.ts +95 -0
- package/script/helpers/formatNumber.d.ts.map +1 -1
- package/script/helpers/formatNumber.js +95 -0
- package/script/helpers/generationStats.d.ts +287 -1
- package/script/helpers/generationStats.d.ts.map +1 -1
- package/script/helpers/generationStats.js +266 -0
- package/script/helpers/isEmpty.d.ts +62 -0
- package/script/helpers/isEmpty.d.ts.map +1 -1
- package/script/helpers/isEmpty.js +62 -0
- package/script/helpers/isImported.d.ts +80 -0
- package/script/helpers/isImported.d.ts.map +1 -1
- package/script/helpers/isImported.js +80 -0
- package/script/helpers/naming.d.ts +65 -1
- package/script/helpers/naming.d.ts.map +1 -1
- package/script/helpers/naming.js +65 -1
- package/script/helpers/parseModuleName.d.ts +175 -0
- package/script/helpers/parseModuleName.d.ts.map +1 -1
- package/script/helpers/parseModuleName.js +153 -0
- package/script/helpers/toResolvedArtifactPath.d.ts +145 -0
- package/script/helpers/toResolvedArtifactPath.d.ts.map +1 -1
- package/script/helpers/toResolvedArtifactPath.js +140 -0
- package/script/oas/example/Example.d.ts +81 -1
- package/script/oas/example/Example.d.ts.map +1 -1
- package/script/oas/example/Example.js +74 -1
- package/script/oas/header/Header.d.ts +217 -1
- package/script/oas/header/Header.d.ts.map +1 -1
- package/script/oas/header/Header.js +207 -1
- package/script/oas/info/Info.d.ts +72 -0
- package/script/oas/info/Info.d.ts.map +1 -1
- package/script/oas/info/Info.js +62 -0
- package/script/oas/mediaType/MediaType.d.ts +85 -0
- package/script/oas/mediaType/MediaType.d.ts.map +1 -1
- package/script/oas/mediaType/MediaType.js +77 -0
- package/script/oas/pathItem/PathItem.d.ts +92 -0
- package/script/oas/pathItem/PathItem.d.ts.map +1 -1
- package/script/oas/pathItem/PathItem.js +85 -0
- package/script/oas/requestBody/RequestBody.d.ts +264 -0
- package/script/oas/requestBody/RequestBody.d.ts.map +1 -1
- package/script/oas/requestBody/RequestBody.js +257 -0
- package/script/oas/schema/Schema.d.ts +117 -0
- package/script/oas/schema/Schema.d.ts.map +1 -1
- package/script/typescript/identifiers.d.ts +159 -0
- package/script/typescript/identifiers.d.ts.map +1 -1
- package/script/typescript/identifiers.js +159 -0
- package/script/typescript/withDescription.d.ts +119 -0
- package/script/typescript/withDescription.d.ts.map +1 -1
- package/script/typescript/withDescription.js +119 -0
|
@@ -7,8 +7,125 @@ import type { OasObject } from '../object/Object.js';
|
|
|
7
7
|
import type { OasString } from '../string/String.js';
|
|
8
8
|
import type { OasUnknown } from '../unknown/Unknown.js';
|
|
9
9
|
import type { OasUnion } from '../union/Union.js';
|
|
10
|
+
/**
|
|
11
|
+
* Union type representing all possible OpenAPI Schema objects in the SKMTC system.
|
|
12
|
+
*
|
|
13
|
+
* `OasSchema` is the fundamental type for representing any OpenAPI schema definition
|
|
14
|
+
* after it has been parsed and processed by the SKMTC pipeline. It encompasses all
|
|
15
|
+
* JSON Schema types supported by OpenAPI 3.x specifications, providing type-safe
|
|
16
|
+
* access to schema properties and validation constraints.
|
|
17
|
+
*
|
|
18
|
+
* This union type is used throughout the system for schema processing, type generation,
|
|
19
|
+
* and validation. Each variant corresponds to a specific JSON Schema type with its
|
|
20
|
+
* own set of properties and validation rules.
|
|
21
|
+
*
|
|
22
|
+
* ## Supported Schema Types
|
|
23
|
+
*
|
|
24
|
+
* - **{@link OasArray}**: Array schemas with item type definitions and constraints
|
|
25
|
+
* - **{@link OasBoolean}**: Boolean schemas with optional default values
|
|
26
|
+
* - **{@link OasInteger}**: Integer schemas with numeric constraints and formats
|
|
27
|
+
* - **{@link OasNumber}**: Number schemas with numeric constraints and formats
|
|
28
|
+
* - **{@link OasObject}**: Object schemas with properties, required fields, and constraints
|
|
29
|
+
* - **{@link OasString}**: String schemas with length constraints, patterns, and formats
|
|
30
|
+
* - **{@link OasUnknown}**: Schemas with unknown or mixed types
|
|
31
|
+
* - **{@link OasUnion}**: Union schemas representing oneOf/anyOf/allOf constructs
|
|
32
|
+
*
|
|
33
|
+
* @example Type checking and processing
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import type { OasSchema } from '@skmtc/core';
|
|
36
|
+
*
|
|
37
|
+
* function processSchema(schema: OasSchema): string {
|
|
38
|
+
* if (schema.type === 'object') {
|
|
39
|
+
* // TypeScript knows this is OasObject
|
|
40
|
+
* return `Object with ${Object.keys(schema.properties || {}).length} properties`;
|
|
41
|
+
* } else if (schema.type === 'array') {
|
|
42
|
+
* // TypeScript knows this is OasArray
|
|
43
|
+
* return `Array of ${schema.items.type} items`;
|
|
44
|
+
* } else if (schema.type === 'string') {
|
|
45
|
+
* // TypeScript knows this is OasString
|
|
46
|
+
* return `String${schema.format ? ` (${schema.format})` : ''}`;
|
|
47
|
+
* }
|
|
48
|
+
* // Handle other types...
|
|
49
|
+
* return `${schema.type} type`;
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Schema validation and constraints
|
|
54
|
+
* ```typescript
|
|
55
|
+
* function validateSchemaConstraints(schema: OasSchema, value: unknown): boolean {
|
|
56
|
+
* switch (schema.type) {
|
|
57
|
+
* case 'string':
|
|
58
|
+
* if (typeof value !== 'string') return false;
|
|
59
|
+
* if (schema.minLength && value.length < schema.minLength) return false;
|
|
60
|
+
* if (schema.maxLength && value.length > schema.maxLength) return false;
|
|
61
|
+
* if (schema.pattern && !new RegExp(schema.pattern).test(value)) return false;
|
|
62
|
+
* return true;
|
|
63
|
+
*
|
|
64
|
+
* case 'integer':
|
|
65
|
+
* case 'number':
|
|
66
|
+
* if (typeof value !== 'number') return false;
|
|
67
|
+
* if (schema.minimum && value < schema.minimum) return false;
|
|
68
|
+
* if (schema.maximum && value > schema.maximum) return false;
|
|
69
|
+
* return true;
|
|
70
|
+
*
|
|
71
|
+
* case 'array':
|
|
72
|
+
* if (!Array.isArray(value)) return false;
|
|
73
|
+
* if (schema.minItems && value.length < schema.minItems) return false;
|
|
74
|
+
* if (schema.maxItems && value.length > schema.maxItems) return false;
|
|
75
|
+
* return true;
|
|
76
|
+
*
|
|
77
|
+
* default:
|
|
78
|
+
* return true;
|
|
79
|
+
* }
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Code generation based on schema type
|
|
84
|
+
* ```typescript
|
|
85
|
+
* class TypeScriptGenerator {
|
|
86
|
+
* generateType(schema: OasSchema): string {
|
|
87
|
+
* switch (schema.type) {
|
|
88
|
+
* case 'object':
|
|
89
|
+
* return this.generateInterface(schema);
|
|
90
|
+
* case 'array':
|
|
91
|
+
* return `Array<${this.generateType(schema.items)}>`;
|
|
92
|
+
* case 'string':
|
|
93
|
+
* if (schema.enums) {
|
|
94
|
+
* return schema.enums.map(e => `'${e}'`).join(' | ');
|
|
95
|
+
* }
|
|
96
|
+
* return 'string';
|
|
97
|
+
* case 'integer':
|
|
98
|
+
* case 'number':
|
|
99
|
+
* return 'number';
|
|
100
|
+
* case 'boolean':
|
|
101
|
+
* return 'boolean';
|
|
102
|
+
* case 'union':
|
|
103
|
+
* return schema.variants.map(v => this.generateType(v)).join(' | ');
|
|
104
|
+
* default:
|
|
105
|
+
* return 'unknown';
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
10
111
|
export type OasSchema = OasArray | OasBoolean | OasInteger | OasNumber | OasObject | OasString | OasUnknown | OasUnion;
|
|
112
|
+
/**
|
|
113
|
+
* Configuration options for JSON Schema conversion operations.
|
|
114
|
+
*
|
|
115
|
+
* These options control how OAS schemas are converted back to JSON Schema format,
|
|
116
|
+
* particularly around reference resolution and schema inlining behavior.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const options: ToJsonSchemaOptions = {
|
|
121
|
+
* resolve: true // Resolve $ref references during conversion
|
|
122
|
+
* };
|
|
123
|
+
*
|
|
124
|
+
* const jsonSchema = schema.toJsonSchema(options);
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
11
127
|
export type ToJsonSchemaOptions = {
|
|
128
|
+
/** Whether to resolve $ref references during conversion (default: false) */
|
|
12
129
|
resolve: boolean;
|
|
13
130
|
};
|
|
14
131
|
//# sourceMappingURL=Schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../../src/oas/schema/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAEjD,MAAM,MAAM,SAAS,GACjB,QAAQ,GACR,UAAU,GACV,UAAU,GACV,SAAS,GACT,SAAS,GACT,SAAS,GACT,UAAU,GACV,QAAQ,CAAA;AAEZ,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,OAAO,CAAA;CACjB,CAAA"}
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../../src/oas/schema/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoGG;AACH,MAAM,MAAM,SAAS,GACjB,QAAQ,GACR,UAAU,GACV,UAAU,GACV,SAAS,GACT,SAAS,GACT,SAAS,GACT,UAAU,GACV,QAAQ,CAAA;AAEZ;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,4EAA4E;IAC5E,OAAO,EAAE,OAAO,CAAA;CACjB,CAAA"}
|
|
@@ -1,3 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safely formats a key for use in object literals, adding quotes if needed.
|
|
3
|
+
*
|
|
4
|
+
* This function determines whether a property key needs to be quoted in JavaScript/TypeScript
|
|
5
|
+
* object literals based on identifier naming rules. Valid identifiers are left unquoted,
|
|
6
|
+
* while invalid identifiers (containing spaces, special characters, starting with numbers, etc.)
|
|
7
|
+
* are wrapped in single quotes.
|
|
8
|
+
*
|
|
9
|
+
* @param key - The property key to format
|
|
10
|
+
* @returns The key as-is if it's a valid identifier, otherwise wrapped in quotes
|
|
11
|
+
*
|
|
12
|
+
* @example Valid identifiers (no quotes needed)
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { handleKey } from '@skmtc/core';
|
|
15
|
+
*
|
|
16
|
+
* console.log(handleKey('name')); // 'name'
|
|
17
|
+
* console.log(handleKey('userName')); // 'userName'
|
|
18
|
+
* console.log(handleKey('_private')); // '_private'
|
|
19
|
+
* console.log(handleKey('$special')); // '$special'
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Invalid identifiers (quotes added)
|
|
23
|
+
* ```typescript
|
|
24
|
+
* console.log(handleKey('first-name')); // "'first-name'"
|
|
25
|
+
* console.log(handleKey('2nd-place')); // "'2nd-place'"
|
|
26
|
+
* console.log(handleKey('user name')); // "'user name'"
|
|
27
|
+
* console.log(handleKey('api-key')); // "'api-key'"
|
|
28
|
+
* console.log(handleKey('data.nested')); // "'data.nested'"
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example Usage in object generation
|
|
32
|
+
* ```typescript
|
|
33
|
+
* class ObjectGenerator {
|
|
34
|
+
* generateObjectLiteral(properties: Record<string, string>) {
|
|
35
|
+
* const props = Object.entries(properties)
|
|
36
|
+
* .map(([key, value]) => `${handleKey(key)}: ${value}`)
|
|
37
|
+
* .join(', ');
|
|
38
|
+
*
|
|
39
|
+
* return `{ ${props} }`;
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const generator = new ObjectGenerator();
|
|
44
|
+
* const result = generator.generateObjectLiteral({
|
|
45
|
+
* 'name': 'string',
|
|
46
|
+
* 'first-name': 'string',
|
|
47
|
+
* 'age': 'number'
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* console.log(result);
|
|
51
|
+
* // { name: string, 'first-name': string, age: number }
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example TypeScript interface generation
|
|
55
|
+
* ```typescript
|
|
56
|
+
* function generateInterface(name: string, properties: Record<string, string>) {
|
|
57
|
+
* const props = Object.entries(properties)
|
|
58
|
+
* .map(([key, type]) => ` ${handleKey(key)}: ${type};`)
|
|
59
|
+
* .join('\n');
|
|
60
|
+
*
|
|
61
|
+
* return `interface ${name} {\n${props}\n}`;
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* const userInterface = generateInterface('User', {
|
|
65
|
+
* 'id': 'string',
|
|
66
|
+
* 'first-name': 'string',
|
|
67
|
+
* 'last_name': 'string',
|
|
68
|
+
* '2fa-enabled': 'boolean'
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* console.log(userInterface);
|
|
72
|
+
* // interface User {
|
|
73
|
+
* // id: string;
|
|
74
|
+
* // 'first-name': string;
|
|
75
|
+
* // last_name: string;
|
|
76
|
+
* // '2fa-enabled': boolean;
|
|
77
|
+
* // }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
1
80
|
export declare const handleKey: (key: string) => string;
|
|
81
|
+
/**
|
|
82
|
+
* Safely formats property access, using dot notation or bracket notation as appropriate.
|
|
83
|
+
*
|
|
84
|
+
* This function generates the appropriate property access syntax for JavaScript/TypeScript
|
|
85
|
+
* code. Valid identifiers use dot notation (`parent.property`), while invalid identifiers
|
|
86
|
+
* use bracket notation (`parent['property']`).
|
|
87
|
+
*
|
|
88
|
+
* @param name - The property name to access
|
|
89
|
+
* @param parent - The parent object expression
|
|
90
|
+
* @returns Properly formatted property access expression
|
|
91
|
+
*
|
|
92
|
+
* @example Valid identifiers (dot notation)
|
|
93
|
+
* ```typescript
|
|
94
|
+
* import { handlePropertyName } from '@skmtc/core';
|
|
95
|
+
*
|
|
96
|
+
* console.log(handlePropertyName('name', 'user')); // 'user.name'
|
|
97
|
+
* console.log(handlePropertyName('firstName', 'user')); // 'user.firstName'
|
|
98
|
+
* console.log(handlePropertyName('_id', 'document')); // 'document._id'
|
|
99
|
+
* console.log(handlePropertyName('$meta', 'data')); // 'data.$meta'
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example Invalid identifiers (bracket notation)
|
|
103
|
+
* ```typescript
|
|
104
|
+
* console.log(handlePropertyName('first-name', 'user')); // "user['first-name']"
|
|
105
|
+
* console.log(handlePropertyName('2nd-place', 'result')); // "result['2nd-place']"
|
|
106
|
+
* console.log(handlePropertyName('user name', 'form')); // "form['user name']"
|
|
107
|
+
* console.log(handlePropertyName('api-key', 'config')); // "config['api-key']"
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example Usage in accessor generation
|
|
111
|
+
* ```typescript
|
|
112
|
+
* class PropertyAccessor {
|
|
113
|
+
* generateGetter(objectName: string, propertyPath: string[]) {
|
|
114
|
+
* return propertyPath.reduce((acc, prop) =>
|
|
115
|
+
* handlePropertyName(prop, acc), objectName
|
|
116
|
+
* );
|
|
117
|
+
* }
|
|
118
|
+
* }
|
|
119
|
+
*
|
|
120
|
+
* const accessor = new PropertyAccessor();
|
|
121
|
+
* const path1 = accessor.generateGetter('data', ['user', 'profile', 'firstName']);
|
|
122
|
+
* const path2 = accessor.generateGetter('config', ['api-settings', 'retry-count']);
|
|
123
|
+
*
|
|
124
|
+
* console.log(path1); // 'data.user.profile.firstName'
|
|
125
|
+
* console.log(path2); // "config['api-settings']['retry-count']"
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example Complex object navigation
|
|
129
|
+
* ```typescript
|
|
130
|
+
* function generateObjectAccess(base: string, properties: string[]) {
|
|
131
|
+
* let result = base;
|
|
132
|
+
*
|
|
133
|
+
* for (const prop of properties) {
|
|
134
|
+
* result = handlePropertyName(prop, result);
|
|
135
|
+
* }
|
|
136
|
+
*
|
|
137
|
+
* return result;
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* const access1 = generateObjectAccess('response', ['data', 'user-info', 'name']);
|
|
141
|
+
* const access2 = generateObjectAccess('settings', ['ui', 'theme', 'darkMode']);
|
|
142
|
+
*
|
|
143
|
+
* console.log(access1); // "response.data['user-info'].name"
|
|
144
|
+
* console.log(access2); // "settings.ui.theme.darkMode"
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @example Function generation
|
|
148
|
+
* ```typescript
|
|
149
|
+
* function generatePropertyGetter(objName: string, propName: string) {
|
|
150
|
+
* const access = handlePropertyName(propName, objName);
|
|
151
|
+
* return `function get${propName.replace(/[^a-zA-Z0-9]/g, '')}() { return ${access}; }`;
|
|
152
|
+
* }
|
|
153
|
+
*
|
|
154
|
+
* const getter1 = generatePropertyGetter('user', 'firstName');
|
|
155
|
+
* const getter2 = generatePropertyGetter('config', 'api-key');
|
|
156
|
+
*
|
|
157
|
+
* console.log(getter1); // "function getfirstName() { return user.firstName; }"
|
|
158
|
+
* console.log(getter2); // "function getapikey() { return config['api-key']; }"
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
2
161
|
export declare const handlePropertyName: (name: string, parent: string) => string;
|
|
3
162
|
//# sourceMappingURL=identifiers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identifiers.d.ts","sourceRoot":"","sources":["../../src/typescript/identifiers.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,KAAG,MAEvC,CAAA;AAED,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,EAAE,QAAQ,MAAM,KAAG,MAEjE,CAAA"}
|
|
1
|
+
{"version":3,"file":"identifiers.d.ts","sourceRoot":"","sources":["../../src/typescript/identifiers.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8EG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,KAAG,MAEvC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+EG;AACH,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,EAAE,QAAQ,MAAM,KAAG,MAEjE,CAAA"}
|
|
@@ -1,7 +1,166 @@
|
|
|
1
1
|
import { isIdentifierName } from '@babel/helper-validator-identifier';
|
|
2
|
+
/**
|
|
3
|
+
* Safely formats a key for use in object literals, adding quotes if needed.
|
|
4
|
+
*
|
|
5
|
+
* This function determines whether a property key needs to be quoted in JavaScript/TypeScript
|
|
6
|
+
* object literals based on identifier naming rules. Valid identifiers are left unquoted,
|
|
7
|
+
* while invalid identifiers (containing spaces, special characters, starting with numbers, etc.)
|
|
8
|
+
* are wrapped in single quotes.
|
|
9
|
+
*
|
|
10
|
+
* @param key - The property key to format
|
|
11
|
+
* @returns The key as-is if it's a valid identifier, otherwise wrapped in quotes
|
|
12
|
+
*
|
|
13
|
+
* @example Valid identifiers (no quotes needed)
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { handleKey } from '@skmtc/core';
|
|
16
|
+
*
|
|
17
|
+
* console.log(handleKey('name')); // 'name'
|
|
18
|
+
* console.log(handleKey('userName')); // 'userName'
|
|
19
|
+
* console.log(handleKey('_private')); // '_private'
|
|
20
|
+
* console.log(handleKey('$special')); // '$special'
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example Invalid identifiers (quotes added)
|
|
24
|
+
* ```typescript
|
|
25
|
+
* console.log(handleKey('first-name')); // "'first-name'"
|
|
26
|
+
* console.log(handleKey('2nd-place')); // "'2nd-place'"
|
|
27
|
+
* console.log(handleKey('user name')); // "'user name'"
|
|
28
|
+
* console.log(handleKey('api-key')); // "'api-key'"
|
|
29
|
+
* console.log(handleKey('data.nested')); // "'data.nested'"
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example Usage in object generation
|
|
33
|
+
* ```typescript
|
|
34
|
+
* class ObjectGenerator {
|
|
35
|
+
* generateObjectLiteral(properties: Record<string, string>) {
|
|
36
|
+
* const props = Object.entries(properties)
|
|
37
|
+
* .map(([key, value]) => `${handleKey(key)}: ${value}`)
|
|
38
|
+
* .join(', ');
|
|
39
|
+
*
|
|
40
|
+
* return `{ ${props} }`;
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* const generator = new ObjectGenerator();
|
|
45
|
+
* const result = generator.generateObjectLiteral({
|
|
46
|
+
* 'name': 'string',
|
|
47
|
+
* 'first-name': 'string',
|
|
48
|
+
* 'age': 'number'
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* console.log(result);
|
|
52
|
+
* // { name: string, 'first-name': string, age: number }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example TypeScript interface generation
|
|
56
|
+
* ```typescript
|
|
57
|
+
* function generateInterface(name: string, properties: Record<string, string>) {
|
|
58
|
+
* const props = Object.entries(properties)
|
|
59
|
+
* .map(([key, type]) => ` ${handleKey(key)}: ${type};`)
|
|
60
|
+
* .join('\n');
|
|
61
|
+
*
|
|
62
|
+
* return `interface ${name} {\n${props}\n}`;
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* const userInterface = generateInterface('User', {
|
|
66
|
+
* 'id': 'string',
|
|
67
|
+
* 'first-name': 'string',
|
|
68
|
+
* 'last_name': 'string',
|
|
69
|
+
* '2fa-enabled': 'boolean'
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* console.log(userInterface);
|
|
73
|
+
* // interface User {
|
|
74
|
+
* // id: string;
|
|
75
|
+
* // 'first-name': string;
|
|
76
|
+
* // last_name: string;
|
|
77
|
+
* // '2fa-enabled': boolean;
|
|
78
|
+
* // }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
2
81
|
export const handleKey = (key) => {
|
|
3
82
|
return isIdentifierName(key) ? key : `'${key}'`;
|
|
4
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* Safely formats property access, using dot notation or bracket notation as appropriate.
|
|
86
|
+
*
|
|
87
|
+
* This function generates the appropriate property access syntax for JavaScript/TypeScript
|
|
88
|
+
* code. Valid identifiers use dot notation (`parent.property`), while invalid identifiers
|
|
89
|
+
* use bracket notation (`parent['property']`).
|
|
90
|
+
*
|
|
91
|
+
* @param name - The property name to access
|
|
92
|
+
* @param parent - The parent object expression
|
|
93
|
+
* @returns Properly formatted property access expression
|
|
94
|
+
*
|
|
95
|
+
* @example Valid identifiers (dot notation)
|
|
96
|
+
* ```typescript
|
|
97
|
+
* import { handlePropertyName } from '@skmtc/core';
|
|
98
|
+
*
|
|
99
|
+
* console.log(handlePropertyName('name', 'user')); // 'user.name'
|
|
100
|
+
* console.log(handlePropertyName('firstName', 'user')); // 'user.firstName'
|
|
101
|
+
* console.log(handlePropertyName('_id', 'document')); // 'document._id'
|
|
102
|
+
* console.log(handlePropertyName('$meta', 'data')); // 'data.$meta'
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example Invalid identifiers (bracket notation)
|
|
106
|
+
* ```typescript
|
|
107
|
+
* console.log(handlePropertyName('first-name', 'user')); // "user['first-name']"
|
|
108
|
+
* console.log(handlePropertyName('2nd-place', 'result')); // "result['2nd-place']"
|
|
109
|
+
* console.log(handlePropertyName('user name', 'form')); // "form['user name']"
|
|
110
|
+
* console.log(handlePropertyName('api-key', 'config')); // "config['api-key']"
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @example Usage in accessor generation
|
|
114
|
+
* ```typescript
|
|
115
|
+
* class PropertyAccessor {
|
|
116
|
+
* generateGetter(objectName: string, propertyPath: string[]) {
|
|
117
|
+
* return propertyPath.reduce((acc, prop) =>
|
|
118
|
+
* handlePropertyName(prop, acc), objectName
|
|
119
|
+
* );
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
*
|
|
123
|
+
* const accessor = new PropertyAccessor();
|
|
124
|
+
* const path1 = accessor.generateGetter('data', ['user', 'profile', 'firstName']);
|
|
125
|
+
* const path2 = accessor.generateGetter('config', ['api-settings', 'retry-count']);
|
|
126
|
+
*
|
|
127
|
+
* console.log(path1); // 'data.user.profile.firstName'
|
|
128
|
+
* console.log(path2); // "config['api-settings']['retry-count']"
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @example Complex object navigation
|
|
132
|
+
* ```typescript
|
|
133
|
+
* function generateObjectAccess(base: string, properties: string[]) {
|
|
134
|
+
* let result = base;
|
|
135
|
+
*
|
|
136
|
+
* for (const prop of properties) {
|
|
137
|
+
* result = handlePropertyName(prop, result);
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* return result;
|
|
141
|
+
* }
|
|
142
|
+
*
|
|
143
|
+
* const access1 = generateObjectAccess('response', ['data', 'user-info', 'name']);
|
|
144
|
+
* const access2 = generateObjectAccess('settings', ['ui', 'theme', 'darkMode']);
|
|
145
|
+
*
|
|
146
|
+
* console.log(access1); // "response.data['user-info'].name"
|
|
147
|
+
* console.log(access2); // "settings.ui.theme.darkMode"
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @example Function generation
|
|
151
|
+
* ```typescript
|
|
152
|
+
* function generatePropertyGetter(objName: string, propName: string) {
|
|
153
|
+
* const access = handlePropertyName(propName, objName);
|
|
154
|
+
* return `function get${propName.replace(/[^a-zA-Z0-9]/g, '')}() { return ${access}; }`;
|
|
155
|
+
* }
|
|
156
|
+
*
|
|
157
|
+
* const getter1 = generatePropertyGetter('user', 'firstName');
|
|
158
|
+
* const getter2 = generatePropertyGetter('config', 'api-key');
|
|
159
|
+
*
|
|
160
|
+
* console.log(getter1); // "function getfirstName() { return user.firstName; }"
|
|
161
|
+
* console.log(getter2); // "function getapikey() { return config['api-key']; }"
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
5
164
|
export const handlePropertyName = (name, parent) => {
|
|
6
165
|
return isIdentifierName(name) ? `${parent}.${name}` : `${parent}['${name}']`;
|
|
7
166
|
};
|
|
@@ -1,4 +1,123 @@
|
|
|
1
1
|
import type { Stringable } from '../dsl/Stringable.js';
|
|
2
2
|
import type { Modifiers } from '../types/Modifiers.js';
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a value with a JSDoc comment if a description is provided.
|
|
5
|
+
*
|
|
6
|
+
* This utility function is used throughout the SKMTC code generation system to
|
|
7
|
+
* add JSDoc documentation comments to generated TypeScript code. If a description
|
|
8
|
+
* is provided, it formats it as a single-line JSDoc comment above the value.
|
|
9
|
+
* If no description is provided, the value is returned unchanged.
|
|
10
|
+
*
|
|
11
|
+
* @param value - The code value to potentially wrap with documentation
|
|
12
|
+
* @param modifiers - Modifiers object containing optional description
|
|
13
|
+
* @param modifiers.description - Optional description text for the JSDoc comment
|
|
14
|
+
* @returns The value with JSDoc comment prepended if description exists, otherwise just the value
|
|
15
|
+
*
|
|
16
|
+
* @example With description
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { withDescription } from '@skmtc/core';
|
|
19
|
+
*
|
|
20
|
+
* const result = withDescription(
|
|
21
|
+
* 'export const API_URL = "https://api.example.com";',
|
|
22
|
+
* { description: 'Base URL for all API requests' }
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* console.log(result);
|
|
26
|
+
* // /** Base URL for all API requests *\/
|
|
27
|
+
* // export const API_URL = "https://api.example.com";
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example Without description
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const result = withDescription(
|
|
33
|
+
* 'export const API_URL = "https://api.example.com";',
|
|
34
|
+
* { description: undefined }
|
|
35
|
+
* );
|
|
36
|
+
*
|
|
37
|
+
* console.log(result);
|
|
38
|
+
* // export const API_URL = "https://api.example.com";
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example Using with Definition class
|
|
42
|
+
* ```typescript
|
|
43
|
+
* class ModelGenerator {
|
|
44
|
+
* generateInterface(name: string, description?: string) {
|
|
45
|
+
* const interfaceCode = `interface ${name} {
|
|
46
|
+
* id: string;
|
|
47
|
+
* name: string;
|
|
48
|
+
* }`;
|
|
49
|
+
*
|
|
50
|
+
* return withDescription(interfaceCode, { description });
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* const generator = new ModelGenerator();
|
|
55
|
+
* const documented = generator.generateInterface('User', 'Represents a user in the system');
|
|
56
|
+
*
|
|
57
|
+
* console.log(documented);
|
|
58
|
+
* // /** Represents a user in the system *\/
|
|
59
|
+
* // interface User {
|
|
60
|
+
* // id: string;
|
|
61
|
+
* // name: string;
|
|
62
|
+
* // }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @example Integration with generated definitions
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import { Definition, Identifier, withDescription } from '@skmtc/core';
|
|
68
|
+
*
|
|
69
|
+
* class DocumentedGenerator {
|
|
70
|
+
* createDefinition(name: string, value: string, docs?: string) {
|
|
71
|
+
* return new Definition({
|
|
72
|
+
* context: this.context,
|
|
73
|
+
* identifier: Identifier.createVariable(name),
|
|
74
|
+
* description: docs, // This gets processed by withDescription internally
|
|
75
|
+
* value: {
|
|
76
|
+
* generatorKey: 'documented',
|
|
77
|
+
* content: value
|
|
78
|
+
* }
|
|
79
|
+
* });
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @example Complex documentation
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const complexDoc = withDescription(
|
|
87
|
+
* `export type Status = 'active' | 'inactive' | 'pending';`,
|
|
88
|
+
* {
|
|
89
|
+
* description: 'User account status - active users can access all features'
|
|
90
|
+
* }
|
|
91
|
+
* );
|
|
92
|
+
*
|
|
93
|
+
* console.log(complexDoc);
|
|
94
|
+
* // /** User account status - active users can access all features *\/
|
|
95
|
+
* // export type Status = 'active' | 'inactive' | 'pending';
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @example Conditional documentation
|
|
99
|
+
* ```typescript
|
|
100
|
+
* function generateWithOptionalDocs(
|
|
101
|
+
* code: string,
|
|
102
|
+
* includeDescription: boolean
|
|
103
|
+
* ) {
|
|
104
|
+
* return withDescription(code, {
|
|
105
|
+
* description: includeDescription
|
|
106
|
+
* ? 'This is an auto-generated definition'
|
|
107
|
+
* : undefined
|
|
108
|
+
* });
|
|
109
|
+
* }
|
|
110
|
+
*
|
|
111
|
+
* const withDocs = generateWithOptionalDocs('export const VALUE = 42;', true);
|
|
112
|
+
* const withoutDocs = generateWithOptionalDocs('export const VALUE = 42;', false);
|
|
113
|
+
*
|
|
114
|
+
* console.log(withDocs);
|
|
115
|
+
* // /** This is an auto-generated definition *\/
|
|
116
|
+
* // export const VALUE = 42;
|
|
117
|
+
*
|
|
118
|
+
* console.log(withoutDocs);
|
|
119
|
+
* // export const VALUE = 42;
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
3
122
|
export declare const withDescription: (value: Stringable, { description }: Modifiers) => string;
|
|
4
123
|
//# sourceMappingURL=withDescription.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"withDescription.d.ts","sourceRoot":"","sources":["../../src/typescript/withDescription.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEtD,eAAO,MAAM,eAAe,GAC1B,OAAO,UAAU,EACjB,iBAAiB,SAAS,KACzB,MAEF,CAAA"}
|
|
1
|
+
{"version":3,"file":"withDescription.d.ts","sourceRoot":"","sources":["../../src/typescript/withDescription.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsHG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,UAAU,EACjB,iBAAiB,SAAS,KACzB,MAEF,CAAA"}
|