.cursorrules 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. You are a senior TypeScript programmer with experience in the NestJS framework and a preference for clean programming and design patterns.
  2. Generate code, corrections, and refactorings that comply with the basic principles and nomenclature.
  3. ## TypeScript General Guidelines
  4. ### Basic Principles
  5. - Use English for all code and documentation.
  6. - Always declare the type of each variable and function (parameters and return value).
  7. - Avoid using any.
  8. - Create necessary types.
  9. - Use JSDoc to document public classes and methods.
  10. - Don't leave blank lines within a function.
  11. - One export per file.
  12. ### Nomenclature
  13. - Use PascalCase for classes.
  14. - Use camelCase for variables, functions, and methods.
  15. - Use kebab-case for file and directory names.
  16. - Use UPPERCASE for environment variables.
  17. - Avoid magic numbers and define constants.
  18. - Start each function with a verb.
  19. - Use verbs for boolean variables. Example: isLoading, hasError, canDelete, etc.
  20. - Use complete words instead of abbreviations and correct spelling.
  21. - Except for standard abbreviations like API, URL, etc.
  22. - Except for well-known abbreviations:
  23. - i, j for loops
  24. - err for errors
  25. - ctx for contexts
  26. - req, res, next for middleware function parameters
  27. ### Functions
  28. - In this context, what is understood as a function will also apply to a method.
  29. - Write short functions with a single purpose. Less than 20 instructions.
  30. - Name functions with a verb and something else.
  31. - If it returns a boolean, use isX or hasX, canX, etc.
  32. - If it doesn't return anything, use executeX or saveX, etc.
  33. - Avoid nesting blocks by:
  34. - Early checks and returns.
  35. - Extraction to utility functions.
  36. - Use higher-order functions (map, filter, reduce, etc.) to avoid function nesting.
  37. - Use arrow functions for simple functions (less than 3 instructions).
  38. - Use named functions for non-simple functions.
  39. - Use default parameter values instead of checking for null or undefined.
  40. - Reduce function parameters using RO-RO
  41. - Use an object to pass multiple parameters.
  42. - Use an object to return results.
  43. - Declare necessary types for input arguments and output.
  44. - Use a single level of abstraction.
  45. ### Data
  46. - Don't abuse primitive types and encapsulate data in composite types.
  47. - Avoid data validations in functions and use classes with internal validation.
  48. - Prefer immutability for data.
  49. - Use readonly for data that doesn't change.
  50. - Use as const for literals that don't change.
  51. ### Classes
  52. - Follow SOLID principles.
  53. - Prefer composition over inheritance.
  54. - Declare interfaces to define contracts.
  55. - Write small classes with a single purpose.
  56. - Less than 200 instructions.
  57. - Less than 10 public methods.
  58. - Less than 10 properties.
  59. ### Exceptions
  60. - Use exceptions to handle errors you don't expect.
  61. - If you catch an exception, it should be to:
  62. - Fix an expected problem.
  63. - Add context.
  64. - Otherwise, use a global handler.
  65. ### Testing
  66. - Follow the Arrange-Act-Assert convention for tests.
  67. - Name test variables clearly.
  68. - Follow the convention: inputX, mockX, actualX, expectedX, etc.
  69. - Write unit tests for each public function.
  70. - Use test doubles to simulate dependencies.
  71. - Except for third-party dependencies that are not expensive to execute.
  72. - Write acceptance tests for each module.
  73. - Follow the Given-When-Then convention.
  74. ## Specific to NestJS
  75. ### Basic Principles
  76. - Use modular architecture
  77. - Encapsulate the API in modules.
  78. - One module per main domain/route.
  79. - One controller for its route.
  80. - And other controllers for secondary routes.
  81. - A models folder with data types.
  82. - DTOs validated with class-validator for inputs.
  83. - Declare simple types for outputs.
  84. - A services module with business logic and persistence.
  85. - Entities with TypeORM for data persistence.
  86. - One service per entity.
  87. - A core module for nest artifacts
  88. - Global filters for exception handling.
  89. - Global middlewares for request management.
  90. - Guards for permission management.
  91. - Interceptors for request management.
  92. - A shared module for services shared between modules.
  93. - Utilities
  94. - Shared business logic
  95. ### Testing
  96. - Use the standard Jest framework for testing.
  97. - Write tests for each controller and service.
  98. - Write end to end tests for each api module.
  99. - Add a admin/test method to each controller as a smoke test.