Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/WithRemoteConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/renderers/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/renderers/Mapping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
buildApi,
isApiOutdated,
isEffectiveApi,
normalizeApi
normalizeApi,
normalizeApiResponseData
} from '../utils/api';

/**
Expand Down Expand Up @@ -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');
Expand Down
11 changes: 3 additions & 8 deletions src/store/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
},
Expand Down
3 changes: 2 additions & 1 deletion src/store/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions src/store/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -176,7 +177,7 @@ export const ServiceStore = iRendererStore

json.data &&
self.updateData(
json.data,
normalizeApiResponseData(json.data),
undefined,
!!(api as ApiObject).replaceData
);
Expand Down Expand Up @@ -264,7 +265,7 @@ export const ServiceStore = iRendererStore

json.data &&
self.updateData(
json.data,
normalizeApiResponseData(json.data),
undefined,
!!(api as ApiObject).replaceData
);
Expand Down Expand Up @@ -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) &&
Expand Down
13 changes: 13 additions & 0 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;