@miso.ai/doggoganger 0.9.1-beta.0 → 0.9.1-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/umd/doggoganger-browser.min.js +1 -1
- package/package.json +3 -1
- package/src/api/ask.js +21 -2
- package/src/api.js +5 -0
- package/src/browser.js +2 -0
- package/src/data/answers.js +6 -2
- package/src/data/articles.js +1 -1
- package/src/data/fields.js +24 -0
- package/src/data/index.js +1 -0
- package/src/data/products.js +1 -1
- package/src/data/questions.js +1 -1
- package/src/fetch.js +35 -0
- package/src/index.js +1 -2
- package/src/route/ask.js +8 -14
- package/src/route/interactions.js +2 -9
- package/src/route/products.js +2 -9
- package/src/route/recommendation.js +3 -14
- package/src/route/search.js +2 -7
- package/src/route/utils.js +46 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t="undefined"!=typeof globalThis?globalThis:t||self).doggoganger=n()}(this,(function(){"use strict";function t(t,n){return null!=n&&n>t?t+Math.floor(Math.random()*(n-t)):t}function n(){return Math.random().toString(36).substring(2,10)}function e(t){return`https://picsum.photos/seed/${Math.floor(1e3*Math.random())}/${Array.isArray(t)?t.length>1?`${t[0]}/${t[1]}`:""+t[0]:""+t}`}function r(t,n){return void 0!==n?Math.ceil(t*n):t}function o(){return 2*Math.random()-1}var s=["lorem","ipsum","dolor","sit","amet","consectetur","adipiscing","elit","curabitur","vel","hendrerit","libero","eleifend","blandit","nunc","ornare","odio","ut","orci","gravida","imperdiet","nullam","purus","lacinia","a","pretium","quis","congue","praesent","sagittis","laoreet","auctor","mauris","non","velit","eros","dictum","proin","accumsan","sapien","nec","massa","volutpat","venenatis","sed","eu","molestie","lacus","quisque","porttitor","ligula","dui","mollis","tempus","at","magna","vestibulum","turpis","ac","diam","tincidunt","id","condimentum","enim","sodales","in","hac","habitasse","platea","dictumst","aenean","neque","fusce","augue","leo","eget","semper","mattis","tortor","scelerisque","nulla","interdum","tellus","malesuada","rhoncus","porta","sem","aliquet","et","nam","suspendisse","potenti","vivamus","luctus","fringilla","erat","donec","justo","vehicula","ultricies","varius","ante","primis","faucibus","ultrices","posuere","cubilia","curae","etiam","cursus","aliquam","quam","dapibus","nisl","feugiat","egestas","class","aptent","taciti","sociosqu","ad","litora","torquent","per","conubia","nostra","inceptos","himenaeos","phasellus","nibh","pulvinar","vitae","urna","iaculis","lobortis","nisi","viverra","arcu","morbi","pellentesque","metus","commodo","ut","facilisis","felis","tristique","ullamcorper","placerat","aenean","convallis","sollicitudin","integer","rutrum","duis","est","etiam","bibendum","donec","pharetra","vulputate","maecenas","mi","fermentum","consequat","suscipit","aliquam","habitant","senectus","netus","fames","quisque","euismod","curabitur","lectus","elementum","tempor","risus","cras"];function i({decorates:n=[],output:e="string",size:r,min:o,max:i,...a}={}){let c=function(n=[5,10]){const e="number"==typeof n?n:t(...n);return function*(t){let n=0;for(let r of t){if(n++>=e)break;yield r}}}(r||[o,i])(function*({words:t=s,fixedStarts:n=0}={}){const e=t.length;for(let r=0;;r++)yield t[n>r?r:Math.floor(Math.random()*e)]}(a));for(const t of n)c=u(t)(c);return u(e)(c)}const a={string:function({separator:t=" "}={}){return n=>[...n].join(t)},array:function(){return t=>[...t]},title:function({}={}){return function*(t){for(let n of t)yield c(n)}},description:function({wordsPerSentence:t={avg:24,std:5,min:1},punctuation:n="."}={}){return function*(e){let r,o=0;for(let s of e)r&&(yield r),r=s,0===o&&(r=c(r),o=l(t)),0==--o&&(r+=n);r&&(r.endsWith(n)||(r+=n),yield r)}},multiline:function({wordsPerLine:t={avg:10,std:3,min:1}}={}){return n=>{let e=l(t),r="";for(let o of n)r&&(0==e--?(r+="\n",e=l(t)):r+=" "),r+=o;return r}}};function u(t){switch(typeof t){case"string":return a[t]();case"function":return t;case"object":if(Array.isArray(t)){const[n,e={}]=t;return a[n](e)}}throw Error("Unrecognized decorator/output form: "+t)}function c(t){return t[0].toUpperCase()+t.substring(1)}function l(t){if("number"==typeof t)return Math.round(n);let{avg:n,std:e,min:r,max:s}=t;void 0===e&&(e=n/4);let i=(o()+o()+o())*e+n;return void 0!==r&&(i=Math.max(r,i)),void 0!==s&&(i=Math.min(s,i)),Math.round(i)}const f="// module\nimport { a, b } from './module';\nimport * as module from './module';\nimport module from './module';\nexport default module;\nexport const a = 0;\nexport * from './module';\nexport * as module from './module';\n\n// variables\nlet a = 10;\nconst b = 20;\n\n// function declaration\nfunction sum(x, y) {\n return x + y;\n}\n\n// generator function\nfunction* iterator() {\n yield 0;\n yield 1;\n}\n\n// arrow function\nconst multiply = (x, y) => x * y;\n\n// class\nclass Person {\n constructor(name, age) {\n this.name = name;\n this.age = age;\n }\n\n greet() {\n console.log('Hello, my name is Miso.');\n }\n}\n\n// primitive\nconst str = 'Hello, world!';\nconst num = 10.99;\n\n// object\nconst person = new Person('John', 30);\nperson.greet();\n\n// object literal\nconst object = {\n name: 'John',\n [x]: 10,\n ...props,\n};\n\n// array literal\nconst arr = [1, 2, 3, 4, 5, ...props];\n\n// regexp literal\nconst regexp = /\\w+/g;\n\n// operators\nconst sum = a + b;\nconst product = a * b;\nconst negation = -a;\nconst max = a > b ? a : b;\n\n// flow control\nlet i = 9;\nfor (const n of arr) {\n if (n > i) {\n console.log(n);\n }\n i++;\n}\n\n// async/await\n(async () => {\n const result = await asyncFunction();\n})();\n\n// try/catch\ntry {\n} catch (e) {\n}\n\n// destructuring\nconst { name, age, ...rest } = person;\nconst [ x, y, ...rest ] = arr;\n\n// template literals\nconsole.log(`The sum of ${a} and ${b} is ${sum(a, b)}.`);";var d=Object.freeze({__proto__:null,javascript:function(){return f},js:function(){return f}});function p({features:n,blocks:r=[8,12],sampling:o=1}={}){let s=[];return[s,n]=function(t=[]){const n=new Set,e=[];for(const r of t)if(r.startsWith("lang-")){let t=r.slice(5);"javascript"===t&&(t="js"),n.add(t)}else e.push(r);return[[...n],e]}(n),function(t,n){const e=[];for(const r of t)1>n&&Math.random()>=n||e.push(r());return e}([()=>m({features:n}),()=>g({features:n}),...s.map((t=>()=>h({lang:t,features:n}))),...s.length?[]:[()=>h({features:n})],()=>g({features:n}),()=>function({columns:n=[2,4],rows:e=[2,8]}){n=t(...n),e=t(...e);const r=[...$({size:1},n-1),{size:[3,8]}],o=i({size:n,output:"array"}),s=r.map((()=>"---")),a=[o,s];for(let t=0;e-1>t;t++)a.push(r.map((({size:t})=>i({size:t}))));return a.map(x).join("\n")}({features:n}),()=>function({url:t,imageSize:n=[400,250],...r}={}){return t=t||e(n),`![${function({size:t=[1,3],content:n}={}){return n||i({size:t})}(r)}](${t})`}(),()=>g({features:n}),()=>"*-_".charAt(t(0,2)).repeat(t(3,6)),()=>m({features:n}),()=>g({features:n}),()=>w({features:n}),()=>g({features:n})],o).join("\n\n")}function m({level:n=[1,6],size:e=[1,8],content:r}){const o=r||i({size:e});return`${"#".repeat(t(...n))} ${o}`}function h({lang:n,content:e,size:r,fenceChar:o="`"}){return"random"===o&&(o="`~".charAt(t(0,1))),e=e||function({lang:t,size:n=[10,30]}){return t&&d[t]?d[t]():i({output:"multiline",size:n})}({lang:n,size:r}),`${o.repeat(3)}${n||""}\n${e}\n${o.repeat(3)}`}function g({size:t=[20,50]}){return i({size:t,decorates:["description",z()]})}const _=["ordered","bullet","task"];function w({features:n,type:e="random",count:r=[1,8],size:o=[5,15]}){r="number"==typeof r?r:t(...r);const s="random"===e?_[Math.floor(3*Math.random())]:e,i=[];for(;r>0;){const a=t(1,r);let u=g({features:n,size:o});a>1&&(u+="\n"+w({features:n,type:e,count:a-1,size:o})),i.push(M(s,u)),r-=a}return i.join("\n")}const y={"code-span":()=>["`","`"],"emphasis-1":()=>$(b(1),2),"emphasis-2":()=>$(b(2),2),"emphasis-3":()=>$(b(3),2),strikethrough:()=>["~","~"],link:({url:t="https://miso.ai"}={})=>["[",`](${t})`]};function b(n=[1,3]){return n="number"==typeof n?n:t(...n),"_*".charAt(t(0,1)).repeat(n)}const q=Object.keys(y),v=new Set(q);function z({features:n=q,size:e=[1,3],rest:r=[0,8]}={}){n=n.filter((t=>v.has(t)));const o=function(t){for(let n=t.length-1;n>0;n--){const e=Math.floor(Math.random()*(n+1));[t[n],t[e]]=[t[e],t[n]]}return t}([...n]);let s=o.length-1;const i=()=>"number"==typeof r?r:t(...r);return function*(r){let a,u,c=i();for(const l of r)if(u&&(yield u),0===c)if(a)u=`${l}${a}`,a=void 0,c=i();else{const[r,i]=y[0>s?n[t(0,n.length-1)]:o[s--]]();u=`${r}${l}`,a=i,c="number"==typeof e?e:t(...e)}else u=l,c--;u&&(yield void 0!==a?`${u}${a}`:u)}}function M(t,n){const[e,r]=n.split("\n",2),o=`${function(t,n="random"){switch(t){case"ordered":return"1.";case"bullet":return"-";case"task":return`- [${("random"===n?.5>Math.random():n)?"x":" "}]`;default:throw Error("unknown list item type: "+t)}}(t)} ${e}`;return r?o+"\n"+function(t,n){return n.split("\n").map((n=>" ".repeat(t)+n)).join("\n")}("ordered"===t?3:2,r):o}function x(t){return`| ${t.join(" | ")} |`}function $(t,n){const e=[];for(let r=0;n>r;r++)e.push("function"==typeof t?t():t);return e}function j({size:t=300}={}){return e(t)}function S({size:t=[1,3]}={}){return i({size:t,decorates:["title"],output:"array"})}function k({size:t=[1,4]}={}){return i({size:t,output:"array"})}function O({size:t=[2,6]}={}){return i({size:t,decorates:["title"]})}function A({size:t=[10,20],...n}={}){return i({size:t,decorates:[Object.keys(n).length?["description",n]:"description"]})}function T(){return Math.floor(1e4*Math.random())/100}function*C({rows:t,...n}={}){for(let e=0;t>e;e++)yield F({...n,index:e})}function F({}={}){const e=n(),r=function(n,e){const r=t(...e),o=[];for(let t=0;r>t;t++)o.push(n());return o}(T,[1,2]);return r.sort(),{product_id:e,authors:S(),categories:[],tags:k(),title:O(),description:A(),cover_image:j(),url:"/products/"+e,sale_price:r[0],original_price:r[r.length-1],rating:Math.floor(500*Math.random())/100+1,availability:Math.random()>.3?"IN_STOCK":"OUT_OF_STOCK"}}function*D({rows:t,...n}={}){for(let e=0;t>e;e++)yield L({...n,index:e})}function L({}={}){const t=n();return{product_id:t,authors:S(),categories:[],tags:k(),title:O({size:[4,10]}),snippet:A({size:[20,40]}),cover_image:j(),url:"/products/"+t}}function*P({rows:t,...n}={}){for(let e=0;t>e;e++)yield U({...n,index:e})}function U({}={}){return A({size:[4,8],punctuation:"?"})}function W({question:t,parent_question_id:n,timestamp:e=Date.now()},{answerFormat:o="markdown",answerSampling:s,answerLanguages:a=[]}={}){const u="10000000-1000-4000-8000-100000000000".replace(/[018]/g,(t=>(t^16*Math.random()>>t/4).toString(16))),c=function(t){const n=new Date(t).toISOString();return n.endsWith("Z")?n.slice(0,-1):n}(e),l=void 0!==s?Math.max(0,Math.min(1,s)):void 0,f=function({format:t,sampling:n,features:e}){return"markdown"===t?p({sampling:n,features:e}):i({min:r(50,n),max:r(50,n),decorates:["description"]})}({format:o,sampling:l,features:a.length?a.map((t=>"lang-"+t)):void 0}),d=[...D({rows:E(6,8,l)})];return{question:t,question_id:u,...n&&{parent_question_id:n},datetime:c,answer:f,sources:[...D({rows:E(4,6,l)})],related_resources:d,followup_questions:[...P({rows:E(3,6)})]}}function E(n,e,o){return t(r(n,o),r(e,o))}const H=[{name:"fetch",duration:1.5,text:"Checking the question and fetching results... "},{name:"verify",duration:1.5,text:"Verifying results... "},{name:"generate",duration:1.5,text:"Generating answer... "}];class I{constructor(t={}){this._options=t,this._answers=new Map}questions(t,n={}){return new J(t,{...this._options,...n})}}class J{constructor({question:t,parent_question_id:n},{answerFormat:e,answerSampling:r,answerLanguages:o,...s}={}){this._options=Object.freeze(s);const i=this.timestamp=Date.now();this._data=W({question:t,parent_question_id:n,timestamp:i},{answerFormat:e,answerSampling:r,answerLanguages:o})}get question_id(){return this._data.question_id}get(){const t=(Date.now()-this.timestamp)*(this._options.speedRate||1)/1e3,[n,e,r]=this._answer(t),o=this._sources(t,r),s=this._related_resources(t,r),i=this._followup_questions(t,r),{question_id:a,question:u,datetime:c,parent_question_id:l}=this._data;return{answer:e,answer_stage:n,datetime:c,finished:r,parent_question_id:l,question:u,question_id:a,sources:o,related_resources:s,followup_questions:i}}_answer(t){for(const n of H)if(0>(t-=n.duration))return[n.name,n.text,!1];const{answer:n}=this._data,e=Math.floor(100*t),r=e>=n.length;return["result",r?n:n.slice(0,e),r]}_sources(t,n){const{sources:e}=this._data;if(n)return e;const{length:r}=e;return e.slice(0,Math.floor(r*t/3))}_related_resources(t,n){const{related_resources:e}=this._data;if(n)return e;const{length:r}=e;return e.slice(0,Math.floor(r*t/3))}_followup_questions(t,n){const{followup_questions:e}=this._data;if(n||!e)return e;const{length:r}=e;return e.slice(0,Math.floor(r*t/3))}}class K{constructor(t){this._options=t}search({rows:t=5}){return{products:[...C({rows:t})]}}}class G{constructor(t){this._options=t}user_to_products({rows:t=5}){return{products:[...C({rows:t})]}}product_to_products({rows:t=5}){return{products:[...C({rows:t})]}}}class N{constructor(t){this._options=t}upload(t){return[]}}class R{constructor(t){this._options=t}upload(t){return[]}ids(){const t=[];for(let n=0;5e3>n;n++)t.push(V(n));return{ids:t}}}function V(t){return`${10>t?"p_000":100>t?"p_00":1e3>t?"p_0":"p_"}${t}`}class Z{constructor(t){this.ask=new I(t),this.search=new K(t),this.recommendation=new G(t),this.interactions=new N(t),this.products=new R(t)}}return{buildApi:(...t)=>new Z(...t)}}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).doggoganger=e()}(this,(function(){"use strict";function t(){return"10000000-1000-4000-8000-100000000000".replace(/[018]/g,(t=>(t^16*Math.random()>>t/4).toString(16)))}function e(t,e){return null!=e&&e>t?t+Math.floor(Math.random()*(e-t)):t}function n(t,n){const r=e(...n),o=[];for(let e=0;r>e;e++)o.push(t());return o}function r(){return Math.random().toString(36).substring(2,10)}function o(t){for(let e=t.length-1;e>0;e--){const n=Math.floor(Math.random()*(e+1));[t[e],t[n]]=[t[n],t[e]]}return t}function s(t){return`https://picsum.photos/seed/${Math.floor(1e3*Math.random())}/${Array.isArray(t)?t.length>1?`${t[0]}/${t[1]}`:""+t[0]:""+t}`}function i(t){const e=new Date(t).toISOString();return e.endsWith("Z")?e.slice(0,-1):e}function a(t,e){return void 0!==e?Math.ceil(t*e):t}function u(){return c()+c()+c()}function c(){return 2*Math.random()-1}var l=Object.freeze({__proto__:null,formatDatetime:i,gaussRandom:u,id:r,imageUrl:s,randomInt:e,repeat:n,sample:a,shuffle:o,uuid:t}),f=["lorem","ipsum","dolor","sit","amet","consectetur","adipiscing","elit","curabitur","vel","hendrerit","libero","eleifend","blandit","nunc","ornare","odio","ut","orci","gravida","imperdiet","nullam","purus","lacinia","a","pretium","quis","congue","praesent","sagittis","laoreet","auctor","mauris","non","velit","eros","dictum","proin","accumsan","sapien","nec","massa","volutpat","venenatis","sed","eu","molestie","lacus","quisque","porttitor","ligula","dui","mollis","tempus","at","magna","vestibulum","turpis","ac","diam","tincidunt","id","condimentum","enim","sodales","in","hac","habitasse","platea","dictumst","aenean","neque","fusce","augue","leo","eget","semper","mattis","tortor","scelerisque","nulla","interdum","tellus","malesuada","rhoncus","porta","sem","aliquet","et","nam","suspendisse","potenti","vivamus","luctus","fringilla","erat","donec","justo","vehicula","ultricies","varius","ante","primis","faucibus","ultrices","posuere","cubilia","curae","etiam","cursus","aliquam","quam","dapibus","nisl","feugiat","egestas","class","aptent","taciti","sociosqu","ad","litora","torquent","per","conubia","nostra","inceptos","himenaeos","phasellus","nibh","pulvinar","vitae","urna","iaculis","lobortis","nisi","viverra","arcu","morbi","pellentesque","metus","commodo","ut","facilisis","felis","tristique","ullamcorper","placerat","aenean","convallis","sollicitudin","integer","rutrum","duis","est","etiam","bibendum","donec","pharetra","vulputate","maecenas","mi","fermentum","consequat","suscipit","aliquam","habitant","senectus","netus","fames","quisque","euismod","curabitur","lectus","elementum","tempor","risus","cras"];function d({decorates:t=[],output:e="string",size:n,min:r,max:o,...s}={}){let i=b(n||[r,o])(h(s));for(const e of t)i=m(e)(i);return m(e)(i)}const p={string:g,array:_,title:y,description:v,multiline:w};function m(t){switch(typeof t){case"string":return p[t]();case"function":return t;case"object":if(Array.isArray(t)){const[e,n={}]=t;return p[e](n)}}throw Error("Unrecognized decorator/output form: "+t)}function*h({words:t=f,fixedStarts:e=0}={}){const n=t.length;for(let r=0;;r++)yield t[e>r?r:Math.floor(Math.random()*n)]}function g({separator:t=" "}={}){return e=>[...e].join(t)}function _(){return t=>[...t]}function w({wordsPerLine:t={avg:10,std:3,min:1}}={}){return e=>{let n=q(t),r="";for(let o of e)r&&(0==n--?(r+="\n",n=q(t)):r+=" "),r+=o;return r}}function b(t=[5,10]){const n="number"==typeof t?t:e(...t);return function*(t){let e=0;for(let r of t){if(e++>=n)break;yield r}}}function y({}={}){return function*(t){for(let e of t)yield z(e)}}function v({wordsPerSentence:t={avg:24,std:5,min:1},punctuation:e="."}={}){return function*(n){let r,o=0;for(let s of n)r&&(yield r),r=s,0===o&&(r=z(r),o=q(t)),0==--o&&(r+=e);r&&(r.endsWith(e)||(r+=e),yield r)}}function z(t){return t[0].toUpperCase()+t.substring(1)}function q(t){if("number"==typeof t)return Math.round(e);let{avg:e,std:n,min:r,max:o}=t;void 0===n&&(n=e/4);let s=u()*n+e;return void 0!==r&&(s=Math.max(r,s)),void 0!==o&&(s=Math.min(o,s)),Math.round(s)}var $=Object.freeze({__proto__:null,array:_,base:h,description:v,limit:b,lorem:d,multiline:w,string:g,title:y});const M="// module\nimport { a, b } from './module';\nimport * as module from './module';\nimport module from './module';\nexport default module;\nexport const a = 0;\nexport * from './module';\nexport * as module from './module';\n\n// variables\nlet a = 10;\nconst b = 20;\n\n// function declaration\nfunction sum(x, y) {\n return x + y;\n}\n\n// generator function\nfunction* iterator() {\n yield 0;\n yield 1;\n}\n\n// arrow function\nconst multiply = (x, y) => x * y;\n\n// class\nclass Person {\n constructor(name, age) {\n this.name = name;\n this.age = age;\n }\n\n greet() {\n console.log('Hello, my name is Miso.');\n }\n}\n\n// primitive\nconst str = 'Hello, world!';\nconst num = 10.99;\n\n// object\nconst person = new Person('John', 30);\nperson.greet();\n\n// object literal\nconst object = {\n name: 'John',\n [x]: 10,\n ...props,\n};\n\n// array literal\nconst arr = [1, 2, 3, 4, 5, ...props];\n\n// regexp literal\nconst regexp = /\\w+/g;\n\n// operators\nconst sum = a + b;\nconst product = a * b;\nconst negation = -a;\nconst max = a > b ? a : b;\n\n// flow control\nlet i = 9;\nfor (const n of arr) {\n if (n > i) {\n console.log(n);\n }\n i++;\n}\n\n// async/await\n(async () => {\n const result = await asyncFunction();\n})();\n\n// try/catch\ntry {\n} catch (e) {\n}\n\n// destructuring\nconst { name, age, ...rest } = person;\nconst [ x, y, ...rest ] = arr;\n\n// template literals\nconsole.log(`The sum of ${a} and ${b} is ${sum(a, b)}.`);";var x=Object.freeze({__proto__:null,javascript:function(){return M},js:function(){return M}});function j({features:t,blocks:e=[8,12],sampling:n=1}={}){let r=[];return[r,t]=function(t=[]){const e=new Set,n=[];for(const r of t)if(r.startsWith("lang-")){let t=r.slice(5);"javascript"===t&&(t="js"),e.add(t)}else n.push(r);return[[...e],n]}(t),function(t,e){const n=[];for(const r of t)1>e&&Math.random()>=e||n.push(r());return n}([()=>S({features:t}),()=>A({features:t}),...r.map((e=>()=>O({lang:e,features:t}))),...r.length?[]:[()=>O({features:t})],()=>A({features:t}),()=>C({features:t}),()=>D(),()=>A({features:t}),()=>k(),()=>S({features:t}),()=>A({features:t}),()=>F({features:t}),()=>A({features:t})],n).join("\n\n")}function k(){return"*-_".charAt(e(0,2)).repeat(e(3,6))}function S({level:t=[1,6],size:n=[1,8],content:r}){const o=r||d({size:n});return`${"#".repeat(e(...t))} ${o}`}function O({lang:t,content:n,size:r,fenceChar:o="`"}){return"random"===o&&(o="`~".charAt(e(0,1))),n=n||B({lang:t,size:r}),`${o.repeat(3)}${t||""}\n${n}\n${o.repeat(3)}`}function A({size:t=[20,50]}){return d({size:t,decorates:["description",U()]})}function C({columns:t=[2,4],rows:n=[2,8]}){t=e(...t),n=e(...n);const r=[...K({size:1},t-1),{size:[3,8]}],o=[d({size:t,output:"array"}),r.map((()=>"---"))];for(let t=0;n-1>t;t++)o.push(r.map((({size:t})=>d({size:t}))));return o.map(J).join("\n")}function D({url:t,imageSize:e=[400,250],...n}={}){return t=t||s(e),``}const T=["ordered","bullet","task"];function F({features:t,type:n="random",count:r=[1,8],size:o=[5,15]}){r="number"==typeof r?r:e(...r);const s="random"===n?T[Math.floor(3*Math.random())]:n,i=[];for(;r>0;){const a=e(1,r);let u=A({features:t,size:o});a>1&&(u+="\n"+F({features:t,type:n,count:a-1,size:o})),i.push(W(s,u)),r-=a}return i.join("\n")}const H={"code-span":()=>["`","`"],"emphasis-1":()=>K(I(1),2),"emphasis-2":()=>K(I(2),2),"emphasis-3":()=>K(I(3),2),strikethrough:()=>["~","~"],link:({url:t="https://miso.ai"}={})=>["[",`](${t})`]};function I(t=[1,3]){return t="number"==typeof t?t:e(...t),"_*".charAt(e(0,1)).repeat(t)}const L=Object.keys(H),P=new Set(L);function U({features:t=L,size:n=[1,3],rest:r=[0,8]}={}){t=t.filter((t=>P.has(t)));const s=o([...t]);let i=s.length-1;const a=()=>"number"==typeof r?r:e(...r);return function*(r){let o,u,c=a();for(const l of r)if(u&&(yield u),0===c)if(o)u=`${l}${o}`,o=void 0,c=a();else{const[r,a]=H[0>i?t[e(0,t.length-1)]:s[i--]]();u=`${r}${l}`,o=a,c="number"==typeof n?n:e(...n)}else u=l,c--;u&&(yield void 0!==o?`${u}${o}`:u)}}function E({size:t=[1,3],content:e}={}){return e||d({size:t})}function R(t,e){return e.split("\n").map((e=>" ".repeat(t)+e)).join("\n")}function W(t,e){const[n,r]=e.split("\n",2),o=`${function(t,e="random"){switch(t){case"ordered":return"1.";case"bullet":return"-";case"task":return`- [${("random"===e?.5>Math.random():e)?"x":" "}]`;default:throw Error("unknown list item type: "+t)}}(t)} ${n}`;return r?o+"\n"+R("ordered"===t?3:2,r):o}function B({lang:t,size:e=[10,30]}){return t&&x[t]?x[t]():d({output:"multiline",size:e})}function J(t){return`| ${t.join(" | ")} |`}function K(t,e){const n=[];for(let r=0;e>r;r++)n.push("function"==typeof t?t():t);return n}var G=Object.freeze({__proto__:null,atxHeading:S,blockquote:function({size:t=[3,5]}){return d({size:t}).split("\n").map((t=>"> "+t)).join("\n")},codeSpan:function(t){return`\`${E(t)}\``},decorate:U,emphasis:function({level:t=[1,3],options:n}){t="number"==typeof t?t:e(...t);const r="_*".charAt(e(0,1)).repeat(t);return`${r}${E(n)}${r}`},fencedCodeBlock:O,hr:k,image:D,indent:R,indentedCodeBlock:function({lang:t,content:e,size:n}){return R(4,e=e||B({lang:t,size:n}))},link:function({url:t="https://miso.ai",...e}={}){return`[${E(e)}](${t})`},linkReferenceDefinition:function({label:t,destination:e,title:n}){return`[${t}]: ${e}${void 0!==n?" "+n:""}`},list:F,listItem:W,markdown:j,paragraph:A,setextHeading:function({level:t=[1,2],size:n=[1,8],content:r}){return`${r||d({size:n})}\n${"=-".charAt(e(...t)-1).repeat(3)}`},table:C});function N({size:t=300}={}){return s(t)}function Q({size:t=[1,3]}={}){return d({size:t,decorates:["title"],output:"array"})}function V({size:t=[1,4]}={}){return d({size:t,output:"array"})}function Z({size:t=[2,6]}={}){return d({size:t,decorates:["title"]})}function X({size:t=[10,20],...e}={}){return d({size:t,decorates:[Object.keys(e).length?["description",e]:"description"]})}function Y({paragraphs:t=8,sections:e,paragraph:n,image:r}={}){n={size:[30,60],...n},void 0===e&&(e=Math.floor(t*(1+Math.random())/4));const o=[],s=t/(e=Math.max(1,Math.min(e,t)));let i=0;for(let t=0;e>t;t++){for(t>0&&o.push(`<h4>${Z()}</h4>`);i++<(t+1)*s;)o.push(`<p>${X(n)}</p>`);o.push(`<div class="image-container"><img src="${N(r)}"></div>`)}return o.join("")}function tt(){return Math.random()>.3?"IN_STOCK":"OUT_OF_STOCK"}function et(){return Math.floor(1e4*Math.random())/100}function nt(){return Math.floor(500*Math.random())/100+1}function rt({format:t,sampling:e,features:n}){return"markdown"===t?j({sampling:e,features:n}):d({min:a(50,e),max:a(50,e),decorates:["description"]})}function*ot({rows:t,...e}={}){for(let n=0;t>n;n++)yield st({...e,index:n})}function st({}={}){const t=r(),e=n(et,[1,2]);return e.sort(),{product_id:t,authors:Q(),categories:[],tags:V(),title:Z(),description:X(),cover_image:N(),url:"https://dummy.miso.ai/products/"+t,sale_price:e[0],original_price:e[e.length-1],rating:nt(),availability:tt()}}function*it({rows:t,...e}={}){for(let n=0;t>n;n++)yield at({...e,index:n})}function at({}={}){const t=r();return{product_id:t,authors:Q(),categories:[],tags:V(),title:Z({size:[4,10]}),snippet:X({size:[20,40]}),html:Y(),cover_image:N(),url:"/products/"+t}}function*ut({rows:t=5,...e}={}){for(let n=0;t>n;n++)yield ct({...e,index:n})}function ct({}={}){return X({size:[4,8],punctuation:"?"})}function lt({question:e,parent_question_id:n,timestamp:r=Date.now()},{answerFormat:o="markdown",answerSampling:s,answerLanguages:a=[]}={}){const u=t(),c=i(r),l=void 0!==s?Math.max(0,Math.min(1,s)):void 0,f=rt({format:o,sampling:l,features:a.length?a.map((t=>"lang-"+t)):void 0}),d=[...it({rows:ft(6,8,l)})].map(dt);return{question:e,question_id:u,...n&&{parent_question_id:n},datetime:c,answer:f,sources:[...it({rows:ft(4,6,l)})].map(dt),related_resources:d,followup_questions:[...ut({rows:ft(3,6)})]}}function ft(t,n,r){return e(a(t,r),a(n,r))}function dt({html:t,...e}){return e}var pt=Object.freeze({__proto__:null,answer:lt,articles:it,fields:Object.freeze({__proto__:null,answer:rt,authors:Q,availability:tt,description:X,html:Y,image:N,price:et,rating:nt,tags:V,title:Z}),lorem:$,md:G,products:ot,questions:ut,utils:l});const mt=[{name:"fetch",duration:1.5,text:"Checking the question and fetching results... "},{name:"verify",duration:1.5,text:"Verifying results... "},{name:"generate",duration:1.5,text:"Generating answer... "}];class ht{constructor(t={}){this._options=t,this._answers=new Map}questions(t,e={}){const n=new gt(t,{...this._options,...e}),{question_id:r}=n;return this._answers.set(r,n),{question_id:r}}answer(t){const e=this._answers.get(t);if(!e){const e=Error("Question not found: "+t);throw e.status=404,e}return e.get()}related_questions(t){return{related_questions:[...ut(t)]}}}class gt{constructor({question:t,parent_question_id:e},{answerFormat:n,answerSampling:r,answerLanguages:o,...s}={}){this._options=Object.freeze(s);const i=this.timestamp=Date.now();this._data=lt({question:t,parent_question_id:e,timestamp:i},{answerFormat:n,answerSampling:r,answerLanguages:o})}get question_id(){return this._data.question_id}get(){const t=(Date.now()-this.timestamp)*(this._options.speedRate||1)/1e3,[e,n,r]=this._answer(t),o=this._sources(t,r),s=this._related_resources(t,r),i=this._followup_questions(t,r),{question_id:a,question:u,datetime:c,parent_question_id:l}=this._data;return{answer:n,answer_stage:e,datetime:c,finished:r,parent_question_id:l,question:u,question_id:a,sources:o,related_resources:s,followup_questions:i}}_answer(t){for(const e of mt)if(0>(t-=e.duration))return[e.name,e.text,!1];const{answer:e}=this._data,n=Math.floor(100*t),r=n>=e.length;return["result",r?e:e.slice(0,n),r]}_sources(t,e){const{sources:n}=this._data;if(e)return n;const{length:r}=n;return n.slice(0,Math.floor(r*t/3))}_related_resources(t,e){const{related_resources:n}=this._data;if(e)return n;const{length:r}=n;return n.slice(0,Math.floor(r*t/3))}_followup_questions(t,e){const{followup_questions:n}=this._data;if(e||!n)return n;const{length:r}=n;return n.slice(0,Math.floor(r*t/3))}}class _t{constructor(t){this._options=t}search({rows:t=5}){return{products:[...ot({rows:t})]}}}class wt{constructor(t){this._options=t}user_to_products({rows:t=5}){return{products:[...ot({rows:t})]}}product_to_products({rows:t=5}){return{products:[...ot({rows:t})]}}}class bt{constructor(t){this._options=t}upload(t){return[]}}class yt{constructor(t){this._options=t}upload(t){return[]}ids(){const t=[];for(let e=0;5e3>e;e++)t.push(vt(e));return{ids:t}}}function vt(t){return`${10>t?"p_000":100>t?"p_00":1e3>t?"p_0":"p_"}${t}`}class zt{constructor(t){this.ask=new ht(t),this.search=new _t(t),this.recommendation=new wt(t),this.interactions=new bt(t),this.products=new yt(t)}}return{buildApi:(...t)=>new zt(...t),data:pt}}));
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miso.ai/doggoganger",
|
|
3
|
-
"version": "0.9.1-beta.
|
|
3
|
+
"version": "0.9.1-beta.11",
|
|
4
4
|
"description": "A dummy miso endpoint for demo and testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"doggoganger": "cli/index.js"
|
|
8
8
|
},
|
|
9
|
+
"main": "src/index.js",
|
|
10
|
+
"module": "src/index.js",
|
|
9
11
|
"publishConfig": {
|
|
10
12
|
"access": "public"
|
|
11
13
|
},
|
package/src/api/ask.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { answer } from '../data/index.js';
|
|
1
|
+
import { answer, questions } from '../data/index.js';
|
|
2
2
|
|
|
3
3
|
const CPS = 100;
|
|
4
4
|
const ITEMS_LOADING_TIME = 3;
|
|
@@ -28,7 +28,26 @@ export default class Ask {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
questions(payload, options = {}) {
|
|
31
|
-
|
|
31
|
+
const answer = new Answer(payload, { ...this._options, ...options });
|
|
32
|
+
const { question_id } = answer;
|
|
33
|
+
this._answers.set(question_id, answer);
|
|
34
|
+
return { question_id };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
answer(questionId) {
|
|
38
|
+
const answer = this._answers.get(questionId);
|
|
39
|
+
if (!answer) {
|
|
40
|
+
const error = new Error(`Question not found: ${questionId}`);
|
|
41
|
+
error.status = 404;
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
return answer.get();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
related_questions(payload) {
|
|
48
|
+
return {
|
|
49
|
+
related_questions: [...questions(payload)],
|
|
50
|
+
};
|
|
32
51
|
}
|
|
33
52
|
|
|
34
53
|
}
|
package/src/api.js
ADDED
package/src/browser.js
CHANGED
package/src/data/answers.js
CHANGED
|
@@ -12,8 +12,8 @@ export function answer({ question, parent_question_id, timestamp = Date.now() },
|
|
|
12
12
|
const features = answerLanguages.length ? answerLanguages.map(language => `lang-${language}`) : undefined;
|
|
13
13
|
|
|
14
14
|
const answer = fields.answer({ format: answerFormat, sampling, features });
|
|
15
|
-
const related_resources = [...articles({ rows: sampleRandomInt(6, 8, sampling) })];
|
|
16
|
-
const sources = [...articles({ rows: sampleRandomInt(4, 6, sampling) })];
|
|
15
|
+
const related_resources = [...articles({ rows: sampleRandomInt(6, 8, sampling) })].map(excludeHtml);
|
|
16
|
+
const sources = [...articles({ rows: sampleRandomInt(4, 6, sampling) })].map(excludeHtml);
|
|
17
17
|
const followup_questions = [...questions({ rows: sampleRandomInt(3, 6) })];
|
|
18
18
|
|
|
19
19
|
return {
|
|
@@ -31,3 +31,7 @@ export function answer({ question, parent_question_id, timestamp = Date.now() },
|
|
|
31
31
|
function sampleRandomInt(min, max, sampling) {
|
|
32
32
|
return randomInt(sample(min, sampling), sample(max, sampling));
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
function excludeHtml({ html, ...rest }) {
|
|
36
|
+
return rest;
|
|
37
|
+
}
|
package/src/data/articles.js
CHANGED
package/src/data/fields.js
CHANGED
|
@@ -38,6 +38,30 @@ export function description({ size = [10, 20], ...options } = {}) {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
export function html({ paragraphs = 8, sections, paragraph, image: imageOptions } = {}) {
|
|
42
|
+
paragraph = {
|
|
43
|
+
size: [30, 60],
|
|
44
|
+
...paragraph,
|
|
45
|
+
};
|
|
46
|
+
if (sections === undefined) {
|
|
47
|
+
sections = Math.floor(paragraphs * (1 + Math.random()) / 4);
|
|
48
|
+
}
|
|
49
|
+
sections = Math.max(1, Math.min(sections, paragraphs));
|
|
50
|
+
const content = [];
|
|
51
|
+
const pps = paragraphs / sections;
|
|
52
|
+
let j = 0;
|
|
53
|
+
for (let i = 0; i < sections; i++) {
|
|
54
|
+
if (i > 0) {
|
|
55
|
+
content.push(`<h4>${title()}</h4>`);
|
|
56
|
+
}
|
|
57
|
+
while (j++ < (i + 1) * pps) {
|
|
58
|
+
content.push(`<p>${description(paragraph)}</p>`);
|
|
59
|
+
}
|
|
60
|
+
content.push(`<div class="image-container"><img src="${image(imageOptions)}"></div>`);
|
|
61
|
+
}
|
|
62
|
+
return content.join('');
|
|
63
|
+
}
|
|
64
|
+
|
|
41
65
|
export function availability() {
|
|
42
66
|
return Math.random() > 0.3 ? 'IN_STOCK' : 'OUT_OF_STOCK';
|
|
43
67
|
}
|
package/src/data/index.js
CHANGED
package/src/data/products.js
CHANGED
|
@@ -21,7 +21,7 @@ function product({} = {}) {
|
|
|
21
21
|
description: fields.description(),
|
|
22
22
|
//html,
|
|
23
23
|
cover_image: fields.image(),
|
|
24
|
-
url:
|
|
24
|
+
url: `https://dummy.miso.ai/products/${id}`,
|
|
25
25
|
sale_price: prices[0],
|
|
26
26
|
original_price: prices[prices.length - 1],
|
|
27
27
|
rating: fields.rating(),
|
package/src/data/questions.js
CHANGED
package/src/fetch.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { wrapResponse } from './route/utils.js';
|
|
2
|
+
|
|
3
|
+
export default async function fetch(api, url, { method = 'GET', body } = {}) {
|
|
4
|
+
method = method.toUpperCase();
|
|
5
|
+
if (typeof body === 'string') {
|
|
6
|
+
body = JSON.parse(body);
|
|
7
|
+
}
|
|
8
|
+
url = new URL(url);
|
|
9
|
+
const segments = url.pathname.substring(1).split('/');
|
|
10
|
+
if (segments[0] === 'v1') {
|
|
11
|
+
segments.splice(0, 1);
|
|
12
|
+
}
|
|
13
|
+
const group = segments[0];
|
|
14
|
+
let name = segments[1];
|
|
15
|
+
let type = 'query';
|
|
16
|
+
|
|
17
|
+
if (group === 'interactions' && segments.length === 1) {
|
|
18
|
+
name = 'upload';
|
|
19
|
+
type = 'data';
|
|
20
|
+
} else if (group === 'ask' && segments.length === 4 && segments[1] === 'questions' && segments[3] === 'answer') {
|
|
21
|
+
name = 'answer';
|
|
22
|
+
body = segments[2];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!api[group][name]) {
|
|
26
|
+
throw new Error(`Unknown path: ${url.pathname}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const fn = api[group][name].bind(api[group]);
|
|
30
|
+
const resBody = await wrapResponse(fn, type)(body);
|
|
31
|
+
|
|
32
|
+
return new Response(JSON.stringify(resBody), {
|
|
33
|
+
status: 200,
|
|
34
|
+
});
|
|
35
|
+
}
|
package/src/index.js
CHANGED
package/src/route/ask.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
-
import { parseBodyIfNecessary } from './utils.js';
|
|
2
|
+
import { handler, parseBodyIfNecessary } from './utils.js';
|
|
3
3
|
|
|
4
4
|
function getOptionsFromCtx(ctx) {
|
|
5
5
|
const speedRate = Number(ctx.get('x-speed-rate')) || undefined;
|
|
@@ -11,27 +11,21 @@ function getOptionsFromCtx(ctx) {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export default function(api) {
|
|
14
|
-
const answers = new Map();
|
|
15
14
|
const router = new Router();
|
|
16
15
|
|
|
17
16
|
router.post('/questions', (ctx) => {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
answers.set(question_id, answer);
|
|
22
|
-
ctx.body = JSON.stringify({ data: { question_id } });
|
|
17
|
+
const payload = parseBodyIfNecessary(ctx.request.body);
|
|
18
|
+
const data = api.ask.questions(payload, getOptionsFromCtx(ctx));
|
|
19
|
+
ctx.body = JSON.stringify({ data });
|
|
23
20
|
});
|
|
24
21
|
|
|
25
22
|
router.get('/questions/:id/answer', (ctx) => {
|
|
26
23
|
const { id } = ctx.params;
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
ctx.status = 404;
|
|
30
|
-
} else {
|
|
31
|
-
const data = answer.get();
|
|
32
|
-
ctx.body = JSON.stringify({ data });
|
|
33
|
-
}
|
|
24
|
+
const data = api.ask.answer(id);
|
|
25
|
+
ctx.body = JSON.stringify({ data });
|
|
34
26
|
});
|
|
35
27
|
|
|
28
|
+
router.post('/related_questions', handler(api.ask.related_questions, 'query'));
|
|
29
|
+
|
|
36
30
|
return router;
|
|
37
31
|
}
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
-
import {
|
|
2
|
+
import { handler } from './utils.js';
|
|
3
3
|
|
|
4
4
|
export default function(api) {
|
|
5
5
|
const router = new Router();
|
|
6
6
|
|
|
7
|
-
router.post('/', (
|
|
8
|
-
const { data } = parseBodyIfNecessary(ctx.request.body);
|
|
9
|
-
ctx.body = {
|
|
10
|
-
took: 5,
|
|
11
|
-
errors: false,
|
|
12
|
-
data: api.interactions.upload(data),
|
|
13
|
-
};
|
|
14
|
-
});
|
|
7
|
+
router.post('/', handler(api.interactions.upload, 'data'));
|
|
15
8
|
|
|
16
9
|
return router;
|
|
17
10
|
}
|
package/src/route/products.js
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
-
import {
|
|
2
|
+
import { handler } from './utils.js';
|
|
3
3
|
|
|
4
4
|
export default function(api) {
|
|
5
5
|
const router = new Router();
|
|
6
6
|
|
|
7
|
-
router.post('/', (
|
|
8
|
-
const { data } = parseBodyIfNecessary(ctx.request.body);
|
|
9
|
-
ctx.body = {
|
|
10
|
-
took: 5,
|
|
11
|
-
errors: false,
|
|
12
|
-
data: api.products.upload(data),
|
|
13
|
-
};
|
|
14
|
-
});
|
|
7
|
+
router.post('/', handler(api.products.upload, 'data'));
|
|
15
8
|
|
|
16
9
|
router.get('/_ids', (ctx) => {
|
|
17
10
|
ctx.body = {
|
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
-
import {
|
|
2
|
+
import { handler } from './utils.js';
|
|
3
3
|
|
|
4
4
|
export default function(api) {
|
|
5
5
|
const router = new Router();
|
|
6
6
|
|
|
7
|
-
router.post('/user_to_products', (
|
|
8
|
-
|
|
9
|
-
ctx.body = {
|
|
10
|
-
data: api.recommendation.user_to_products({ rows }),
|
|
11
|
-
};
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
router.post('/product_to_products', (ctx) => {
|
|
15
|
-
const { rows = 5 } = parseBodyIfNecessary(ctx.request.body);
|
|
16
|
-
ctx.body = {
|
|
17
|
-
data: api.recommendation.product_to_products({ rows }),
|
|
18
|
-
};
|
|
19
|
-
});
|
|
7
|
+
router.post('/user_to_products', handler(api.recommendation.user_to_products, 'query'));
|
|
8
|
+
router.post('/product_to_products', handler(api.recommendation.product_to_products, 'query'));
|
|
20
9
|
|
|
21
10
|
return router;
|
|
22
11
|
}
|
package/src/route/search.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
-
import {
|
|
2
|
+
import { handler } from './utils.js';
|
|
3
3
|
|
|
4
4
|
export default function(api) {
|
|
5
5
|
const router = new Router();
|
|
6
6
|
|
|
7
|
-
router.post('/search', (
|
|
8
|
-
const { rows = 5 } = parseBodyIfNecessary(ctx.request.body);
|
|
9
|
-
ctx.body = {
|
|
10
|
-
data: api.search.search({ rows }),
|
|
11
|
-
};
|
|
12
|
-
});
|
|
7
|
+
router.post('/search', handler(api.search.search, 'query'));
|
|
13
8
|
|
|
14
9
|
return router;
|
|
15
10
|
}
|
package/src/route/utils.js
CHANGED
|
@@ -4,3 +4,49 @@ export function parseBodyIfNecessary(body) {
|
|
|
4
4
|
}
|
|
5
5
|
return body;
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
export function handler(fn, response) {
|
|
9
|
+
fn = wrapResponse(fn, response);
|
|
10
|
+
return async (ctx) => {
|
|
11
|
+
try {
|
|
12
|
+
ctx.body = await fn(parseBodyIfNecessary(ctx.request.body));
|
|
13
|
+
} catch (error) {
|
|
14
|
+
ctx.status = error.status || 500;
|
|
15
|
+
ctx.body = {
|
|
16
|
+
errors: true,
|
|
17
|
+
message: error.message || '',
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function wrapResponse(fn, response) {
|
|
24
|
+
response = responseFunction(response);
|
|
25
|
+
return async (payload) => {
|
|
26
|
+
try {
|
|
27
|
+
return response(await fn(payload));
|
|
28
|
+
} catch (error) {
|
|
29
|
+
error.status = error.status || 500;
|
|
30
|
+
error.data = {
|
|
31
|
+
errors: true,
|
|
32
|
+
message: error.message,
|
|
33
|
+
};
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function responseFunction(response) {
|
|
40
|
+
if (typeof response === 'function') {
|
|
41
|
+
return response;
|
|
42
|
+
}
|
|
43
|
+
switch (response) {
|
|
44
|
+
case 'query':
|
|
45
|
+
// TODO: miso_id
|
|
46
|
+
return data => ({ data });
|
|
47
|
+
case 'data':
|
|
48
|
+
return data => ({ took: 5, errors: false, data });
|
|
49
|
+
default:
|
|
50
|
+
throw new Error(`Unknown response type: ${response}`);
|
|
51
|
+
}
|
|
52
|
+
}
|