Writing code
toPathTemplate
Converts OpenAPI path parameters into JavaScript template literal syntax.
This utility function transforms OpenAPI path syntax (using curly braces) into JavaScript template literal syntax (using ${} expressions). It's essential for generating URL builders, API client methods, and other dynamic path construction code where OpenAPI path parameters need to be interpolated at runtime.
The function supports both simple parameter substitution and qualified parameter access (when parameters come from an object property). A simple parameter is the variable toParameterVariable names, the one FunctionParameter destructures. The static text is escaped for a template literal.
Signature
import { toPathTemplate } from '@skmtc/lang-typescript'
toPathTemplate(path: string, queryArg?: string): stringParameters
OpenAPI path string with {parameter} syntax
Optional object name to qualify parameter access
import { toPathTemplate } from '@skmtc/lang-typescript';
const template = toPathTemplate('/users/{id}/posts/{postId}');
console.log(template); // '/users/${id}/posts/${postId}'
// Usage in generated code:
// const url = `/users/${id}/posts/${postId}`;const template = toPathTemplate('/users/{id}/posts/{postId}', 'params');
console.log(template); // '/users/${params.id}/posts/${params.postId}'
// Usage in generated code:
// function buildUrl(params: {id: string, postId: string}) {
// return `/users/${params.id}/posts/${params.postId}`;
// }toPathTemplate('/teams/{enterprise-team}'); // '/teams/${enterpriseTeam}'
toPathTemplate('/teams/{enterprise-team}', 'params'); // "/teams/${params['enterprise-team']}"class ApiClientGenerator {
generateMethod(path: string, method: string) {
const template = toPathTemplate(path, 'args');
return `
async ${method}(args: PathParams) {
const url = \`${template}\`;
return this.request('${method.toUpperCase()}', url);
}`;
}
}
const generator = new ApiClientGenerator();
const method = generator.generateMethod('/users/{userId}/profile', 'getProfile');
console.log(method);
// async getProfile(args: PathParams) {
// const url = `/users/${args.userId}/profile`;
// return this.request('GET', url);
// }function generateUrlBuilder(endpoints: Array<{name: string, path: string}>) {
const builders = endpoints.map(endpoint => {
const template = toPathTemplate(endpoint.path);
return `${endpoint.name}: (${extractParams(endpoint.path)}) => \`${template}\``;
});
return `export const urls = {\n ${builders.join(',\n ')}\n};`;
}
const urlBuilder = generateUrlBuilder([
{ name: 'user', path: '/users/{id}' },
{ name: 'userPosts', path: '/users/{id}/posts/{postId}' }
]);
// Generates:
// export const urls = {
// user: (id) => `/users/${id}`,
// userPosts: (id, postId) => `/users/${id}/posts/${postId}`
// };function generateRouteComponent(path: string, componentName: string) {
const template = toPathTemplate(path, 'params');
return `
function ${componentName}() {
const params = useParams();
const apiUrl = \`/api${template}\`;
// Use apiUrl for data fetching
return <div>Content for {apiUrl}</div>;
}`;
}const complexPath = '/organizations/{orgId}/projects/{projectId}/issues/{issueId}/comments/{commentId}';
const simple = toPathTemplate(complexPath);
console.log(simple);
// '/organizations/${orgId}/projects/${projectId}/issues/${issueId}/comments/${commentId}'
const qualified = toPathTemplate(complexPath, 'pathParams');
console.log(qualified);
// '/organizations/${pathParams.orgId}/projects/${pathParams.projectId}/issues/${pathParams.issueId}/comments/${pathParams.commentId}'Returns
Template literal string with ${} parameter syntax