semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
72 lines • 3.56 kB
JavaScript
import { __awaiter } from "tslib";
import { instanceOfUriList } from '../utils/instanceOf/instanceOfUriList';
import { LinkUtil } from 'semantic-link';
import anylogger from 'anylogger';
import { LinkRelation } from '../linkRelation';
import { defaultEditFormStrategy } from './editFormMergeStrategy';
import { get } from './get';
import { TrackedRepresentationFactory } from './trackedRepresentationFactory';
import { instanceOfForm } from '../utils/instanceOf/instanceOfForm';
import { instanceOfCollection } from '../utils/instanceOf/instanceOfCollection';
const log = anylogger('Update');
/**
* Update on existing resource
* TODO: accept but don't require TrackedRepresentation interface
* TODO: always returns resource but hard to know if error. Either throw or return undefined
* Note: underlying TrackedRepresentationFactory.update has this strategy
*/
export function update(resource, document, options) {
return __awaiter(this, void 0, void 0, function* () {
// PATCH
if (instanceOfCollection(resource)) {
if (!instanceOfUriList(document)) {
log.debug(resource, document, options);
throw new Error('To update a collection, a document of type UriList must be supplied');
}
throw new Error('Update collection not implemented');
}
// PUT
// update a single resource
return updateSingleton(resource, document, options);
});
}
/**
* Update a singleton.
*
* If the singleton has a form relation (default: edit-form) then construction the across-the-wire
* PUT representation from only the form attributes. Otherwise, take the incoming documentation as given.
* @throws
*/
function updateSingleton(resource, document, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!document) {
log.debug('No document provided to update for resource %s', LinkUtil.getUri(resource, LinkRelation.Self));
return resource;
}
const { formRel = LinkRelation.EditForm } = Object.assign({}, options);
if (LinkUtil.matches(resource, formRel)) {
const form = yield get(resource, Object.assign(Object.assign({}, options), { rel: formRel }));
if (instanceOfForm(form)) {
const { makePutRepresentationStrategy = defaultEditFormStrategy } = Object.assign({}, options);
const representation = yield makePutRepresentationStrategy(resource, document, form, options);
if (representation) {
// WARNING: update has under-baked error reporting and does not throw errors
yield TrackedRepresentationFactory.update(resource, representation, options);
}
else {
log.debug('No update required based on form for \'%s\'', LinkUtil.getUri(resource, LinkRelation.Self));
}
}
else {
log.warn('Update link \'%s\' on \'%s\' is not a form - no update made', formRel, LinkUtil.getUri(resource, LinkRelation.Self));
}
}
else {
log.debug('Update without form - resource has no \'%s\' form on \'%s\'', formRel, LinkUtil.getUri(resource, LinkRelation.Self));
// WARNING: update has under-baked error reporting and does not throw errors
yield TrackedRepresentationFactory.update(resource, document, options);
}
return resource;
});
}
//# sourceMappingURL=update.js.map