Skip to content

Commit

Permalink
Refactor adapters to native classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed Sep 4, 2022
1 parent a713b7c commit 35acb38
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
88 changes: 52 additions & 36 deletions addon/adapters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { keys } from '@ember/polyfills';
import RSVP from 'rsvp';
import { run } from '@ember/runloop';
import { isEmpty, typeOf } from '@ember/utils';
import { computed, get } from '@ember/object';
import { get } from '@ember/object';
import DS from 'ember-data';
import ImportExportMixin from '../mixins/adapters/import-export';
import { _buildKey } from '../helpers/storage';
import { importData, exportData } from 'ember-local-storage/helpers/import-export';
import { _buildKey } from 'ember-local-storage/helpers/storage';

const getKeys = Object.keys || keys;

Expand All @@ -16,24 +16,39 @@ const {
// Ember data ships with ember-inflector
import { singularize, pluralize } from 'ember-inflector';

export default JSONAPIAdapter.extend(ImportExportMixin, {
_debug: false,
_indices: computed(function() { return {}; }),
coalesceFindRequests: false,
export default class BaseAdapter extends JSONAPIAdapter {
constructor() {
super(...arguments);

this._indices = {};
}

_debug = false;
coalesceFindRequests = false;

// TODO: v2.0 - What are the defaults now? What versions to support?
isNewSerializerAPI: true,
isNewSerializerAPI = true;

// TODO: v2.0 - Can we deprecate or remove that? What are the defaults now? What versions to support?
// Reload behavior
shouldReloadRecord() { return true; },
shouldReloadAll() { return true; },
shouldBackgroundReloadRecord() { return true; },
shouldBackgroundReloadAll() { return true; },
shouldReloadRecord() { return true; }
shouldReloadAll() { return true; }
shouldBackgroundReloadRecord() { return true; }
shouldBackgroundReloadAll() { return true; }

generateIdForRecord() {
return Math.random().toString(32).slice(2).substr(0, 8);
},
}

// Import
importData(store, content, options) {
return importData(store, content, options);
}

// Export
exportData(store, types, options) {
return exportData(store, types, options);
}

// Relationship sugar
createRecord(store, type, snapshot) {
Expand All @@ -53,8 +68,8 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
}
});

return this._super.apply(this, arguments);
},
return super.createRecord(...arguments);
}

deleteRecord(store, type, snapshot) {
snapshot.eachRelationship(function(name, relationship) {
Expand Down Expand Up @@ -82,12 +97,12 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
}
});

return this._super.apply(this, arguments);
},
return super.deleteRecord(...arguments);
}

// Polyfill queryRecord
queryRecord(store, type, query) {
let records = this._super.apply(this, arguments);
let records = super.queryRecord(...arguments);

if (!records) {
var url = this.buildURL(type.modelName, null, null, 'queryRecord', query);
Expand All @@ -104,13 +119,13 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
.then(function(result) {
return {data: result.data[0] || null};
});
},
}

// TODO: v2.0 - What are the defaults now? What versions to support?
// Delegate to _handleStorageRequest
ajax() {
return this._handleStorageRequest.apply(this, arguments);
},
}

// Delegate to _handleStorageRequest
makeRequest(request) {
Expand All @@ -119,17 +134,17 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
request.method,
{ data: request.data }
);
},
}

// Work arround ds-improved-ajax Feature Flag
_makeRequest() {
return this.makeRequest.apply(this, arguments);
},
}

// Remove the ajax() deprecation warning
_hasCustomizedAjax() {
return false;
},
}

// Delegate to _handle${type}Request
_handleStorageRequest(url, type, options = {}) {
Expand All @@ -150,7 +165,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
);
}
}, 'DS: LocalStorageAdapter#_handleStorageRequest ' + type + ' to ' + url);
},
}

_handleGETRequest(url, query) {
const { type, id } = this._urlParts(url);
Expand Down Expand Up @@ -181,7 +196,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
}

return records;
},
}

_handlePOSTRequest(url, record) {
const { type, id } = record.data;
Expand All @@ -191,7 +206,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
get(this, '_storage')[storageKey] = JSON.stringify(record.data);

return null;
},
}

_handlePATCHRequest(url, record) {
const { type, id } = record.data;
Expand All @@ -201,7 +216,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
get(this, '_storage')[storageKey] = JSON.stringify(record.data);

return null;
},
}

_handleDELETERequest(url) {
const { type, id } = this._urlParts(url);
Expand All @@ -211,7 +226,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
delete get(this, '_storage')[storageKey];

return null;
},
}

// TODO: Extract into utility functions in private/query.js
_queryFilter(data, serializer, query = {}) {
Expand Down Expand Up @@ -286,15 +301,16 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
});
}
}
},
}

// TODO: Extract into utility function
_matches(recordValue, queryValue) {
if (typeOf(queryValue) === 'regexp') {
return queryValue.test(recordValue);
}

return recordValue === queryValue;
},
}

_urlParts(url) {
const parts = url.split('/');
Expand All @@ -314,28 +330,28 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
type: type,
id: id
};
},
}

_storageKey(type, id) {
return _buildKey(this, type + '-' + id);
},
}

// Should be overwriten
// Signature: _getIndex(type)
_getIndex() {
},
}

_indexHasKey(type, id) {
return this._getIndex(type).indexOf(id) !== -1;
},
}

_addToIndex(type, id) {
if (!this._indexHasKey(type, id)) {
this._getIndex(type).addObject(id);
}
},
}

_removeFromIndex(type, id) {
this._getIndex(type).removeObject(id);
}
});
}
6 changes: 3 additions & 3 deletions addon/adapters/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import BaseAdapter from './base';
import { getStorage, _buildKey } from '../helpers/storage';
import StorageArray from '../local/array';

export default BaseAdapter.extend({
_storage: getStorage('local'),
export default class LocalStorageAdapter extends BaseAdapter {
_storage = getStorage('local');

_getIndex(type) {
const indices = get(this, '_indices');
Expand All @@ -19,4 +19,4 @@ export default BaseAdapter.extend({

return indices[type];
}
});
}
6 changes: 3 additions & 3 deletions addon/adapters/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import BaseAdapter from './base';
import { getStorage, _buildKey } from '../helpers/storage';
import StorageArray from '../session/array';

export default BaseAdapter.extend({
_storage: getStorage('session'),
export default class SessionStorageAdapter extends BaseAdapter {
_storage = getStorage('session');

_getIndex(type) {
const indices = get(this, '_indices');
Expand All @@ -19,4 +19,4 @@ export default BaseAdapter.extend({

return indices[type];
}
});
}

0 comments on commit 35acb38

Please sign in to comment.