axios上的typescript反序列化获取请求响应对象

时间:2017-02-22 15:27:38

标签: typescript deserialization typescript2.0 axios

我有以下axios获取请求:

export function getCurrentCart() {
    return axios.get(Constants.ENDPOINT_MENU_TIMESLOTS, getConfig())
        .then(res => {
            let data = (res as AxiosResponse).data;
            if (data != null) {
                // parsing logic to singleton CartStore instance here
                ...
            } else {
                console.error("data is null");
            }
        });
}

检索ts CartStore类:

export class CartStore {
    id: number;
    temporaryTotal: number;
    status: string = "SHOPPING";
    statusLockTime?: any;
    updatedAt?: string;
    createdAt?: string;
    coupon?: Coupon;

    readonly items = observable.shallowArray<CartItem>([]);  // this is a mobx observable

    @computed get hasItem(): boolean {
        return this.items.length > 0;
    }

    @action clear() {
        this.items.clear();
    }
}

其中CouponCartItem是其他模型类。

以下是JSON响应示例:

{
  "createdAt": "2017-02-16T20:37:13",
  "updatedAt": "2017-02-22T22:48:50",
  "id": 1,
  "status": "SHOPPING",
  "statusLockTime": null,
  "temporaryTotal": 0,
  "items": [
    ... array of some other json object
  ],
  "coupon": {
    ... json object
  }
}

你们推荐哪些deserializer librar(ies)?另一个问题是:如何在不使用任何库的情况下以最TS方式进行操作?

0 个答案:

没有答案