yracnet / vite-plugin-api-routes

Create API routes from path directory like to Nextjs
MIT License
27 stars 4 forks source link

New Version 1.2.0-beta #18

Open yracnet opened 7 months ago

yracnet commented 7 months ago

Hello friend,

This library has been very useful in my projects, but now that I've completed a very large project, I've encountered some issues regarding SOLID principles.

Regarding the Single Responsibility Principle: Defining GET and PUSH methods in the same file has become complex when using common methods between classes. Maintenance becomes more complicated as you need more controls in a file that can define all 5 verbs in one place (extreme case).

// Example
// src/api/session.js 
export const GET = () => {}
export const POST = () => {}
export const PUT = () => {}
export const PUSH = () => {}
export const DELETE = () => {}

A colleague suggested enhancing a method mapping as follows:

// Initial Structure
// At a glance, it's not clear what methods /src/admin/post/index.js defines
src/
      auth/
          login.js
          takeover.js
      admin/
          index.js
          post/
               index.js
               [id]/
                   index.js

The proposal for the new directory mapping:

// Proposed Structure
// At first glance, you can see the methods defined in /src/admin/post/
src/
      auth/
          login/
               POST.js
          takeover/
               POST.js
      admin/
          USE.js
          post/
               GET.js
               POST.js
               [id]/
                   PUSH.js
                   PUT.js
                   DELETE.js

Where the implementation of the file will change to:

// Source:     /src/api/admin/post/GET.js
// Mapping: GET /api/admin/post

export default (req, res, next) => {
      //......
}

As you can see, the concept of single responsibility appears, and the directory structure is more readable. This option allows declaring files in the same directory without affecting the initial project mapping.

src/
      admin/
          post/
               validator.js  // will not be included in the MAPPING
               GET.js
               POST.js
               [id]/
                   PUSH.js
                   PUT.js
                   DELETE.js

Well, since the library is already used by you, I would like to know your opinion.

Thanks

cc: @leaftail1880 @oscar811 @PlayWolfYT @ablydevin

leaftail1880 commented 7 months ago

Initially i thought that would be cool to keep both of mapping types available, but now i think that this will lack code readability and make it harder to understand if some methods would be defined in one file, and some in separated ones. So i think that this change you suggesting would be really nice