chore: add support for scope_id in URL prefixes and method in URL suffixes

This commit is contained in:
Randall Morey committed 2020-07-15 16:23:38 -04:00
1 parent 89b736744f
commit 9f48f8c85e
3 files changed
+195 -31

No files matched your search

+35 -1
View File
@@ -1,7 +1,7 @@
import RESTAdapter from '@ember-data/adapter/rest';
import AdapterBuildURLMixin from '../mixins/adapter-build-url';
import config from 'ember-get-config';
import { get } from '@ember/object';
import { get, getWithDefault } from '@ember/object';
import { InvalidError } from '@ember-data/adapter/error';
import { dasherize } from '@ember/string';
import { pluralize } from 'ember-inflector';
@@ -58,6 +58,40 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
// =methods
/**
* Prepends a scope to the URL prefix, if a scope is passed via the
* snapshot's `adapterOptions.scope_id` field.
* @override
* @param {string} path
* @param {string} parentURL
* @param {string} modelName
* @param {string} id
* @param {object} snapshot
* @return {string}
*/
urlPrefix(path, parentURL, modelName, id, snapshot={}) {
const prefix = super.urlPrefix(...arguments);
let scopePath = getWithDefault(snapshot, 'adapterOptions.scope_id', '');
// Ensure a slash is added between prefix + scope path if needed.
if (scopePath && prefix.charAt(prefix.length - 1) !== '/') {
scopePath = `/${scopePath}`;
}
return `${prefix}${scopePath}`;
}
/**
* Appends a custom method after a colon, if a method is passed via the
* snapshot's `adapterOptions.method` field.
* @param {string} modelName
* @param {string} id
* @param {object} snapshot
* @return {string}
*/
urlSuffix(modelName, id, snapshot={}) {
const method = getWithDefault(snapshot, 'adapterOptions.method', '');
return method ? `:${method}` : '';
}
/**
* Transforms the type to a dasherized string used in our API paths.
*
+104 -29
View File
@@ -16,36 +16,35 @@ export default Mixin.create({
// =methods
/**
* This override behaves identically to the default method by default.
* It exists only to document the additional paramters to the method.
* @override
* @param {string} path
* @param {string} parentURL
* @param {string} modelName
* @param {string} id
* @param {object} snapshot
* @return {string} urlPrefix
*/
urlPrefix(/* path, parentURL, modelName, id, snapshot */) {
// What's this super syntax?
// Ember mixins are still based on the older Ember Object, rather than
// native ES classes. Thus we have to call super in this strange way.
// https://blog.emberjs.com/2019/01/26/emberjs-native-class-update-2019-edition.html
return this._super(...arguments);
},
/**
* This override behaves identically to the default method by default.
* It exists only to document the additional paramters to the method.
* @override
* @param {string} path
* @param {string} parentURL
* @param {string} modelName
* @param {string} id
* @param {object} snapshot
* @return {string} urlPrefix
*/
urlPrefix(/* path, parentURL, modelName, id, snapshot */) {
// What's this super syntax?
// Ember mixins are still based on the older Ember Object, rather than
// native ES classes. Thus we have to call super in this strange way.
// https://blog.emberjs.com/2019/01/26/emberjs-native-class-update-2019-edition.html
return this._super(...arguments);
},
/**
* Generates a string to be appended to a URL. Empty string by default.
* @param {string} modelName
* @param {string} id
* @param {?object} snapshot
* @return {string}
*/
urlSuffix(/* modelName, id, snapshot */) {
return '';
},
/**
* Generates a string to be appended to a URL. Empty string by default.
* @param {string} modelName
* @param {string} id
* @param {?object} snapshot
* @return {string}
*/
urlSuffix(/* modelName, id, snapshot */) {
return '';
},
/**
* Overrides the default `_buildURL` with a nearly identical version.
@@ -73,6 +72,82 @@ export default Mixin.create({
url = url.join('/');
if (!host && url && url.charAt(0) !== '/') url = '/' + url;
return `${url}${suffix}`;
},
/**
* Overrides the default method simply to pass an additional argument
* `snapshot` to the `_buildURL` method.
* @override
* @param {string} id
* @param {string} modelName
* @param {object} snapshot
* @return {string}
*/
urlForFindRecord(id, modelName, snapshot) {
return this._buildURL(modelName, id, snapshot);
},
/**
* Overrides the default method simply to pass an additional argument
* `snapshot` to the `_buildURL` method.
* @override
* @param {string} modelName
* @param {object} snapshot
* @return {string}
*/
urlForFindAll(modelName, snapshot) {
return this._buildURL(modelName, null, snapshot);
},
/**
* Overrides the default method simply to pass an additional argument
* `snapshot` to the `_buildURL` method.
* @override
* @param {string} id
* @param {string} modelName
* @param {object} snapshot
* @return {string}
*/
urlForFindBelongsTo(id, modelName, snapshot) {
return this._buildURL(modelName, id, snapshot);
},
/**
* Overrides the default method simply to pass an additional argument
* `snapshot` to the `_buildURL` method.
* @override
* @param {string} modelName
* @param {object} snapshot
* @return {string}
*/
urlForCreateRecord(modelName, snapshot) {
return this._buildURL(modelName, null, snapshot);
},
/**
* Overrides the default method simply to pass an additional argument
* `snapshot` to the `_buildURL` method.
* @override
* @param {string} id
* @param {string} modelName
* @param {object} snapshot
* @return {string}
*/
urlForUpdateRecord(id, modelName, snapshot) {
return this._buildURL(modelName, id, snapshot);
},
/**
* Overrides the default method simply to pass an additional argument
* `snapshot` to the `_buildURL` method.
* @override
* @param {string} id
* @param {string} modelName
* @param {object} snapshot
* @return {string}
*/
urlForDeleteRecord(id, modelName, snapshot) {
return this._buildURL(modelName, id, snapshot);
}
});