UNPKG

type-transformer

Version:

Transformation / serialization / deserialization of plain JavaScript objects to typed objects and vice versa

2 lines (1 loc) 14.3 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var extendStatics=function(e,t){return(extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function __extends(e,t){function r(){this.constructor=e}extendStatics(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function __generator(e,t){var r,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=(i=a.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}function __values(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}}function __read(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}!function(e){e.PLAIN_TO_TYPED="PLAIN_TO_TYPED",e.TYPED_TO_PLAIN="TYPED_TO_PLAIN"}(exports.Direction||(exports.Direction={})),function(e){e.EXPOSE="EXPOSE",e.EXCLUDE="EXCLUDE"}(exports.Visibility||(exports.Visibility={}));var Selectable=function(){function e(){this.selectors=[]}return e.prototype.addSelector=function(e,t){this.selectors.push([e,t])},e.prototype.selected=function(e,t){var r,n,i=this.defaultVisibility;try{for(var o=__values(this.selectors),a=o.next();!a.done;a=o.next()){var s=__read(a.value,2),c=s[0],l=s[1];if(!l||this.doesSelectorApply(e,t,l)){if(!c(e,t,l))return exports.Visibility.EXCLUDE;i=exports.Visibility.EXPOSE}}}catch(e){r={error:e}}finally{try{a&&!a.done&&(n=o.return)&&n.call(o)}finally{if(r)throw r.error}}return i},e.prototype.doesSelectorApply=function(e,t,r){return(!r.toPlainOnly||t===exports.Direction.TYPED_TO_PLAIN)&&(!r.toTypedOnly||t===exports.Direction.PLAIN_TO_TYPED)},e}(),AlwaysExpose=function(){return!0},AlwaysExclude=function(){return!1},Schema=function(){function e(e,t){this.registry=e,this.type=t,this.inherits=!1,this._fields=new Map,this._postProcessors=[]}return e.prototype.getField=function(e){var t=this._fields.get(e);return t||(t=new Field(e),this._fields.set(e,t)),t},Object.defineProperty(e.prototype,"defaultVisibility",{get:function(){if(this._defaultVisibility)return this._defaultVisibility;if(this.inherits){var e=this.getAncestor();if(e)return e.defaultVisibility}},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"fields",{get:function(){var e,t,r,n;if(!this.inherits)return this._fields.values();var i=new Map;try{for(var o=__values(this.inheritanceChain()),a=o.next();!a.done;a=o.next()){var s=a.value;try{for(var c=__values(s._fields.values()),l=c.next();!l.done;l=c.next()){var u=l.value;i.has(u.name)||i.set(u.name,u)}}catch(e){r={error:e}}finally{try{l&&!l.done&&(n=c.return)&&n.call(c)}finally{if(r)throw r.error}}}}catch(t){e={error:t}}finally{try{a&&!a.done&&(t=o.return)&&t.call(o)}finally{if(e)throw e.error}}return i.values()},enumerable:!0,configurable:!0}),e.prototype.excludeAll=function(){return this._defaultVisibility=exports.Visibility.EXCLUDE,this},e.prototype.exposeAll=function(){return this._defaultVisibility=exports.Visibility.EXPOSE,this},e.prototype.inherit=function(){return this.inherits=!0,this},e.prototype.postProcess=function(e,t){return this._postProcessors.push(new ObjectTransformer(e,t)),this},e.prototype.getPostProcessors=function(e,t){return this._postProcessors.filter(function(r){var n=r.selected(e,t);return!n||n===exports.Visibility.EXPOSE})},e.prototype.inheritanceChain=function(){var e;return __generator(this,function(t){switch(t.label){case 0:e=this,t.label=1;case 1:return[4,e];case 2:t.sent(),t.label=3;case 3:if(e=e.getAncestor())return[3,1];t.label=4;case 4:return[2]}})},e.prototype.getAncestor=function(){for(var e,t=this.type;Object.prototype!==(t=Object.getPrototypeOf(t));)if(e=this.registry.getSchema(t))return e},e}(),Field=function(e){function t(t){var r=e.call(this)||this;return r.name=t,r._transformers=[],r}return __extends(t,e),t.prototype.select=function(e){return this.addSelector(e),this},t.prototype.exclude=function(e){return this.addSelector(AlwaysExclude,e),this},t.prototype.expose=function(e){return this.addSelector(AlwaysExpose,e),e&&(this._propName=e.propName),this},t.prototype.nested=function(e){return this.nestedType=e,this},t.prototype.transform=function(e,t){var r=new ValueTransformer(e,t);return this._transformers.push(r),this},t.prototype.getValueTransformers=function(e,t){return this._transformers.filter(function(r){var n=r.selected(e,t);return!n||n===exports.Visibility.EXPOSE})},Object.defineProperty(t.prototype,"propName",{get:function(){return this._propName||this.name},enumerable:!0,configurable:!0}),t}(Selectable),Transformer=function(e){function t(t,r){var n=e.call(this)||this;return n.transformFn=t,r&&(n.addSelector(AlwaysExpose,r),n.defaultVisibility=exports.Visibility.EXCLUDE),n}return __extends(t,e),t.prototype.transform=function(e,t,r){return this.transformFn(e,t,r)},t}(Selectable),ValueTransformer=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return __extends(t,e),t}(Transformer),ObjectTransformer=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return __extends(t,e),t}(Transformer),SchemaRegistry=function(){function e(){this.schemasByType=new Map}return e.prototype.getOrCreateSchema=function(e){var t=this.schemasByType.get(e);return t||(t=new Schema(this,e),this.schemasByType.set(e,t)),t},e.prototype.getSchema=function(e){return this.schemasByType.get(e)},e.prototype.clear=function(){this.schemasByType.clear()},e}(),defaultSchemaRegistry=new SchemaRegistry;!function(e){e.NONE="NONE",e.BREAK="BREAK",e.RESOLVE="RESOLVE",e.ERROR="ERROR"}(exports.CircularRefStrategy||(exports.CircularRefStrategy={}));var ReferenceNode=function(){function e(e,t){this.object=e,this.referencedBy=new Map,t&&this.referencedBy.set(t.object,t)}return e.prototype.isReferencedBy=function(e){var t,r;if(this.referencedBy.has(e))return!0;try{for(var n=__values(this.referencedBy.values()),i=n.next();!i.done;i=n.next()){if(i.value.isReferencedBy(e))return!0}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}return!1},e.prototype.getReferencing=function(e){var t,r;if(this.referencedBy.has(e))return this.referencedBy.get(e).targetObject;try{for(var n=__values(this.referencedBy.values()),i=n.next();!i.done;i=n.next()){var o=i.value.getReferencing(e);if(o)return o}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}},e.prototype.addReferencer=function(e){this.referencedBy.set(e.object,e)},e}(),ReferenceGraph=function(){function e(e){this.nodeStack=[],this.allNodes=new Map,this.circularStrategy=exports.CircularRefStrategy.BREAK,e&&(this.circularStrategy=e)}return Object.defineProperty(e.prototype,"current",{get:function(){if(this.nodeStack.length)return this.nodeStack[this.nodeStack.length-1]},enumerable:!0,configurable:!0}),e.prototype.preventCircularDependencies=function(e,t,r){if(this.circularStrategy!==exports.CircularRefStrategy.NONE&&this.isCircularReference(e))return this.handleCircularReference(e);this.descend(e).targetObject=t;var n=r();return this.ascend(),n},e.prototype.descend=function(e){var t=this.current,r=this.allNodes.get(e);return r&&t?r.addReferencer(t):(r=new ReferenceNode(e,t),this.allNodes.set(e,r)),this.nodeStack.push(r),r},e.prototype.ascend=function(){this.nodeStack.pop()},e.prototype.isCircularReference=function(e){return!!this.allNodes.has(e)&&this.current.isReferencedBy(e)},e.prototype.handleCircularReference=function(e){switch(this.circularStrategy){case exports.CircularRefStrategy.BREAK:return;case exports.CircularRefStrategy.ERROR:throw new Error("A circular reference was detected.");case exports.CircularRefStrategy.RESOLVE:return this.current.getReferencing(e);default:throw new Error("Unkown strategy for handling circular references: "+this.circularStrategy)}},e}(),Transformer$1=function(){function e(e){this.options=e,this.referenceGraph=new ReferenceGraph(e.circularRefStrategy)}return e.prototype.toPlain=function(e,t,r){var n=this,i=e;if("object"==typeof i&&null!==i){i.toJS&&(i=i.toJS());var o=__read(this.getTargetObject(i,void 0,r),2),a=o[0],s=o[1];t||i.constructor===Object||(t=i.constructor),i=this.referenceGraph.preventCircularDependencies(i,s,function(){var e,r,o,c;if(a)try{for(var l=__values(i),u=l.next();!u.done;u=l.next()){var f=u.value;f=n.toPlain(f),s.push(f)}}catch(t){e={error:t}}finally{try{u&&!u.done&&(r=l.return)&&r.call(l)}finally{if(e)throw e.error}}else{var p=n.getFields(exports.Direction.TYPED_TO_PLAIN,i,t);try{for(var y=__values(p),h=y.next();!h.done;h=y.next()){var d=__read(h.value,2),v=d[0],_=d[1];n.applyFieldToPlain(i,s,v,_)}}catch(e){o={error:e}}finally{try{h&&!h.done&&(c=y.return)&&c.call(y)}finally{if(o)throw o.error}}}return s})}var c=this.getSchema(t);return i=this.postProcessObject(e,i,exports.Direction.TYPED_TO_PLAIN,c)},e.prototype.applyFieldToPlain=function(e,t,r,n){var i,o,a=r,s=e[r];if(void 0!==s||!this.options.skipUndefinedProperties){if("function"==typeof s&&(s=e[r]()),n){a=n.propName instanceof Function?n.propName(e,exports.Direction.TYPED_TO_PLAIN):n.propName;var c=n.getValueTransformers(e,exports.Direction.TYPED_TO_PLAIN);try{for(var l=__values(c),u=l.next();!u.done;u=l.next()){s=u.value.transform(s,e,exports.Direction.TYPED_TO_PLAIN)}}catch(e){i={error:e}}finally{try{u&&!u.done&&(o=l.return)&&o.call(l)}finally{if(i)throw i.error}}}return s=this.toPlain(s),t[a]=s,t}},e.prototype.toTyped=function(e,t,r){var n=this,i=e;if("object"==typeof i&&null!==i){var o=__read(this.getTargetObject(i,t,r),2),a=o[0],s=o[1];return this.referenceGraph.preventCircularDependencies(i,s,function(){var e,r,o,c;if(a)try{for(var l=__values(i),u=l.next();!u.done;u=l.next()){var f=u.value;f=n.toTyped(f,t),s.push(f)}}catch(t){e={error:t}}finally{try{u&&!u.done&&(r=l.return)&&r.call(l)}finally{if(e)throw e.error}}else{var p=n.getFields(exports.Direction.PLAIN_TO_TYPED,i,t);try{for(var y=__values(p),h=y.next();!h.done;h=y.next()){var d=__read(h.value,2),v=d[0],_=d[1];s=n.applyFieldToTyped(i,s,v,_)}}catch(e){o={error:e}}finally{try{h&&!h.done&&(c=y.return)&&c.call(y)}finally{if(o)throw o.error}}}return s})}var c=this.getSchema(t);return i=this.postProcessObject(e,i,exports.Direction.PLAIN_TO_TYPED,c)},e.prototype.applyFieldToTyped=function(e,t,r,n){var i,o,a,s=r;n&&(s=n.propName instanceof Function?n.propName(e,exports.Direction.TYPED_TO_PLAIN):n.propName,a=n.nestedType);var c=e[s];if(n){var l=n.getValueTransformers(e,exports.Direction.PLAIN_TO_TYPED);try{for(var u=__values(l),f=u.next();!f.done;f=u.next()){c=f.value.transform(c,e,exports.Direction.PLAIN_TO_TYPED)}}catch(e){i={error:e}}finally{try{f&&!f.done&&(o=u.return)&&o.call(u)}finally{if(i)throw i.error}}}return c=this.toTyped(c,a),t.set&&"Map"===t.constructor.name?t=t.set(r,c):t[r]=c,t},e.prototype.postProcessObject=function(e,t,r,n){var i,o;if(n){var a=n.getPostProcessors(e,r);try{for(var s=__values(a),c=s.next();!c.done;c=s.next()){t=c.value.transform(e,t,r)}}catch(e){i={error:e}}finally{try{c&&!c.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}return t},e.prototype.getFields=function(e,t,r){var n,i,o=exports.Visibility.EXPOSE,a=this.getSchema(r);a&&a.defaultVisibility&&(o=a.defaultVisibility);var s=new Map;if(o===exports.Visibility.EXPOSE&&Object.keys(t).forEach(function(e){return s.set(e,void 0)}),a)try{for(var c=__values(a.fields),l=c.next();!l.done;l=c.next()){var u=l.value,f=u.selected(t,e)||o;f===exports.Visibility.EXPOSE?s.set(u.name,u):f===exports.Visibility.EXCLUDE&&s.delete(u.name)}}catch(e){n={error:e}}finally{try{l&&!l.done&&(i=c.return)&&i.call(c)}finally{if(n)throw n.error}}return s},e.prototype.getSchema=function(e){return e?this.options.registry.getSchema(e):void 0},e.prototype.getTargetObject=function(e,t,r){var n=e instanceof Array||e instanceof Set;return[n,r||(n?[]:t?new t:{})]},e}();function expose(e){return function(t,r){if(t instanceof Function)defaultSchemaRegistry.getOrCreateSchema(t).exposeAll();else if(r){defaultSchemaRegistry.getOrCreateSchema(t.constructor).getField(r).expose(e)}}}function exclude(){return function(e,t){if(e instanceof Function)defaultSchemaRegistry.getOrCreateSchema(e).excludeAll();else if(t){defaultSchemaRegistry.getOrCreateSchema(e.constructor).getField(t).exclude()}}}function select(e){return function(t,r){if(!r)throw'Cannot use "@select" on a class.';defaultSchemaRegistry.getOrCreateSchema(t.constructor).getField(r).select(e)}}function inherit(){return function(e){defaultSchemaRegistry.getOrCreateSchema(e).inherit()}}function createRegistry(){return new SchemaRegistry}function createSchema(e,t){return(t=t||defaultSchemaRegistry).getOrCreateSchema(e)}function typedToPlain(e,t,r){return new Transformer$1(getOptions(t)).toPlain(e,void 0,r)}function plainToTyped(e,t,r,n){return new Transformer$1(getOptions(r)).toTyped(e,t,n)}function getOptions(e){return(e=e||{}).registry||(e.registry=defaultSchemaRegistry),e}exports.createRegistry=createRegistry,exports.createSchema=createSchema,exports.typedToPlain=typedToPlain,exports.plainToTyped=plainToTyped,exports.expose=expose,exports.exclude=exclude,exports.select=select,exports.inherit=inherit,exports.Selectable=Selectable;