@skmtc/core 0.0.787 → 0.0.789
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/context/GenerateContext.d.ts +95 -0
- package/esm/context/GenerateContext.d.ts.map +1 -1
- package/esm/context/GenerateContext.js +95 -0
- package/esm/context/ParseContext.d.ts +178 -0
- package/esm/context/ParseContext.d.ts.map +1 -1
- package/esm/context/ParseContext.js +108 -0
- package/esm/dsl/Inserted.d.ts +240 -0
- package/esm/dsl/Inserted.d.ts.map +1 -1
- package/esm/dsl/Inserted.js +231 -0
- package/esm/dsl/JsonFile.d.ts +162 -0
- package/esm/dsl/JsonFile.d.ts.map +1 -1
- package/esm/dsl/JsonFile.js +157 -0
- package/esm/helpers/refFns.d.ts +160 -0
- package/esm/helpers/refFns.d.ts.map +1 -1
- package/esm/helpers/refFns.js +156 -0
- package/esm/oas/components/Components.d.ts +252 -2
- package/esm/oas/components/Components.d.ts.map +1 -1
- package/esm/oas/components/Components.js +241 -2
- package/esm/oas/operation/Operation.d.ts +107 -1
- package/esm/oas/operation/Operation.d.ts.map +1 -1
- package/esm/oas/operation/Operation.js +107 -1
- package/esm/oas/ref/Ref.d.ts +86 -0
- package/esm/oas/ref/Ref.d.ts.map +1 -1
- package/esm/oas/ref/Ref.js +86 -0
- package/esm/oas/union/Union.d.ts +119 -5
- package/esm/oas/union/Union.d.ts.map +1 -1
- package/esm/oas/union/Union.js +108 -5
- package/package.json +1 -1
- package/script/context/GenerateContext.d.ts +95 -0
- package/script/context/GenerateContext.d.ts.map +1 -1
- package/script/context/GenerateContext.js +95 -0
- package/script/context/ParseContext.d.ts +178 -0
- package/script/context/ParseContext.d.ts.map +1 -1
- package/script/context/ParseContext.js +108 -0
- package/script/dsl/Inserted.d.ts +240 -0
- package/script/dsl/Inserted.d.ts.map +1 -1
- package/script/dsl/Inserted.js +231 -0
- package/script/dsl/JsonFile.d.ts +162 -0
- package/script/dsl/JsonFile.d.ts.map +1 -1
- package/script/dsl/JsonFile.js +157 -0
- package/script/helpers/refFns.d.ts +160 -0
- package/script/helpers/refFns.d.ts.map +1 -1
- package/script/helpers/refFns.js +156 -0
- package/script/oas/components/Components.d.ts +252 -2
- package/script/oas/components/Components.d.ts.map +1 -1
- package/script/oas/components/Components.js +241 -2
- package/script/oas/operation/Operation.d.ts +107 -1
- package/script/oas/operation/Operation.d.ts.map +1 -1
- package/script/oas/operation/Operation.js +107 -1
- package/script/oas/ref/Ref.d.ts +86 -0
- package/script/oas/ref/Ref.d.ts.map +1 -1
- package/script/oas/ref/Ref.js +86 -0
- package/script/oas/union/Union.d.ts +119 -5
- package/script/oas/union/Union.d.ts.map +1 -1
- package/script/oas/union/Union.js +108 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonFile.d.ts","sourceRoot":"","sources":["../../src/dsl/JsonFile.ts"],"names":[],"mappings":"AAAA,KAAK,YAAY,GAAG;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACjC,CAAA;AAED,qBAAa,QAAQ;IACnB,QAAQ,EAAE,MAAM,CAAS;
|
|
1
|
+
{"version":3,"file":"JsonFile.d.ts","sourceRoot":"","sources":["../../src/dsl/JsonFile.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,KAAK,YAAY,GAAG;IAClB,gDAAgD;IAChD,IAAI,EAAE,MAAM,CAAA;IACZ,4CAA4C;IAC5C,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACjC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+GG;AACH,qBAAa,QAAQ;IACnB,kDAAkD;IAClD,QAAQ,EAAE,MAAM,CAAS;IAEzB,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAA;IAEZ,4CAA4C;IAC5C,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAEhC;;;;;;;;;;;;;;;;;;OAkBG;gBACS,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,YAAY;IAK3C;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,QAAQ,IAAI,MAAM;CAGnB"}
|
package/script/dsl/JsonFile.js
CHANGED
|
@@ -1,20 +1,154 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.JsonFile = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Represents a JSON file in the SKMTC DSL system.
|
|
6
|
+
*
|
|
7
|
+
* The `JsonFile` class is used to generate JSON files as part of the code generation
|
|
8
|
+
* process. It provides a simple interface for creating configuration files, manifests,
|
|
9
|
+
* package.json files, and other JSON-based artifacts with proper formatting.
|
|
10
|
+
*
|
|
11
|
+
* ## Key Features
|
|
12
|
+
*
|
|
13
|
+
* - **JSON Formatting**: Automatically formats JSON with 2-space indentation
|
|
14
|
+
* - **Type Safety**: Accepts any valid JSON-serializable content structure
|
|
15
|
+
* - **Path Management**: Associates content with specific file paths
|
|
16
|
+
* - **String Conversion**: Easy integration with file writing systems
|
|
17
|
+
*
|
|
18
|
+
* @example Basic JSON file creation
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { JsonFile } from '@skmtc/core';
|
|
21
|
+
*
|
|
22
|
+
* const packageJson = new JsonFile({
|
|
23
|
+
* path: './package.json',
|
|
24
|
+
* content: {
|
|
25
|
+
* name: 'my-api-client',
|
|
26
|
+
* version: '1.0.0',
|
|
27
|
+
* type: 'module',
|
|
28
|
+
* main: './index.js',
|
|
29
|
+
* dependencies: {
|
|
30
|
+
* 'axios': '^1.0.0'
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* const jsonContent = packageJson.toString();
|
|
36
|
+
* console.log(jsonContent);
|
|
37
|
+
* // {
|
|
38
|
+
* // "name": "my-api-client",
|
|
39
|
+
* // "version": "1.0.0",
|
|
40
|
+
* // "type": "module",
|
|
41
|
+
* // "main": "./index.js",
|
|
42
|
+
* // "dependencies": {
|
|
43
|
+
* // "axios": "^1.0.0"
|
|
44
|
+
* // }
|
|
45
|
+
* // }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example Configuration file generation
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const configFile = new JsonFile({
|
|
51
|
+
* path: './src/config/api.json',
|
|
52
|
+
* content: {
|
|
53
|
+
* baseUrl: 'https://api.example.com',
|
|
54
|
+
* timeout: 5000,
|
|
55
|
+
* retryAttempts: 3,
|
|
56
|
+
* endpoints: {
|
|
57
|
+
* users: '/users',
|
|
58
|
+
* products: '/products',
|
|
59
|
+
* orders: '/orders'
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* // Write to file system
|
|
65
|
+
* await Deno.writeTextFile(configFile.path, configFile.toString());
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example OpenAPI specification output
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const openApiFile = new JsonFile({
|
|
71
|
+
* path: './dist/openapi.json',
|
|
72
|
+
* content: {
|
|
73
|
+
* openapi: '3.0.0',
|
|
74
|
+
* info: {
|
|
75
|
+
* title: 'My API',
|
|
76
|
+
* version: '1.0.0'
|
|
77
|
+
* },
|
|
78
|
+
* paths: {
|
|
79
|
+
* '/users': {
|
|
80
|
+
* get: {
|
|
81
|
+
* summary: 'List users',
|
|
82
|
+
* responses: {
|
|
83
|
+
* '200': {
|
|
84
|
+
* description: 'Success'
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
* }
|
|
91
|
+
* });
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @example Using with generator registration
|
|
95
|
+
* ```typescript
|
|
96
|
+
* class MyGenerator extends ContentBase {
|
|
97
|
+
* generate() {
|
|
98
|
+
* const manifestFile = new JsonFile({
|
|
99
|
+
* path: './generated/manifest.json',
|
|
100
|
+
* content: {
|
|
101
|
+
* generatedAt: new Date().toISOString(),
|
|
102
|
+
* version: '1.0.0',
|
|
103
|
+
* files: [
|
|
104
|
+
* './models/User.ts',
|
|
105
|
+
* './models/Product.ts'
|
|
106
|
+
* ]
|
|
107
|
+
* }
|
|
108
|
+
* });
|
|
109
|
+
*
|
|
110
|
+
* // Register with the generation context
|
|
111
|
+
* this.register({ jsonFile: manifestFile });
|
|
112
|
+
* }
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
4
116
|
class JsonFile {
|
|
117
|
+
/**
|
|
118
|
+
* Creates a new JsonFile instance.
|
|
119
|
+
*
|
|
120
|
+
* @param args - JSON file configuration
|
|
121
|
+
* @param args.path - The output path for this JSON file
|
|
122
|
+
* @param args.content - The JSON content to write
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const configFile = new JsonFile({
|
|
127
|
+
* path: './config/settings.json',
|
|
128
|
+
* content: {
|
|
129
|
+
* theme: 'dark',
|
|
130
|
+
* language: 'en',
|
|
131
|
+
* features: ['auth', 'notifications']
|
|
132
|
+
* }
|
|
133
|
+
* });
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
5
136
|
constructor({ path, content }) {
|
|
137
|
+
/** The file type, always 'json' for JSON files */
|
|
6
138
|
Object.defineProperty(this, "fileType", {
|
|
7
139
|
enumerable: true,
|
|
8
140
|
configurable: true,
|
|
9
141
|
writable: true,
|
|
10
142
|
value: 'json'
|
|
11
143
|
});
|
|
144
|
+
/** The file path for this generated JSON file */
|
|
12
145
|
Object.defineProperty(this, "path", {
|
|
13
146
|
enumerable: true,
|
|
14
147
|
configurable: true,
|
|
15
148
|
writable: true,
|
|
16
149
|
value: void 0
|
|
17
150
|
});
|
|
151
|
+
/** The JSON content to write to the file */
|
|
18
152
|
Object.defineProperty(this, "content", {
|
|
19
153
|
enumerable: true,
|
|
20
154
|
configurable: true,
|
|
@@ -24,6 +158,29 @@ class JsonFile {
|
|
|
24
158
|
this.path = path;
|
|
25
159
|
this.content = content;
|
|
26
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Converts the JSON content to a formatted string.
|
|
163
|
+
*
|
|
164
|
+
* This method serializes the content object to JSON with 2-space indentation
|
|
165
|
+
* for readable output. The resulting string is suitable for writing directly
|
|
166
|
+
* to a file or including in the generation pipeline.
|
|
167
|
+
*
|
|
168
|
+
* @returns The formatted JSON content as a string
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const file = new JsonFile({
|
|
173
|
+
* path: './data.json',
|
|
174
|
+
* content: { name: 'test', value: 42 }
|
|
175
|
+
* });
|
|
176
|
+
*
|
|
177
|
+
* console.log(file.toString());
|
|
178
|
+
* // {
|
|
179
|
+
* // "name": "test",
|
|
180
|
+
* // "value": 42
|
|
181
|
+
* // }
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
27
184
|
toString() {
|
|
28
185
|
return JSON.stringify(this.content, null, 2);
|
|
29
186
|
}
|
|
@@ -2,11 +2,171 @@ import "../_dnt.polyfills.js";
|
|
|
2
2
|
import type { RefName } from '../types/RefName.js';
|
|
3
3
|
import type { OpenAPIV3 } from 'openapi-types';
|
|
4
4
|
import type { ReferenceObject } from '../oas/_merge-all-of/types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Extracts a reference name from an OpenAPI $ref string.
|
|
7
|
+
*
|
|
8
|
+
* This function parses JSON Pointer references (like '#/components/schemas/User')
|
|
9
|
+
* and extracts the final component as a branded RefName type. It's used throughout
|
|
10
|
+
* the SKMTC system to convert string references into type-safe reference names.
|
|
11
|
+
*
|
|
12
|
+
* @param $ref - The OpenAPI reference string (e.g., '#/components/schemas/User')
|
|
13
|
+
* @returns The extracted reference name as a branded RefName type
|
|
14
|
+
*
|
|
15
|
+
* @throws {Error} When the reference string is invalid or empty
|
|
16
|
+
*
|
|
17
|
+
* @example Basic usage
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { toRefName } from '@skmtc/core/helpers';
|
|
20
|
+
*
|
|
21
|
+
* const refName = toRefName('#/components/schemas/User');
|
|
22
|
+
* console.log(refName); // 'User' (typed as RefName)
|
|
23
|
+
*
|
|
24
|
+
* const responseRef = toRefName('#/components/responses/ErrorResponse');
|
|
25
|
+
* console.log(responseRef); // 'ErrorResponse' (typed as RefName)
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Error cases
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // These will throw errors:
|
|
31
|
+
* toRefName('invalid/ref/'); // Error: 'Invalid reference'
|
|
32
|
+
* toRefName('#/components/schemas/'); // Error: 'Invalid reference'
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example Integration with OAS processing
|
|
36
|
+
* ```typescript
|
|
37
|
+
* class SchemaProcessor {
|
|
38
|
+
* processReference(ref: string) {
|
|
39
|
+
* const refName = toRefName(ref);
|
|
40
|
+
* const schema = this.document.components.schemas[refName];
|
|
41
|
+
* return this.processSchema(schema, refName);
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
5
46
|
export declare const toRefName: ($ref: string) => RefName;
|
|
47
|
+
/**
|
|
48
|
+
* Type representing an object with a $ref property.
|
|
49
|
+
*/
|
|
6
50
|
type Ref = {
|
|
51
|
+
/** The OpenAPI reference string */
|
|
7
52
|
$ref: string;
|
|
8
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Type guard function to check if a value is an OpenAPI reference object.
|
|
56
|
+
*
|
|
57
|
+
* This function performs runtime type checking to determine if an unknown value
|
|
58
|
+
* is an OpenAPI Reference Object (contains a $ref property with a string value).
|
|
59
|
+
* It's essential for safely handling mixed schema/reference data during processing.
|
|
60
|
+
*
|
|
61
|
+
* @param value - The value to check for reference properties
|
|
62
|
+
* @returns True if the value is a reference object, false otherwise
|
|
63
|
+
*
|
|
64
|
+
* @example Basic type checking
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { isRef } from '@skmtc/core/helpers';
|
|
67
|
+
*
|
|
68
|
+
* const schemaOrRef: unknown = { $ref: '#/components/schemas/User' };
|
|
69
|
+
*
|
|
70
|
+
* if (isRef(schemaOrRef)) {
|
|
71
|
+
* // TypeScript now knows schemaOrRef has a $ref property
|
|
72
|
+
* console.log(schemaOrRef.$ref); // Safe to access
|
|
73
|
+
* const refName = toRefName(schemaOrRef.$ref);
|
|
74
|
+
* } else {
|
|
75
|
+
* // Handle as actual schema object
|
|
76
|
+
* console.log('Direct schema object');
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example Processing mixed data
|
|
81
|
+
* ```typescript
|
|
82
|
+
* function processSchemaOrRef(data: unknown) {
|
|
83
|
+
* if (isRef(data)) {
|
|
84
|
+
* // Handle reference
|
|
85
|
+
* return resolveReference(data.$ref);
|
|
86
|
+
* } else {
|
|
87
|
+
* // Handle direct schema
|
|
88
|
+
* return processSchema(data as SchemaObject);
|
|
89
|
+
* }
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example Array processing
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const mixedArray: unknown[] = [
|
|
96
|
+
* { type: 'string' },
|
|
97
|
+
* { $ref: '#/components/schemas/User' },
|
|
98
|
+
* { type: 'number' }
|
|
99
|
+
* ];
|
|
100
|
+
*
|
|
101
|
+
* const references = mixedArray.filter(isRef);
|
|
102
|
+
* const schemas = mixedArray.filter(item => !isRef(item));
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
9
105
|
export declare const isRef: (value: unknown) => value is Ref;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a reference resolver function for an OpenAPI document.
|
|
108
|
+
*
|
|
109
|
+
* This higher-order function returns a resolver that can dereference OpenAPI
|
|
110
|
+
* JSON Pointer references within the context of a specific document. It handles
|
|
111
|
+
* nested references by recursively resolving reference chains until it reaches
|
|
112
|
+
* an actual schema object.
|
|
113
|
+
*
|
|
114
|
+
* The resolver is particularly useful during schema processing where you need
|
|
115
|
+
* to follow reference chains to get the actual schema definitions.
|
|
116
|
+
*
|
|
117
|
+
* @param oasDocument - The OpenAPI document containing the schemas to resolve against
|
|
118
|
+
* @returns A function that resolves reference objects to schema objects
|
|
119
|
+
*
|
|
120
|
+
* @throws {Error} When a reference cannot be resolved or points to a non-existent schema
|
|
121
|
+
*
|
|
122
|
+
* @example Basic usage
|
|
123
|
+
* ```typescript
|
|
124
|
+
* import { toGetRef } from '@skmtc/core/helpers';
|
|
125
|
+
*
|
|
126
|
+
* const resolver = toGetRef(openApiDocument);
|
|
127
|
+
*
|
|
128
|
+
* const userSchema = resolver({ $ref: '#/components/schemas/User' });
|
|
129
|
+
* console.log(userSchema.type); // 'object'
|
|
130
|
+
* console.log(userSchema.properties); // User properties
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @example Resolving nested references
|
|
134
|
+
* ```typescript
|
|
135
|
+
* // If User references Address, and Address references Country
|
|
136
|
+
* const resolver = toGetRef(document);
|
|
137
|
+
*
|
|
138
|
+
* // This will follow the chain: User -> Address -> Country
|
|
139
|
+
* const resolvedSchema = resolver({ $ref: '#/components/schemas/User' });
|
|
140
|
+
* // Returns the final resolved schema object
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @example Error handling
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const resolver = toGetRef(document);
|
|
146
|
+
*
|
|
147
|
+
* try {
|
|
148
|
+
* const schema = resolver({ $ref: '#/components/schemas/NonExistent' });
|
|
149
|
+
* } catch (error) {
|
|
150
|
+
* console.error('Reference resolution failed:', error.message);
|
|
151
|
+
* // Error: Invalid reference: #/components/schemas/NonExistent
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @example Integration with schema processing
|
|
156
|
+
* ```typescript
|
|
157
|
+
* class SchemaProcessor {
|
|
158
|
+
* private resolver = toGetRef(this.document);
|
|
159
|
+
*
|
|
160
|
+
* processProperty(property: SchemaObject | ReferenceObject) {
|
|
161
|
+
* if (isRef(property)) {
|
|
162
|
+
* const resolved = this.resolver(property);
|
|
163
|
+
* return this.processSchema(resolved);
|
|
164
|
+
* }
|
|
165
|
+
* return this.processSchema(property);
|
|
166
|
+
* }
|
|
167
|
+
* }
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
10
170
|
export declare const toGetRef: (oasDocument: OpenAPIV3.Document) => ({ $ref }: ReferenceObject) => OpenAPIV3.SchemaObject;
|
|
11
171
|
export {};
|
|
12
172
|
//# sourceMappingURL=refFns.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refFns.d.ts","sourceRoot":"","sources":["../../src/helpers/refFns.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAEpE,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,OASxC,CAAA;AAED,KAAK,GAAG,GAAG;IACT,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,eAAO,MAAM,KAAK,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,GAM/C,CAAA;AAED,eAAO,MAAM,QAAQ,GAClB,aAAa,SAAS,CAAC,QAAQ,MAC/B,UAAU,eAAe,KAAG,SAAS,CAAC,YActC,CAAA"}
|
|
1
|
+
{"version":3,"file":"refFns.d.ts","sourceRoot":"","sources":["../../src/helpers/refFns.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,OASxC,CAAA;AAED;;GAEG;AACH,KAAK,GAAG,GAAG;IACT,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,GAM/C,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,eAAO,MAAM,QAAQ,GAClB,aAAa,SAAS,CAAC,QAAQ,MAC/B,UAAU,eAAe,KAAG,SAAS,CAAC,YActC,CAAA"}
|
package/script/helpers/refFns.js
CHANGED
|
@@ -2,6 +2,47 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toGetRef = exports.isRef = exports.toRefName = void 0;
|
|
4
4
|
require("../_dnt.polyfills.js");
|
|
5
|
+
/**
|
|
6
|
+
* Extracts a reference name from an OpenAPI $ref string.
|
|
7
|
+
*
|
|
8
|
+
* This function parses JSON Pointer references (like '#/components/schemas/User')
|
|
9
|
+
* and extracts the final component as a branded RefName type. It's used throughout
|
|
10
|
+
* the SKMTC system to convert string references into type-safe reference names.
|
|
11
|
+
*
|
|
12
|
+
* @param $ref - The OpenAPI reference string (e.g., '#/components/schemas/User')
|
|
13
|
+
* @returns The extracted reference name as a branded RefName type
|
|
14
|
+
*
|
|
15
|
+
* @throws {Error} When the reference string is invalid or empty
|
|
16
|
+
*
|
|
17
|
+
* @example Basic usage
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { toRefName } from '@skmtc/core/helpers';
|
|
20
|
+
*
|
|
21
|
+
* const refName = toRefName('#/components/schemas/User');
|
|
22
|
+
* console.log(refName); // 'User' (typed as RefName)
|
|
23
|
+
*
|
|
24
|
+
* const responseRef = toRefName('#/components/responses/ErrorResponse');
|
|
25
|
+
* console.log(responseRef); // 'ErrorResponse' (typed as RefName)
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Error cases
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // These will throw errors:
|
|
31
|
+
* toRefName('invalid/ref/'); // Error: 'Invalid reference'
|
|
32
|
+
* toRefName('#/components/schemas/'); // Error: 'Invalid reference'
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example Integration with OAS processing
|
|
36
|
+
* ```typescript
|
|
37
|
+
* class SchemaProcessor {
|
|
38
|
+
* processReference(ref: string) {
|
|
39
|
+
* const refName = toRefName(ref);
|
|
40
|
+
* const schema = this.document.components.schemas[refName];
|
|
41
|
+
* return this.processSchema(schema, refName);
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
5
46
|
const toRefName = ($ref) => {
|
|
6
47
|
// TODO: Add validation here to ensure reference exists
|
|
7
48
|
const refName = $ref.split('/').slice(-1)[0];
|
|
@@ -11,6 +52,57 @@ const toRefName = ($ref) => {
|
|
|
11
52
|
return refName;
|
|
12
53
|
};
|
|
13
54
|
exports.toRefName = toRefName;
|
|
55
|
+
/**
|
|
56
|
+
* Type guard function to check if a value is an OpenAPI reference object.
|
|
57
|
+
*
|
|
58
|
+
* This function performs runtime type checking to determine if an unknown value
|
|
59
|
+
* is an OpenAPI Reference Object (contains a $ref property with a string value).
|
|
60
|
+
* It's essential for safely handling mixed schema/reference data during processing.
|
|
61
|
+
*
|
|
62
|
+
* @param value - The value to check for reference properties
|
|
63
|
+
* @returns True if the value is a reference object, false otherwise
|
|
64
|
+
*
|
|
65
|
+
* @example Basic type checking
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import { isRef } from '@skmtc/core/helpers';
|
|
68
|
+
*
|
|
69
|
+
* const schemaOrRef: unknown = { $ref: '#/components/schemas/User' };
|
|
70
|
+
*
|
|
71
|
+
* if (isRef(schemaOrRef)) {
|
|
72
|
+
* // TypeScript now knows schemaOrRef has a $ref property
|
|
73
|
+
* console.log(schemaOrRef.$ref); // Safe to access
|
|
74
|
+
* const refName = toRefName(schemaOrRef.$ref);
|
|
75
|
+
* } else {
|
|
76
|
+
* // Handle as actual schema object
|
|
77
|
+
* console.log('Direct schema object');
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Processing mixed data
|
|
82
|
+
* ```typescript
|
|
83
|
+
* function processSchemaOrRef(data: unknown) {
|
|
84
|
+
* if (isRef(data)) {
|
|
85
|
+
* // Handle reference
|
|
86
|
+
* return resolveReference(data.$ref);
|
|
87
|
+
* } else {
|
|
88
|
+
* // Handle direct schema
|
|
89
|
+
* return processSchema(data as SchemaObject);
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @example Array processing
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const mixedArray: unknown[] = [
|
|
97
|
+
* { type: 'string' },
|
|
98
|
+
* { $ref: '#/components/schemas/User' },
|
|
99
|
+
* { type: 'number' }
|
|
100
|
+
* ];
|
|
101
|
+
*
|
|
102
|
+
* const references = mixedArray.filter(isRef);
|
|
103
|
+
* const schemas = mixedArray.filter(item => !isRef(item));
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
14
106
|
const isRef = (value) => {
|
|
15
107
|
if (value && typeof value === 'object' && '$ref' in value && typeof value.$ref === 'string') {
|
|
16
108
|
return true;
|
|
@@ -20,6 +112,70 @@ const isRef = (value) => {
|
|
|
20
112
|
}
|
|
21
113
|
};
|
|
22
114
|
exports.isRef = isRef;
|
|
115
|
+
/**
|
|
116
|
+
* Creates a reference resolver function for an OpenAPI document.
|
|
117
|
+
*
|
|
118
|
+
* This higher-order function returns a resolver that can dereference OpenAPI
|
|
119
|
+
* JSON Pointer references within the context of a specific document. It handles
|
|
120
|
+
* nested references by recursively resolving reference chains until it reaches
|
|
121
|
+
* an actual schema object.
|
|
122
|
+
*
|
|
123
|
+
* The resolver is particularly useful during schema processing where you need
|
|
124
|
+
* to follow reference chains to get the actual schema definitions.
|
|
125
|
+
*
|
|
126
|
+
* @param oasDocument - The OpenAPI document containing the schemas to resolve against
|
|
127
|
+
* @returns A function that resolves reference objects to schema objects
|
|
128
|
+
*
|
|
129
|
+
* @throws {Error} When a reference cannot be resolved or points to a non-existent schema
|
|
130
|
+
*
|
|
131
|
+
* @example Basic usage
|
|
132
|
+
* ```typescript
|
|
133
|
+
* import { toGetRef } from '@skmtc/core/helpers';
|
|
134
|
+
*
|
|
135
|
+
* const resolver = toGetRef(openApiDocument);
|
|
136
|
+
*
|
|
137
|
+
* const userSchema = resolver({ $ref: '#/components/schemas/User' });
|
|
138
|
+
* console.log(userSchema.type); // 'object'
|
|
139
|
+
* console.log(userSchema.properties); // User properties
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @example Resolving nested references
|
|
143
|
+
* ```typescript
|
|
144
|
+
* // If User references Address, and Address references Country
|
|
145
|
+
* const resolver = toGetRef(document);
|
|
146
|
+
*
|
|
147
|
+
* // This will follow the chain: User -> Address -> Country
|
|
148
|
+
* const resolvedSchema = resolver({ $ref: '#/components/schemas/User' });
|
|
149
|
+
* // Returns the final resolved schema object
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @example Error handling
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const resolver = toGetRef(document);
|
|
155
|
+
*
|
|
156
|
+
* try {
|
|
157
|
+
* const schema = resolver({ $ref: '#/components/schemas/NonExistent' });
|
|
158
|
+
* } catch (error) {
|
|
159
|
+
* console.error('Reference resolution failed:', error.message);
|
|
160
|
+
* // Error: Invalid reference: #/components/schemas/NonExistent
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* @example Integration with schema processing
|
|
165
|
+
* ```typescript
|
|
166
|
+
* class SchemaProcessor {
|
|
167
|
+
* private resolver = toGetRef(this.document);
|
|
168
|
+
*
|
|
169
|
+
* processProperty(property: SchemaObject | ReferenceObject) {
|
|
170
|
+
* if (isRef(property)) {
|
|
171
|
+
* const resolved = this.resolver(property);
|
|
172
|
+
* return this.processSchema(resolved);
|
|
173
|
+
* }
|
|
174
|
+
* return this.processSchema(property);
|
|
175
|
+
* }
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
23
179
|
const toGetRef = (oasDocument) => ({ $ref }) => {
|
|
24
180
|
const refName = (0, exports.toRefName)($ref);
|
|
25
181
|
const item = oasDocument.components?.schemas?.[refName];
|