AI Skill Report Card
Generating TypeScript POST Controllers
Quick Start15 / 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
Workflow13 / 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
bodyparameter - 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
Examples18 / 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
Best Practices
- 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
Common Pitfalls
- 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
anyfor body parameters - always define specific types - Don't forget to mock
genRequestInstancein tests - Don't skip JSDoc documentation - it's essential for API documentation