@skmtc/core 0.0.793 → 0.0.795

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.
Files changed (63) hide show
  1. package/esm/context/StackTrail.d.ts +293 -0
  2. package/esm/context/StackTrail.d.ts.map +1 -1
  3. package/esm/context/StackTrail.js +294 -0
  4. package/esm/helpers/ResultsLog.d.ts +193 -0
  5. package/esm/helpers/ResultsLog.d.ts.map +1 -1
  6. package/esm/helpers/ResultsLog.js +213 -0
  7. package/esm/helpers/tracer.d.ts +124 -0
  8. package/esm/helpers/tracer.d.ts.map +1 -1
  9. package/esm/helpers/tracer.js +124 -0
  10. package/esm/types/Modifiers.d.ts +68 -0
  11. package/esm/types/Modifiers.d.ts.map +1 -1
  12. package/esm/types/TypeSystem.d.ts +419 -0
  13. package/esm/types/TypeSystem.d.ts.map +1 -1
  14. package/esm/typescript/FunctionParameter.d.ts +313 -0
  15. package/esm/typescript/FunctionParameter.d.ts.map +1 -1
  16. package/esm/typescript/FunctionParameter.js +328 -0
  17. package/esm/typescript/PathParams.d.ts +198 -0
  18. package/esm/typescript/PathParams.d.ts.map +1 -1
  19. package/esm/typescript/PathParams.js +190 -0
  20. package/esm/typescript/ReactRouterPathParams.d.ts +190 -0
  21. package/esm/typescript/ReactRouterPathParams.d.ts.map +1 -1
  22. package/esm/typescript/ReactRouterPathParams.js +183 -0
  23. package/esm/typescript/keyValues.d.ts +125 -0
  24. package/esm/typescript/keyValues.d.ts.map +1 -1
  25. package/esm/typescript/keyValues.js +125 -1
  26. package/esm/typescript/toPathParams.d.ts +179 -0
  27. package/esm/typescript/toPathParams.d.ts.map +1 -1
  28. package/esm/typescript/toPathParams.js +179 -0
  29. package/esm/typescript/toPathTemplate.d.ts +114 -0
  30. package/esm/typescript/toPathTemplate.d.ts.map +1 -1
  31. package/esm/typescript/toPathTemplate.js +114 -0
  32. package/package.json +1 -1
  33. package/script/context/StackTrail.d.ts +293 -0
  34. package/script/context/StackTrail.d.ts.map +1 -1
  35. package/script/context/StackTrail.js +294 -0
  36. package/script/helpers/ResultsLog.d.ts +193 -0
  37. package/script/helpers/ResultsLog.d.ts.map +1 -1
  38. package/script/helpers/ResultsLog.js +213 -0
  39. package/script/helpers/tracer.d.ts +124 -0
  40. package/script/helpers/tracer.d.ts.map +1 -1
  41. package/script/helpers/tracer.js +124 -0
  42. package/script/types/Modifiers.d.ts +68 -0
  43. package/script/types/Modifiers.d.ts.map +1 -1
  44. package/script/types/TypeSystem.d.ts +419 -0
  45. package/script/types/TypeSystem.d.ts.map +1 -1
  46. package/script/typescript/FunctionParameter.d.ts +313 -0
  47. package/script/typescript/FunctionParameter.d.ts.map +1 -1
  48. package/script/typescript/FunctionParameter.js +328 -0
  49. package/script/typescript/PathParams.d.ts +198 -0
  50. package/script/typescript/PathParams.d.ts.map +1 -1
  51. package/script/typescript/PathParams.js +190 -0
  52. package/script/typescript/ReactRouterPathParams.d.ts +190 -0
  53. package/script/typescript/ReactRouterPathParams.d.ts.map +1 -1
  54. package/script/typescript/ReactRouterPathParams.js +183 -0
  55. package/script/typescript/keyValues.d.ts +125 -0
  56. package/script/typescript/keyValues.d.ts.map +1 -1
  57. package/script/typescript/keyValues.js +125 -1
  58. package/script/typescript/toPathParams.d.ts +179 -0
  59. package/script/typescript/toPathParams.d.ts.map +1 -1
  60. package/script/typescript/toPathParams.js +179 -0
  61. package/script/typescript/toPathTemplate.d.ts +114 -0
  62. package/script/typescript/toPathTemplate.d.ts.map +1 -1
  63. package/script/typescript/toPathTemplate.js +114 -0
@@ -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,
@@ -2,18 +2,208 @@ import { ContentBase } from '../dsl/ContentBase.js';
2
2
  import type { GenerateContext } from '../context/GenerateContext.js';
3
3
  import type { OasOperation } from '../oas/operation/Operation.js';
4
4
  import type { GeneratorKey } from '../types/GeneratorKeys.js';
5
+ /**
6
+ * Constructor arguments for {@link ReactRouterPathParams}.
7
+ */
5
8
  type CreateArgs = {
9
+ /** The generation context for registering imports and handling dependencies */
6
10
  context: GenerateContext;
11
+ /** The generator key identifying this component */
7
12
  generatorKey: GeneratorKey;
13
+ /** The OpenAPI operation containing path parameters */
8
14
  operation: OasOperation;
15
+ /** The destination file path for import registration */
9
16
  destinationPath: string;
10
17
  };
18
+ /**
19
+ * Generates React Router path parameter handling code for OpenAPI operations.
20
+ *
21
+ * `ReactRouterPathParams` is a specialized content generator that creates the necessary
22
+ * React code for extracting and validating path parameters from React Router's `useParams`
23
+ * hook. It automatically generates parameter extraction, assertion logic, and prop passing
24
+ * code based on OpenAPI operation path parameters.
25
+ *
26
+ * This class is essential for generating type-safe React components that need to access
27
+ * URL path parameters, ensuring runtime validation and proper TypeScript typing. It
28
+ * integrates with React Router's `useParams` hook and adds runtime assertions using
29
+ * the `tiny-invariant` library.
30
+ *
31
+ * ## Key Features
32
+ *
33
+ * - **Automatic Parameter Extraction**: Generates `useParams()` destructuring code
34
+ * - **Runtime Assertions**: Creates invariant checks for required parameters
35
+ * - **Prop Passing**: Generates prop spreading syntax for component integration
36
+ * - **Import Management**: Automatically registers required imports
37
+ * - **Type Safety**: Ensures parameters are properly validated at runtime
38
+ *
39
+ * @example Basic usage with single path parameter
40
+ * ```typescript
41
+ * import { ReactRouterPathParams } from '@skmtc/core';
42
+ *
43
+ * const operation = new OasOperation({
44
+ * path: '/users/{id}',
45
+ * method: 'get',
46
+ * // ... other operation properties
47
+ * });
48
+ *
49
+ * const pathParams = new ReactRouterPathParams({
50
+ * context: generateContext,
51
+ * generatorKey: 'react-router-params',
52
+ * operation,
53
+ * destinationPath: './UserDetail.tsx'
54
+ * });
55
+ *
56
+ * console.log(pathParams.getParams);
57
+ * // 'const { id } = useParams()'
58
+ *
59
+ * console.log(pathParams.assertParams);
60
+ * // 'invariant(id, 'Expected id to be defined')'
61
+ *
62
+ * console.log(pathParams.passProps);
63
+ * // 'id={id}'
64
+ * ```
65
+ *
66
+ * @example Multiple path parameters
67
+ * ```typescript
68
+ * const complexOperation = new OasOperation({
69
+ * path: '/organizations/{orgId}/projects/{projectId}/issues/{issueId}',
70
+ * method: 'get',
71
+ * // ... other properties
72
+ * });
73
+ *
74
+ * const multiParams = new ReactRouterPathParams({
75
+ * context: generateContext,
76
+ * generatorKey: 'react-router-params',
77
+ * operation: complexOperation,
78
+ * destinationPath: './IssueDetail.tsx'
79
+ * });
80
+ *
81
+ * console.log(multiParams.getParams);
82
+ * // 'const { orgId, projectId, issueId } = useParams()'
83
+ *
84
+ * console.log(multiParams.assertParams);
85
+ * // 'invariant(orgId, 'Expected orgId to be defined')
86
+ * // invariant(projectId, 'Expected projectId to be defined')
87
+ * // invariant(issueId, 'Expected issueId to be defined')'
88
+ *
89
+ * console.log(multiParams.passProps);
90
+ * // 'orgId={orgId} projectId={projectId} issueId={issueId}'
91
+ * ```
92
+ *
93
+ * @example Generated React component integration
94
+ * ```typescript
95
+ * // Generated component might look like:
96
+ * import { useParams } from 'react-router-dom';
97
+ * import invariant from 'tiny-invariant';
98
+ *
99
+ * function UserDetail() {
100
+ * const { id } = useParams();
101
+ * invariant(id, 'Expected id to be defined');
102
+ *
103
+ * return <UserDetailView id={id} />;
104
+ * }
105
+ * ```
106
+ *
107
+ * @example No path parameters (empty generation)
108
+ * ```typescript
109
+ * const simpleOperation = new OasOperation({
110
+ * path: '/users',
111
+ * method: 'get',
112
+ * // ... other properties
113
+ * });
114
+ *
115
+ * const emptyParams = new ReactRouterPathParams({
116
+ * context: generateContext,
117
+ * generatorKey: 'react-router-params',
118
+ * operation: simpleOperation,
119
+ * destinationPath: './UserList.tsx'
120
+ * });
121
+ *
122
+ * console.log(emptyParams.getParams); // ''
123
+ * console.log(emptyParams.assertParams); // ''
124
+ * console.log(emptyParams.passProps); // ''
125
+ * // No imports registered for this case
126
+ * ```
127
+ *
128
+ * @example Integration in component generation
129
+ * ```typescript
130
+ * class ReactComponentGenerator {
131
+ * generateDetailComponent(operation: OasOperation) {
132
+ * const pathParams = new ReactRouterPathParams({
133
+ * context: this.context,
134
+ * generatorKey: 'react-component',
135
+ * operation,
136
+ * destinationPath: this.getDestinationPath(operation)
137
+ * });
138
+ *
139
+ * if (pathParams.names.length === 0) {
140
+ * // Generate simple component without parameters
141
+ * return this.generateSimpleComponent(operation);
142
+ * }
143
+ *
144
+ * return `
145
+ * function ${operation.operationId}() {
146
+ * ${pathParams.getParams}
147
+ * ${pathParams.assertParams}
148
+ *
149
+ * return (
150
+ * <DetailView ${pathParams.passProps} />
151
+ * );
152
+ * }`;
153
+ * }
154
+ * }
155
+ * ```
156
+ *
157
+ * @example Custom parameter validation
158
+ * ```typescript
159
+ * class CustomReactRouterParams extends ReactRouterPathParams {
160
+ * generateCustomAssertions(paramName: string): string {
161
+ * return `
162
+ * invariant(${paramName}, 'Expected ${paramName} to be defined');
163
+ * invariant(typeof ${paramName} === 'string', '${paramName} must be a string');
164
+ * invariant(${paramName}.length > 0, '${paramName} cannot be empty');`;
165
+ * }
166
+ * }
167
+ * ```
168
+ */
11
169
  export declare class ReactRouterPathParams extends ContentBase {
170
+ /** Generated code for extracting parameters from useParams() */
12
171
  getParams: string;
172
+ /** Generated code for runtime parameter assertions */
13
173
  assertParams: string;
174
+ /** Generated code for passing parameters as component props */
14
175
  passProps: string;
176
+ /** Array of parameter names extracted from the operation path */
15
177
  names: string[];
178
+ /**
179
+ * Creates a new ReactRouterPathParams instance with generated parameter handling code.
180
+ *
181
+ * The constructor analyzes the OpenAPI operation to extract path parameters and
182
+ * generates the necessary React code for parameter extraction, validation, and
183
+ * prop passing. It automatically registers required imports when parameters are present.
184
+ *
185
+ * @param args - Configuration for React Router parameter generation
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const pathParams = new ReactRouterPathParams({
190
+ * context: generateContext,
191
+ * generatorKey: 'react-params',
192
+ * operation: userDetailOperation, // has path '/users/{id}'
193
+ * destinationPath: './UserDetail.tsx'
194
+ * });
195
+ * ```
196
+ */
16
197
  constructor({ context, operation, generatorKey, destinationPath }: CreateArgs);
198
+ /**
199
+ * Returns an empty string as this class provides code through its properties.
200
+ *
201
+ * The ReactRouterPathParams class doesn't generate output through toString(),
202
+ * instead providing generated code through the `getParams`, `assertParams`,
203
+ * and `passProps` properties which can be used directly in template generation.
204
+ *
205
+ * @returns Empty string
206
+ */
17
207
  toString(): string;
18
208
  }
19
209
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"ReactRouterPathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/ReactRouterPathParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAE7D,KAAK,UAAU,GAAG;IAChB,OAAO,EAAE,eAAe,CAAA;IACxB,YAAY,EAAE,YAAY,CAAA;IAC1B,SAAS,EAAE,YAAY,CAAA;IACvB,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AAED,qBAAa,qBAAsB,SAAQ,WAAW;IACpD,SAAS,EAAE,MAAM,CAAK;IAEtB,YAAY,EAAE,MAAM,CAAK;IAEzB,SAAS,EAAE,MAAM,CAAK;IAEtB,KAAK,EAAE,MAAM,EAAE,CAAA;gBAEH,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,EAAE,UAAU;IAwBpE,QAAQ,IAAI,MAAM;CAG5B"}
1
+ {"version":3,"file":"ReactRouterPathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/ReactRouterPathParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAE7D;;GAEG;AACH,KAAK,UAAU,GAAG;IAChB,+EAA+E;IAC/E,OAAO,EAAE,eAAe,CAAA;IACxB,mDAAmD;IACnD,YAAY,EAAE,YAAY,CAAA;IAC1B,uDAAuD;IACvD,SAAS,EAAE,YAAY,CAAA;IACvB,wDAAwD;IACxD,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsJG;AACH,qBAAa,qBAAsB,SAAQ,WAAW;IACpD,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAK;IAEtB,sDAAsD;IACtD,YAAY,EAAE,MAAM,CAAK;IAEzB,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAK;IAEtB,iEAAiE;IACjE,KAAK,EAAE,MAAM,EAAE,CAAA;IAEf;;;;;;;;;;;;;;;;;;OAkBG;gBACS,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,EAAE,UAAU;IAwB7E;;;;;;;;OAQG;IACM,QAAQ,IAAI,MAAM;CAG5B"}
@@ -2,27 +2,201 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReactRouterPathParams = void 0;
4
4
  const ContentBase_js_1 = require("../dsl/ContentBase.js");
5
+ /**
6
+ * Generates React Router path parameter handling code for OpenAPI operations.
7
+ *
8
+ * `ReactRouterPathParams` is a specialized content generator that creates the necessary
9
+ * React code for extracting and validating path parameters from React Router's `useParams`
10
+ * hook. It automatically generates parameter extraction, assertion logic, and prop passing
11
+ * code based on OpenAPI operation path parameters.
12
+ *
13
+ * This class is essential for generating type-safe React components that need to access
14
+ * URL path parameters, ensuring runtime validation and proper TypeScript typing. It
15
+ * integrates with React Router's `useParams` hook and adds runtime assertions using
16
+ * the `tiny-invariant` library.
17
+ *
18
+ * ## Key Features
19
+ *
20
+ * - **Automatic Parameter Extraction**: Generates `useParams()` destructuring code
21
+ * - **Runtime Assertions**: Creates invariant checks for required parameters
22
+ * - **Prop Passing**: Generates prop spreading syntax for component integration
23
+ * - **Import Management**: Automatically registers required imports
24
+ * - **Type Safety**: Ensures parameters are properly validated at runtime
25
+ *
26
+ * @example Basic usage with single path parameter
27
+ * ```typescript
28
+ * import { ReactRouterPathParams } from '@skmtc/core';
29
+ *
30
+ * const operation = new OasOperation({
31
+ * path: '/users/{id}',
32
+ * method: 'get',
33
+ * // ... other operation properties
34
+ * });
35
+ *
36
+ * const pathParams = new ReactRouterPathParams({
37
+ * context: generateContext,
38
+ * generatorKey: 'react-router-params',
39
+ * operation,
40
+ * destinationPath: './UserDetail.tsx'
41
+ * });
42
+ *
43
+ * console.log(pathParams.getParams);
44
+ * // 'const { id } = useParams()'
45
+ *
46
+ * console.log(pathParams.assertParams);
47
+ * // 'invariant(id, 'Expected id to be defined')'
48
+ *
49
+ * console.log(pathParams.passProps);
50
+ * // 'id={id}'
51
+ * ```
52
+ *
53
+ * @example Multiple path parameters
54
+ * ```typescript
55
+ * const complexOperation = new OasOperation({
56
+ * path: '/organizations/{orgId}/projects/{projectId}/issues/{issueId}',
57
+ * method: 'get',
58
+ * // ... other properties
59
+ * });
60
+ *
61
+ * const multiParams = new ReactRouterPathParams({
62
+ * context: generateContext,
63
+ * generatorKey: 'react-router-params',
64
+ * operation: complexOperation,
65
+ * destinationPath: './IssueDetail.tsx'
66
+ * });
67
+ *
68
+ * console.log(multiParams.getParams);
69
+ * // 'const { orgId, projectId, issueId } = useParams()'
70
+ *
71
+ * console.log(multiParams.assertParams);
72
+ * // 'invariant(orgId, 'Expected orgId to be defined')
73
+ * // invariant(projectId, 'Expected projectId to be defined')
74
+ * // invariant(issueId, 'Expected issueId to be defined')'
75
+ *
76
+ * console.log(multiParams.passProps);
77
+ * // 'orgId={orgId} projectId={projectId} issueId={issueId}'
78
+ * ```
79
+ *
80
+ * @example Generated React component integration
81
+ * ```typescript
82
+ * // Generated component might look like:
83
+ * import { useParams } from 'react-router-dom';
84
+ * import invariant from 'tiny-invariant';
85
+ *
86
+ * function UserDetail() {
87
+ * const { id } = useParams();
88
+ * invariant(id, 'Expected id to be defined');
89
+ *
90
+ * return <UserDetailView id={id} />;
91
+ * }
92
+ * ```
93
+ *
94
+ * @example No path parameters (empty generation)
95
+ * ```typescript
96
+ * const simpleOperation = new OasOperation({
97
+ * path: '/users',
98
+ * method: 'get',
99
+ * // ... other properties
100
+ * });
101
+ *
102
+ * const emptyParams = new ReactRouterPathParams({
103
+ * context: generateContext,
104
+ * generatorKey: 'react-router-params',
105
+ * operation: simpleOperation,
106
+ * destinationPath: './UserList.tsx'
107
+ * });
108
+ *
109
+ * console.log(emptyParams.getParams); // ''
110
+ * console.log(emptyParams.assertParams); // ''
111
+ * console.log(emptyParams.passProps); // ''
112
+ * // No imports registered for this case
113
+ * ```
114
+ *
115
+ * @example Integration in component generation
116
+ * ```typescript
117
+ * class ReactComponentGenerator {
118
+ * generateDetailComponent(operation: OasOperation) {
119
+ * const pathParams = new ReactRouterPathParams({
120
+ * context: this.context,
121
+ * generatorKey: 'react-component',
122
+ * operation,
123
+ * destinationPath: this.getDestinationPath(operation)
124
+ * });
125
+ *
126
+ * if (pathParams.names.length === 0) {
127
+ * // Generate simple component without parameters
128
+ * return this.generateSimpleComponent(operation);
129
+ * }
130
+ *
131
+ * return `
132
+ * function ${operation.operationId}() {
133
+ * ${pathParams.getParams}
134
+ * ${pathParams.assertParams}
135
+ *
136
+ * return (
137
+ * <DetailView ${pathParams.passProps} />
138
+ * );
139
+ * }`;
140
+ * }
141
+ * }
142
+ * ```
143
+ *
144
+ * @example Custom parameter validation
145
+ * ```typescript
146
+ * class CustomReactRouterParams extends ReactRouterPathParams {
147
+ * generateCustomAssertions(paramName: string): string {
148
+ * return `
149
+ * invariant(${paramName}, 'Expected ${paramName} to be defined');
150
+ * invariant(typeof ${paramName} === 'string', '${paramName} must be a string');
151
+ * invariant(${paramName}.length > 0, '${paramName} cannot be empty');`;
152
+ * }
153
+ * }
154
+ * ```
155
+ */
5
156
  class ReactRouterPathParams extends ContentBase_js_1.ContentBase {
157
+ /**
158
+ * Creates a new ReactRouterPathParams instance with generated parameter handling code.
159
+ *
160
+ * The constructor analyzes the OpenAPI operation to extract path parameters and
161
+ * generates the necessary React code for parameter extraction, validation, and
162
+ * prop passing. It automatically registers required imports when parameters are present.
163
+ *
164
+ * @param args - Configuration for React Router parameter generation
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * const pathParams = new ReactRouterPathParams({
169
+ * context: generateContext,
170
+ * generatorKey: 'react-params',
171
+ * operation: userDetailOperation, // has path '/users/{id}'
172
+ * destinationPath: './UserDetail.tsx'
173
+ * });
174
+ * ```
175
+ */
6
176
  constructor({ context, operation, generatorKey, destinationPath }) {
7
177
  super({ context, generatorKey });
178
+ /** Generated code for extracting parameters from useParams() */
8
179
  Object.defineProperty(this, "getParams", {
9
180
  enumerable: true,
10
181
  configurable: true,
11
182
  writable: true,
12
183
  value: ''
13
184
  });
185
+ /** Generated code for runtime parameter assertions */
14
186
  Object.defineProperty(this, "assertParams", {
15
187
  enumerable: true,
16
188
  configurable: true,
17
189
  writable: true,
18
190
  value: ''
19
191
  });
192
+ /** Generated code for passing parameters as component props */
20
193
  Object.defineProperty(this, "passProps", {
21
194
  enumerable: true,
22
195
  configurable: true,
23
196
  writable: true,
24
197
  value: ''
25
198
  });
199
+ /** Array of parameter names extracted from the operation path */
26
200
  Object.defineProperty(this, "names", {
27
201
  enumerable: true,
28
202
  configurable: true,
@@ -46,6 +220,15 @@ class ReactRouterPathParams extends ContentBase_js_1.ContentBase {
46
220
  });
47
221
  }
48
222
  }
223
+ /**
224
+ * Returns an empty string as this class provides code through its properties.
225
+ *
226
+ * The ReactRouterPathParams class doesn't generate output through toString(),
227
+ * instead providing generated code through the `getParams`, `assertParams`,
228
+ * and `passProps` properties which can be used directly in template generation.
229
+ *
230
+ * @returns Empty string
231
+ */
49
232
  toString() {
50
233
  return ``;
51
234
  }