Skip to content

Commit

Permalink
[Chore]: convert adapters to class syntax (#7350)
Browse files Browse the repository at this point in the history
* [Chore]: convert adapters to class syntax
  • Loading branch information
snewcomer authored Oct 22, 2020
1 parent 2f8962d commit e98f21c
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2561,7 +2561,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) {
});

if (hasJQuery) {
test('on error appends errorThrown for sanity', function(assert) {
test('on error appends errorThrown for sanity', async function(assert) {
assert.expect(2);

let jqXHR = {
Expand All @@ -2581,12 +2581,12 @@ module('integration/adapter/rest_adapter - REST Adapter', function(hooks) {
assert.ok(false);
};

return run(() => {
return store.findRecord('post', '1').catch(err => {
assert.equal(err, errorThrown);
assert.ok(err, 'promise rejected');
});
});
try {
await store.findRecord('post', '1');
} catch (err) {
assert.equal(err, errorThrown);
assert.ok(err, 'promise rejected');
}
});

test('on error wraps the error string in an DS.AdapterError object', function(assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ module('unit/adapters/json-api-adapter/ajax-options - building requests', functi
setupTest(hooks);

hooks.beforeEach(function() {
this.owner.register('adapter:application', JSONAPIAdapter.extend({ useFetch: true }));
this.owner.register(
'adapter:application',
class extends JSONAPIAdapter {
useFetch = true;
}
);
this.owner.register('serializer:application', JSONAPISerializer.extend());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h
this.owner.register('model:person', Person);
this.owner.register('model:place', Place);

this.owner.register('adapter:application', RESTAdapter.extend({ useFetch: true }));
this.owner.register(
'adapter:application',
class extends RESTAdapter {
useFetch = true;
}
);
this.owner.register('serializer:application', RESTSerializer.extend());
});

Expand Down Expand Up @@ -243,7 +248,12 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h

module('ajax-options - ajax', function(hooks) {
hooks.beforeEach(function() {
this.owner.register('adapter:application', RESTAdapter.extend({ useFetch: false }));
this.owner.register(
'adapter:application',
class extends RESTAdapter {
useFetch = false;
}
);
});

test('ajaxOptions() Content-Type is not set with ajax GET', function(assert) {
Expand All @@ -268,8 +278,14 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h
assert.notOk(ajaxOptions.contentType, 'contentType not set with POST no data');
});

test('ajaxOptions() Content-Type is set with ajax POST with data', function(assert) {
test('ajaxOptions() Content-Type is set with ajax POST with data if not useFetch', function(assert) {
let store = this.owner.lookup('service:store');
this.owner.register(
'adapter:application',
class extends RESTAdapter {
useFetch = false;
}
);
let adapter = store.adapterFor('application');

let url = 'example.com';
Expand All @@ -278,5 +294,26 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h

assert.equal(ajaxOptions.contentType, 'application/json; charset=utf-8', 'contentType is set with POST');
});

test('ajaxOptions() Content-Type is set with ajax POST with data if useFetch', function(assert) {
let store = this.owner.lookup('service:store');
this.owner.register(
'adapter:application',
class extends RESTAdapter {
useFetch = true;
}
);
let adapter = store.adapterFor('application');

let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, { data: { type: 'post' } });

assert.equal(
ajaxOptions.headers['content-type'],
'application/json; charset=utf-8',
'contentType is set with POST'
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ module('unit/adapters/rest_adapter/group_records_for_find_many_test - DS.RESTAda
requests = [];
lengths = [];

const ApplicationAdapter = RESTAdapter.extend({
coalesceFindRequests: true,
class ApplicationAdapter extends RESTAdapter {
coalesceFindRequests = true;

findRecord(store, type, id, snapshot) {
return { id };
},
}

ajax(url, type, options) {
requests.push({
Expand All @@ -48,8 +48,8 @@ module('unit/adapters/rest_adapter/group_records_for_find_many_test - DS.RESTAda

let testRecords = options.data.ids.map(id => ({ id }));
return EmberPromise.resolve({ testRecords: testRecords });
},
});
}
}

this.owner.register('adapter:application', ApplicationAdapter);
this.owner.register('serializer:application', RESTSerializer.extend());
Expand Down
29 changes: 10 additions & 19 deletions packages/adapter/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import EmberObject from '@ember/object';
@class Adapter
@extends EmberObject
*/
export default EmberObject.extend({
export default class Adapter extends EmberObject {
/**
If you would like your adapter to use a custom serializer you can
set the `defaultSerializer` property to be the name of the custom
Expand All @@ -77,7 +77,7 @@ export default EmberObject.extend({
@property defaultSerializer
@type {String}
*/
defaultSerializer: '-default',
defaultSerializer = '-default';

/**
The `findRecord()` method is invoked when the store is asked for a record that
Expand Down Expand Up @@ -113,7 +113,6 @@ export default EmberObject.extend({
@param {Snapshot} snapshot
@return {Promise} promise
*/
findRecord: null,

/**
The `findAll()` method is used to retrieve all records for a given type.
Expand Down Expand Up @@ -145,7 +144,6 @@ export default EmberObject.extend({
@param {SnapshotRecordArray} snapshotRecordArray
@return {Promise} promise
*/
findAll: null,

/**
This method is called when you call `query` on the store.
Expand Down Expand Up @@ -178,7 +176,6 @@ export default EmberObject.extend({
@param {Object} adapterOptions
@return {Promise} promise
*/
query: null,

/**
The `queryRecord()` method is invoked when the store is asked for a single
Expand Down Expand Up @@ -217,7 +214,6 @@ export default EmberObject.extend({
@param {Object} adapterOptions
@return {Promise} promise
*/
queryRecord: null,

/**
If the globally unique IDs for your records should be generated on the client,
Expand Down Expand Up @@ -251,7 +247,6 @@ export default EmberObject.extend({
newly created record.
@return {(String|Number)} id
*/
generateIdForRecord: null,

/**
Proxies to the serializer's `serialize` method.
Expand All @@ -278,7 +273,7 @@ export default EmberObject.extend({
*/
serialize(snapshot, options) {
return snapshot.serialize(options);
},
}

/**
Implement this method in a subclass to handle the creation of
Expand Down Expand Up @@ -321,7 +316,6 @@ export default EmberObject.extend({
@param {Snapshot} snapshot
@return {Promise} promise
*/
createRecord: null,

/**
Implement this method in a subclass to handle the updating of
Expand Down Expand Up @@ -373,7 +367,6 @@ export default EmberObject.extend({
@param {Snapshot} snapshot
@return {Promise} promise
*/
updateRecord: null,

/**
Implement this method in a subclass to handle the deletion of
Expand Down Expand Up @@ -417,7 +410,6 @@ export default EmberObject.extend({
@param {Snapshot} snapshot
@return {Promise} promise
*/
deleteRecord: null,

/**
By default the store will try to coalesce all `fetchRecord` calls within the same runloop
Expand All @@ -428,7 +420,7 @@ export default EmberObject.extend({
@property coalesceFindRequests
@type {boolean}
*/
coalesceFindRequests: true,
coalesceFindRequests = true;

/**
The store will call `findMany` instead of multiple `findRecord`
Expand Down Expand Up @@ -467,7 +459,6 @@ export default EmberObject.extend({
@param {Array} snapshots
@return {Promise} promise
*/
findMany: null,

/**
Organize records into groups, each of which is to be passed to separate
Expand All @@ -486,7 +477,7 @@ export default EmberObject.extend({
*/
groupRecordsForFindMany(store, snapshots) {
return [snapshots];
},
}

/**
This method is used by the store to determine if the store should
Expand Down Expand Up @@ -536,7 +527,7 @@ export default EmberObject.extend({
*/
shouldReloadRecord(store, snapshot) {
return false;
},
}

/**
This method is used by the store to determine if the store should
Expand Down Expand Up @@ -591,7 +582,7 @@ export default EmberObject.extend({
*/
shouldReloadAll(store, snapshotRecordArray) {
return !snapshotRecordArray.length;
},
}

/**
This method is used by the store to determine if the store should
Expand Down Expand Up @@ -627,7 +618,7 @@ export default EmberObject.extend({
*/
shouldBackgroundReloadRecord(store, snapshot) {
return true;
},
}

/**
This method is used by the store to determine if the store should
Expand Down Expand Up @@ -663,7 +654,7 @@ export default EmberObject.extend({
*/
shouldBackgroundReloadAll(store, snapshotRecordArray) {
return true;
},
});
}
}

export { BuildURLMixin } from './-private';
20 changes: 10 additions & 10 deletions packages/adapter/addon/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ import RESTAdapter from './rest';
@constructor
@extends RESTAdapter
*/
const JSONAPIAdapter = RESTAdapter.extend({
defaultSerializer: '-json-api',
class JSONAPIAdapter extends RESTAdapter {
defaultSerializer = '-json-api';

_defaultContentType: 'application/vnd.api+json',
_defaultContentType = 'application/vnd.api+json';

/**
@method ajaxOptions
Expand All @@ -157,12 +157,12 @@ const JSONAPIAdapter = RESTAdapter.extend({
@return {Object}
*/
ajaxOptions(url, type, options = {}) {
let hash = this._super(url, type, options);
let hash = super.ajaxOptions(url, type, options);

hash.headers['Accept'] = hash.headers['Accept'] || 'application/vnd.api+json';

return hash;
},
}

/**
By default the JSONAPIAdapter will send each find request coming from a `store.find`
Expand Down Expand Up @@ -219,25 +219,25 @@ const JSONAPIAdapter = RESTAdapter.extend({
@property coalesceFindRequests
@type {boolean}
*/
coalesceFindRequests: false,
coalesceFindRequests = false;

findMany(store, type, ids, snapshots) {
let url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
return this.ajax(url, 'GET', { data: { filter: { id: ids.join(',') } } });
},
}

pathForType(modelName) {
let dasherized = dasherize(modelName);
return pluralize(dasherized);
},
}

updateRecord(store, type, snapshot) {
const data = serializeIntoHash(store, type, snapshot);

let url = this.buildURL(type.modelName, snapshot.id, snapshot, 'updateRecord');

return this.ajax(url, 'PATCH', { data: data });
},
});
}
}

export default JSONAPIAdapter;
Loading

0 comments on commit e98f21c

Please sign in to comment.