@skmtc/core 0.0.793 → 0.0.794
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/helpers/ResultsLog.d.ts +193 -0
- package/esm/helpers/ResultsLog.d.ts.map +1 -1
- package/esm/helpers/ResultsLog.js +213 -0
- package/esm/helpers/tracer.d.ts +124 -0
- package/esm/helpers/tracer.d.ts.map +1 -1
- package/esm/helpers/tracer.js +124 -0
- package/esm/typescript/PathParams.d.ts +198 -0
- package/esm/typescript/PathParams.d.ts.map +1 -1
- package/esm/typescript/PathParams.js +190 -0
- package/esm/typescript/ReactRouterPathParams.d.ts +190 -0
- package/esm/typescript/ReactRouterPathParams.d.ts.map +1 -1
- package/esm/typescript/ReactRouterPathParams.js +183 -0
- package/esm/typescript/keyValues.d.ts +125 -0
- package/esm/typescript/keyValues.d.ts.map +1 -1
- package/esm/typescript/keyValues.js +125 -1
- package/esm/typescript/toPathParams.d.ts +179 -0
- package/esm/typescript/toPathParams.d.ts.map +1 -1
- package/esm/typescript/toPathParams.js +179 -0
- package/esm/typescript/toPathTemplate.d.ts +114 -0
- package/esm/typescript/toPathTemplate.d.ts.map +1 -1
- package/esm/typescript/toPathTemplate.js +114 -0
- package/package.json +1 -1
- package/script/helpers/ResultsLog.d.ts +193 -0
- package/script/helpers/ResultsLog.d.ts.map +1 -1
- package/script/helpers/ResultsLog.js +213 -0
- package/script/helpers/tracer.d.ts +124 -0
- package/script/helpers/tracer.d.ts.map +1 -1
- package/script/helpers/tracer.js +124 -0
- package/script/typescript/PathParams.d.ts +198 -0
- package/script/typescript/PathParams.d.ts.map +1 -1
- package/script/typescript/PathParams.js +190 -0
- package/script/typescript/ReactRouterPathParams.d.ts +190 -0
- package/script/typescript/ReactRouterPathParams.d.ts.map +1 -1
- package/script/typescript/ReactRouterPathParams.js +183 -0
- package/script/typescript/keyValues.d.ts +125 -0
- package/script/typescript/keyValues.d.ts.map +1 -1
- package/script/typescript/keyValues.js +125 -1
- package/script/typescript/toPathParams.d.ts +179 -0
- package/script/typescript/toPathParams.d.ts.map +1 -1
- package/script/typescript/toPathParams.js +179 -0
- package/script/typescript/toPathTemplate.d.ts +114 -0
- package/script/typescript/toPathTemplate.d.ts.map +1 -1
- package/script/typescript/toPathTemplate.js +114 -0
package/script/helpers/tracer.js
CHANGED
|
@@ -1,6 +1,130 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.tracer = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Executes a function with distributed tracing support and stack trail management.
|
|
6
|
+
*
|
|
7
|
+
* This utility function provides distributed tracing capabilities for the SKMTC
|
|
8
|
+
* generation pipeline. It manages stack trail tokens that help track execution
|
|
9
|
+
* context across complex nested operations, making debugging and performance
|
|
10
|
+
* analysis much easier.
|
|
11
|
+
*
|
|
12
|
+
* The tracer ensures proper cleanup of stack trail tokens even if the wrapped
|
|
13
|
+
* function throws an error, maintaining the integrity of the tracing system.
|
|
14
|
+
* This is essential for accurate debugging and performance monitoring in
|
|
15
|
+
* complex code generation scenarios.
|
|
16
|
+
*
|
|
17
|
+
* @template T - The return type of the traced function
|
|
18
|
+
* @param stackTrail - The stack trail instance for tracking execution context
|
|
19
|
+
* @param token - Token(s) to identify this execution step in traces
|
|
20
|
+
* @param fn - The function to execute with tracing
|
|
21
|
+
* @param log - Logger instance for error reporting
|
|
22
|
+
* @returns The result of the wrapped function execution
|
|
23
|
+
*
|
|
24
|
+
* @throws Re-throws any error from the wrapped function after cleanup
|
|
25
|
+
*
|
|
26
|
+
* @example Basic tracing usage
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { tracer } from '@skmtc/core';
|
|
29
|
+
*
|
|
30
|
+
* class ModelGenerator {
|
|
31
|
+
* generateModel(stackTrail: StackTrail, logger: Logger) {
|
|
32
|
+
* return tracer(stackTrail, 'generate-model', () => {
|
|
33
|
+
* // Complex model generation logic here
|
|
34
|
+
* return this.createModelDefinition();
|
|
35
|
+
* }, logger);
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example Nested tracing
|
|
41
|
+
* ```typescript
|
|
42
|
+
* class ComplexProcessor {
|
|
43
|
+
* processSchema(stackTrail: StackTrail, logger: Logger) {
|
|
44
|
+
* return tracer(stackTrail, 'process-schema', () => {
|
|
45
|
+
* const parsed = tracer(stackTrail, 'parse', () => {
|
|
46
|
+
* return this.parseSchema();
|
|
47
|
+
* }, logger);
|
|
48
|
+
*
|
|
49
|
+
* const validated = tracer(stackTrail, 'validate', () => {
|
|
50
|
+
* return this.validateSchema(parsed);
|
|
51
|
+
* }, logger);
|
|
52
|
+
*
|
|
53
|
+
* return tracer(stackTrail, 'transform', () => {
|
|
54
|
+
* return this.transformSchema(validated);
|
|
55
|
+
* }, logger);
|
|
56
|
+
* }, logger);
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // Stack trail will show: ['process-schema', 'parse'] -> ['process-schema', 'validate'] -> ['process-schema', 'transform']
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example Multiple token tracing
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const result = tracer(
|
|
66
|
+
* stackTrail,
|
|
67
|
+
* ['file-generation', 'models.ts'], // Multiple tokens for detailed context
|
|
68
|
+
* () => {
|
|
69
|
+
* return generateModelsFile();
|
|
70
|
+
* },
|
|
71
|
+
* logger
|
|
72
|
+
* );
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @example Error handling with tracing
|
|
76
|
+
* ```typescript
|
|
77
|
+
* class SafeGenerator {
|
|
78
|
+
* generateWithTracing(stackTrail: StackTrail, logger: Logger) {
|
|
79
|
+
* try {
|
|
80
|
+
* return tracer(stackTrail, 'risky-operation', () => {
|
|
81
|
+
* // This might throw an error
|
|
82
|
+
* return this.complexGeneration();
|
|
83
|
+
* }, logger);
|
|
84
|
+
* } catch (error) {
|
|
85
|
+
* // Stack trail is automatically cleaned up
|
|
86
|
+
* logger.error(`Generation failed: ${error.message}`);
|
|
87
|
+
* return this.fallbackGeneration();
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example Performance monitoring
|
|
94
|
+
* ```typescript
|
|
95
|
+
* class PerformanceAwareGenerator {
|
|
96
|
+
* timedGeneration(stackTrail: StackTrail, logger: Logger) {
|
|
97
|
+
* const startTime = Date.now();
|
|
98
|
+
*
|
|
99
|
+
* const result = tracer(stackTrail, 'timed-generation', () => {
|
|
100
|
+
* return this.heavyComputation();
|
|
101
|
+
* }, logger);
|
|
102
|
+
*
|
|
103
|
+
* const duration = Date.now() - startTime;
|
|
104
|
+
* logger.info(`Generation completed in ${duration}ms`);
|
|
105
|
+
*
|
|
106
|
+
* return result;
|
|
107
|
+
* }
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example Integration with context classes
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // Typically used within context classes like this:
|
|
114
|
+
* class GenerateContext {
|
|
115
|
+
* trace<T>(token: string | string[], fn: () => T): T {
|
|
116
|
+
* return tracer(this.stackTrail, token, fn, this.logger);
|
|
117
|
+
* }
|
|
118
|
+
*
|
|
119
|
+
* processModel(schema: OasSchema) {
|
|
120
|
+
* return this.trace(['model', schema.type], () => {
|
|
121
|
+
* // Model processing logic
|
|
122
|
+
* return this.createModel(schema);
|
|
123
|
+
* });
|
|
124
|
+
* }
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
4
128
|
const tracer = (stackTrail, token, fn, log) => {
|
|
5
129
|
stackTrail.append(token);
|
|
6
130
|
try {
|
|
@@ -2,18 +2,216 @@ import type { GenerateContext } from '../context/GenerateContext.js';
|
|
|
2
2
|
import { Definition } from '../dsl/Definition.js';
|
|
3
3
|
import type { TypeSystemObject } from '../types/TypeSystem.js';
|
|
4
4
|
import { FunctionParameter } from './FunctionParameter.js';
|
|
5
|
+
/**
|
|
6
|
+
* Constructor arguments for {@link PathParams}.
|
|
7
|
+
*/
|
|
5
8
|
type ConstructorArgs = {
|
|
9
|
+
/** The generation context for registering types and handling imports */
|
|
6
10
|
context: GenerateContext;
|
|
11
|
+
/** Optional argument name for the parameter (if undefined, uses destructuring) */
|
|
7
12
|
argName?: string;
|
|
13
|
+
/** Name of the TypeScript type to generate */
|
|
8
14
|
typeName: string;
|
|
15
|
+
/** The type system object defining the parameter structure */
|
|
9
16
|
typeValue: TypeSystemObject;
|
|
17
|
+
/** OpenAPI path template with parameter placeholders */
|
|
10
18
|
pathTemplate: string;
|
|
11
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Manages path parameter type definitions and function parameters for API endpoints.
|
|
22
|
+
*
|
|
23
|
+
* `PathParams` is a utility class that combines type generation, function parameter
|
|
24
|
+
* handling, and path templating for OpenAPI path parameters. It creates TypeScript
|
|
25
|
+
* type definitions for path parameters and generates the corresponding function
|
|
26
|
+
* parameters that can be used in API client methods.
|
|
27
|
+
*
|
|
28
|
+
* This class is essential for generating type-safe API clients where path parameters
|
|
29
|
+
* are properly typed and validated at compile time. It handles both destructured
|
|
30
|
+
* parameters (when no argument name is provided) and named parameters.
|
|
31
|
+
*
|
|
32
|
+
* ## Key Features
|
|
33
|
+
*
|
|
34
|
+
* - **Type Definition Generation**: Creates TypeScript types for path parameters
|
|
35
|
+
* - **Function Parameter Integration**: Generates properly typed function parameters
|
|
36
|
+
* - **Path Template Processing**: Converts OpenAPI paths to template literals
|
|
37
|
+
* - **Destructuring Support**: Handles both named and destructured parameter patterns
|
|
38
|
+
* - **Context Integration**: Fully integrated with the SKMTC generation pipeline
|
|
39
|
+
*
|
|
40
|
+
* @example Basic path parameters with destructuring
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { PathParams } from '@skmtc/core';
|
|
43
|
+
*
|
|
44
|
+
* const pathParams = new PathParams({
|
|
45
|
+
* context: generateContext,
|
|
46
|
+
* typeName: 'GetUserParams',
|
|
47
|
+
* typeValue: {
|
|
48
|
+
* type: 'object',
|
|
49
|
+
* objectProperties: {
|
|
50
|
+
* properties: {
|
|
51
|
+
* id: { type: 'string' },
|
|
52
|
+
* format: { type: 'string' }
|
|
53
|
+
* }
|
|
54
|
+
* }
|
|
55
|
+
* },
|
|
56
|
+
* pathTemplate: '/users/{id}'
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* console.log(pathParams.path); // '/users/${id}'
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example Named parameter usage
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const namedPathParams = new PathParams({
|
|
65
|
+
* context: generateContext,
|
|
66
|
+
* argName: 'params',
|
|
67
|
+
* typeName: 'UserPathParams',
|
|
68
|
+
* typeValue: {
|
|
69
|
+
* type: 'object',
|
|
70
|
+
* objectProperties: {
|
|
71
|
+
* properties: {
|
|
72
|
+
* userId: { type: 'string' },
|
|
73
|
+
* projectId: { type: 'string' }
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
* },
|
|
77
|
+
* pathTemplate: '/users/{userId}/projects/{projectId}'
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* console.log(pathParams.path); // '/users/${params.userId}/projects/${params.projectId}'
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example API client method generation
|
|
84
|
+
* ```typescript
|
|
85
|
+
* class ApiMethodGenerator {
|
|
86
|
+
* generateGetMethod(operationPath: string, parameters: any) {
|
|
87
|
+
* const pathParams = new PathParams({
|
|
88
|
+
* context: this.context,
|
|
89
|
+
* argName: 'pathParams',
|
|
90
|
+
* typeName: 'PathParameters',
|
|
91
|
+
* typeValue: parameters,
|
|
92
|
+
* pathTemplate: operationPath
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* return `
|
|
96
|
+
* async getData(${pathParams.parameter.toString()}) {
|
|
97
|
+
* const url = \`${pathParams.path}\`;
|
|
98
|
+
* return this.client.get(url);
|
|
99
|
+
* }`;
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
*
|
|
103
|
+
* // Generates:
|
|
104
|
+
* // async getData(pathParams: PathParameters) {
|
|
105
|
+
* // const url = `/users/${pathParams.userId}/projects/${pathParams.projectId}`;
|
|
106
|
+
* // return this.client.get(url);
|
|
107
|
+
* // }
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example With destructured parameters
|
|
111
|
+
* ```typescript
|
|
112
|
+
* const destructuredParams = new PathParams({
|
|
113
|
+
* context: generateContext,
|
|
114
|
+
* // No argName provided, so uses destructuring
|
|
115
|
+
* typeName: 'OrderParams',
|
|
116
|
+
* typeValue: {
|
|
117
|
+
* type: 'object',
|
|
118
|
+
* objectProperties: {
|
|
119
|
+
* properties: {
|
|
120
|
+
* orderId: { type: 'string' },
|
|
121
|
+
* customerId: { type: 'string' }
|
|
122
|
+
* }
|
|
123
|
+
* }
|
|
124
|
+
* },
|
|
125
|
+
* pathTemplate: '/customers/{customerId}/orders/{orderId}'
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* // Function parameter will be: { orderId, customerId }: OrderParams
|
|
129
|
+
* // Path template will be: '/customers/${customerId}/orders/${orderId}'
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @example Complex nested path parameters
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const complexPathParams = new PathParams({
|
|
135
|
+
* context: generateContext,
|
|
136
|
+
* argName: 'pathData',
|
|
137
|
+
* typeName: 'ComplexPathParams',
|
|
138
|
+
* typeValue: {
|
|
139
|
+
* type: 'object',
|
|
140
|
+
* objectProperties: {
|
|
141
|
+
* properties: {
|
|
142
|
+
* orgId: { type: 'string' },
|
|
143
|
+
* projectId: { type: 'string' },
|
|
144
|
+
* issueId: { type: 'number' },
|
|
145
|
+
* commentId: { type: 'string' }
|
|
146
|
+
* }
|
|
147
|
+
* }
|
|
148
|
+
* },
|
|
149
|
+
* pathTemplate: '/orgs/{orgId}/projects/{projectId}/issues/{issueId}/comments/{commentId}'
|
|
150
|
+
* });
|
|
151
|
+
*
|
|
152
|
+
* // Generates path: '/orgs/${pathData.orgId}/projects/${pathData.projectId}/issues/${pathData.issueId}/comments/${pathData.commentId}'
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @example Integration with operation generation
|
|
156
|
+
* ```typescript
|
|
157
|
+
* class OperationGenerator {
|
|
158
|
+
* generateOperation(operation: OasOperation) {
|
|
159
|
+
* const pathParameters = this.extractPathParameters(operation);
|
|
160
|
+
*
|
|
161
|
+
* if (pathParameters.length > 0) {
|
|
162
|
+
* const pathParams = new PathParams({
|
|
163
|
+
* context: this.context,
|
|
164
|
+
* typeName: `${operation.operationId}PathParams`,
|
|
165
|
+
* typeValue: this.buildParametersType(pathParameters),
|
|
166
|
+
* pathTemplate: operation.path
|
|
167
|
+
* });
|
|
168
|
+
*
|
|
169
|
+
* return this.generateMethodWithPathParams(operation, pathParams);
|
|
170
|
+
* }
|
|
171
|
+
*
|
|
172
|
+
* return this.generateSimpleMethod(operation);
|
|
173
|
+
* }
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
12
177
|
export declare class PathParams {
|
|
178
|
+
/** The generation context used for type registration and imports */
|
|
13
179
|
context: GenerateContext;
|
|
180
|
+
/** The TypeScript type definition for the path parameters */
|
|
14
181
|
typeDefinition: Definition<TypeSystemObject>;
|
|
182
|
+
/** The function parameter representation for method signatures */
|
|
15
183
|
parameter: FunctionParameter;
|
|
184
|
+
/** The processed path template with parameter substitutions */
|
|
16
185
|
path: string;
|
|
186
|
+
/**
|
|
187
|
+
* Creates a new PathParams instance with type definitions and path processing.
|
|
188
|
+
*
|
|
189
|
+
* The constructor sets up the complete parameter handling pipeline:
|
|
190
|
+
* 1. Creates a TypeScript type definition for the parameters
|
|
191
|
+
* 2. Generates a function parameter (either named or destructured)
|
|
192
|
+
* 3. Processes the path template for runtime interpolation
|
|
193
|
+
*
|
|
194
|
+
* @param args - Configuration for path parameter generation
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* const pathParams = new PathParams({
|
|
199
|
+
* context: generateContext,
|
|
200
|
+
* argName: 'params',
|
|
201
|
+
* typeName: 'UserPathParams',
|
|
202
|
+
* typeValue: {
|
|
203
|
+
* type: 'object',
|
|
204
|
+
* objectProperties: {
|
|
205
|
+
* properties: {
|
|
206
|
+
* id: { type: 'string' },
|
|
207
|
+
* format: { type: 'string' }
|
|
208
|
+
* }
|
|
209
|
+
* }
|
|
210
|
+
* },
|
|
211
|
+
* pathTemplate: '/users/{id}/profile'
|
|
212
|
+
* });
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
17
215
|
constructor({ context, argName, typeName, typeValue, pathTemplate }: ConstructorArgs);
|
|
18
216
|
}
|
|
19
217
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/PathParams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAC9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAG1D,KAAK,eAAe,GAAG;IACrB,OAAO,EAAE,eAAe,CAAA;IACxB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,gBAAgB,CAAA;IAC3B,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED,qBAAa,UAAU;IACrB,OAAO,EAAE,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"PathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/PathParams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAC9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAG1D;;GAEG;AACH,KAAK,eAAe,GAAG;IACrB,wEAAwE;IACxE,OAAO,EAAE,eAAe,CAAA;IACxB,kFAAkF;IAClF,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,8CAA8C;IAC9C,QAAQ,EAAE,MAAM,CAAA;IAChB,8DAA8D;IAC9D,SAAS,EAAE,gBAAgB,CAAA;IAC3B,wDAAwD;IACxD,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4JG;AACH,qBAAa,UAAU;IACrB,oEAAoE;IACpE,OAAO,EAAE,eAAe,CAAA;IAExB,6DAA6D;IAC7D,cAAc,EAAE,UAAU,CAAC,gBAAgB,CAAC,CAAA;IAE5C,kEAAkE;IAClE,SAAS,EAAE,iBAAiB,CAAA;IAE5B,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAA;IAEZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;gBACS,EACV,OAAO,EACP,OAAO,EACP,QAAQ,EACR,SAAS,EACT,YAAY,EACb,EAAE,eAAe;CAwBnB"}
|
|
@@ -5,26 +5,216 @@ const Definition_js_1 = require("../dsl/Definition.js");
|
|
|
5
5
|
const Identifier_js_1 = require("../dsl/Identifier.js");
|
|
6
6
|
const FunctionParameter_js_1 = require("./FunctionParameter.js");
|
|
7
7
|
const toPathTemplate_js_1 = require("./toPathTemplate.js");
|
|
8
|
+
/**
|
|
9
|
+
* Manages path parameter type definitions and function parameters for API endpoints.
|
|
10
|
+
*
|
|
11
|
+
* `PathParams` is a utility class that combines type generation, function parameter
|
|
12
|
+
* handling, and path templating for OpenAPI path parameters. It creates TypeScript
|
|
13
|
+
* type definitions for path parameters and generates the corresponding function
|
|
14
|
+
* parameters that can be used in API client methods.
|
|
15
|
+
*
|
|
16
|
+
* This class is essential for generating type-safe API clients where path parameters
|
|
17
|
+
* are properly typed and validated at compile time. It handles both destructured
|
|
18
|
+
* parameters (when no argument name is provided) and named parameters.
|
|
19
|
+
*
|
|
20
|
+
* ## Key Features
|
|
21
|
+
*
|
|
22
|
+
* - **Type Definition Generation**: Creates TypeScript types for path parameters
|
|
23
|
+
* - **Function Parameter Integration**: Generates properly typed function parameters
|
|
24
|
+
* - **Path Template Processing**: Converts OpenAPI paths to template literals
|
|
25
|
+
* - **Destructuring Support**: Handles both named and destructured parameter patterns
|
|
26
|
+
* - **Context Integration**: Fully integrated with the SKMTC generation pipeline
|
|
27
|
+
*
|
|
28
|
+
* @example Basic path parameters with destructuring
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { PathParams } from '@skmtc/core';
|
|
31
|
+
*
|
|
32
|
+
* const pathParams = new PathParams({
|
|
33
|
+
* context: generateContext,
|
|
34
|
+
* typeName: 'GetUserParams',
|
|
35
|
+
* typeValue: {
|
|
36
|
+
* type: 'object',
|
|
37
|
+
* objectProperties: {
|
|
38
|
+
* properties: {
|
|
39
|
+
* id: { type: 'string' },
|
|
40
|
+
* format: { type: 'string' }
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* },
|
|
44
|
+
* pathTemplate: '/users/{id}'
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* console.log(pathParams.path); // '/users/${id}'
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @example Named parameter usage
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const namedPathParams = new PathParams({
|
|
53
|
+
* context: generateContext,
|
|
54
|
+
* argName: 'params',
|
|
55
|
+
* typeName: 'UserPathParams',
|
|
56
|
+
* typeValue: {
|
|
57
|
+
* type: 'object',
|
|
58
|
+
* objectProperties: {
|
|
59
|
+
* properties: {
|
|
60
|
+
* userId: { type: 'string' },
|
|
61
|
+
* projectId: { type: 'string' }
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* },
|
|
65
|
+
* pathTemplate: '/users/{userId}/projects/{projectId}'
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* console.log(pathParams.path); // '/users/${params.userId}/projects/${params.projectId}'
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example API client method generation
|
|
72
|
+
* ```typescript
|
|
73
|
+
* class ApiMethodGenerator {
|
|
74
|
+
* generateGetMethod(operationPath: string, parameters: any) {
|
|
75
|
+
* const pathParams = new PathParams({
|
|
76
|
+
* context: this.context,
|
|
77
|
+
* argName: 'pathParams',
|
|
78
|
+
* typeName: 'PathParameters',
|
|
79
|
+
* typeValue: parameters,
|
|
80
|
+
* pathTemplate: operationPath
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* return `
|
|
84
|
+
* async getData(${pathParams.parameter.toString()}) {
|
|
85
|
+
* const url = \`${pathParams.path}\`;
|
|
86
|
+
* return this.client.get(url);
|
|
87
|
+
* }`;
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* // Generates:
|
|
92
|
+
* // async getData(pathParams: PathParameters) {
|
|
93
|
+
* // const url = `/users/${pathParams.userId}/projects/${pathParams.projectId}`;
|
|
94
|
+
* // return this.client.get(url);
|
|
95
|
+
* // }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @example With destructured parameters
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const destructuredParams = new PathParams({
|
|
101
|
+
* context: generateContext,
|
|
102
|
+
* // No argName provided, so uses destructuring
|
|
103
|
+
* typeName: 'OrderParams',
|
|
104
|
+
* typeValue: {
|
|
105
|
+
* type: 'object',
|
|
106
|
+
* objectProperties: {
|
|
107
|
+
* properties: {
|
|
108
|
+
* orderId: { type: 'string' },
|
|
109
|
+
* customerId: { type: 'string' }
|
|
110
|
+
* }
|
|
111
|
+
* }
|
|
112
|
+
* },
|
|
113
|
+
* pathTemplate: '/customers/{customerId}/orders/{orderId}'
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* // Function parameter will be: { orderId, customerId }: OrderParams
|
|
117
|
+
* // Path template will be: '/customers/${customerId}/orders/${orderId}'
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @example Complex nested path parameters
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const complexPathParams = new PathParams({
|
|
123
|
+
* context: generateContext,
|
|
124
|
+
* argName: 'pathData',
|
|
125
|
+
* typeName: 'ComplexPathParams',
|
|
126
|
+
* typeValue: {
|
|
127
|
+
* type: 'object',
|
|
128
|
+
* objectProperties: {
|
|
129
|
+
* properties: {
|
|
130
|
+
* orgId: { type: 'string' },
|
|
131
|
+
* projectId: { type: 'string' },
|
|
132
|
+
* issueId: { type: 'number' },
|
|
133
|
+
* commentId: { type: 'string' }
|
|
134
|
+
* }
|
|
135
|
+
* }
|
|
136
|
+
* },
|
|
137
|
+
* pathTemplate: '/orgs/{orgId}/projects/{projectId}/issues/{issueId}/comments/{commentId}'
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* // Generates path: '/orgs/${pathData.orgId}/projects/${pathData.projectId}/issues/${pathData.issueId}/comments/${pathData.commentId}'
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @example Integration with operation generation
|
|
144
|
+
* ```typescript
|
|
145
|
+
* class OperationGenerator {
|
|
146
|
+
* generateOperation(operation: OasOperation) {
|
|
147
|
+
* const pathParameters = this.extractPathParameters(operation);
|
|
148
|
+
*
|
|
149
|
+
* if (pathParameters.length > 0) {
|
|
150
|
+
* const pathParams = new PathParams({
|
|
151
|
+
* context: this.context,
|
|
152
|
+
* typeName: `${operation.operationId}PathParams`,
|
|
153
|
+
* typeValue: this.buildParametersType(pathParameters),
|
|
154
|
+
* pathTemplate: operation.path
|
|
155
|
+
* });
|
|
156
|
+
*
|
|
157
|
+
* return this.generateMethodWithPathParams(operation, pathParams);
|
|
158
|
+
* }
|
|
159
|
+
*
|
|
160
|
+
* return this.generateSimpleMethod(operation);
|
|
161
|
+
* }
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
8
165
|
class PathParams {
|
|
166
|
+
/**
|
|
167
|
+
* Creates a new PathParams instance with type definitions and path processing.
|
|
168
|
+
*
|
|
169
|
+
* The constructor sets up the complete parameter handling pipeline:
|
|
170
|
+
* 1. Creates a TypeScript type definition for the parameters
|
|
171
|
+
* 2. Generates a function parameter (either named or destructured)
|
|
172
|
+
* 3. Processes the path template for runtime interpolation
|
|
173
|
+
*
|
|
174
|
+
* @param args - Configuration for path parameter generation
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const pathParams = new PathParams({
|
|
179
|
+
* context: generateContext,
|
|
180
|
+
* argName: 'params',
|
|
181
|
+
* typeName: 'UserPathParams',
|
|
182
|
+
* typeValue: {
|
|
183
|
+
* type: 'object',
|
|
184
|
+
* objectProperties: {
|
|
185
|
+
* properties: {
|
|
186
|
+
* id: { type: 'string' },
|
|
187
|
+
* format: { type: 'string' }
|
|
188
|
+
* }
|
|
189
|
+
* }
|
|
190
|
+
* },
|
|
191
|
+
* pathTemplate: '/users/{id}/profile'
|
|
192
|
+
* });
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
9
195
|
constructor({ context, argName, typeName, typeValue, pathTemplate }) {
|
|
196
|
+
/** The generation context used for type registration and imports */
|
|
10
197
|
Object.defineProperty(this, "context", {
|
|
11
198
|
enumerable: true,
|
|
12
199
|
configurable: true,
|
|
13
200
|
writable: true,
|
|
14
201
|
value: void 0
|
|
15
202
|
});
|
|
203
|
+
/** The TypeScript type definition for the path parameters */
|
|
16
204
|
Object.defineProperty(this, "typeDefinition", {
|
|
17
205
|
enumerable: true,
|
|
18
206
|
configurable: true,
|
|
19
207
|
writable: true,
|
|
20
208
|
value: void 0
|
|
21
209
|
});
|
|
210
|
+
/** The function parameter representation for method signatures */
|
|
22
211
|
Object.defineProperty(this, "parameter", {
|
|
23
212
|
enumerable: true,
|
|
24
213
|
configurable: true,
|
|
25
214
|
writable: true,
|
|
26
215
|
value: void 0
|
|
27
216
|
});
|
|
217
|
+
/** The processed path template with parameter substitutions */
|
|
28
218
|
Object.defineProperty(this, "path", {
|
|
29
219
|
enumerable: true,
|
|
30
220
|
configurable: true,
|