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 aa1f837 commit 8517ff7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 49 deletions.
100 changes: 59 additions & 41 deletions addon/adapters/base.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import { get } from '@ember/object';
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 JSONAPIAdapter from '@ember-data/adapter/json-api';
import ImportExportMixin from '../mixins/adapters/import-export';
import { _buildKey } from '../helpers/storage';
import RSVP from 'rsvp';

import {
importData,
exportData,
} from 'ember-local-storage/helpers/import-export';
import { _buildKey } from 'ember-local-storage/helpers/storage';

const getKeys = Object.keys || keys;

// 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;
},
}

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 @@ -55,8 +72,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 All @@ -79,12 +96,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 @@ -100,30 +117,30 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
return records.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) {
return this._handleStorageRequest(request.url, 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 @@ -140,7 +157,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
run(null, reject, `There is nothing to handle _handle${type}Request`);
}
}, 'DS: LocalStorageAdapter#_handleStorageRequest ' + type + ' to ' + url);
},
}

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

return records;
},
}

_handlePOSTRequest(url, record) {
const { type, id } = record.data;
Expand All @@ -181,7 +198,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 @@ -191,7 +208,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 @@ -201,7 +218,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 @@ -279,15 +296,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 @@ -307,27 +325,27 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
type: type,
id: id,
};
},
}

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

// Should be overwriten
// Signature: _getIndex(type)
_getIndex() {},
_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);
},
});
}
}
8 changes: 4 additions & 4 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 @@ -16,5 +16,5 @@ export default BaseAdapter.extend({
}

return indices[type];
},
});
}
}
8 changes: 4 additions & 4 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 @@ -16,5 +16,5 @@ export default BaseAdapter.extend({
}

return indices[type];
},
});
}
}

0 comments on commit 8517ff7

Please sign in to comment.