From da62b41e5b303237edcc7b2079cf913d22066594 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Sun, 4 Sep 2022 23:23:26 +0200 Subject: [PATCH] Refactor adapters to native classes --- addon/adapters/base.js | 90 +++++++++++++++++++++------------------ addon/adapters/local.js | 8 ++-- addon/adapters/session.js | 8 ++-- 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/addon/adapters/base.js b/addon/adapters/base.js index 46903b9..fc4a0ee 100644 --- a/addon/adapters/base.js +++ b/addon/adapters/base.js @@ -1,8 +1,9 @@ -import RSVP from 'rsvp'; +import JSONAPIAdapter from '@ember-data/adapter/json-api'; +import { get } from '@ember/object'; 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 RSVP from 'rsvp'; + import { importData, exportData, @@ -12,43 +13,47 @@ import { _buildKey } from 'ember-local-storage/helpers/storage'; // Ember data ships with ember-inflector import { singularize, pluralize } from 'ember-inflector'; -export default JSONAPIAdapter.extend({ - _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 & Export + // 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) { @@ -64,8 +69,8 @@ export default JSONAPIAdapter.extend({ } }); - return this._super.apply(this, arguments); - }, + return super.createRecord(...arguments); + } deleteRecord(store, type, snapshot) { snapshot.eachRelationship(function (name, relationship) { @@ -88,12 +93,12 @@ export default JSONAPIAdapter.extend({ } }); - 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); @@ -109,30 +114,30 @@ export default JSONAPIAdapter.extend({ 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 = {}) { @@ -149,7 +154,7 @@ export default JSONAPIAdapter.extend({ 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); @@ -180,7 +185,7 @@ export default JSONAPIAdapter.extend({ } return records; - }, + } _handlePOSTRequest(url, record) { const { type, id } = record.data; @@ -190,7 +195,7 @@ export default JSONAPIAdapter.extend({ get(this, '_storage')[storageKey] = JSON.stringify(record.data); return null; - }, + } _handlePATCHRequest(url, record) { const { type, id } = record.data; @@ -200,7 +205,7 @@ export default JSONAPIAdapter.extend({ get(this, '_storage')[storageKey] = JSON.stringify(record.data); return null; - }, + } _handleDELETERequest(url) { const { type, id } = this._urlParts(url); @@ -210,7 +215,7 @@ export default JSONAPIAdapter.extend({ delete get(this, '_storage')[storageKey]; return null; - }, + } // TODO: Extract into utility functions in private/query.js _queryFilter(data, serializer, query = {}) { @@ -288,15 +293,16 @@ export default JSONAPIAdapter.extend({ }); } } - }, + } + // 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('/'); @@ -316,27 +322,27 @@ export default JSONAPIAdapter.extend({ 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); - }, -}); + } +} diff --git a/addon/adapters/local.js b/addon/adapters/local.js index 0333d18..4b7d30a 100644 --- a/addon/adapters/local.js +++ b/addon/adapters/local.js @@ -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'); @@ -16,5 +16,5 @@ export default BaseAdapter.extend({ } return indices[type]; - }, -}); + } +} diff --git a/addon/adapters/session.js b/addon/adapters/session.js index eb015fe..abc7daf 100644 --- a/addon/adapters/session.js +++ b/addon/adapters/session.js @@ -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'); @@ -16,5 +16,5 @@ export default BaseAdapter.extend({ } return indices[type]; - }, -}); + } +}