Skip to content

Commit

Permalink
Run jarn lint:js:fix with plugin:prettier/recommended
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed Sep 4, 2022
1 parent 9dbfc9c commit 25754df
Show file tree
Hide file tree
Showing 59 changed files with 1,098 additions and 1,002 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:ember/recommended',
// 'plugin:prettier/recommended',
'plugin:prettier/recommended',
],
env: {
browser: true
browser: true,
},
rules: {
'ember/avoid-leaking-state-in-ember-objects': 'off',
Expand Down
123 changes: 59 additions & 64 deletions addon/adapters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { run } from '@ember/runloop';
import { isEmpty, typeOf } from '@ember/utils';
import { get } from '@ember/object';
import DS from 'ember-data';
import { importData, exportData } from 'ember-local-storage/helpers/import-export';
import {
importData,
exportData,
} from 'ember-local-storage/helpers/import-export';
import { _buildKey } from 'ember-local-storage/helpers/storage';

const getKeys = Object.keys || keys;

const {
JSONAPIAdapter
} = DS;
const { JSONAPIAdapter } = DS;

// Ember data ships with ember-inflector
import { singularize, pluralize } from 'ember-inflector';
Expand All @@ -31,10 +32,18 @@ export default class BaseAdapter extends JSONAPIAdapter {

// 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);
Expand All @@ -52,48 +61,39 @@ export default class BaseAdapter extends JSONAPIAdapter {

// Relationship sugar
createRecord(store, type, snapshot) {
snapshot.eachRelationship(function(name, relationship) {
const {
kind,
options
} = relationship;
snapshot.eachRelationship(function (name, relationship) {
const { kind, options } = relationship;

if (kind === 'belongsTo' && options.autoSave) {
snapshot.record.get(name)
.then(function(record) {
if (record) {
record.save();
}
});
snapshot.record.get(name).then(function (record) {
if (record) {
record.save();
}
});
}
});

return super.createRecord(...arguments);
}

deleteRecord(store, type, snapshot) {
snapshot.eachRelationship(function(name, relationship) {
const {
kind,
options
} = relationship;
snapshot.eachRelationship(function (name, relationship) {
const { kind, options } = relationship;

if (kind === 'hasMany' && options.dependent === 'destroy') {
snapshot.record.get(name)
.then(function(records) {
records.forEach(function(record) {
record.destroyRecord();
});
snapshot.record.get(name).then(function (records) {
records.forEach(function (record) {
record.destroyRecord();
});
});
}

if (kind === 'belongsTo' && options.autoSave) {
snapshot.record.get(name)
.then(function(record) {
if (record) {
record.save();
}
});
snapshot.record.get(name).then(function (record) {
if (record) {
record.save();
}
});
}
});

Expand All @@ -115,10 +115,9 @@ export default class BaseAdapter extends JSONAPIAdapter {
records = this.ajax(url, 'GET', { data: query });
}

return records
.then(function(result) {
return {data: result.data[0] || null};
});
return records.then(function (result) {
return { data: result.data[0] || null };
});
}

// TODO: v2.0 - What are the defaults now? What versions to support?
Expand All @@ -129,11 +128,9 @@ export default class BaseAdapter extends JSONAPIAdapter {

// Delegate to _handleStorageRequest
makeRequest(request) {
return this._handleStorageRequest(
request.url,
request.method,
{ data: request.data }
);
return this._handleStorageRequest(request.url, request.method, {
data: request.data,
});
}

// Work arround ds-improved-ajax Feature Flag
Expand All @@ -156,13 +153,9 @@ export default class BaseAdapter extends JSONAPIAdapter {
const handler = this[`_handle${type}Request`];
if (handler) {
const data = handler.call(this, url, options.data);
run(null, resolve, {data: data});
run(null, resolve, { data: data });
} else {
run(
null,
reject,
`There is nothing to handle _handle${type}Request`
);
run(null, reject, `There is nothing to handle _handle${type}Request`);
}
}, 'DS: LocalStorageAdapter#_handleStorageRequest ' + type + ' to ' + url);
}
Expand All @@ -174,16 +167,16 @@ export default class BaseAdapter extends JSONAPIAdapter {

if (id) {
if (!storage[storageKey]) {
throw this.handleResponse(404, {}, "Not found", { url, method: 'GET' });
throw this.handleResponse(404, {}, 'Not found', { url, method: 'GET' });
}
return JSON.parse(storage[storageKey]);
}

const records = this._getIndex(type)
.filter(function(storageKey) {
.filter(function (storageKey) {
return storage[storageKey];
})
.map(function(storageKey) {
.map(function (storageKey) {
return JSON.parse(storage[storageKey]);
});

Expand Down Expand Up @@ -272,18 +265,21 @@ export default class BaseAdapter extends JSONAPIAdapter {
} else if (queryType === 'array') {
// belongsTo
if (dataType === 'object') {
const queryMessage = query.map(function(item) {
return getKeys(item).map(function(key) {
return key + ': ' + item[key];
});
}).join(', ');
const queryMessage = query
.map(function (item) {
return getKeys(item).map(function (key) {
return key + ': ' + item[key];
});
})
.join(', ');

throw new Error(
'You can not provide an array with a belongsTo relation. ' +
'Query: ' + queryMessage
'Query: ' +
queryMessage
);

// hasMany
// hasMany
} else {
return query.every((queryValue) => {
return this._queryFilter(data, serializer, queryValue);
Expand All @@ -294,7 +290,7 @@ export default class BaseAdapter extends JSONAPIAdapter {
if (dataType === 'object') {
return this._matches(data.id, query);

// hasMany
// hasMany
} else {
return data.some((record) => {
return this._queryFilter(record, serializer, query);
Expand Down Expand Up @@ -328,7 +324,7 @@ export default class BaseAdapter extends JSONAPIAdapter {

return {
type: type,
id: id
id: id,
};
}

Expand All @@ -338,8 +334,7 @@ export default class BaseAdapter extends JSONAPIAdapter {

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

_indexHasKey(type, id) {
return this._getIndex(type).indexOf(id) !== -1;
Expand Down
4 changes: 1 addition & 3 deletions addon/adapters/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export default class LocalStorageAdapter extends BaseAdapter {
if (!indices[type]) {
let storageKey = _buildKey(this, 'index-' + type);

indices[type] = StorageArray
.extend({ _storageKey: storageKey })
.create();
indices[type] = StorageArray.extend({ _storageKey: storageKey }).create();
}

return indices[type];
Expand Down
4 changes: 1 addition & 3 deletions addon/adapters/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export default class SessionStorageAdapter extends BaseAdapter {
if (!indices[type]) {
let storageKey = _buildKey(this, 'index-' + type);

indices[type] = StorageArray
.extend({ _storageKey: storageKey })
.create();
indices[type] = StorageArray.extend({ _storageKey: storageKey }).create();
}

return indices[type];
Expand Down
61 changes: 35 additions & 26 deletions addon/helpers/import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import { A } from '@ember/array';

export function importData(store, content, options) {
// merge defaults
options = Object.assign({
json: true,
truncate: true
}, options || {});
options = Object.assign(
{
json: true,
truncate: true,
},
options || {}
);

let truncateTypes = A(), reloadTypes = A();
let truncateTypes = A(),
reloadTypes = A();

content = options.json ? JSON.parse(content) : content;

Expand Down Expand Up @@ -42,38 +46,43 @@ export function importData(store, content, options) {
reloadTypes.addObject(singularize(record.type));

return adapter._handleStorageRequest(null, 'POST', {
data: {data: record}
data: { data: record },
});
});

return all(promises)
.then(function() {
// reload from store
reloadTypes.forEach(function(type) {
store.findAll(type);
});
return all(promises).then(function () {
// reload from store
reloadTypes.forEach(function (type) {
store.findAll(type);
});
});
}

export function exportData(store, types, options) {
// merge defaults
options = Object.assign({
json: true,
download: false,
filename: 'ember-data.json'
}, options || {});
options = Object.assign(
{
json: true,
download: false,
filename: 'ember-data.json',
},
options || {}
);

let json, data;

// collect data
data = types.reduce((records, type) => {
const adapter = store.adapterFor(singularize(type));
const url = adapter.buildURL(type);
const exportData = adapter._handleGETRequest(url);

records.data = records.data.concat(exportData);
return records;
}, {data: []});
data = types.reduce(
(records, type) => {
const adapter = store.adapterFor(singularize(type));
const url = adapter.buildURL(type);
const exportData = adapter._handleGETRequest(url);

records.data = records.data.concat(exportData);
return records;
},
{ data: [] }
);

if (options.json || options.download) {
json = JSON.stringify(data);
Expand All @@ -85,7 +94,7 @@ export function exportData(store, types, options) {

if (options.download) {
window.saveAs(
new Blob([json], {type: 'application/json;charset=utf-8'}),
new Blob([json], { type: 'application/json;charset=utf-8' }),
options.filename
);
}
Expand Down
Loading

0 comments on commit 25754df

Please sign in to comment.