UNPKG

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.74 kB
import { __awaiter } from "tslib"; import { NamedRepresentationFactory } from './namedRepresentationFactory'; import { TrackedRepresentationFactory } from './trackedRepresentationFactory'; import { RepresentationUtil } from '../utils/representationUtil'; import anylogger from 'anylogger'; import { instanceOfCollection } from '../utils/instanceOf/instanceOfCollection'; import { LinkRelation } from '../linkRelation'; const log = anylogger('get'); /** * Retrieve a resource based on its context and options, and its current state (ie hydrated or not) * * Note: a returned resource will not always be the same (ie self) but rather a different linked resource. * * TODO: where 'named' resources are known, return that type based on the 'rel' in options. */ export function get(resource, options) { return __awaiter(this, void 0, void 0, function* () { const { rel = undefined, where = undefined, } = Object.assign({}, options); const relIsNotSelfOrEmpty = rel && rel !== LinkRelation.Self; // look at the context resource and ensure that it is first hydrated before loading sub resources if (relIsNotSelfOrEmpty) { log.debug('get context resource on \'self\''); resource = yield TrackedRepresentationFactory.load(resource, Object.assign(Object.assign({}, options), { rel: LinkRelation.Self })); } // find specific item in collection if (where) { log.debug('using \'where\' to locate resource on get'); // when combined with rel, the sub resource should be the collection if (relIsNotSelfOrEmpty) { const namedSubResource = yield NamedRepresentationFactory.load(resource, options); if (namedSubResource) { log.debug('named sub resource found on \'%s\'', rel); resource = namedSubResource; // now that sub resource is loaded, re-contextualise to this resource (ie will become 'self') options === null || options === void 0 ? true : delete options.rel; } else { log.warn('named sub resource not found on \'%s\'', rel); } } if (instanceOfCollection(resource)) { log.debug('get collection resource (with items: %s)', (options === null || options === void 0 ? void 0 : options.includeItems) || false); // synchronise collection by applying all current rules (eg includeItems) const collection = yield TrackedRepresentationFactory.load(resource, options); // then check for existence const item = RepresentationUtil.findInCollection(collection, options); if (item) { log.debug('item in collection found'); return yield TrackedRepresentationFactory.load(item, options); } else { log.debug('item in collection not found '); return undefined; } } else { log.warn('Where options cannot be used outside of a collection, skipping where'); // fall through to return context resource } } // named resources // do not add 'self' as sub resource if (relIsNotSelfOrEmpty) { log.debug('get named singleton sub resource'); return yield NamedRepresentationFactory.load(resource, options); } // otherwise all resources log.debug('get resource'); return yield TrackedRepresentationFactory.load(resource, options); }); } //# sourceMappingURL=get.js.map