AI Skill Report Card

Generating TypeScript POST Controllers

B+78·May 6, 2026·Source: Web
15 / 15
TypeScript
// Without URL params export const createUserController = async ( body: User.CreateUserParams, ): Promise<AxiosResponse<User.User, any>> => { const url = '/api/users/'; const instance = genRequestInstance(); try { const response = await instance.post(url, body); return response; } catch (error) { console.error('Erro na requisição:', error); return Promise.reject(error); } }; // With URL params export const createUserProjectController = async ( userId: User.User['id'], body: Project.CreateProjectParams, ): Promise<AxiosResponse<Project.Project, any>> => { const url = `/api/users/${userId}/projects/`; const instance = genRequestInstance(); try { const response = await instance.post(url, body); return response; } catch (error) { console.error('Erro na requisição:', error); return Promise.reject(error); } };
Recommendation
Remove the Portuguese error messages ('Erro na requisição:') and use English consistently throughout
13 / 15

Progress:

  • Identify if function needs URL parameters
  • Define function name following pattern: create{Resource}Controller
  • Add JSDoc documentation with @param, @returns, @throws
  • Implement try-catch with console.error and Promise.reject
  • Generate corresponding unit tests in tests.ts

Step 1: Determine Parameter Structure

  • No URL params: Single body parameter
  • With URL params: URL parameters first (as individual params), body last

Step 2: Generate Function Signature

TypeScript
/** * Creates a new {resource} * @param {ParamType} param - Description of parameter * @param {BodyType} body - Request body data * @returns {Promise<AxiosResponse<ReturnType, any>>} Response with created resource * @throws {Error} When request fails */ export const create{Resource}Controller = async ( // parameters here ): Promise<AxiosResponse<ReturnType, any>> => {

Step 3: Implement Controller Body

  • Construct URL with template literals for params
  • Use genRequestInstance()
  • Wrap in try-catch with error logging
  • Return Promise.reject(error) on failure

Step 4: Generate Unit Tests

Create tests.ts file with basic test structure using Jest/Vitest patterns.

Recommendation
Simplify the workflow section by removing the checklist format and making it more direct
18 / 20

Example 1: Simple POST without URL params Input: Create product controller Output:

TypeScript
/** * Creates a new product * @param {Product.CreateProductParams} body - Product creation data * @returns {Promise<AxiosResponse<Product.Product, any>>} Response with created product * @throws {Error} When request fails */ export const createProductController = async ( body: Product.CreateProductParams, ): Promise<AxiosResponse<Product.Product, any>> => { const url = '/api/products/'; const instance = genRequestInstance(); try { const response = await instance.post(url, body); return response; } catch (error) { console.error('Erro na requisição:', error); return Promise.reject(error); } };

Example 2: POST with URL parameters Input: Create order item for specific order Output:

TypeScript
/** * Creates a new order item for a specific order * @param {Order.Order['id']} orderId - Order identifier * @param {OrderItem.CreateOrderItemParams} body - Order item creation data * @returns {Promise<AxiosResponse<OrderItem.OrderItem, any>>} Response with created order item * @throws {Error} When request fails */ export const createOrderItemController = async ( orderId: Order.Order['id'], body: OrderItem.CreateOrderItemParams, ): Promise<AxiosResponse<OrderItem.OrderItem, any>> => { const url = `/api/orders/${orderId}/items/`; const instance = genRequestInstance(); try { const response = await instance.post(url, body); return response; } catch (error) { console.error('Erro na requisição:', error); return Promise.reject(error); } };

Example 3: Corresponding test file

TypeScript
// tests.ts import { createProductController } from './controllers'; import { genRequestInstance } from '../utils'; jest.mock('../utils', () => ({ genRequestInstance: jest.fn(), })); describe('createProductController', () => { const mockPost = jest.fn(); const mockInstance = { post: mockPost }; beforeEach(() => { (genRequestInstance as jest.Mock).mockReturnValue(mockInstance); }); it('should create product successfully', async () => { const mockResponse = { data: { id: 1, name: 'Test Product' } }; mockPost.mockResolvedValue(mockResponse); const body = { name: 'Test Product', price: 100 }; const result = await createProductController(body); expect(mockPost).toHaveBeenCalledWith('/api/products/', body); expect(result).toBe(mockResponse); }); it('should handle errors properly', async () => { const mockError = new Error('Network error'); mockPost.mockRejectedValue(mockError); console.error = jest.fn(); const body = { name: 'Test Product', price: 100 }; await expect(createProductController(body)).rejects.toBe(mockError); expect(console.error).toHaveBeenCalledWith('Erro na requisição:', mockError); }); });
Recommendation
Add a section about handling different HTTP status codes and response validation patterns
  • Always use descriptive function names: create{Resource}Controller
  • Include comprehensive JSDoc with all parameters and return types
  • Use typed parameters following namespace patterns: Module.SubModule.Type
  • Always end URLs with trailing slash for consistency
  • Use template literals for URL construction with parameters
  • Include proper error logging with console.error
  • Don't forget Promise.reject(error) in catch blocks
  • Don't mix parameter order - URL params always come before body
  • Don't use generic types like any for body parameters - always define specific types
  • Don't forget to mock genRequestInstance in tests
  • Don't skip JSDoc documentation - it's essential for API documentation
0
Grade B+AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
15/15
Workflow
13/15
Examples
18/20
Completeness
15/20
Format
15/15
Conciseness
12/15