@skmtc/core 0.0.792 → 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/dsl/GeneratedValueList.d.ts +158 -0
- package/esm/dsl/GeneratedValueList.d.ts.map +1 -1
- package/esm/dsl/GeneratedValueList.js +158 -0
- package/esm/dsl/Stringable.d.ts +76 -0
- package/esm/dsl/Stringable.d.ts.map +1 -1
- package/esm/dsl/Stringable.js +75 -0
- package/esm/helpers/ResultsLog.d.ts +193 -0
- package/esm/helpers/ResultsLog.d.ts.map +1 -1
- package/esm/helpers/ResultsLog.js +213 -0
- package/esm/helpers/collateExamples.d.ts +150 -0
- package/esm/helpers/collateExamples.d.ts.map +1 -1
- package/esm/helpers/collateExamples.js +145 -0
- package/esm/helpers/formatNumber.d.ts +95 -0
- package/esm/helpers/formatNumber.d.ts.map +1 -1
- package/esm/helpers/formatNumber.js +95 -0
- package/esm/helpers/generationStats.d.ts +287 -1
- package/esm/helpers/generationStats.d.ts.map +1 -1
- package/esm/helpers/generationStats.js +266 -0
- package/esm/helpers/isEmpty.d.ts +62 -0
- package/esm/helpers/isEmpty.d.ts.map +1 -1
- package/esm/helpers/isEmpty.js +62 -0
- package/esm/helpers/isImported.d.ts +80 -0
- package/esm/helpers/isImported.d.ts.map +1 -1
- package/esm/helpers/isImported.js +80 -0
- package/esm/helpers/parseModuleName.d.ts +175 -0
- package/esm/helpers/parseModuleName.d.ts.map +1 -1
- package/esm/helpers/parseModuleName.js +153 -0
- package/esm/helpers/toResolvedArtifactPath.d.ts +145 -0
- package/esm/helpers/toResolvedArtifactPath.d.ts.map +1 -1
- package/esm/helpers/toResolvedArtifactPath.js +140 -0
- package/esm/helpers/tracer.d.ts +124 -0
- package/esm/helpers/tracer.d.ts.map +1 -1
- package/esm/helpers/tracer.js +124 -0
- package/esm/oas/schema/Schema.d.ts +117 -0
- package/esm/oas/schema/Schema.d.ts.map +1 -1
- 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/identifiers.d.ts +159 -0
- package/esm/typescript/identifiers.d.ts.map +1 -1
- package/esm/typescript/identifiers.js +159 -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/esm/typescript/withDescription.d.ts +119 -0
- package/esm/typescript/withDescription.d.ts.map +1 -1
- package/esm/typescript/withDescription.js +119 -0
- package/package.json +1 -1
- package/script/dsl/GeneratedValueList.d.ts +158 -0
- package/script/dsl/GeneratedValueList.d.ts.map +1 -1
- package/script/dsl/GeneratedValueList.js +158 -0
- package/script/dsl/Stringable.d.ts +76 -0
- package/script/dsl/Stringable.d.ts.map +1 -1
- package/script/dsl/Stringable.js +75 -0
- package/script/helpers/ResultsLog.d.ts +193 -0
- package/script/helpers/ResultsLog.d.ts.map +1 -1
- package/script/helpers/ResultsLog.js +213 -0
- package/script/helpers/collateExamples.d.ts +150 -0
- package/script/helpers/collateExamples.d.ts.map +1 -1
- package/script/helpers/collateExamples.js +145 -0
- package/script/helpers/formatNumber.d.ts +95 -0
- package/script/helpers/formatNumber.d.ts.map +1 -1
- package/script/helpers/formatNumber.js +95 -0
- package/script/helpers/generationStats.d.ts +287 -1
- package/script/helpers/generationStats.d.ts.map +1 -1
- package/script/helpers/generationStats.js +266 -0
- package/script/helpers/isEmpty.d.ts +62 -0
- package/script/helpers/isEmpty.d.ts.map +1 -1
- package/script/helpers/isEmpty.js +62 -0
- package/script/helpers/isImported.d.ts +80 -0
- package/script/helpers/isImported.d.ts.map +1 -1
- package/script/helpers/isImported.js +80 -0
- package/script/helpers/parseModuleName.d.ts +175 -0
- package/script/helpers/parseModuleName.d.ts.map +1 -1
- package/script/helpers/parseModuleName.js +153 -0
- package/script/helpers/toResolvedArtifactPath.d.ts +145 -0
- package/script/helpers/toResolvedArtifactPath.d.ts.map +1 -1
- package/script/helpers/toResolvedArtifactPath.js +140 -0
- package/script/helpers/tracer.d.ts +124 -0
- package/script/helpers/tracer.d.ts.map +1 -1
- package/script/helpers/tracer.js +124 -0
- package/script/oas/schema/Schema.d.ts +117 -0
- package/script/oas/schema/Schema.d.ts.map +1 -1
- 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/identifiers.d.ts +159 -0
- package/script/typescript/identifiers.d.ts.map +1 -1
- package/script/typescript/identifiers.js +159 -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/typescript/withDescription.d.ts +119 -0
- package/script/typescript/withDescription.d.ts.map +1 -1
- package/script/typescript/withDescription.js +119 -0
|
@@ -1,2 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts OpenAPI path parameters to React Router path parameter syntax.
|
|
3
|
+
*
|
|
4
|
+
* This utility function transforms OpenAPI path syntax (using curly braces) into
|
|
5
|
+
* React Router path parameter syntax (using colon prefix). It's essential for
|
|
6
|
+
* generating React Router route definitions and other routing configurations
|
|
7
|
+
* that need to match URL parameters.
|
|
8
|
+
*
|
|
9
|
+
* The function performs a simple but crucial transformation that enables
|
|
10
|
+
* OpenAPI-defined paths to be used directly in React Router route definitions,
|
|
11
|
+
* maintaining the same parameter names while adapting the syntax.
|
|
12
|
+
*
|
|
13
|
+
* @param path - OpenAPI path string with {parameter} syntax
|
|
14
|
+
* @returns React Router path string with :parameter syntax
|
|
15
|
+
*
|
|
16
|
+
* @example Basic path parameter conversion
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { toPathParams } from '@skmtc/core';
|
|
19
|
+
*
|
|
20
|
+
* const routePath = toPathParams('/users/{id}/posts/{postId}');
|
|
21
|
+
* console.log(routePath); // '/users/:id/posts/:postId'
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example React Router route generation
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { Routes, Route } from 'react-router-dom';
|
|
27
|
+
*
|
|
28
|
+
* const apiPaths = [
|
|
29
|
+
* '/users/{id}',
|
|
30
|
+
* '/users/{id}/posts/{postId}',
|
|
31
|
+
* '/organizations/{orgId}/members/{userId}'
|
|
32
|
+
* ];
|
|
33
|
+
*
|
|
34
|
+
* const routePaths = apiPaths.map(toPathParams);
|
|
35
|
+
* // [
|
|
36
|
+
* // '/users/:id',
|
|
37
|
+
* // '/users/:id/posts/:postId',
|
|
38
|
+
* // '/organizations/:orgId/members/:userId'
|
|
39
|
+
* // ]
|
|
40
|
+
*
|
|
41
|
+
* function App() {
|
|
42
|
+
* return (
|
|
43
|
+
* <Routes>
|
|
44
|
+
* {routePaths.map(path => (
|
|
45
|
+
* <Route key={path} path={path} element={<DetailView />} />
|
|
46
|
+
* ))}
|
|
47
|
+
* </Routes>
|
|
48
|
+
* );
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Express.js route generation
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import express from 'express';
|
|
55
|
+
*
|
|
56
|
+
* class RouteGenerator {
|
|
57
|
+
* generateExpressRoutes(openApiPaths: string[]) {
|
|
58
|
+
* const router = express.Router();
|
|
59
|
+
*
|
|
60
|
+
* openApiPaths.forEach(path => {
|
|
61
|
+
* const expressPath = toPathParams(path);
|
|
62
|
+
*
|
|
63
|
+
* router.get(expressPath, (req, res) => {
|
|
64
|
+
* // Handler logic using req.params
|
|
65
|
+
* res.json({ path: expressPath, params: req.params });
|
|
66
|
+
* });
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* return router;
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
*
|
|
73
|
+
* const generator = new RouteGenerator();
|
|
74
|
+
* const router = generator.generateExpressRoutes([
|
|
75
|
+
* '/api/users/{userId}',
|
|
76
|
+
* '/api/users/{userId}/orders/{orderId}'
|
|
77
|
+
* ]);
|
|
78
|
+
* // Generates routes for:
|
|
79
|
+
* // '/api/users/:userId'
|
|
80
|
+
* // '/api/users/:userId/orders/:orderId'
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Route configuration generation
|
|
84
|
+
* ```typescript
|
|
85
|
+
* interface RouteConfig {
|
|
86
|
+
* path: string;
|
|
87
|
+
* component: string;
|
|
88
|
+
* exact?: boolean;
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* function generateRouteConfigs(apiEndpoints: Array<{path: string, component: string}>): RouteConfig[] {
|
|
92
|
+
* return apiEndpoints.map(endpoint => ({
|
|
93
|
+
* path: toPathParams(endpoint.path),
|
|
94
|
+
* component: endpoint.component,
|
|
95
|
+
* exact: true
|
|
96
|
+
* }));
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* const routeConfigs = generateRouteConfigs([
|
|
100
|
+
* { path: '/users/{id}', component: 'UserDetail' },
|
|
101
|
+
* { path: '/users/{id}/settings', component: 'UserSettings' },
|
|
102
|
+
* { path: '/projects/{projectId}/tasks/{taskId}', component: 'TaskDetail' }
|
|
103
|
+
* ]);
|
|
104
|
+
*
|
|
105
|
+
* console.log(routeConfigs);
|
|
106
|
+
* // [
|
|
107
|
+
* // { path: '/users/:id', component: 'UserDetail', exact: true },
|
|
108
|
+
* // { path: '/users/:id/settings', component: 'UserSettings', exact: true },
|
|
109
|
+
* // { path: '/projects/:projectId/tasks/:taskId', component: 'TaskDetail', exact: true }
|
|
110
|
+
* // ]
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @example Next.js dynamic route generation
|
|
114
|
+
* ```typescript
|
|
115
|
+
* class NextJsRouteGenerator {
|
|
116
|
+
* generateFileStructure(openApiPaths: string[]) {
|
|
117
|
+
* return openApiPaths.map(path => {
|
|
118
|
+
* // Convert to Next.js file-based routing syntax
|
|
119
|
+
* const routerPath = toPathParams(path);
|
|
120
|
+
* const fileName = routerPath
|
|
121
|
+
* .replace(/:/g, '[') // :id becomes [id
|
|
122
|
+
* .replace(/\/\[([^\]]+)/g, '/[$1]') // [id becomes [id]
|
|
123
|
+
* .replace(/^\//,'') // Remove leading slash
|
|
124
|
+
* + '.tsx';
|
|
125
|
+
*
|
|
126
|
+
* return {
|
|
127
|
+
* originalPath: path,
|
|
128
|
+
* routerPath,
|
|
129
|
+
* fileName
|
|
130
|
+
* };
|
|
131
|
+
* });
|
|
132
|
+
* }
|
|
133
|
+
* }
|
|
134
|
+
*
|
|
135
|
+
* const generator = new NextJsRouteGenerator();
|
|
136
|
+
* const structure = generator.generateFileStructure([
|
|
137
|
+
* '/users/{id}',
|
|
138
|
+
* '/posts/{slug}/comments/{commentId}'
|
|
139
|
+
* ]);
|
|
140
|
+
*
|
|
141
|
+
* console.log(structure);
|
|
142
|
+
* // [
|
|
143
|
+
* // {
|
|
144
|
+
* // originalPath: '/users/{id}',
|
|
145
|
+
* // routerPath: '/users/:id',
|
|
146
|
+
* // fileName: 'users/[id].tsx'
|
|
147
|
+
* // },
|
|
148
|
+
* // {
|
|
149
|
+
* // originalPath: '/posts/{slug}/comments/{commentId}',
|
|
150
|
+
* // routerPath: '/posts/:slug/comments/:commentId',
|
|
151
|
+
* // fileName: 'posts/[slug]/comments/[commentId].tsx'
|
|
152
|
+
* // }
|
|
153
|
+
* // ]
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example Integration with path matching
|
|
157
|
+
* ```typescript
|
|
158
|
+
* import { matchPath } from 'react-router-dom';
|
|
159
|
+
*
|
|
160
|
+
* class PathMatcher {
|
|
161
|
+
* createMatcher(openApiPath: string) {
|
|
162
|
+
* const routerPath = toPathParams(openApiPath);
|
|
163
|
+
*
|
|
164
|
+
* return (pathname: string) => {
|
|
165
|
+
* return matchPath(
|
|
166
|
+
* { path: routerPath, exact: true },
|
|
167
|
+
* pathname
|
|
168
|
+
* );
|
|
169
|
+
* };
|
|
170
|
+
* }
|
|
171
|
+
* }
|
|
172
|
+
*
|
|
173
|
+
* const matcher = new PathMatcher();
|
|
174
|
+
* const userMatcher = matcher.createMatcher('/users/{id}');
|
|
175
|
+
*
|
|
176
|
+
* const match = userMatcher('/users/123');
|
|
177
|
+
* console.log(match?.params); // { id: '123' }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
1
180
|
export declare const toPathParams: (path: string) => string;
|
|
2
181
|
//# sourceMappingURL=toPathParams.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toPathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/toPathParams.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,MAE3C,CAAA"}
|
|
1
|
+
{"version":3,"file":"toPathParams.d.ts","sourceRoot":"","sources":["../../src/typescript/toPathParams.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkLG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,MAE3C,CAAA"}
|
|
@@ -1,3 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts OpenAPI path parameters to React Router path parameter syntax.
|
|
3
|
+
*
|
|
4
|
+
* This utility function transforms OpenAPI path syntax (using curly braces) into
|
|
5
|
+
* React Router path parameter syntax (using colon prefix). It's essential for
|
|
6
|
+
* generating React Router route definitions and other routing configurations
|
|
7
|
+
* that need to match URL parameters.
|
|
8
|
+
*
|
|
9
|
+
* The function performs a simple but crucial transformation that enables
|
|
10
|
+
* OpenAPI-defined paths to be used directly in React Router route definitions,
|
|
11
|
+
* maintaining the same parameter names while adapting the syntax.
|
|
12
|
+
*
|
|
13
|
+
* @param path - OpenAPI path string with {parameter} syntax
|
|
14
|
+
* @returns React Router path string with :parameter syntax
|
|
15
|
+
*
|
|
16
|
+
* @example Basic path parameter conversion
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { toPathParams } from '@skmtc/core';
|
|
19
|
+
*
|
|
20
|
+
* const routePath = toPathParams('/users/{id}/posts/{postId}');
|
|
21
|
+
* console.log(routePath); // '/users/:id/posts/:postId'
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example React Router route generation
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { Routes, Route } from 'react-router-dom';
|
|
27
|
+
*
|
|
28
|
+
* const apiPaths = [
|
|
29
|
+
* '/users/{id}',
|
|
30
|
+
* '/users/{id}/posts/{postId}',
|
|
31
|
+
* '/organizations/{orgId}/members/{userId}'
|
|
32
|
+
* ];
|
|
33
|
+
*
|
|
34
|
+
* const routePaths = apiPaths.map(toPathParams);
|
|
35
|
+
* // [
|
|
36
|
+
* // '/users/:id',
|
|
37
|
+
* // '/users/:id/posts/:postId',
|
|
38
|
+
* // '/organizations/:orgId/members/:userId'
|
|
39
|
+
* // ]
|
|
40
|
+
*
|
|
41
|
+
* function App() {
|
|
42
|
+
* return (
|
|
43
|
+
* <Routes>
|
|
44
|
+
* {routePaths.map(path => (
|
|
45
|
+
* <Route key={path} path={path} element={<DetailView />} />
|
|
46
|
+
* ))}
|
|
47
|
+
* </Routes>
|
|
48
|
+
* );
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Express.js route generation
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import express from 'express';
|
|
55
|
+
*
|
|
56
|
+
* class RouteGenerator {
|
|
57
|
+
* generateExpressRoutes(openApiPaths: string[]) {
|
|
58
|
+
* const router = express.Router();
|
|
59
|
+
*
|
|
60
|
+
* openApiPaths.forEach(path => {
|
|
61
|
+
* const expressPath = toPathParams(path);
|
|
62
|
+
*
|
|
63
|
+
* router.get(expressPath, (req, res) => {
|
|
64
|
+
* // Handler logic using req.params
|
|
65
|
+
* res.json({ path: expressPath, params: req.params });
|
|
66
|
+
* });
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* return router;
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
*
|
|
73
|
+
* const generator = new RouteGenerator();
|
|
74
|
+
* const router = generator.generateExpressRoutes([
|
|
75
|
+
* '/api/users/{userId}',
|
|
76
|
+
* '/api/users/{userId}/orders/{orderId}'
|
|
77
|
+
* ]);
|
|
78
|
+
* // Generates routes for:
|
|
79
|
+
* // '/api/users/:userId'
|
|
80
|
+
* // '/api/users/:userId/orders/:orderId'
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Route configuration generation
|
|
84
|
+
* ```typescript
|
|
85
|
+
* interface RouteConfig {
|
|
86
|
+
* path: string;
|
|
87
|
+
* component: string;
|
|
88
|
+
* exact?: boolean;
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* function generateRouteConfigs(apiEndpoints: Array<{path: string, component: string}>): RouteConfig[] {
|
|
92
|
+
* return apiEndpoints.map(endpoint => ({
|
|
93
|
+
* path: toPathParams(endpoint.path),
|
|
94
|
+
* component: endpoint.component,
|
|
95
|
+
* exact: true
|
|
96
|
+
* }));
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* const routeConfigs = generateRouteConfigs([
|
|
100
|
+
* { path: '/users/{id}', component: 'UserDetail' },
|
|
101
|
+
* { path: '/users/{id}/settings', component: 'UserSettings' },
|
|
102
|
+
* { path: '/projects/{projectId}/tasks/{taskId}', component: 'TaskDetail' }
|
|
103
|
+
* ]);
|
|
104
|
+
*
|
|
105
|
+
* console.log(routeConfigs);
|
|
106
|
+
* // [
|
|
107
|
+
* // { path: '/users/:id', component: 'UserDetail', exact: true },
|
|
108
|
+
* // { path: '/users/:id/settings', component: 'UserSettings', exact: true },
|
|
109
|
+
* // { path: '/projects/:projectId/tasks/:taskId', component: 'TaskDetail', exact: true }
|
|
110
|
+
* // ]
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @example Next.js dynamic route generation
|
|
114
|
+
* ```typescript
|
|
115
|
+
* class NextJsRouteGenerator {
|
|
116
|
+
* generateFileStructure(openApiPaths: string[]) {
|
|
117
|
+
* return openApiPaths.map(path => {
|
|
118
|
+
* // Convert to Next.js file-based routing syntax
|
|
119
|
+
* const routerPath = toPathParams(path);
|
|
120
|
+
* const fileName = routerPath
|
|
121
|
+
* .replace(/:/g, '[') // :id becomes [id
|
|
122
|
+
* .replace(/\/\[([^\]]+)/g, '/[$1]') // [id becomes [id]
|
|
123
|
+
* .replace(/^\//,'') // Remove leading slash
|
|
124
|
+
* + '.tsx';
|
|
125
|
+
*
|
|
126
|
+
* return {
|
|
127
|
+
* originalPath: path,
|
|
128
|
+
* routerPath,
|
|
129
|
+
* fileName
|
|
130
|
+
* };
|
|
131
|
+
* });
|
|
132
|
+
* }
|
|
133
|
+
* }
|
|
134
|
+
*
|
|
135
|
+
* const generator = new NextJsRouteGenerator();
|
|
136
|
+
* const structure = generator.generateFileStructure([
|
|
137
|
+
* '/users/{id}',
|
|
138
|
+
* '/posts/{slug}/comments/{commentId}'
|
|
139
|
+
* ]);
|
|
140
|
+
*
|
|
141
|
+
* console.log(structure);
|
|
142
|
+
* // [
|
|
143
|
+
* // {
|
|
144
|
+
* // originalPath: '/users/{id}',
|
|
145
|
+
* // routerPath: '/users/:id',
|
|
146
|
+
* // fileName: 'users/[id].tsx'
|
|
147
|
+
* // },
|
|
148
|
+
* // {
|
|
149
|
+
* // originalPath: '/posts/{slug}/comments/{commentId}',
|
|
150
|
+
* // routerPath: '/posts/:slug/comments/:commentId',
|
|
151
|
+
* // fileName: 'posts/[slug]/comments/[commentId].tsx'
|
|
152
|
+
* // }
|
|
153
|
+
* // ]
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example Integration with path matching
|
|
157
|
+
* ```typescript
|
|
158
|
+
* import { matchPath } from 'react-router-dom';
|
|
159
|
+
*
|
|
160
|
+
* class PathMatcher {
|
|
161
|
+
* createMatcher(openApiPath: string) {
|
|
162
|
+
* const routerPath = toPathParams(openApiPath);
|
|
163
|
+
*
|
|
164
|
+
* return (pathname: string) => {
|
|
165
|
+
* return matchPath(
|
|
166
|
+
* { path: routerPath, exact: true },
|
|
167
|
+
* pathname
|
|
168
|
+
* );
|
|
169
|
+
* };
|
|
170
|
+
* }
|
|
171
|
+
* }
|
|
172
|
+
*
|
|
173
|
+
* const matcher = new PathMatcher();
|
|
174
|
+
* const userMatcher = matcher.createMatcher('/users/{id}');
|
|
175
|
+
*
|
|
176
|
+
* const match = userMatcher('/users/123');
|
|
177
|
+
* console.log(match?.params); // { id: '123' }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
1
180
|
export const toPathParams = (path) => {
|
|
2
181
|
return `${path.replaceAll(/{([^}]*)}/g, ':$1')}`;
|
|
3
182
|
};
|
|
@@ -1,2 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts OpenAPI path parameters into JavaScript template literal syntax.
|
|
3
|
+
*
|
|
4
|
+
* This utility function transforms OpenAPI path syntax (using curly braces) into
|
|
5
|
+
* JavaScript template literal syntax (using ${} expressions). It's essential for
|
|
6
|
+
* generating URL builders, API client methods, and other dynamic path construction
|
|
7
|
+
* code where OpenAPI path parameters need to be interpolated at runtime.
|
|
8
|
+
*
|
|
9
|
+
* The function supports both simple parameter substitution and qualified parameter
|
|
10
|
+
* access (when parameters come from an object property).
|
|
11
|
+
*
|
|
12
|
+
* @param path - OpenAPI path string with {parameter} syntax
|
|
13
|
+
* @param queryArg - Optional object name to qualify parameter access
|
|
14
|
+
* @returns Template literal string with ${} parameter syntax
|
|
15
|
+
*
|
|
16
|
+
* @example Basic path parameter conversion
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { toPathTemplate } from '@skmtc/core';
|
|
19
|
+
*
|
|
20
|
+
* const template = toPathTemplate('/users/{id}/posts/{postId}');
|
|
21
|
+
* console.log(template); // '/users/${id}/posts/${postId}'
|
|
22
|
+
*
|
|
23
|
+
* // Usage in generated code:
|
|
24
|
+
* // const url = `/users/${id}/posts/${postId}`;
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example With query argument qualification
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const template = toPathTemplate('/users/{id}/posts/{postId}', 'params');
|
|
30
|
+
* console.log(template); // '/users/${params.id}/posts/${params.postId}'
|
|
31
|
+
*
|
|
32
|
+
* // Usage in generated code:
|
|
33
|
+
* // function buildUrl(params: {id: string, postId: string}) {
|
|
34
|
+
* // return `/users/${params.id}/posts/${params.postId}`;
|
|
35
|
+
* // }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example API client method generation
|
|
39
|
+
* ```typescript
|
|
40
|
+
* class ApiClientGenerator {
|
|
41
|
+
* generateMethod(path: string, method: string) {
|
|
42
|
+
* const template = toPathTemplate(path, 'args');
|
|
43
|
+
*
|
|
44
|
+
* return `
|
|
45
|
+
* async ${method}(args: PathParams) {
|
|
46
|
+
* const url = \`${template}\`;
|
|
47
|
+
* return this.request('${method.toUpperCase()}', url);
|
|
48
|
+
* }`;
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* const generator = new ApiClientGenerator();
|
|
53
|
+
* const method = generator.generateMethod('/users/{userId}/profile', 'getProfile');
|
|
54
|
+
*
|
|
55
|
+
* console.log(method);
|
|
56
|
+
* // async getProfile(args: PathParams) {
|
|
57
|
+
* // const url = `/users/${args.userId}/profile`;
|
|
58
|
+
* // return this.request('GET', url);
|
|
59
|
+
* // }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example URL builder generation
|
|
63
|
+
* ```typescript
|
|
64
|
+
* function generateUrlBuilder(endpoints: Array<{name: string, path: string}>) {
|
|
65
|
+
* const builders = endpoints.map(endpoint => {
|
|
66
|
+
* const template = toPathTemplate(endpoint.path);
|
|
67
|
+
*
|
|
68
|
+
* return `${endpoint.name}: (${extractParams(endpoint.path)}) => \`${template}\``;
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* return `export const urls = {\n ${builders.join(',\n ')}\n};`;
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* const urlBuilder = generateUrlBuilder([
|
|
75
|
+
* { name: 'user', path: '/users/{id}' },
|
|
76
|
+
* { name: 'userPosts', path: '/users/{id}/posts/{postId}' }
|
|
77
|
+
* ]);
|
|
78
|
+
*
|
|
79
|
+
* // Generates:
|
|
80
|
+
* // export const urls = {
|
|
81
|
+
* // user: (id) => `/users/${id}`,
|
|
82
|
+
* // userPosts: (id, postId) => `/users/${id}/posts/${postId}`
|
|
83
|
+
* // };
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @example React Router integration
|
|
87
|
+
* ```typescript
|
|
88
|
+
* function generateRouteComponent(path: string, componentName: string) {
|
|
89
|
+
* const template = toPathTemplate(path, 'params');
|
|
90
|
+
*
|
|
91
|
+
* return `
|
|
92
|
+
* function ${componentName}() {
|
|
93
|
+
* const params = useParams();
|
|
94
|
+
* const apiUrl = \`/api${template}\`;
|
|
95
|
+
*
|
|
96
|
+
* // Use apiUrl for data fetching
|
|
97
|
+
* return <div>Content for {apiUrl}</div>;
|
|
98
|
+
* }`;
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example Complex nested parameters
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const complexPath = '/organizations/{orgId}/projects/{projectId}/issues/{issueId}/comments/{commentId}';
|
|
105
|
+
*
|
|
106
|
+
* const simple = toPathTemplate(complexPath);
|
|
107
|
+
* console.log(simple);
|
|
108
|
+
* // '/organizations/${orgId}/projects/${projectId}/issues/${issueId}/comments/${commentId}'
|
|
109
|
+
*
|
|
110
|
+
* const qualified = toPathTemplate(complexPath, 'pathParams');
|
|
111
|
+
* console.log(qualified);
|
|
112
|
+
* // '/organizations/${pathParams.orgId}/projects/${pathParams.projectId}/issues/${pathParams.issueId}/comments/${pathParams.commentId}'
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
1
115
|
export declare const toPathTemplate: (path: string, queryArg?: string) => string;
|
|
2
116
|
//# sourceMappingURL=toPathTemplate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toPathTemplate.d.ts","sourceRoot":"","sources":["../../src/typescript/toPathTemplate.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,WAAW,MAAM,KAAG,MAKhE,CAAA"}
|
|
1
|
+
{"version":3,"file":"toPathTemplate.d.ts","sourceRoot":"","sources":["../../src/typescript/toPathTemplate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiHG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,WAAW,MAAM,KAAG,MAKhE,CAAA"}
|
|
@@ -1,3 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts OpenAPI path parameters into JavaScript template literal syntax.
|
|
3
|
+
*
|
|
4
|
+
* This utility function transforms OpenAPI path syntax (using curly braces) into
|
|
5
|
+
* JavaScript template literal syntax (using ${} expressions). It's essential for
|
|
6
|
+
* generating URL builders, API client methods, and other dynamic path construction
|
|
7
|
+
* code where OpenAPI path parameters need to be interpolated at runtime.
|
|
8
|
+
*
|
|
9
|
+
* The function supports both simple parameter substitution and qualified parameter
|
|
10
|
+
* access (when parameters come from an object property).
|
|
11
|
+
*
|
|
12
|
+
* @param path - OpenAPI path string with {parameter} syntax
|
|
13
|
+
* @param queryArg - Optional object name to qualify parameter access
|
|
14
|
+
* @returns Template literal string with ${} parameter syntax
|
|
15
|
+
*
|
|
16
|
+
* @example Basic path parameter conversion
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { toPathTemplate } from '@skmtc/core';
|
|
19
|
+
*
|
|
20
|
+
* const template = toPathTemplate('/users/{id}/posts/{postId}');
|
|
21
|
+
* console.log(template); // '/users/${id}/posts/${postId}'
|
|
22
|
+
*
|
|
23
|
+
* // Usage in generated code:
|
|
24
|
+
* // const url = `/users/${id}/posts/${postId}`;
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example With query argument qualification
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const template = toPathTemplate('/users/{id}/posts/{postId}', 'params');
|
|
30
|
+
* console.log(template); // '/users/${params.id}/posts/${params.postId}'
|
|
31
|
+
*
|
|
32
|
+
* // Usage in generated code:
|
|
33
|
+
* // function buildUrl(params: {id: string, postId: string}) {
|
|
34
|
+
* // return `/users/${params.id}/posts/${params.postId}`;
|
|
35
|
+
* // }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example API client method generation
|
|
39
|
+
* ```typescript
|
|
40
|
+
* class ApiClientGenerator {
|
|
41
|
+
* generateMethod(path: string, method: string) {
|
|
42
|
+
* const template = toPathTemplate(path, 'args');
|
|
43
|
+
*
|
|
44
|
+
* return `
|
|
45
|
+
* async ${method}(args: PathParams) {
|
|
46
|
+
* const url = \`${template}\`;
|
|
47
|
+
* return this.request('${method.toUpperCase()}', url);
|
|
48
|
+
* }`;
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* const generator = new ApiClientGenerator();
|
|
53
|
+
* const method = generator.generateMethod('/users/{userId}/profile', 'getProfile');
|
|
54
|
+
*
|
|
55
|
+
* console.log(method);
|
|
56
|
+
* // async getProfile(args: PathParams) {
|
|
57
|
+
* // const url = `/users/${args.userId}/profile`;
|
|
58
|
+
* // return this.request('GET', url);
|
|
59
|
+
* // }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example URL builder generation
|
|
63
|
+
* ```typescript
|
|
64
|
+
* function generateUrlBuilder(endpoints: Array<{name: string, path: string}>) {
|
|
65
|
+
* const builders = endpoints.map(endpoint => {
|
|
66
|
+
* const template = toPathTemplate(endpoint.path);
|
|
67
|
+
*
|
|
68
|
+
* return `${endpoint.name}: (${extractParams(endpoint.path)}) => \`${template}\``;
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* return `export const urls = {\n ${builders.join(',\n ')}\n};`;
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* const urlBuilder = generateUrlBuilder([
|
|
75
|
+
* { name: 'user', path: '/users/{id}' },
|
|
76
|
+
* { name: 'userPosts', path: '/users/{id}/posts/{postId}' }
|
|
77
|
+
* ]);
|
|
78
|
+
*
|
|
79
|
+
* // Generates:
|
|
80
|
+
* // export const urls = {
|
|
81
|
+
* // user: (id) => `/users/${id}`,
|
|
82
|
+
* // userPosts: (id, postId) => `/users/${id}/posts/${postId}`
|
|
83
|
+
* // };
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @example React Router integration
|
|
87
|
+
* ```typescript
|
|
88
|
+
* function generateRouteComponent(path: string, componentName: string) {
|
|
89
|
+
* const template = toPathTemplate(path, 'params');
|
|
90
|
+
*
|
|
91
|
+
* return `
|
|
92
|
+
* function ${componentName}() {
|
|
93
|
+
* const params = useParams();
|
|
94
|
+
* const apiUrl = \`/api${template}\`;
|
|
95
|
+
*
|
|
96
|
+
* // Use apiUrl for data fetching
|
|
97
|
+
* return <div>Content for {apiUrl}</div>;
|
|
98
|
+
* }`;
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example Complex nested parameters
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const complexPath = '/organizations/{orgId}/projects/{projectId}/issues/{issueId}/comments/{commentId}';
|
|
105
|
+
*
|
|
106
|
+
* const simple = toPathTemplate(complexPath);
|
|
107
|
+
* console.log(simple);
|
|
108
|
+
* // '/organizations/${orgId}/projects/${projectId}/issues/${issueId}/comments/${commentId}'
|
|
109
|
+
*
|
|
110
|
+
* const qualified = toPathTemplate(complexPath, 'pathParams');
|
|
111
|
+
* console.log(qualified);
|
|
112
|
+
* // '/organizations/${pathParams.orgId}/projects/${pathParams.projectId}/issues/${pathParams.issueId}/comments/${pathParams.commentId}'
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
1
115
|
export const toPathTemplate = (path, queryArg) => {
|
|
2
116
|
return `${path.replaceAll(/{([^}]*)}/g, queryArg ? '${' + queryArg + '.$1}' : '${$1}')}`;
|
|
3
117
|
};
|