open-vault/ui/app/adapters/transform/base.js
Chelsea Shaw 41bf752080
Ui/transform templates (#9981)
Add CRUD capabilities on transform templates. Disallow read or edit for built-ins.
2020-09-17 14:08:06 -05:00

66 lines
1.7 KiB
JavaScript

import ApplicationAdapter from '../application';
import { encodePath } from 'vault/utils/path-encoding-helpers';
export default ApplicationAdapter.extend({
namespace: 'v1',
pathForType(type) {
return type.replace('transform/', '');
},
createOrUpdate(store, type, snapshot) {
const serializer = store.serializerFor(type.modelName);
const data = serializer.serialize(snapshot);
const { id } = snapshot;
let url = this.url(snapshot.record.get('backend'), type.modelName, id);
return this.ajax(url, 'POST', { data });
},
createRecord() {
return this.createOrUpdate(...arguments);
},
updateRecord() {
return this.createOrUpdate(...arguments, 'update');
},
deleteRecord(store, type, snapshot) {
const { id } = snapshot;
return this.ajax(this.url(snapshot.record.get('backend'), type.modelName, id), 'DELETE');
},
url(backend, modelType, id) {
let type = this.pathForType(modelType);
let url = `/${this.namespace}/${encodePath(backend)}/${encodePath(type)}`;
if (id) {
return `${url}/${encodePath(id)}`;
}
return url + '?list=true';
},
fetchByQuery(query) {
const { backend, modelName, id } = query;
return this.ajax(this.url(backend, modelName, id), 'GET').then(resp => {
return {
...resp,
backend,
};
});
},
query(store, type, query) {
return this.fetchByQuery(query);
},
queryRecord(store, type, query) {
return this.ajax(this.url(query.backend, type.modelName, query.id), 'GET').then(result => {
// CBS TODO: Add name to response and unmap name <> id on models
return {
id: query.id,
...result,
};
});
},
});