---
name: 'api-service-generator'
description: 'Generates production-ready API Services, DTOs, and Response Types from OpenAPI specifications. Invoke when user provides an OpenAPI spec or asks to create API services.'
---

# API Service Generator

This skill automates the creation of API services, request DTOs, and response types based on an OpenAPI (Swagger) specification, following the project's architectural standards.

## When to Use

- Invoke when the user provides an OpenAPI specification (YAML or JSON).
- Invoke when the user asks to "create API services" or "implement endpoints" from a spec.
- Invoke when updating existing services based on a new API version.

## Standards & Patterns

### 1. File Structure

- **Services**: `src/services/<serviceName>.service.ts`
- **Request DTOs**: `src/dto/<serviceName>.dto.ts`
- **Response Types**: `src/types/<serviceName>.type.ts`

### 2. Service Object

- Use **PascalCase** for the service object name (e.g., `AuthService`).
- Use **camelCase** for methods (e.g., `postLogin`).
- Methods should follow the naming: `[httpMethod][ActionName]` (e.g., `getDetail`, `postCreate`).
- Return type: `Promise<AxiosResponse<ResponseType>>`.
- Single line spacing between methods.

### 3. Instance Creation

```typescript
import { AxiosResponse } from 'axios';
import createAxiosInstance from './createInstance';

const API = createAxiosInstance({
  env: 'VITE_API_BASE_URL',
  prefix: '/v2/iam',
});
```

### 4. DTOs & Types

- **DTOs**: Interfaces for request bodies and query params.
- **Types**: Interfaces for response bodies.
- Use clear, descriptive noun-based names (e.g., `LoginBody`, `LoginResponse`).
- Use `UserProfileResponse` instead of `GetUserProfileResponse`.

## Workflow

1. **Parse the Spec**: Identify paths, methods, request schemas, and response schemas.
2. **Group by Module**: Group endpoints by their primary path segment (e.g., `/auth/*` -> `AuthService`).
3. **Generate Interfaces**:
   - Create `src/dto/<module>.dto.ts` for all request-related interfaces.
   - Create `src/types/<module>.type.ts` for all response-related interfaces.
4. **Generate Service**:
   - Create `src/services/<module>.service.ts`.
   - Implement methods for each endpoint in the group.
5. **Register Service**:
   - Export the service from `src/main.ts` for global availability.

## Example Transformation

**Input (OpenAPI):**

```yaml
/auth/login:
  post:
    summary: Login
    requestBody:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/LoginRequest'
    responses:
      '200':
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LoginResponse'
```

**Output (Service):**

```typescript
const AuthService = {
  postLogin: (body: LoginRequest): Promise<AxiosResponse<LoginResponse>> => {
    return API.post('/auth/login', body);
  },
};
```
