semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
82 lines • 4.46 kB
JavaScript
import { __awaiter } from "tslib";
import { LinkUtil } from 'semantic-link';
import { LinkRelConvertUtil } from '../utils/linkRelConvertUtil';
import { LinkRelation } from '../linkRelation';
import { TrackedRepresentationUtil } from '../utils/trackedRepresentationUtil';
import { TrackedRepresentationFactory } from './trackedRepresentationFactory';
import anylogger from 'anylogger';
import { SparseRepresentationFactory } from './sparseRepresentationFactory';
import { RepresentationUtil } from '../utils/representationUtil';
const log = anylogger('NamedRepresentationFactory');
/**
* Where the rel is multiple pick the first matched link to be converted as the name, otherwise use the given rel
* @param rel
* @param representation
*/
export const firstLinkNameStrategy = (includeTitle) => (rel, representation) => {
if (representation && Array.isArray(rel)) {
const [first] = LinkUtil.filter(representation, rel);
if (first) {
return LinkRelConvertUtil.relTypeToCamel(first, includeTitle);
}
else {
// broken and should not fall through
// what is the return strategy on link not found?
log.error('Rel not found on representation');
return '';
}
}
else {
return LinkRelConvertUtil.relTypeToCamel(rel, includeTitle);
}
};
export class NamedRepresentationFactory {
/**
* Manages the loading (returning) of a named resource (sub-resource collection or singleton) on a context based on
* the {@link ResourceQueryOptions.rel}.
*
* Note: the naming strategy is currently not injectable but the value can be overridden {@link ResourceQueryOptions.name}
* @see LinkRelConvertUtil.relTypeToCamel
*
* @see TrackedRepresentationFactory
* @param resource context resource that has the sub-resource added (and is tracked {@link State.collection} and {@link State.singleton})
* @param options specify the {@link ResourceQueryOptions.rel} to pick the name resource
*/
static load(resource, options) {
return __awaiter(this, void 0, void 0, function* () {
const { rel = undefined, useLegacyNameStrategy = false, } = Object.assign({}, options);
const { nameStrategy = useLegacyNameStrategy ? firstLinkNameStrategy(false) : NamedRepresentationFactory.defaultNameStrategy, } = Object.assign({}, options);
const { name = nameStrategy(rel, resource), } = Object.assign({}, options);
if (rel && name) {
if (TrackedRepresentationUtil.isTracked(resource, name)) {
const namedResource = RepresentationUtil.getProperty(resource, name);
if (namedResource) {
log.debug('');
// don't just return value but ensure it has loading rules respected (eg expires)
return yield TrackedRepresentationFactory.load(namedResource, Object.assign(Object.assign({}, options), { rel: LinkRelation.Self }));
} // else fall through to undefined
// if the resource is tracked it is very unlikely that this resource doesn't exist
log.warn('Named resource \'%s\' on %s is undefined', name, LinkUtil.getUri(resource, LinkRelation.Self));
}
else {
const uri = LinkUtil.getUri(resource, rel);
if (uri) {
const sparse = SparseRepresentationFactory.make(Object.assign(Object.assign({}, options), { uri }));
const namedResource = yield TrackedRepresentationFactory.load(sparse, Object.assign(Object.assign({}, options), { rel: LinkRelation.Self }));
if (namedResource) {
TrackedRepresentationUtil.add(resource, name, namedResource, options);
}
return namedResource;
} // else fall through to undefined
}
}
else {
log.warn('Named resource \'%s\' not found on %s', name, LinkUtil.getUri(resource, LinkRelation.Self));
return Promise.reject('No named resource (or rel) specified');
}
return undefined;
});
}
}
NamedRepresentationFactory.defaultNameStrategy = firstLinkNameStrategy(true);
//# sourceMappingURL=namedRepresentationFactory.js.map