在JavaScript中调用对象后的默认字符串值

我有一个js对象,我在其中从api返回端点地址。这对我来说是一个非常好的解决方案,看起来像这样:

export const API_BASE_URL = 'http://localhost:3000';
export const USERS = '/Users';

export default {
  users: {
    checkEmail: (email) => `${API_BASE_URL}${USERS}/${email}/checkEmail`,
    notifications: `${API_BASE_URL}${USERS}/notifications`,
    messages: `${API_BASE_URL}${USERS}/messages`,
  },
};

现在,我可以在redux-saga中调用此地址来执行xhr查询:

import { api } from 'utils';

const requestURL = api.users.notifications; 

But I'm a bit stuck because now I have a problem - base path is missing here: '/users'.

Now when I call api.users, then I get a object. I would like to have a default value after calling the object like:

import { api } from 'utils';

const requestURL = api.users; // http://localhost:3000/Users
const requestURL2 = api.users.notifications; // http://localhost:3000/Users/notifications

我知道我可以在对象中添加名称为“ base”的新字符串,然后在其中添加“ / Users”,但是我不喜欢这种解决方案,而且我认为有更好的解决方案。

评论