meilisearch-admin/app/models/key.js
Stanislav Romanov cc0c17a5a3 Add edit form
2022-09-19 13:22:29 +03:00

43 lines
811 B
JavaScript

import query from '../utils/query';
export default class Key {
constructor(data = {}) {
for (const key in data) {
this[key] = data[key];
}
}
get path() {
return `keys/${this.uid}`;
}
get isNew() {
return Boolean(!this.uid);
}
get editablePayload() {
let keys = ['name', 'description'];
if (this.isNew) {
keys = keys.concat(['actions', 'indexes', 'expiresAt', 'uid']);
}
return Object.fromEntries(
Object.entries(this).filter(([key]) => keys.includes(key))
);
}
save(data) {
const path = this.isNew ? 'keys' : this.path;
const method = this.isNew ? 'POST' : 'PATCH';
if (typeof data === 'object') {
data = JSON.stringify(data);
}
return query(path, {
method: method,
body: data,
});
}
}