Skip to content

Commit

Permalink
Renovation: odata/mixins es6 refactoring (#11444)
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyLahtak authored Jan 27, 2020
1 parent 5ccb47d commit d6be4df
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 103 deletions.
19 changes: 11 additions & 8 deletions js/data/odata/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { isDefined, isPlainObject } from '../../core/utils/type';
import { each } from '../../core/utils/iterator';
import errorsModule from '../errors';
import ODataStore from './store';
import { SharedMethods, formatFunctionInvocationUrl, escapeServiceOperationParams } from './mixins';
import RequestDispatcher from './request_dispatcher';
import { escapeServiceOperationParams, formatFunctionInvocationUrl } from './utils';
import { when, Deferred } from '../../core/utils/deferred';
import './query_adapter';

const ODataContext = Class.inherit({

ctor(options) {
this._extractServiceOptions(options);
this._requestDispatcher = new RequestDispatcher(options);

this._errorHandler = options.errorHandler;

Expand All @@ -20,7 +21,7 @@ const ODataContext = Class.inherit({
{},
options,
{
url: `${this._url}/${encodeURIComponent(entityOptions.name || entityAlias)}`
url: `${this._requestDispatcher.url}/${encodeURIComponent(entityOptions.name || entityAlias)}`
},
entityOptions
));
Expand All @@ -35,7 +36,7 @@ const ODataContext = Class.inherit({
httpMethod = httpMethod.toLowerCase();

const d = new Deferred();
let url = `${this._url}/${encodeURIComponent(operationName)}`;
let url = `${this._requestDispatcher.url}/${encodeURIComponent(operationName)}`;
let payload;

if(this.version() === 4) {
Expand All @@ -48,7 +49,7 @@ const ODataContext = Class.inherit({
}
}

when(this._sendRequest(url, httpMethod, escapeServiceOperationParams(params, this.version()), payload))
when(this._requestDispatcher.sendRequest(url, httpMethod, escapeServiceOperationParams(params, this.version()), payload))
.done((r) => {
if(isPlainObject(r) && operationName in r) {
r = r[operationName];
Expand Down Expand Up @@ -77,9 +78,11 @@ const ODataContext = Class.inherit({
uri: store._byKeyUrl(key, true)
}
};
}
},

})
.include(SharedMethods);
version() {
return this._requestDispatcher.version;
},
});

export default ODataContext;
71 changes: 0 additions & 71 deletions js/data/odata/mixins.js

This file was deleted.

55 changes: 55 additions & 0 deletions js/data/odata/request_dispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { sendRequest } from './utils';
import './query_adapter';

const DEFAULT_PROTOCOL_VERSION = 2;

export default class RequestDispatcher {
constructor(options) {
options = options || {};

this._url = String(options.url).replace(/\/+$/, '');
this._beforeSend = options.beforeSend;
this._jsonp = options.jsonp;
this._version = options.version || DEFAULT_PROTOCOL_VERSION;
this._withCredentials = options.withCredentials;
this._deserializeDates = options.deserializeDates;
this._filterToLower = options.filterToLower;
}

sendRequest(url, method, params, payload) {
return sendRequest(this.version,
{
url,
method,
params: params || {},
payload
},
{
beforeSend: this._beforeSend,
jsonp: this._jsonp,
withCredentials: this._withCredentials,
deserializeDates: this._deserializeDates
}
);
}

get version() {
return this._version;
}

get beforeSend() {
return this._beforeSend;
}

get url() {
return this._url;
}

get jsonp() {
return this._jsonp;
}

get filterToLower() {
return this._filterToLower;
}
}
49 changes: 26 additions & 23 deletions js/data/odata/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const proxyUrlFormatter = require('../proxy_url_formatter');
const errors = require('../errors').errors;
const query = require('../query');
const Store = require('../abstract_store');
const mixins = require('./mixins');
const RequestDispatcher = require('./request_dispatcher').default;
const deferredUtils = require('../../core/utils/deferred');
const when = deferredUtils.when;
const Deferred = deferredUtils.Deferred;
Expand Down Expand Up @@ -45,8 +45,7 @@ const ODataStore = Store.inherit({
ctor: function(options) {
this.callBase(options);

this._extractServiceOptions(options);

this._requestDispatcher = new RequestDispatcher(options);

let key = this.key();
let fieldTypes = options.fieldTypes;
Expand Down Expand Up @@ -84,11 +83,11 @@ const ODataStore = Store.inherit({
const params = {};

if(extraOptions) {
params['$expand'] = odataUtils.generateExpand(this._version, extraOptions.expand, extraOptions.select) || undefined;
params['$select'] = odataUtils.generateSelect(this._version, extraOptions.select) || undefined;
params['$expand'] = odataUtils.generateExpand(this.version(), extraOptions.expand, extraOptions.select) || undefined;
params['$select'] = odataUtils.generateSelect(this.version(), extraOptions.select) || undefined;
}

return this._sendRequest(this._byKeyUrl(key), 'GET', params);
return this._requestDispatcher.sendRequest(this._byKeyUrl(key), 'GET', params);
},

createQuery: function(loadOptions) {
Expand All @@ -98,33 +97,33 @@ const ODataStore = Store.inherit({
const queryOptions = {
adapter: 'odata',

beforeSend: this._beforeSend,
beforeSend: this._requestDispatcher.beforeSend,
errorHandler: this._errorHandler,
jsonp: this._jsonp,
version: this._version,
withCredentials: this._withCredentials,
jsonp: this._requestDispatcher.jsonp,
version: this._requestDispatcher.version,
withCredentials: this._requestDispatcher._withCredentials,
expand: loadOptions.expand,
requireTotalCount: loadOptions.requireTotalCount,
deserializeDates: this._deserializeDates,
deserializeDates: this._requestDispatcher._deserializeDates,
fieldTypes: this._fieldTypes
};

// NOTE: For AppBuilder, do not remove
if(isDefined(loadOptions.urlOverride)) {
url = loadOptions.urlOverride;
} else {
url = this._url;
url = this._requestDispatcher.url;
}

if(isDefined(this._filterToLower)) {
queryOptions.filterToLower = this._filterToLower;
if(isDefined(this._requestDispatcher.filterToLower)) {
queryOptions.filterToLower = this._requestDispatcher.filterToLower;
}

if(loadOptions.customQueryParams) {
const params = mixins.escapeServiceOperationParams(loadOptions.customQueryParams, this.version());
const params = odataUtils.escapeServiceOperationParams(loadOptions.customQueryParams, this.version());

if(this.version() === 4) {
url = mixins.formatFunctionInvocationUrl(url, params);
url = odataUtils.formatFunctionInvocationUrl(url, params);
} else {
queryOptions.params = params;
}
Expand All @@ -139,7 +138,7 @@ const ODataStore = Store.inherit({
const that = this;
const d = new Deferred();

when(this._sendRequest(this._url, 'POST', null, values))
when(this._requestDispatcher.sendRequest(this._requestDispatcher.url, 'POST', null, values))
.done(function(serverResponse) {
d.resolve(config().useLegacyStoreResult ? values : (serverResponse || values), that.keyOf(serverResponse));
})
Expand All @@ -152,7 +151,7 @@ const ODataStore = Store.inherit({
const d = new Deferred();

when(
this._sendRequest(this._byKeyUrl(key), this._updateMethod, null, values)
this._requestDispatcher.sendRequest(this._byKeyUrl(key), this._updateMethod, null, values)
).done(
function(serverResponse) {
if(config().useLegacyStoreResult) {
Expand All @@ -170,7 +169,7 @@ const ODataStore = Store.inherit({
const d = new Deferred();

when(
this._sendRequest(this._byKeyUrl(key), 'DELETE')
this._requestDispatcher.sendRequest(this._byKeyUrl(key), 'DELETE')
).done(
function() {
d.resolve(key);
Expand Down Expand Up @@ -200,14 +199,18 @@ const ODataStore = Store.inherit({

_byKeyUrl: function(value, useOriginalHost) {
const baseUrl = useOriginalHost
? proxyUrlFormatter.formatLocalUrl(this._url)
: this._url;
? proxyUrlFormatter.formatLocalUrl(this._requestDispatcher.url)
: this._requestDispatcher.url;

const convertedKey = this._convertKey(value);

return baseUrl + '(' + encodeURIComponent(odataUtils.serializeKey(convertedKey, this._version)) + ')';
return baseUrl + '(' + encodeURIComponent(odataUtils.serializeKey(convertedKey, this.version())) + ')';
},

version() {
return this._requestDispatcher.version;
}

}, 'odata').include(mixins.SharedMethods);
}, 'odata');

module.exports = ODataStore;
23 changes: 23 additions & 0 deletions js/data/odata/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { grep } from '../../core/utils/common';
import { Deferred } from '../../core/utils/deferred';
import { errors } from '../errors';
import { XHR_ERROR_UNLOAD, errorMessageFromXhr } from '../utils';
import { format as stringFormat } from '../../core/utils/string';

const GUID_REGEX = /^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$/;

Expand Down Expand Up @@ -599,6 +600,28 @@ export const generateExpand = (oDataVersion, expand, select) =>
? generatorV2(expand, select)
: generatorV4(expand, select);

export const formatFunctionInvocationUrl = (baseUrl, args) =>
stringFormat(
'{0}({1})',
baseUrl,
map(args || {}, (value, key) => stringFormat('{0}={1}', key, value)).join(',')
);

export const escapeServiceOperationParams = (params, version) => {
if(!params) {
return params;
}

// From WCF Data Services docs:
// The type of each parameter must be a primitive type.
// Any data of a non-primitive type must be serialized and passed into a string parameter
const result = {};
each(params, (k, v) => {
result[k] = serializeValue(v, version);
});
return result;
};

///#DEBUG
export const OData__internals = {
interpretJsonFormat
Expand Down
2 changes: 1 addition & 1 deletion testing/tests/DevExpress.data/dataSourceCreating.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ QUnit.test('options.store is ODataStore config', function(assert) {
});

assert.ok(source.store() instanceof ODataStore);
assert.equal(source.store()._url, url);
assert.equal(source.store()._requestDispatcher.url, url);
});

QUnit.test('options.store is LocalStore config', function(assert) {
Expand Down

0 comments on commit d6be4df

Please sign in to comment.