mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 02:58:05 +08:00
chore: 如果接口返回的数据不是键值对,强制转成键值对 (#2904)
This commit is contained in:
parent
b23fc3ff71
commit
0830beb0f6
@ -12,7 +12,12 @@ import {withStore} from './WithStore';
|
||||
import {EnvContext, RendererEnv} from '../env';
|
||||
|
||||
import {flow, Instance, isAlive, types} from 'mobx-state-tree';
|
||||
import {buildApi, isEffectiveApi, normalizeApi} from '../utils/api';
|
||||
import {
|
||||
buildApi,
|
||||
isEffectiveApi,
|
||||
normalizeApi,
|
||||
normalizeApiResponseData
|
||||
} from '../utils/api';
|
||||
import {
|
||||
isPureVariable,
|
||||
resolveVariableAndFilter,
|
||||
@ -46,7 +51,7 @@ export const Store = types
|
||||
}
|
||||
|
||||
if (ret.ok) {
|
||||
const data = ret.data || {};
|
||||
const data = normalizeApiResponseData(ret.data);
|
||||
let options = config.adaptor
|
||||
? config.adaptor(data, component.props)
|
||||
: data;
|
||||
|
@ -12,7 +12,11 @@ import {
|
||||
isPureVariable,
|
||||
dataMapping
|
||||
} from '../utils/tpl-builtin';
|
||||
import {isApiOutdated, isEffectiveApi} from '../utils/api';
|
||||
import {
|
||||
isApiOutdated,
|
||||
isEffectiveApi,
|
||||
normalizeApiResponseData
|
||||
} from '../utils/api';
|
||||
import {ScopedContext, IScopedContext} from '../Scoped';
|
||||
import {createObject, findObjectsWithKey} from '../utils/helper';
|
||||
import Spinner from '../components/Spinner';
|
||||
@ -342,7 +346,7 @@ export class Chart extends React.Component<ChartProps> {
|
||||
}
|
||||
delete this.reloadCancel;
|
||||
|
||||
const data = result.data || {};
|
||||
const data = normalizeApiResponseData(result.data);
|
||||
// 说明返回的是数据接口。
|
||||
if (!data.series && this.props.config) {
|
||||
const ctx = createObject(this.props.data, data);
|
||||
|
@ -20,7 +20,8 @@ import {
|
||||
buildApi,
|
||||
isApiOutdated,
|
||||
isEffectiveApi,
|
||||
normalizeApi
|
||||
normalizeApi,
|
||||
normalizeApiResponseData
|
||||
} from '../utils/api';
|
||||
|
||||
/**
|
||||
@ -72,7 +73,7 @@ export const Store = StoreNode.named('MappingStore')
|
||||
const ret: Payload = yield env.fetcher(api, data);
|
||||
|
||||
if (ret.ok) {
|
||||
const data = ret.data || {};
|
||||
const data = normalizeApiResponseData(ret.data);
|
||||
(self as any).setMap(data);
|
||||
} else {
|
||||
throw new Error(ret.msg || 'fetch error');
|
||||
|
@ -21,6 +21,7 @@ import {
|
||||
import {Api, Payload, fetchOptions, Action, ApiObject} from '../types';
|
||||
import pick from 'lodash/pick';
|
||||
import {resolveVariableAndFilter} from '../utils/tpl-builtin';
|
||||
import {normalizeApiResponseData} from '../utils/api';
|
||||
|
||||
class ServerError extends Error {
|
||||
type = 'ServerError';
|
||||
@ -228,13 +229,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
||||
}
|
||||
|
||||
self.updatedAt = Date.now();
|
||||
let result = json.data;
|
||||
|
||||
if (Array.isArray(result)) {
|
||||
result = {
|
||||
items: result
|
||||
};
|
||||
}
|
||||
let result = normalizeApiResponseData(json.data);
|
||||
|
||||
const {
|
||||
total,
|
||||
@ -384,7 +379,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
|
||||
|
||||
if (!isEmpty(json.data) || json.ok) {
|
||||
self.updateData(
|
||||
json.data,
|
||||
normalizeApiResponseData(json.data),
|
||||
{
|
||||
__saved: Date.now()
|
||||
},
|
||||
|
@ -29,6 +29,7 @@ import isEqual from 'lodash/isEqual';
|
||||
import flatten from 'lodash/flatten';
|
||||
import {getStoreById, removeStore} from './manager';
|
||||
import {filter} from '../utils/tpl';
|
||||
import {normalizeApiResponseData} from '../utils/api';
|
||||
|
||||
export const FormStore = ServiceStore.named('FormStore')
|
||||
.props({
|
||||
@ -305,7 +306,7 @@ export const FormStore = ServiceStore.named('FormStore')
|
||||
self.updatedAt = Date.now();
|
||||
|
||||
setValues(
|
||||
json.data,
|
||||
normalizeApiResponseData(json.data),
|
||||
json.ok
|
||||
? {
|
||||
__saved: Date.now()
|
||||
|
@ -3,6 +3,7 @@ import {iRendererStore} from './iRenderer';
|
||||
import {Api, ApiObject, Payload, fetchOptions} from '../types';
|
||||
import {extendObject, isEmpty, isObject} from '../utils/helper';
|
||||
import {ServerError} from '../utils/errors';
|
||||
import {normalizeApiResponseData} from '../utils/api';
|
||||
|
||||
export const ServiceStore = iRendererStore
|
||||
.named('ServiceStore')
|
||||
@ -96,7 +97,7 @@ export const ServiceStore = iRendererStore
|
||||
let replace = !!(api as ApiObject).replaceData;
|
||||
let data = {
|
||||
...(replace ? {} : self.data),
|
||||
...json.data
|
||||
...normalizeApiResponseData(json.data)
|
||||
};
|
||||
reInitData(data, replace);
|
||||
self.hasRemoteData = true;
|
||||
@ -176,7 +177,7 @@ export const ServiceStore = iRendererStore
|
||||
|
||||
json.data &&
|
||||
self.updateData(
|
||||
json.data,
|
||||
normalizeApiResponseData(json.data),
|
||||
undefined,
|
||||
!!(api as ApiObject).replaceData
|
||||
);
|
||||
@ -264,7 +265,7 @@ export const ServiceStore = iRendererStore
|
||||
|
||||
json.data &&
|
||||
self.updateData(
|
||||
json.data,
|
||||
normalizeApiResponseData(json.data),
|
||||
undefined,
|
||||
!!(api as ApiObject).replaceData
|
||||
);
|
||||
@ -406,7 +407,7 @@ export const ServiceStore = iRendererStore
|
||||
: {
|
||||
type: 'wrapper',
|
||||
wrap: false,
|
||||
...json.data
|
||||
...normalizeApiResponseData(json.data)
|
||||
};
|
||||
self.schemaKey = '' + Date.now();
|
||||
isObject(json.data.data) &&
|
||||
|
@ -12,6 +12,7 @@ import {
|
||||
createObject,
|
||||
qsparse
|
||||
} from './helper';
|
||||
import isPlainObject from 'lodash/isPlainObject';
|
||||
|
||||
const rSchema = /(?:^|raw\:)(get|post|put|delete|patch|options|head):/i;
|
||||
|
||||
@ -469,4 +470,16 @@ export function clearApiCache() {
|
||||
apiCaches.splice(0, apiCaches.length);
|
||||
}
|
||||
|
||||
export function normalizeApiResponseData(data: any) {
|
||||
if (typeof data === 'undefined') {
|
||||
data = {};
|
||||
} else if (!isPlainObject(data)) {
|
||||
data = {
|
||||
[Array.isArray(data) ? 'items' : 'result']: data
|
||||
};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// window.apiCaches = apiCaches;
|
||||
|
Loading…
Reference in New Issue
Block a user