Normalizr问题:如何为以下API响应正确构建架构

时间:2018-10-18 21:44:22

标签: react-redux schema normalizr

我从WCF服务获得以下API响应,并且需要对其进行规范化以匹配API响应中的EnvironmentsList,FullEnvironmentsList和ServerTypeList对象。我需要帮助正确构建架构。

API响应:

{
  "EnvironmentList": {
    "EnvironmentDTO": [
      {
        "ID": "77",
        "Name": "SIT UAT02",
        "Administrators": {
          "UserDTO": [
            {
              "UserName": "user1"
            },
            {
              "UserName": "user2"
            }
          ]
        },
        "Servers": {
          "ServerDTO": [
            {
              "ID": "269",
              "Name": "web-u02"
            },
            {
              "ID": "270",
              "Name": "web-u04"
            }
          ]
        }
      },
      {
        "ID": "93",
        "Name": "SIT DEMO",
        "Administrators": null,
        "Servers": null
      }
    ]
  },
  "FullEnvironmentList": {
    "EnvironmentDTO": [
      {
        "ID": "77",
        "Name": "SIT UAT02",
        "Administrators": {
          "UserDTO": [
            {
              "UserName": "user1"
            },
            {
              "UserName": "user2"
            }
          ]
        },
        "Servers": {
          "ServerDTO": [
            {
              "ID": "269",
              "Name": "web-u02"
            },
            {
              "ID": "270",
              "Name": "web-u04"
            }
          ]
        }
      },
      {
        "ID": "93",
        "Name": "SIT DEMO",
        "Administrators": null,
        "Servers": null
      }
    ]
  },
  "ServerTypeList": {
    "ServerType": [
      "Unknown",
      "Database",
      "Web"
    ]
  }
}

架构

import { schema } from 'normalizr';

const environmentList = new schema.Entity('environments', {}, {idAttribute: 'ID'});
const fullEnvironmentList = new schema.Entity('fullenvironments', {}, {idAttribute: 'ID'});
const administrator = new schema.Entity('administrators', {}, {idAttribute: 'UserName'});
const server = new schema.Entity('servers', {}, {idAttribute: 'ID'});

environmentList.define({
    EnvironmentDTO: [environmentList],
    ServerDTO: [server],
    UserDTO: [administrator],
});

fullEnvironmentList.define({
    EnvironmentDTO: [fullEnvironmentList],
    ServerDTO: [server],
    UserDTO: [administrator],
});

export const environmentListSchema = {
    EnvironmentList: environmentList,
    FullEnvironmentList: fullEnvironmentList,
};

期望的标准化输出

{
  entities: {
    environmentList: {
        EnvironmentDTO: {
          77: [...],
          93: [...]
        },
        UserDTO: {
          user1: [...],
          user2: [...]
        },
        ServerDTO: {
          269: [...],
          270: [...]
        }       
    },
    fullEnvironmentList: {
        EnvironmentDTO: {
          77: [...],
          93: [...]
        },
        UserDTO: {
          user1: [...],
          user2: [...]
        },
        ServerDTO: {
          269: [...],
          270: [...]
        }       
    },
    serverTypeList: {
        ServerType: {
          [...]
        }   
    }   
  }
}

任何反馈将不胜感激。

0 个答案:

没有答案