UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

39 lines 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const dataFetchActions_1 = require("./dataFetchActions.cjs"); function validateResponseFormat(response, type, logger = console.error) { if (!response) { logger(`The dataProvider returned an empty response for '${type}'.`); throw new Error('ra.notification.data_provider_error'); } if (!response.hasOwnProperty('data')) { logger(`The response to '${type}' must be like { data: ... }, but the received response does not have a 'data' key. The dataProvider is probably wrong for '${type}'.`); throw new Error('ra.notification.data_provider_error'); } if (dataFetchActions_1.fetchActionsWithArrayOfRecordsResponse.includes(type) && !Array.isArray(response.data)) { logger(`The response to '${type}' must be like { data : [...] }, but the received data is not an array. The dataProvider is probably wrong for '${type}'`); throw new Error('ra.notification.data_provider_error'); } if (dataFetchActions_1.fetchActionsWithArrayOfIdentifiedRecordsResponse.includes(type) && Array.isArray(response.data) && response.data.length > 0 && !response.data[0].hasOwnProperty('id')) { logger(`The response to '${type}' must be like { data : [{ id: 123, ...}, ...] }, but the received data items do not have an 'id' key. The dataProvider is probably wrong for '${type}'`); throw new Error('ra.notification.data_provider_error'); } if (dataFetchActions_1.fetchActionsWithRecordResponse.includes(type) && !response.data.hasOwnProperty('id')) { logger(`The response to '${type}' must be like { data: { id: 123, ... } }, but the received data does not have an 'id' key. The dataProvider is probably wrong for '${type}'`); throw new Error('ra.notification.data_provider_error'); } if (dataFetchActions_1.fetchActionsWithTotalResponse.includes(type) && !response.hasOwnProperty('total') && !response.hasOwnProperty('pageInfo')) { logger(`The response to '${type}' must be like { data: [...], total: 123 } or { data: [...], pageInfo: {...} }, but the received response has neither a 'total' nor a 'pageInfo' key. The dataProvider is probably wrong for '${type}'`); throw new Error('ra.notification.data_provider_error'); } } exports.default = validateResponseFormat; module.exports = exports.default; //# sourceMappingURL=validateResponseFormat.js.map