[feature-request]: Use HTTP method as filename for an API endpoint to delegate HTTP request method #44909
Labels
template: story
A user has filled out a feature request template. Will be converted to Discussions (Idea)
Describe the feature you'd like to request
It'd would be nice to use HTTP methods as a filename scheme when creating API endpoints and be called based on the request type made by the client
Currently
Lets say we have two endpoints created, each will be handling
GET
andPOST
requests.The file directory would look like so
The API endpoint URL would be:
api/bar
api/foo
Then that would mean
bar.ts
andfoo.ts
will have code handlingGET
andPOST
request in those files.foo.ts
bar.ts
There's seem to be redundant code to handle different HTTP method types.
So instead, it would be better to have API endpoint file changed to a folder and the files in that folder contain HTTP method as files that API will call depending on the request type made by the client.
Describe the solution you'd like
What I'm proposing is to split them based on HTTP methods
The API endpoint URL would be:
api/bar
api/foo
api/foobar/baz
sending request would be the same as before.
But now code that handle
GET
andPOST
requests are separatedCheck for more: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
foo/GET.ts
foo/POST.ts
bar/GET.ts
bar/POST.ts
foobar/baz/GET.ts
foobar/baz/POST.ts
pros
cons
Describe alternatives you've considered
An alternative is for exporting certain functions that handle HTTP requests based on the HTTP method types
Ex:
api/foo
endpointCheck for more: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
NextJS looks for certain named functions that will be called depending on the HTTP method used.
Ex: if
GET
request is made, then functionhandleGet
is called automaticallyEx: if
POST
request is made, then functionhandlePost
is called automaticallyIf a HTTP method is used and there's no corresponding function to handle it will return an automatic error code
405
The text was updated successfully, but these errors were encountered: