UNPKG

node-fractal

Version:

Output complex, flexible, AJAX/RESTful data structures.

75 lines 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransformerAbstract = void 0; const lodash_1 = require("lodash"); const _1 = require("."); class TransformerAbstract { constructor() { this.availableIncludes = []; this.defaultIncludes = []; this.currentScope = null; } getAvailableIncludes() { return this.availableIncludes; } setAvailableIncludes(availableIncludes) { this.availableIncludes = availableIncludes; return this; } getDefaultIncludes() { return this.defaultIncludes; } setDefaultIncludes(defaultIncludes) { this.defaultIncludes = defaultIncludes; return this; } getCurrentScope() { return this.currentScope; } setCurrentScope(currentScope) { this.currentScope = currentScope; return this; } processIncludedResources(scope, data) { let includedData = {}; const includes = this.figureOutWhichIncludes(scope); for (let include of includes) { includedData = Object.assign(Object.assign({}, this.includeResourceIfAvailable(scope, data, include)), includedData); } return includedData; } item(data, transformer, resourceKey = null) { return new _1.ItemResource(data, transformer, resourceKey); } collection(data, transformer, resourceKey = null) { return new _1.CollectionResource(data, transformer, resourceKey); } figureOutWhichIncludes(scope) { const includes = this.getDefaultIncludes(); return includes; } includeResourceIfAvailable(scope, data, include) { const resource = this.callIncludeMethod(scope, include, data); if (resource) { const childScope = scope.embedChildScope(include, resource); return { [include]: childScope.toObject(), }; } return {}; } callIncludeMethod(scope, includeName, data) { const scopeIdentifier = scope.getIdentifier(includeName); const methodName = `include${(0, lodash_1.upperFirst)((0, lodash_1.camelCase)(includeName))}`; if (typeof this[methodName] !== 'function') { return false; } const resource = this[methodName](data); if (!(resource instanceof _1.ResourceAbstract)) { throw new Error(`Invalid return value from ${this.constructor.name}.${methodName}. Expected ResourceInterface.`); } return resource; } } exports.TransformerAbstract = TransformerAbstract; //# sourceMappingURL=transformer.js.map