@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
|
@@ -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;
|
|
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
|
}
|
|
@@ -1,3 +1,128 @@
|
|
|
1
1
|
import type { Stringable } from '../dsl/Stringable.js';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a properties object into a formatted TypeScript object literal string.
|
|
4
|
+
*
|
|
5
|
+
* This utility function takes a record of key-value pairs and generates a properly
|
|
6
|
+
* formatted TypeScript object literal. It automatically filters out empty values
|
|
7
|
+
* and handles proper spacing and comma placement. If all properties are empty,
|
|
8
|
+
* it returns the EMPTY constant instead of an empty object.
|
|
9
|
+
*
|
|
10
|
+
* The function is particularly useful for generating TypeScript interface properties,
|
|
11
|
+
* object type definitions, and literal object expressions in code generation scenarios.
|
|
12
|
+
*
|
|
13
|
+
* @param properties - Record of property names to their stringable values
|
|
14
|
+
* @returns Formatted object literal string or EMPTY if no valid properties
|
|
15
|
+
*
|
|
16
|
+
* @example Basic object generation
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { keyValues } from '@skmtc/core';
|
|
19
|
+
*
|
|
20
|
+
* const props = {
|
|
21
|
+
* name: { toString: () => 'string' },
|
|
22
|
+
* age: { toString: () => 'number' },
|
|
23
|
+
* email: { toString: () => 'string' }
|
|
24
|
+
* };
|
|
25
|
+
*
|
|
26
|
+
* const result = keyValues(props);
|
|
27
|
+
* console.log(result);
|
|
28
|
+
* // {name: string,
|
|
29
|
+
* // age: number,
|
|
30
|
+
* // email: string}
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example With empty values filtered
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const propsWithEmpty = {
|
|
36
|
+
* name: { toString: () => 'string' },
|
|
37
|
+
* description: { toString: () => '' }, // Empty, will be filtered
|
|
38
|
+
* age: { toString: () => 'number' },
|
|
39
|
+
* notes: { toString: () => 'EMPTY' } // EMPTY constant, will be filtered
|
|
40
|
+
* };
|
|
41
|
+
*
|
|
42
|
+
* const result = keyValues(propsWithEmpty);
|
|
43
|
+
* console.log(result);
|
|
44
|
+
* // {name: string,
|
|
45
|
+
* // age: number}
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example All empty properties
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const emptyProps = {
|
|
51
|
+
* description: { toString: () => '' },
|
|
52
|
+
* notes: { toString: () => 'EMPTY' }
|
|
53
|
+
* };
|
|
54
|
+
*
|
|
55
|
+
* const result = keyValues(emptyProps);
|
|
56
|
+
* console.log(result === EMPTY); // true
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @example TypeScript interface generation
|
|
60
|
+
* ```typescript
|
|
61
|
+
* class InterfaceGenerator {
|
|
62
|
+
* generateInterface(name: string, properties: Record<string, Stringable>) {
|
|
63
|
+
* const propsString = keyValues(properties);
|
|
64
|
+
*
|
|
65
|
+
* if (propsString === EMPTY) {
|
|
66
|
+
* return `interface ${name} {}`;
|
|
67
|
+
* }
|
|
68
|
+
*
|
|
69
|
+
* return `interface ${name} ${propsString}`;
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
*
|
|
73
|
+
* const generator = new InterfaceGenerator();
|
|
74
|
+
* const userInterface = generator.generateInterface('User', {
|
|
75
|
+
* id: { toString: () => 'string' },
|
|
76
|
+
* name: { toString: () => 'string' },
|
|
77
|
+
* age: { toString: () => 'number' }
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* console.log(userInterface);
|
|
81
|
+
* // interface User {id: string,
|
|
82
|
+
* // name: string,
|
|
83
|
+
* // age: number}
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @example Object literal generation
|
|
87
|
+
* ```typescript
|
|
88
|
+
* function generateObjectLiteral(values: Record<string, string>) {
|
|
89
|
+
* const stringableValues = Object.fromEntries(
|
|
90
|
+
* Object.entries(values).map(([k, v]) => [k, { toString: () => `'${v}'` }])
|
|
91
|
+
* );
|
|
92
|
+
*
|
|
93
|
+
* const result = keyValues(stringableValues);
|
|
94
|
+
* return result === EMPTY ? '{}' : result;
|
|
95
|
+
* }
|
|
96
|
+
*
|
|
97
|
+
* const config = generateObjectLiteral({
|
|
98
|
+
* apiUrl: 'https://api.example.com',
|
|
99
|
+
* timeout: '5000'
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* console.log(config);
|
|
103
|
+
* // {apiUrl: 'https://api.example.com',
|
|
104
|
+
* // timeout: '5000'}
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @example Conditional property inclusion
|
|
108
|
+
* ```typescript
|
|
109
|
+
* class ConditionalGenerator {
|
|
110
|
+
* generateObjectType(fields: Array<{name: string, type: string, optional?: boolean}>) {
|
|
111
|
+
* const properties: Record<string, Stringable> = {};
|
|
112
|
+
*
|
|
113
|
+
* fields.forEach(field => {
|
|
114
|
+
* if (field.type) { // Only include fields with types
|
|
115
|
+
* const optionalMarker = field.optional ? '?' : '';
|
|
116
|
+
* properties[field.name] = {
|
|
117
|
+
* toString: () => `${optionalMarker}: ${field.type}`
|
|
118
|
+
* };
|
|
119
|
+
* }
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* return keyValues(properties);
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
2
127
|
export declare const keyValues: (properties: Record<string, Stringable>) => string;
|
|
3
128
|
//# sourceMappingURL=keyValues.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyValues.d.ts","sourceRoot":"","sources":["../../src/typescript/keyValues.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"keyValues.d.ts","sourceRoot":"","sources":["../../src/typescript/keyValues.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4HG;AACH,eAAO,MAAM,SAAS,GAAI,YAAY,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,KAAG,MAUlE,CAAA"}
|
|
@@ -2,7 +2,131 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.keyValues = void 0;
|
|
4
4
|
const constants_js_1 = require("../dsl/constants.js");
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Converts a properties object into a formatted TypeScript object literal string.
|
|
7
|
+
*
|
|
8
|
+
* This utility function takes a record of key-value pairs and generates a properly
|
|
9
|
+
* formatted TypeScript object literal. It automatically filters out empty values
|
|
10
|
+
* and handles proper spacing and comma placement. If all properties are empty,
|
|
11
|
+
* it returns the EMPTY constant instead of an empty object.
|
|
12
|
+
*
|
|
13
|
+
* The function is particularly useful for generating TypeScript interface properties,
|
|
14
|
+
* object type definitions, and literal object expressions in code generation scenarios.
|
|
15
|
+
*
|
|
16
|
+
* @param properties - Record of property names to their stringable values
|
|
17
|
+
* @returns Formatted object literal string or EMPTY if no valid properties
|
|
18
|
+
*
|
|
19
|
+
* @example Basic object generation
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { keyValues } from '@skmtc/core';
|
|
22
|
+
*
|
|
23
|
+
* const props = {
|
|
24
|
+
* name: { toString: () => 'string' },
|
|
25
|
+
* age: { toString: () => 'number' },
|
|
26
|
+
* email: { toString: () => 'string' }
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* const result = keyValues(props);
|
|
30
|
+
* console.log(result);
|
|
31
|
+
* // {name: string,
|
|
32
|
+
* // age: number,
|
|
33
|
+
* // email: string}
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example With empty values filtered
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const propsWithEmpty = {
|
|
39
|
+
* name: { toString: () => 'string' },
|
|
40
|
+
* description: { toString: () => '' }, // Empty, will be filtered
|
|
41
|
+
* age: { toString: () => 'number' },
|
|
42
|
+
* notes: { toString: () => 'EMPTY' } // EMPTY constant, will be filtered
|
|
43
|
+
* };
|
|
44
|
+
*
|
|
45
|
+
* const result = keyValues(propsWithEmpty);
|
|
46
|
+
* console.log(result);
|
|
47
|
+
* // {name: string,
|
|
48
|
+
* // age: number}
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @example All empty properties
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const emptyProps = {
|
|
54
|
+
* description: { toString: () => '' },
|
|
55
|
+
* notes: { toString: () => 'EMPTY' }
|
|
56
|
+
* };
|
|
57
|
+
*
|
|
58
|
+
* const result = keyValues(emptyProps);
|
|
59
|
+
* console.log(result === EMPTY); // true
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example TypeScript interface generation
|
|
63
|
+
* ```typescript
|
|
64
|
+
* class InterfaceGenerator {
|
|
65
|
+
* generateInterface(name: string, properties: Record<string, Stringable>) {
|
|
66
|
+
* const propsString = keyValues(properties);
|
|
67
|
+
*
|
|
68
|
+
* if (propsString === EMPTY) {
|
|
69
|
+
* return `interface ${name} {}`;
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* return `interface ${name} ${propsString}`;
|
|
73
|
+
* }
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* const generator = new InterfaceGenerator();
|
|
77
|
+
* const userInterface = generator.generateInterface('User', {
|
|
78
|
+
* id: { toString: () => 'string' },
|
|
79
|
+
* name: { toString: () => 'string' },
|
|
80
|
+
* age: { toString: () => 'number' }
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* console.log(userInterface);
|
|
84
|
+
* // interface User {id: string,
|
|
85
|
+
* // name: string,
|
|
86
|
+
* // age: number}
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example Object literal generation
|
|
90
|
+
* ```typescript
|
|
91
|
+
* function generateObjectLiteral(values: Record<string, string>) {
|
|
92
|
+
* const stringableValues = Object.fromEntries(
|
|
93
|
+
* Object.entries(values).map(([k, v]) => [k, { toString: () => `'${v}'` }])
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* const result = keyValues(stringableValues);
|
|
97
|
+
* return result === EMPTY ? '{}' : result;
|
|
98
|
+
* }
|
|
99
|
+
*
|
|
100
|
+
* const config = generateObjectLiteral({
|
|
101
|
+
* apiUrl: 'https://api.example.com',
|
|
102
|
+
* timeout: '5000'
|
|
103
|
+
* });
|
|
104
|
+
*
|
|
105
|
+
* console.log(config);
|
|
106
|
+
* // {apiUrl: 'https://api.example.com',
|
|
107
|
+
* // timeout: '5000'}
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example Conditional property inclusion
|
|
111
|
+
* ```typescript
|
|
112
|
+
* class ConditionalGenerator {
|
|
113
|
+
* generateObjectType(fields: Array<{name: string, type: string, optional?: boolean}>) {
|
|
114
|
+
* const properties: Record<string, Stringable> = {};
|
|
115
|
+
*
|
|
116
|
+
* fields.forEach(field => {
|
|
117
|
+
* if (field.type) { // Only include fields with types
|
|
118
|
+
* const optionalMarker = field.optional ? '?' : '';
|
|
119
|
+
* properties[field.name] = {
|
|
120
|
+
* toString: () => `${optionalMarker}: ${field.type}`
|
|
121
|
+
* };
|
|
122
|
+
* }
|
|
123
|
+
* });
|
|
124
|
+
*
|
|
125
|
+
* return keyValues(properties);
|
|
126
|
+
* }
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
6
130
|
const keyValues = (properties) => {
|
|
7
131
|
const filteredEntries = Object.entries(properties)
|
|
8
132
|
.map(([key, value]) => {
|