Writing code

CLASS

PathParams

@skmtc/lang-typescript 0.19.1

Manages path parameter type definitions and function parameters for API endpoints.

PathParams is a utility class that combines type generation, function parameter handling, and path templating for OpenAPI path parameters. It creates TypeScript type definitions for path parameters and generates the corresponding function parameters that can be used in API client methods.

This class is essential for generating type-safe API clients where path parameters are properly typed and validated at compile time. It handles both destructured parameters (when no argument name is provided) and named parameters.

Key Features

  • Type Definition Generation: Creates TypeScript types for path parameters
  • Function Parameter Integration: Generates properly typed function parameters
  • Path Template Processing: Converts OpenAPI paths to template literals
  • Destructuring Support: Handles both named and destructured parameter patterns
  • Context Integration: Fully integrated with the SKMTC generation pipeline

Signature

import { PathParams } from '@skmtc/lang-typescript'

class PathParams

Examples

Basic path parameters with destructuring
import { PathParams } from '@skmtc/lang-typescript';

const pathParams = new PathParams({
  context: generateContext,
  typeName: 'GetUserParams',
  typeValue: {
    type: 'object',
    objectProperties: {
      properties: {
        id: { type: 'string' },
        format: { type: 'string' }
      }
    }
  },
  pathTemplate: '/users/{id}'
});

console.log(pathParams.path); // '/users/${id}'
Named parameter usage
const namedPathParams = new PathParams({
  context: generateContext,
  argName: 'params',
  typeName: 'UserPathParams',
  typeValue: {
    type: 'object',
    objectProperties: {
      properties: {
        userId: { type: 'string' },
        projectId: { type: 'string' }
      }
    }
  },
  pathTemplate: '/users/{userId}/projects/{projectId}'
});

console.log(pathParams.path); // '/users/${params.userId}/projects/${params.projectId}'
API client method generation
class ApiMethodGenerator {
  generateGetMethod(operationPath: string, parameters: any) {
    const pathParams = new PathParams({
      context: this.context,
      argName: 'pathParams',
      typeName: 'PathParameters',
      typeValue: parameters,
      pathTemplate: operationPath
    });

    return `
async getData(${pathParams.parameter.toString()}) {
  const url = \`${pathParams.path}\`;
  return this.client.get(url);
}`;
  }
}

// Generates:
// async getData(pathParams: PathParameters) {
//   const url = `/users/${pathParams.userId}/projects/${pathParams.projectId}`;
//   return this.client.get(url);
// }
With destructured parameters
const destructuredParams = new PathParams({
  context: generateContext,
  // No argName provided, so uses destructuring
  typeName: 'OrderParams',
  typeValue: {
    type: 'object',
    objectProperties: {
      properties: {
        orderId: { type: 'string' },
        customerId: { type: 'string' }
      }
    }
  },
  pathTemplate: '/customers/{customerId}/orders/{orderId}'
});

// Function parameter will be: { orderId, customerId }: OrderParams
// Path template will be: '/customers/${customerId}/orders/${orderId}'
Complex nested path parameters
const complexPathParams = new PathParams({
  context: generateContext,
  argName: 'pathData',
  typeName: 'ComplexPathParams',
  typeValue: {
    type: 'object',
    objectProperties: {
      properties: {
        orgId: { type: 'string' },
        projectId: { type: 'string' },
        issueId: { type: 'number' },
        commentId: { type: 'string' }
      }
    }
  },
  pathTemplate: '/orgs/{orgId}/projects/{projectId}/issues/{issueId}/comments/{commentId}'
});

// Generates path: '/orgs/${pathData.orgId}/projects/${pathData.projectId}/issues/${pathData.issueId}/comments/${pathData.commentId}'
Integration with operation generation
class MyOasOperationGenerator {
  generateOperation(operation: OasOperation) {
    const pathParameters = this.extractPathParameters(operation);

    if (pathParameters.length > 0) {
      const pathParams = new PathParams({
        context: this.context,
        typeName: `${operation.operationId}PathParams`,
        typeValue: this.buildParametersType(pathParameters),
        pathTemplate: operation.path
      });

      return this.generateMethodWithPathParams(operation, pathParams);
    }

    return this.generateSimpleMethod(operation);
  }
}

Constructor

constructor

constructor(args: ConstructorArgs)

Creates a new PathParams instance with type definitions and path processing.

The constructor sets up the complete parameter handling pipeline:

  1. Creates a TypeScript type definition for the parameters
  2. Generates a function parameter (either named or destructured)
  3. Processes the path template for runtime interpolation
args

Configuration for path parameter generation

Properties

The generation context used for type registration and imports

typeDefinition, TsDefinition<TypeSystemObject>

The TypeScript type definition for the path parameters

The function parameter representation for method signatures

path, string

The processed path template with parameter substitutions