(The code is in JS)
My function for making an API call right now is:
function makeRequest(section, command, requestParams, method) {}
I also have enums such as:
enum USER_SECTION {
SECTION = 'users',
GET_ALL = 'get-all'
...
}
So when I want to make an API request I do:
makeRequest(USER_SECTION.SECTION, USER_SECTION.GET_ALL, {...}, "GET")
I want to create an enum that works as follows:
makeRequest(USER_SECTION.GET_ALL, requestParams)
Which means that in the makeRequest function I have to get
USER_SECTION.SECTION
from USER_SECTION.GET_ALL
My biggest problem is that I dont want to repeat the name of the section in the object for every command. Before I had something like:
USER_SECTION {
GET_ALL = {
section = 'users',
command_name = 'get-all'
},
GET = {
section = 'users',
command_name = 'get'
}
...
}
but I want to avoid having to type section = '...'
in every command.
Since I have a separate enum for every section of my API there has to be a way for me to get a field of the class, enum or whatever the command is a field of.
So my questions are:
Is it possible? Is it worthed? Is there a better way?
Thanks in advance.