@onflow/decode
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -5,2 +5,7 @@ ### Unreleased | ||
### 0.0.6 -- 2020-05-04 | ||
- 2020-04-27 **BREAKING** -- `decodeResponse` now parses JSON-CDC, if present, from a response, and returns the appropriate populated data from the response. | ||
- 2020-04-23 **BREAKING** -- No longer parse JSON-CDC data from Uint8Array in decodeResponse since this step is now performed in send. | ||
### 0.0.5 -- 2020-04-20 | ||
@@ -7,0 +12,0 @@ |
@@ -1,2 +0,2 @@ | ||
function r(){return(r=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}var e=function(r,e,t){try{try{return Promise.resolve(Number(r))}catch(r){throw new Error("Decode Number Error : "+t.join("."))}}catch(r){return Promise.reject(r)}},t=function(r){return Promise.resolve(r)},n=function(r,e,t){try{return Promise.resolve(r.fields.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(i(n.value,e,[].concat(t,[n.name]))).then(function(e){return r[n.name]=e,r})})}catch(r){return Promise.reject(r)}},Promise.resolve({}))).then(function(t){var n=r.id&&c(e,r.id);return n?Promise.resolve(n(t)):t})}catch(r){return Promise.reject(r)}},o={UInt:e,Int:e,UInt8:e,Int8:e,UInt16:e,Int16:e,UInt32:e,Int32:e,UInt64:e,Int64:e,UInt128:e,Int128:e,UInt256:e,Int256:e,Word8:e,Word16:e,Word32:e,Word64:e,UFix64:e,Fix64:e,String:t,Character:t,Bool:t,Address:t,Void:function(){return Promise.resolve(null)},Optional:function(r,e,t){return Promise.resolve(r?i(r,e,t):null)},Reference:function(r){try{return Promise.resolve({address:r.address,type:r.type})}catch(r){return Promise.reject(r)}},Array:function(r,e,t){try{return Promise.resolve(Promise.all(r.map(function(r){return new Promise(function(n){try{return Promise.resolve(i(r,e,[].concat(t,[r.type]))).then(n)}catch(r){return Promise.reject(r)}})})))}catch(r){return Promise.reject(r)}},Dictionary:function(r,e,t){try{return Promise.resolve(r.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(i(n.key,e,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(i(n.value,e,[].concat(t,[n.key]))).then(function(e){return r[o]=e,r})})})}catch(r){return Promise.reject(r)}},Promise.resolve({})))}catch(r){return Promise.reject(r)}},Event:n,Resource:n,Struct:n},c=function(r,e){var t=Object.keys(r).find(function(r){return/^\/.*\/$/.test(r)?new RegExp(r.substring(1,r.length-1)).test(e):r===e});return e&&t&&r[t]},i=function(r,e,t){try{var n=c(e,r.type);if(!n)throw new Error("Undefined Decoder Error: "+r.type+"@"+t.join("."));return Promise.resolve(n(r.value,e,t))}catch(r){return Promise.reject(r)}},s=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var c=r({},o,{},t);return Promise.resolve(i(e,c,n))};exports.decode=s,exports.decodeResponse=function(e,t){void 0===t&&(t={});try{var n=r({},o,{},t),c=Buffer.from(e.encodedData).toString("utf8"),i=JSON.parse(c);return Promise.resolve(s(i,n))}catch(r){return Promise.reject(r)}}; | ||
function e(){return(e=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var r=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},t=function(e){return Promise.resolve(e)},n=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(i(n.value,r,[].concat(t,[n.name]))).then(function(r){return e[n.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var n=e.id&&c(r,e.id);return n?Promise.resolve(n(t)):t})}catch(e){return Promise.reject(e)}},o={UInt:r,Int:r,UInt8:r,Int8:r,UInt16:r,Int16:r,UInt32:r,Int32:r,UInt64:r,Int64:r,UInt128:r,Int128:r,UInt256:r,Int256:r,Word8:r,Word16:r,Word32:r,Word64:r,UFix64:r,Fix64:r,String:t,Character:t,Bool:t,Address:t,Void:function(){return Promise.resolve(null)},Optional:function(e,r,t){return Promise.resolve(e?i(e,r,t):null)},Reference:function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},Array:function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(n){try{return Promise.resolve(i(e,r,[].concat(t,[e.type]))).then(n)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},Dictionary:function(e,r,t){try{return Promise.resolve(e.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(i(n.key,r,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(i(n.value,r,[].concat(t,[n.key]))).then(function(r){return e[o]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},Event:n,Resource:n,Struct:n},c=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},i=function(e,r,t){try{var n=c(r,e.type);if(!n)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(n(e.value,r,t))}catch(e){return Promise.reject(e)}},s=function(r,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var c=e({},o,{},t);return Promise.resolve(i(r,c,n))};exports.decode=s,exports.decodeResponse=function(r,t){void 0===t&&(t={});try{var n=!1,c=e({},o,{},t),i=r.encodedData?(n=!0,Promise.resolve(s(r.encodedData,c))):r.transaction?(n=!0,Promise.resolve(Promise.all(r.transaction.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,o=e.type;return Promise.resolve(s(e.payload,c)).then(function(e){return{type:o,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}}))).then(function(t){return e({},r.transaction,{events:t})})):r.events?(n=!0,Promise.resolve(Promise.all(r.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,o=e.type;return Promise.resolve(s(e.payload,c)).then(function(e){return{type:o,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}})))):r.account?(n=!0,r.account):r.latestBlock?(n=!0,r.latestBlock):r.transactionId?(n=!0,r.transactionId):void 0;return Promise.resolve(i&&i.then?i.then(function(e){return n?e:null}):n?i:null)}catch(e){return Promise.reject(e)}}; | ||
//# sourceMappingURL=decode.js.map |
@@ -1,2 +0,2 @@ | ||
function r(){return(r=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}var e=function(r,e,t){try{try{return Promise.resolve(Number(r))}catch(r){throw new Error("Decode Number Error : "+t.join("."))}}catch(r){return Promise.reject(r)}},t=function(r){return Promise.resolve(r)},n=function(r,e,t){try{return Promise.resolve(r.fields.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(c(n.value,e,[].concat(t,[n.name]))).then(function(e){return r[n.name]=e,r})})}catch(r){return Promise.reject(r)}},Promise.resolve({}))).then(function(t){var n=r.id&&i(e,r.id);return n?Promise.resolve(n(t)):t})}catch(r){return Promise.reject(r)}},o={UInt:e,Int:e,UInt8:e,Int8:e,UInt16:e,Int16:e,UInt32:e,Int32:e,UInt64:e,Int64:e,UInt128:e,Int128:e,UInt256:e,Int256:e,Word8:e,Word16:e,Word32:e,Word64:e,UFix64:e,Fix64:e,String:t,Character:t,Bool:t,Address:t,Void:function(){return Promise.resolve(null)},Optional:function(r,e,t){return Promise.resolve(r?c(r,e,t):null)},Reference:function(r){try{return Promise.resolve({address:r.address,type:r.type})}catch(r){return Promise.reject(r)}},Array:function(r,e,t){try{return Promise.resolve(Promise.all(r.map(function(r){return new Promise(function(n){try{return Promise.resolve(c(r,e,[].concat(t,[r.type]))).then(n)}catch(r){return Promise.reject(r)}})})))}catch(r){return Promise.reject(r)}},Dictionary:function(r,e,t){try{return Promise.resolve(r.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(c(n.key,e,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(c(n.value,e,[].concat(t,[n.key]))).then(function(e){return r[o]=e,r})})})}catch(r){return Promise.reject(r)}},Promise.resolve({})))}catch(r){return Promise.reject(r)}},Event:n,Resource:n,Struct:n},i=function(r,e){var t=Object.keys(r).find(function(r){return/^\/.*\/$/.test(r)?new RegExp(r.substring(1,r.length-1)).test(e):r===e});return e&&t&&r[t]},c=function(r,e,t){try{var n=i(e,r.type);if(!n)throw new Error("Undefined Decoder Error: "+r.type+"@"+t.join("."));return Promise.resolve(n(r.value,e,t))}catch(r){return Promise.reject(r)}},u=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var i=r({},o,{},t);return Promise.resolve(c(e,i,n))},s=function(e,t){void 0===t&&(t={});try{var n=r({},o,{},t),i=Buffer.from(e.encodedData).toString("utf8"),c=JSON.parse(i);return Promise.resolve(u(c,n))}catch(r){return Promise.reject(r)}};export{u as decode,s as decodeResponse}; | ||
function e(){return(e=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var r=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},t=function(e){return Promise.resolve(e)},n=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(i(n.value,r,[].concat(t,[n.name]))).then(function(r){return e[n.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var n=e.id&&c(r,e.id);return n?Promise.resolve(n(t)):t})}catch(e){return Promise.reject(e)}},o={UInt:r,Int:r,UInt8:r,Int8:r,UInt16:r,Int16:r,UInt32:r,Int32:r,UInt64:r,Int64:r,UInt128:r,Int128:r,UInt256:r,Int256:r,Word8:r,Word16:r,Word32:r,Word64:r,UFix64:r,Fix64:r,String:t,Character:t,Bool:t,Address:t,Void:function(){return Promise.resolve(null)},Optional:function(e,r,t){return Promise.resolve(e?i(e,r,t):null)},Reference:function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},Array:function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(n){try{return Promise.resolve(i(e,r,[].concat(t,[e.type]))).then(n)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},Dictionary:function(e,r,t){try{return Promise.resolve(e.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(i(n.key,r,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(i(n.value,r,[].concat(t,[n.key]))).then(function(r){return e[o]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},Event:n,Resource:n,Struct:n},c=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},i=function(e,r,t){try{var n=c(r,e.type);if(!n)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(n(e.value,r,t))}catch(e){return Promise.reject(e)}},s=function(r,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var c=e({},o,{},t);return Promise.resolve(i(r,c,n))},a=function(r,t){void 0===t&&(t={});try{var n=!1,c=e({},o,{},t),i=r.encodedData?(n=!0,Promise.resolve(s(r.encodedData,c))):r.transaction?(n=!0,Promise.resolve(Promise.all(r.transaction.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,o=e.type;return Promise.resolve(s(e.payload,c)).then(function(e){return{type:o,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}}))).then(function(t){return e({},r.transaction,{events:t})})):r.events?(n=!0,Promise.resolve(Promise.all(r.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,o=e.type;return Promise.resolve(s(e.payload,c)).then(function(e){return{type:o,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}})))):r.account?(n=!0,r.account):r.latestBlock?(n=!0,r.latestBlock):r.transactionId?(n=!0,r.transactionId):void 0;return Promise.resolve(i&&i.then?i.then(function(e){return n?e:null}):n?i:null)}catch(e){return Promise.reject(e)}};export{s as decode,a as decodeResponse}; | ||
//# sourceMappingURL=decode.modern.js.map |
@@ -1,2 +0,2 @@ | ||
function r(){return(r=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}var e=function(r,e,t){try{try{return Promise.resolve(Number(r))}catch(r){throw new Error("Decode Number Error : "+t.join("."))}}catch(r){return Promise.reject(r)}},t=function(r){return Promise.resolve(r)},n=function(r,e,t){try{return Promise.resolve(r.fields.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(c(n.value,e,[].concat(t,[n.name]))).then(function(e){return r[n.name]=e,r})})}catch(r){return Promise.reject(r)}},Promise.resolve({}))).then(function(t){var n=r.id&&i(e,r.id);return n?Promise.resolve(n(t)):t})}catch(r){return Promise.reject(r)}},o={UInt:e,Int:e,UInt8:e,Int8:e,UInt16:e,Int16:e,UInt32:e,Int32:e,UInt64:e,Int64:e,UInt128:e,Int128:e,UInt256:e,Int256:e,Word8:e,Word16:e,Word32:e,Word64:e,UFix64:e,Fix64:e,String:t,Character:t,Bool:t,Address:t,Void:function(){return Promise.resolve(null)},Optional:function(r,e,t){return Promise.resolve(r?c(r,e,t):null)},Reference:function(r){try{return Promise.resolve({address:r.address,type:r.type})}catch(r){return Promise.reject(r)}},Array:function(r,e,t){try{return Promise.resolve(Promise.all(r.map(function(r){return new Promise(function(n){try{return Promise.resolve(c(r,e,[].concat(t,[r.type]))).then(n)}catch(r){return Promise.reject(r)}})})))}catch(r){return Promise.reject(r)}},Dictionary:function(r,e,t){try{return Promise.resolve(r.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(c(n.key,e,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(c(n.value,e,[].concat(t,[n.key]))).then(function(e){return r[o]=e,r})})})}catch(r){return Promise.reject(r)}},Promise.resolve({})))}catch(r){return Promise.reject(r)}},Event:n,Resource:n,Struct:n},i=function(r,e){var t=Object.keys(r).find(function(r){return/^\/.*\/$/.test(r)?new RegExp(r.substring(1,r.length-1)).test(e):r===e});return e&&t&&r[t]},c=function(r,e,t){try{var n=i(e,r.type);if(!n)throw new Error("Undefined Decoder Error: "+r.type+"@"+t.join("."));return Promise.resolve(n(r.value,e,t))}catch(r){return Promise.reject(r)}},u=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var i=r({},o,{},t);return Promise.resolve(c(e,i,n))},s=function(e,t){void 0===t&&(t={});try{var n=r({},o,{},t),i=Buffer.from(e.encodedData).toString("utf8"),c=JSON.parse(i);return Promise.resolve(u(c,n))}catch(r){return Promise.reject(r)}};export{u as decode,s as decodeResponse}; | ||
function e(){return(e=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var r=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},t=function(e){return Promise.resolve(e)},n=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(i(n.value,r,[].concat(t,[n.name]))).then(function(r){return e[n.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var n=e.id&&c(r,e.id);return n?Promise.resolve(n(t)):t})}catch(e){return Promise.reject(e)}},o={UInt:r,Int:r,UInt8:r,Int8:r,UInt16:r,Int16:r,UInt32:r,Int32:r,UInt64:r,Int64:r,UInt128:r,Int128:r,UInt256:r,Int256:r,Word8:r,Word16:r,Word32:r,Word64:r,UFix64:r,Fix64:r,String:t,Character:t,Bool:t,Address:t,Void:function(){return Promise.resolve(null)},Optional:function(e,r,t){return Promise.resolve(e?i(e,r,t):null)},Reference:function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},Array:function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(n){try{return Promise.resolve(i(e,r,[].concat(t,[e.type]))).then(n)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},Dictionary:function(e,r,t){try{return Promise.resolve(e.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(i(n.key,r,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(i(n.value,r,[].concat(t,[n.key]))).then(function(r){return e[o]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},Event:n,Resource:n,Struct:n},c=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},i=function(e,r,t){try{var n=c(r,e.type);if(!n)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(n(e.value,r,t))}catch(e){return Promise.reject(e)}},s=function(r,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var c=e({},o,{},t);return Promise.resolve(i(r,c,n))},a=function(r,t){void 0===t&&(t={});try{var n=!1,c=e({},o,{},t),i=r.encodedData?(n=!0,Promise.resolve(s(r.encodedData,c))):r.transaction?(n=!0,Promise.resolve(Promise.all(r.transaction.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,o=e.type;return Promise.resolve(s(e.payload,c)).then(function(e){return{type:o,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}}))).then(function(t){return e({},r.transaction,{events:t})})):r.events?(n=!0,Promise.resolve(Promise.all(r.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,o=e.type;return Promise.resolve(s(e.payload,c)).then(function(e){return{type:o,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}})))):r.account?(n=!0,r.account):r.latestBlock?(n=!0,r.latestBlock):r.transactionId?(n=!0,r.transactionId):void 0;return Promise.resolve(i&&i.then?i.then(function(e){return n?e:null}):n?i:null)}catch(e){return Promise.reject(e)}};export{s as decode,a as decodeResponse}; | ||
//# sourceMappingURL=decode.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).decode={})}(this,function(e){function r(){return(r=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var t=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},n=function(e){return Promise.resolve(e)},o=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(u(n.value,r,[].concat(t,[n.name]))).then(function(r){return e[n.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var n=e.id&&c(r,e.id);return n?Promise.resolve(n(t)):t})}catch(e){return Promise.reject(e)}},i={UInt:t,Int:t,UInt8:t,Int8:t,UInt16:t,Int16:t,UInt32:t,Int32:t,UInt64:t,Int64:t,UInt128:t,Int128:t,UInt256:t,Int256:t,Word8:t,Word16:t,Word32:t,Word64:t,UFix64:t,Fix64:t,String:n,Character:n,Bool:n,Address:n,Void:function(){return Promise.resolve(null)},Optional:function(e,r,t){return Promise.resolve(e?u(e,r,t):null)},Reference:function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},Array:function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(n){try{return Promise.resolve(u(e,r,[].concat(t,[e.type]))).then(n)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},Dictionary:function(e,r,t){try{return Promise.resolve(e.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(u(n.key,r,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(u(n.value,r,[].concat(t,[n.key]))).then(function(r){return e[o]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},Event:o,Resource:o,Struct:o},c=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},u=function(e,r,t){try{var n=c(r,e.type);if(!n)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(n(e.value,r,t))}catch(e){return Promise.reject(e)}},s=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var o=r({},i,{},t);return Promise.resolve(u(e,o,n))};e.decode=s,e.decodeResponse=function(e,t){void 0===t&&(t={});try{var n=r({},i,{},t),o=Buffer.from(e.encodedData).toString("utf8"),c=JSON.parse(o);return Promise.resolve(s(c,n))}catch(e){return Promise.reject(e)}}}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).decode={})}(this,function(e){function r(){return(r=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var t=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},n=function(e){return Promise.resolve(e)},o=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(s(n.value,r,[].concat(t,[n.name]))).then(function(r){return e[n.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var n=e.id&&i(r,e.id);return n?Promise.resolve(n(t)):t})}catch(e){return Promise.reject(e)}},c={UInt:t,Int:t,UInt8:t,Int8:t,UInt16:t,Int16:t,UInt32:t,Int32:t,UInt64:t,Int64:t,UInt128:t,Int128:t,UInt256:t,Int256:t,Word8:t,Word16:t,Word32:t,Word64:t,UFix64:t,Fix64:t,String:n,Character:n,Bool:n,Address:n,Void:function(){return Promise.resolve(null)},Optional:function(e,r,t){return Promise.resolve(e?s(e,r,t):null)},Reference:function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},Array:function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(n){try{return Promise.resolve(s(e,r,[].concat(t,[e.type]))).then(n)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},Dictionary:function(e,r,t){try{return Promise.resolve(e.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(s(n.key,r,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(s(n.value,r,[].concat(t,[n.key]))).then(function(r){return e[o]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},Event:o,Resource:o,Struct:o},i=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},s=function(e,r,t){try{var n=i(r,e.type);if(!n)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(n(e.value,r,t))}catch(e){return Promise.reject(e)}},u=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n=[]);var o=r({},c,{},t);return Promise.resolve(s(e,o,n))};e.decode=u,e.decodeResponse=function(e,t){void 0===t&&(t={});try{var n=!1,o=r({},c,{},t),i=e.encodedData?(n=!0,Promise.resolve(u(e.encodedData,o))):e.transaction?(n=!0,Promise.resolve(Promise.all(e.transaction.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,c=e.type;return Promise.resolve(u(e.payload,o)).then(function(e){return{type:c,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}}))).then(function(t){return r({},e.transaction,{events:t})})):e.events?(n=!0,Promise.resolve(Promise.all(e.events.map(function(e){try{var r=e.eventIndex,t=e.transactionIndex,n=e.transactionId,c=e.type;return Promise.resolve(u(e.payload,o)).then(function(e){return{type:c,transactionId:n,transactionIndex:t,eventIndex:r,data:e}})}catch(e){return Promise.reject(e)}})))):e.account?(n=!0,e.account):e.latestBlock?(n=!0,e.latestBlock):e.transactionId?(n=!0,e.transactionId):void 0;return Promise.resolve(i&&i.then?i.then(function(e){return n?e:null}):n?i:null)}catch(e){return Promise.reject(e)}}}); | ||
//# sourceMappingURL=decode.umd.js.map |
{ | ||
"name": "@onflow/decode", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Flow SDK Decoder", | ||
@@ -10,6 +10,6 @@ "license": "Apache-2.0", | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/dapperlabs/flow-js-sdk.git" | ||
"url": "git+ssh://git@github.com/onflow/flow-js-sdk.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/dapperlabs/flow-js-sdk/issues" | ||
"url": "https://github.com/onflow/flow-js-sdk/issues" | ||
}, | ||
@@ -16,0 +16,0 @@ "jest": { |
> **NOTE:** Requires a Buffer polyfill | ||
# @onflow/bytes | ||
This package provides methods that can decode a JSON-CDC payload from the Access API into JavaScript friendly types. | ||
# @onflow/decode | ||
This package provides methods that can decode a response, which can include JSON-CDC payloads from the Access API into JavaScript friendly types. | ||
# Status | ||
- **Last Updated:** April 21st 2020 | ||
- **Stable:** Yes | ||
- **Risk of Breaking Change**: Extremely Low (If the known upcoming change is taken into account) | ||
This package is fairly stable, might even be the most complete package we have. | ||
We are fairly happy with how it is consumed, and how it works internally. | ||
Other than the known upcoming change listed below this package might be ready for its first major version bump. | ||
# Install | ||
@@ -14,6 +25,14 @@ | ||
**Decoding without ABIs** | ||
Unlike with other blockchains, Flow does not require the use of an ABI to decode responses from the blockchain! A response from the Flow Access Node is encoded in JSON-CDC, which is a self describing payload that this package uses to decode the response into JavaScript friendly types. | ||
**Decoding** | ||
You can use `decodeResponse` on any type of response. If that response includes a JSON-CDC payload, it will be decoded into JavaScript friendly types. The data that is returned from `decodeResponse` is the data received from the Access Node for the sent interaction. | ||
**Decoding a Response** | ||
```javascript | ||
import { decodeResponse } from "@onflow/decode" | ||
import {decodeResponse} from "@onflow/decode" | ||
const decoded = await decodeResponse(myResponse) | ||
@@ -27,5 +46,5 @@ ``` | ||
```javascript | ||
import { decodeResponse } from "@onflow/decode" | ||
import {decodeResponse} from "@onflow/decode" | ||
const decoded = await decodeResponse(myResponse, { | ||
"UInt8": (data) => Number(data) | ||
UInt8: data => Number(data), | ||
}) | ||
@@ -39,7 +58,7 @@ ``` | ||
```javascript | ||
import { decodeResponse } from "@onflow/decode" | ||
import {decodeResponse} from "@onflow/decode" | ||
const decoded = await decodeResponse(myResponse, { | ||
"CryptoKitty": (cryptoKitty) => ({ | ||
name: cryptoKitty.kittyName || "Lil BUB The Magical Space Cat", | ||
}) | ||
CryptoKitty: cryptoKitty => ({ | ||
name: cryptoKitty.kittyName || "Lil BUB The Magical Space Cat", | ||
}), | ||
}) | ||
@@ -55,5 +74,5 @@ ``` | ||
```javascript | ||
import { decodeResponse } from "@onflow/decode" | ||
import {decodeResponse} from "@onflow/decode" | ||
const decoded = await decodeResponse(myResponse, { | ||
"/.CryptoKitty$/": myCryptoKittyDecoder | ||
"/.CryptoKitty$/": myCryptoKittyDecoder, | ||
}) | ||
@@ -67,5 +86,5 @@ ``` | ||
```javascript | ||
import { decode } from "@onflow/decode" | ||
import {decode} from "@onflow/decode" | ||
const jsonCDC = { type: "String", value: "https://discordapp.com/invite/WgAQhtB" } | ||
const jsonCDC = {type: "String", value: "https://discordapp.com/invite/WgAQhtB"} | ||
@@ -72,0 +91,0 @@ const decoded = await deocde(jsonCDC) |
@@ -114,7 +114,40 @@ const decodeNumber = async (num, _, stack) => { | ||
const encoded = response.encodedData | ||
const decodeInstructions = Buffer.from(encoded).toString("utf8") | ||
const decodeInstructionsJson = JSON.parse(decodeInstructions) | ||
if (response.encodedData) { | ||
return await decode(response.encodedData, decoders) | ||
} else if (response.transaction) { | ||
return { | ||
...response.transaction, | ||
events: await Promise.all(response.transaction.events.map( | ||
async function decodeEvents(e) { | ||
return { | ||
type: e.type, | ||
transactionId: e.transactionId, | ||
transactionIndex: e.transactionIndex, | ||
eventIndex: e.eventIndex, | ||
data: await decode(e.payload, decoders) | ||
} | ||
} | ||
)) | ||
} | ||
} else if (response.events) { | ||
return await Promise.all(response.events.map( | ||
async function decodeEvents(e) { | ||
return { | ||
type: e.type, | ||
transactionId: e.transactionId, | ||
transactionIndex: e.transactionIndex, | ||
eventIndex: e.eventIndex, | ||
data: await decode(e.payload, decoders) | ||
} | ||
} | ||
)) | ||
} else if (response.account) { | ||
return response.account | ||
} else if (response.latestBlock) { | ||
return response.latestBlock | ||
} else if (response.transactionId) { | ||
return response.transactionId | ||
} | ||
return await decode(decodeInstructionsJson, decoders) | ||
return null | ||
} |
@@ -15,3 +15,3 @@ import * as root from "./decode" | ||
const response = { | ||
encodedData: Uint8Array.from( | ||
encodedData: JSON.parse(Buffer.from(Uint8Array.from( | ||
Buffer.from( | ||
@@ -21,3 +21,3 @@ "7b2274797065223a22496e74222c2276616c7565223a2237227d0a", | ||
) | ||
), | ||
)).toString("utf8")), | ||
} | ||
@@ -1144,1 +1144,59 @@ | ||
}) | ||
describe("decode GetEvents tests", () => { | ||
it("decodes a GetEvents response correctly", async () => { | ||
const getEventsResponse = { | ||
events: [{ | ||
eventIndex: 123, | ||
transactionId: "abc-123", | ||
transactionIndex: 123, | ||
type: "MyFunAndCoolEvent", | ||
payload: {type: "String", value: "foo"} | ||
}] | ||
} | ||
expect(await decodeResponse(getEventsResponse)).toStrictEqual( | ||
[{ | ||
eventIndex: 123, | ||
transactionId: "abc-123", | ||
transactionIndex: 123, | ||
type: "MyFunAndCoolEvent", | ||
data: "foo" | ||
}] | ||
) | ||
}) | ||
}) | ||
describe("decode GetTransactionStatus tests", () => { | ||
it("decodes a GetEvents response correctly", async () => { | ||
const getTransactionStatusResponse = { | ||
transaction: { | ||
status: 4, | ||
statusCode: 1, | ||
errorMessage: null, | ||
events: [{ | ||
type: "LilBUBTheMagicalSpaceCat.LandedOnMars", | ||
transactionId: "my-fun-and-very-special-txn-id", | ||
transactionIndex: 123456, | ||
eventIndex: 7891011, | ||
payload: {type: "String", value: "Thanks for reviewing these tests!"} | ||
}] | ||
} | ||
} | ||
expect(await decodeResponse(getTransactionStatusResponse)).toStrictEqual( | ||
{ | ||
status: 4, | ||
statusCode: 1, | ||
errorMessage: null, | ||
events: [{ | ||
type: "LilBUBTheMagicalSpaceCat.LandedOnMars", | ||
transactionId: "my-fun-and-very-special-txn-id", | ||
transactionIndex: 123456, | ||
eventIndex: 7891011, | ||
data: "Thanks for reviewing these tests!" | ||
}] | ||
} | ||
) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
85347
1225
0
90