@trpc/client
Advanced tools
Comparing version 1.4.1 to 1.4.3
@@ -900,2 +900,6 @@ 'use strict'; | ||
var retryDelay = function retryDelay(attemptIndex) { | ||
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000); | ||
}; | ||
var TRPCClientError = /*#__PURE__*/function (_Error) { | ||
@@ -921,3 +925,17 @@ _inheritsLoose(TRPCClientError, _Error); | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
var NextInputError = /*#__PURE__*/function (_Error2) { | ||
_inheritsLoose(NextInputError, _Error2); | ||
function NextInputError(originalError) { | ||
var _this2; | ||
_this2 = _Error2.call(this, "nextInput() threw an error - subscription is stopped: " + originalError.message) || this; | ||
_this2.originalError = originalError; | ||
Object.setPrototypeOf(_assertThisInitialized(_this2), NextInputError.prototype); | ||
return _this2; | ||
} | ||
return NextInputError; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
function getAbortController(ac) { | ||
@@ -981,25 +999,25 @@ if (ac) { | ||
function _handleResponse() { | ||
_handleResponse = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(promise) { | ||
_handleResponse = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(promise) { | ||
var res, json, rawJson, err; | ||
return runtime_1.wrap(function _callee4$(_context4) { | ||
return runtime_1.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
res = null; | ||
json = null; | ||
_context4.prev = 2; | ||
_context4.next = 5; | ||
_context5.prev = 2; | ||
_context5.next = 5; | ||
return promise; | ||
case 5: | ||
res = _context4.sent; | ||
_context4.next = 8; | ||
res = _context5.sent; | ||
_context5.next = 8; | ||
return res.json(); | ||
case 8: | ||
rawJson = _context4.sent; | ||
rawJson = _context5.sent; | ||
json = transformer.deserialize(rawJson); | ||
if (!json.ok) { | ||
_context4.next = 13; | ||
_context5.next = 13; | ||
break; | ||
@@ -1009,3 +1027,3 @@ } | ||
opts.onSuccess && opts.onSuccess(json); | ||
return _context4.abrupt("return", json.data); | ||
return _context5.abrupt("return", json.data); | ||
@@ -1019,9 +1037,9 @@ case 13: | ||
case 16: | ||
_context4.prev = 16; | ||
_context4.t0 = _context4["catch"](2); | ||
err = _context4.t0; | ||
_context5.prev = 16; | ||
_context5.t0 = _context5["catch"](2); | ||
err = _context5.t0; | ||
if (!(err instanceof TRPCClientError)) { | ||
err = new TRPCClientError(_context4.t0.message, { | ||
originalError: _context4.t0, | ||
err = new TRPCClientError(_context5.t0.message, { | ||
originalError: _context5.t0, | ||
res: res, | ||
@@ -1037,6 +1055,6 @@ json: json | ||
case "end": | ||
return _context4.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee4, null, [[2, 16]]); | ||
}, _callee5, null, [[2, 16]]); | ||
})); | ||
@@ -1239,2 +1257,86 @@ return _handleResponse.apply(this, arguments); | ||
function subscription(path, opts) { | ||
var stopped = false; // let nextTry: any; // setting as `NodeJS.Timeout` causes compat issues, can probably be solved | ||
var currentPromise = null; | ||
var attemptIndex = 0; | ||
var unsubscribe = function unsubscribe() { | ||
var _currentPromise; | ||
stopped = true; | ||
(_currentPromise = currentPromise) == null ? void 0 : _currentPromise.cancel(); | ||
currentPromise = null; | ||
}; | ||
function exec(_x4) { | ||
return _exec2.apply(this, arguments); | ||
} | ||
function _exec2() { | ||
_exec2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(input) { | ||
var res, nextInput, err; | ||
return runtime_1.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.prev = 0; | ||
currentPromise = subscriptionOnce(path, input); | ||
_context4.next = 4; | ||
return currentPromise; | ||
case 4: | ||
res = _context4.sent; | ||
attemptIndex = 0; | ||
opts.onData && opts.onData(res); | ||
_context4.prev = 7; | ||
nextInput = opts.nextInput(res); | ||
exec(nextInput); | ||
_context4.next = 18; | ||
break; | ||
case 12: | ||
_context4.prev = 12; | ||
_context4.t0 = _context4["catch"](7); | ||
err = new NextInputError(_context4.t0); | ||
opts.onError && opts.onError(err); | ||
unsubscribe(); | ||
return _context4.abrupt("return"); | ||
case 18: | ||
_context4.next = 27; | ||
break; | ||
case 20: | ||
_context4.prev = 20; | ||
_context4.t1 = _context4["catch"](0); | ||
if (!stopped) { | ||
_context4.next = 24; | ||
break; | ||
} | ||
return _context4.abrupt("return"); | ||
case 24: | ||
opts.onError && opts.onError(_context4.t1); | ||
attemptIndex++; | ||
setTimeout(function () { | ||
exec(input); | ||
}, retryDelay(attemptIndex)); | ||
case 27: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, null, [[0, 20], [7, 12]]); | ||
})); | ||
return _exec2.apply(this, arguments); | ||
} | ||
exec(opts.initialInput); | ||
return unsubscribe; | ||
} | ||
return { | ||
@@ -1245,2 +1347,3 @@ request: request, | ||
subscriptionOnce: subscriptionOnce, | ||
subscription: subscription, | ||
transformer: transformer | ||
@@ -1250,4 +1353,5 @@ }; | ||
exports.NextInputError = NextInputError; | ||
exports.TRPCClientError = TRPCClientError; | ||
exports.createTRPCClient = createTRPCClient; | ||
//# sourceMappingURL=client.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";function t(t,r,e,n,o,i,u){try{var a=t[i](u),c=a.value}catch(t){return void e(t)}a.done?r(c):Promise.resolve(c).then(n,o)}function r(r){return function(){var e=this,n=arguments;return new Promise((function(o,i){var u=r.apply(e,n);function a(r){t(u,o,i,a,c,"next",r)}function c(r){t(u,o,i,a,c,"throw",r)}a(void 0)}))}}function e(){return(e=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function n(t){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function o(t,r){return(o=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function u(t,r,e){return(u=i()?Reflect.construct:function(t,r,e){var n=[null];n.push.apply(n,r);var i=new(Function.bind.apply(t,n));return e&&o(i,e.prototype),i}).apply(null,arguments)}function a(t){var r="function"==typeof Map?new Map:void 0;return(a=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(t))return r.get(t);r.set(t,e)}function e(){return u(t,arguments,n(this).constructor)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),o(e,t)})(t)}function c(t,r){return t(r={exports:{}},r.exports),r.exports}Object.defineProperty(exports,"__esModule",{value:!0});var f=c((function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function a(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{a({},"")}catch(t){a=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=Object.create((r&&r.prototype instanceof s?r:s).prototype),i=new E(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(e.method=o,e.arg=i;;){var u=e.delegate;if(u){var a=b(u,e);if(a){if(a===l)continue;return a}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=f(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,i),o}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function s(){}function p(){}function h(){}var d={};d[o]=function(){return this};var v=Object.getPrototypeOf,y=v&&v(v(j([])));y&&y!==r&&e.call(y,o)&&(d=y);var g=h.prototype=s.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(r){a(t,r,(function(t){return this._invoke(r,t)}))}))}function m(t,r){var n;this._invoke=function(o,i){function u(){return new r((function(n,u){!function n(o,i,u,a){var c=f(t[o],t,i);if("throw"!==c.type){var l=c.arg,s=l.value;return s&&"object"==typeof s&&e.call(s,"__await")?r.resolve(s.__await).then((function(t){n("next",t,u,a)}),(function(t){n("throw",t,u,a)})):r.resolve(s).then((function(t){l.value=t,u(l)}),(function(t){return n("throw",t,u,a)}))}a(c.arg)}(o,i,n,u)}))}return n=n?n.then(u,u):u()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return l;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,l;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}function x(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function O(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function j(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return i.next=i}}return{next:_}}function _(){return{value:void 0,done:!0}}return p.prototype=g.constructor=h,h.constructor=p,p.displayName=a(h,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===p||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,a(t,u,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},w(m.prototype),m.prototype[i]=function(){return this},t.AsyncIterator=m,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var u=new m(c(r,e,n,o),i);return t.isGeneratorFunction(e)?u:u.next().then((function(t){return t.done?t.value:u.next()}))},w(g),a(g,u,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=j,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(O),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return u.type="throw",u.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],u=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var a=e.call(i,"catchLoc"),c=e.call(i,"finallyLoc");if(a&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(a){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var u=i?i.completion:{};return u.type=t,u.arg=r,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(u)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),l},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),O(e),l}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;O(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:j(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}})),l=function(t){var r,e;function n(r,e){var o,i=e.res,u=e.json,a=e.originalError;return(o=t.call(this,r)||this).message=r,o.res=i,o.json=u,o.originalError=a,Object.setPrototypeOf(function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(o),n.prototype),o}return e=t,(r=n).prototype=Object.create(e.prototype),r.prototype.constructor=r,r.__proto__=e,n}(a(Error));exports.TRPCClientError=l,exports.createTRPCClient=function(t){var n=t.fetchOpts,o=t.url,i=function(t){if(t)return t;if("undefined"!=typeof window&&window.fetch)return window.fetch;if("undefined"!=typeof global&&global.fetch)return global.fetch;throw new Error("No fetch implementation found")}(null==n?void 0:n.fetch),u=(null==n?void 0:n.AbortController)||("undefined"!=typeof window&&window.AbortController?window.AbortController:"undefined"!=typeof global&&global.AbortController?global.AbortController:null),a=t.transformer,c=void 0===a?{serialize:function(t){return t},deserialize:function(t){return t}}:a,s=function(t){return void 0!==t?c.serialize(t):t};function p(){return(p=r(f.mark((function r(e){var n,o,i;return f.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return n=null,o=null,r.prev=2,r.next=5,e;case 5:return n=r.sent,r.next=8,n.json();case 8:if(!(o=c.deserialize(r.sent)).ok){r.next=13;break}return t.onSuccess&&t.onSuccess(o),r.abrupt("return",o.data);case 13:throw new l(o.error.message,{json:o,res:n});case 16:throw r.prev=16,r.t0=r.catch(2),(i=r.t0)instanceof l||(i=new l(r.t0.message,{originalError:r.t0,res:n,json:o})),t.onError&&t.onError(i),i;case 22:case"end":return r.stop()}}),r,null,[[2,16]])})))).apply(this,arguments)}function h(r){var n=r.type,a=r.input,c=r.path,f={subscription:function(){return{method:"PATCH",body:JSON.stringify({input:s(a)}),url:o+"/"+c}},mutation:function(){return{method:"POST",body:JSON.stringify({input:s(a)}),url:o+"/"+c}},query:function(){return{method:"GET",url:o+"/"+c+(null!=a?"?input="+encodeURIComponent(JSON.stringify(s(a))):"")}}}[n];if(!f)throw new Error('Unhandled type "'+n+'"');var l=u?new u:null,h=f(),d=h.url,v=e({},function(t,r){if(null==t)return{};var e,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)r.indexOf(e=i[n])>=0||(o[e]=t[e]);return o}(h,["url"]),{signal:null==l?void 0:l.signal,headers:e({"content-type":"application/json"},t.getHeaders?t.getHeaders():{})}),y=function(t){return p.apply(this,arguments)}(i(d,v));return y.cancel=function(){null==l||l.abort()},y}var d=function(){var t=r(f.mark((function t(r){var e=arguments;return f.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",h({type:"query",path:r,input:e.length<=1?void 0:e[1]}));case 1:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),v=function(){var t=r(f.mark((function t(r){var e=arguments;return f.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",h({type:"mutation",path:r,input:e.length<=1?void 0:e[1]}));case 1:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();return{request:h,query:d,mutate:v,subscriptionOnce:function(t,e){var n=!1,o=null,i=new Promise((function(i,u){function a(){return c.apply(this,arguments)}function c(){return(c=r(f.mark((function r(){var c,l;return f.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(!n){r.next=2;break}return r.abrupt("return");case 2:return r.prev=2,o=h({type:"subscription",input:e,path:t}),r.next=6,o;case 6:i(r.sent),r.next=14;break;case 10:r.prev=10,r.t0=r.catch(2),408===(null==(c=(l=r.t0).json)?void 0:c.statusCode)?a():u(l);case 14:case"end":return r.stop()}}),r,null,[[2,10]])})))).apply(this,arguments)}a()}));return i.cancel=function(){var t;n=!0,clearTimeout(void 0),(null==(t=o)?void 0:t.cancel)&&o.cancel()},i},transformer:c}}; | ||
"use strict";function t(t,r,e,n,o,i,u){try{var a=t[i](u),c=a.value}catch(t){return void e(t)}a.done?r(c):Promise.resolve(c).then(n,o)}function r(r){return function(){var e=this,n=arguments;return new Promise((function(o,i){var u=r.apply(e,n);function a(r){t(u,o,i,a,c,"next",r)}function c(r){t(u,o,i,a,c,"throw",r)}a(void 0)}))}}function e(){return(e=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function n(t,r){t.prototype=Object.create(r.prototype),t.prototype.constructor=t,t.__proto__=r}function o(t){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function i(t,r){return(i=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function u(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function a(t,r,e){return(a=u()?Reflect.construct:function(t,r,e){var n=[null];n.push.apply(n,r);var o=new(Function.bind.apply(t,n));return e&&i(o,e.prototype),o}).apply(null,arguments)}function c(t){var r="function"==typeof Map?new Map:void 0;return(c=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(t))return r.get(t);r.set(t,e)}function e(){return a(t,arguments,o(this).constructor)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),i(e,t)})(t)}function s(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function f(t,r){return t(r={exports:{}},r.exports),r.exports}Object.defineProperty(exports,"__esModule",{value:!0});var l=f((function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function a(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{a({},"")}catch(t){a=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=Object.create((r&&r.prototype instanceof l?r:l).prototype),i=new O(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(e.method=o,e.arg=i;;){var u=e.delegate;if(u){var a=b(u,e);if(a){if(a===f)continue;return a}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=s(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===f)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,i),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f={};function l(){}function p(){}function h(){}var v={};v[o]=function(){return this};var y=Object.getPrototypeOf,d=y&&y(y(j([])));d&&d!==r&&e.call(d,o)&&(v=d);var g=h.prototype=l.prototype=Object.create(v);function w(t){["next","throw","return"].forEach((function(r){a(t,r,(function(t){return this._invoke(r,t)}))}))}function m(t,r){var n;this._invoke=function(o,i){function u(){return new r((function(n,u){!function n(o,i,u,a){var c=s(t[o],t,i);if("throw"!==c.type){var f=c.arg,l=f.value;return l&&"object"==typeof l&&e.call(l,"__await")?r.resolve(l.__await).then((function(t){n("next",t,u,a)}),(function(t){n("throw",t,u,a)})):r.resolve(l).then((function(t){f.value=t,u(f)}),(function(t){return n("throw",t,u,a)}))}a(c.arg)}(o,i,n,u)}))}return n=n?n.then(u,u):u()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return f;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=s(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,f;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,f):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function x(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function E(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function j(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return i.next=i}}return{next:_}}function _(){return{value:void 0,done:!0}}return p.prototype=g.constructor=h,h.constructor=p,p.displayName=a(h,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===p||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,a(t,u,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},w(m.prototype),m.prototype[i]=function(){return this},t.AsyncIterator=m,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var u=new m(c(r,e,n,o),i);return t.isGeneratorFunction(e)?u:u.next().then((function(t){return t.done?t.value:u.next()}))},w(g),a(g,u,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=j,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return u.type="throw",u.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],u=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var a=e.call(i,"catchLoc"),c=e.call(i,"finallyLoc");if(a&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(a){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var u=i?i.completion:{};return u.type=t,u.arg=r,i?(this.method="next",this.next=i.finallyLoc,f):this.complete(u)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),f},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),E(e),f}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;E(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:j(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}})),p=function(t){return 0===t?0:Math.min(1e3*Math.pow(2,t),3e4)},h=function(t){function r(e,n){var o,i=n.res,u=n.json,a=n.originalError;return(o=t.call(this,e)||this).message=e,o.res=i,o.json=u,o.originalError=a,Object.setPrototypeOf(s(o),r.prototype),o}return n(r,t),r}(c(Error)),v=function(t){function r(e){var n;return(n=t.call(this,"nextInput() threw an error - subscription is stopped: "+e.message)||this).originalError=e,Object.setPrototypeOf(s(n),r.prototype),n}return n(r,t),r}(c(Error));exports.NextInputError=v,exports.TRPCClientError=h,exports.createTRPCClient=function(t){var n=t.fetchOpts,o=t.url,i=function(t){if(t)return t;if("undefined"!=typeof window&&window.fetch)return window.fetch;if("undefined"!=typeof global&&global.fetch)return global.fetch;throw new Error("No fetch implementation found")}(null==n?void 0:n.fetch),u=(null==n?void 0:n.AbortController)||("undefined"!=typeof window&&window.AbortController?window.AbortController:"undefined"!=typeof global&&global.AbortController?global.AbortController:null),a=t.transformer,c=void 0===a?{serialize:function(t){return t},deserialize:function(t){return t}}:a,s=function(t){return void 0!==t?c.serialize(t):t};function f(){return(f=r(l.mark((function r(e){var n,o,i;return l.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return n=null,o=null,r.prev=2,r.next=5,e;case 5:return n=r.sent,r.next=8,n.json();case 8:if(!(o=c.deserialize(r.sent)).ok){r.next=13;break}return t.onSuccess&&t.onSuccess(o),r.abrupt("return",o.data);case 13:throw new h(o.error.message,{json:o,res:n});case 16:throw r.prev=16,r.t0=r.catch(2),(i=r.t0)instanceof h||(i=new h(r.t0.message,{originalError:r.t0,res:n,json:o})),t.onError&&t.onError(i),i;case 22:case"end":return r.stop()}}),r,null,[[2,16]])})))).apply(this,arguments)}function y(r){var n=r.type,a=r.input,c=r.path,l={subscription:function(){return{method:"PATCH",body:JSON.stringify({input:s(a)}),url:o+"/"+c}},mutation:function(){return{method:"POST",body:JSON.stringify({input:s(a)}),url:o+"/"+c}},query:function(){return{method:"GET",url:o+"/"+c+(null!=a?"?input="+encodeURIComponent(JSON.stringify(s(a))):"")}}}[n];if(!l)throw new Error('Unhandled type "'+n+'"');var p=u?new u:null,h=l(),v=h.url,y=e({},function(t,r){if(null==t)return{};var e,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)r.indexOf(e=i[n])>=0||(o[e]=t[e]);return o}(h,["url"]),{signal:null==p?void 0:p.signal,headers:e({"content-type":"application/json"},t.getHeaders?t.getHeaders():{})}),d=function(t){return f.apply(this,arguments)}(i(v,y));return d.cancel=function(){null==p||p.abort()},d}var d=function(){var t=r(l.mark((function t(r){var e=arguments;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",y({type:"query",path:r,input:e.length<=1?void 0:e[1]}));case 1:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),g=function(){var t=r(l.mark((function t(r){var e=arguments;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",y({type:"mutation",path:r,input:e.length<=1?void 0:e[1]}));case 1:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();function w(t,e){var n=!1,o=null,i=new Promise((function(i,u){function a(){return c.apply(this,arguments)}function c(){return(c=r(l.mark((function r(){var c,s;return l.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(!n){r.next=2;break}return r.abrupt("return");case 2:return r.prev=2,o=y({type:"subscription",input:e,path:t}),r.next=6,o;case 6:i(r.sent),r.next=14;break;case 10:r.prev=10,r.t0=r.catch(2),408===(null==(c=(s=r.t0).json)?void 0:c.statusCode)?a():u(s);case 14:case"end":return r.stop()}}),r,null,[[2,10]])})))).apply(this,arguments)}a()}));return i.cancel=function(){var t;n=!0,clearTimeout(void 0),(null==(t=o)?void 0:t.cancel)&&o.cancel()},i}return{request:y,query:d,mutate:g,subscriptionOnce:w,subscription:function(t,e){var n=!1,o=null,i=0,u=function(){var t;n=!0,null==(t=o)||t.cancel(),o=null};function a(t){return c.apply(this,arguments)}function c(){return(c=r(l.mark((function r(c){var s,f;return l.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,o=w(t,c),r.next=4,o;case 4:s=r.sent,i=0,e.onData&&e.onData(s),r.prev=7,a(e.nextInput(s)),r.next=18;break;case 12:return r.prev=12,r.t0=r.catch(7),f=new v(r.t0),e.onError&&e.onError(f),u(),r.abrupt("return");case 18:r.next=27;break;case 20:if(r.prev=20,r.t1=r.catch(0),!n){r.next=24;break}return r.abrupt("return");case 24:e.onError&&e.onError(r.t1),i++,setTimeout((function(){a(c)}),p(i));case 27:case"end":return r.stop()}}),r,null,[[0,20],[7,12]])})))).apply(this,arguments)}return a(e.initialInput),u},transformer:c}}; | ||
//# sourceMappingURL=client.cjs.production.min.js.map |
@@ -896,2 +896,6 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
var retryDelay = function retryDelay(attemptIndex) { | ||
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000); | ||
}; | ||
var TRPCClientError = /*#__PURE__*/function (_Error) { | ||
@@ -917,3 +921,17 @@ _inheritsLoose(TRPCClientError, _Error); | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
var NextInputError = /*#__PURE__*/function (_Error2) { | ||
_inheritsLoose(NextInputError, _Error2); | ||
function NextInputError(originalError) { | ||
var _this2; | ||
_this2 = _Error2.call(this, "nextInput() threw an error - subscription is stopped: " + originalError.message) || this; | ||
_this2.originalError = originalError; | ||
Object.setPrototypeOf(_assertThisInitialized(_this2), NextInputError.prototype); | ||
return _this2; | ||
} | ||
return NextInputError; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
function getAbortController(ac) { | ||
@@ -977,25 +995,25 @@ if (ac) { | ||
function _handleResponse() { | ||
_handleResponse = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(promise) { | ||
_handleResponse = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(promise) { | ||
var res, json, rawJson, err; | ||
return runtime_1.wrap(function _callee4$(_context4) { | ||
return runtime_1.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
res = null; | ||
json = null; | ||
_context4.prev = 2; | ||
_context4.next = 5; | ||
_context5.prev = 2; | ||
_context5.next = 5; | ||
return promise; | ||
case 5: | ||
res = _context4.sent; | ||
_context4.next = 8; | ||
res = _context5.sent; | ||
_context5.next = 8; | ||
return res.json(); | ||
case 8: | ||
rawJson = _context4.sent; | ||
rawJson = _context5.sent; | ||
json = transformer.deserialize(rawJson); | ||
if (!json.ok) { | ||
_context4.next = 13; | ||
_context5.next = 13; | ||
break; | ||
@@ -1005,3 +1023,3 @@ } | ||
opts.onSuccess && opts.onSuccess(json); | ||
return _context4.abrupt("return", json.data); | ||
return _context5.abrupt("return", json.data); | ||
@@ -1015,9 +1033,9 @@ case 13: | ||
case 16: | ||
_context4.prev = 16; | ||
_context4.t0 = _context4["catch"](2); | ||
err = _context4.t0; | ||
_context5.prev = 16; | ||
_context5.t0 = _context5["catch"](2); | ||
err = _context5.t0; | ||
if (!(err instanceof TRPCClientError)) { | ||
err = new TRPCClientError(_context4.t0.message, { | ||
originalError: _context4.t0, | ||
err = new TRPCClientError(_context5.t0.message, { | ||
originalError: _context5.t0, | ||
res: res, | ||
@@ -1033,6 +1051,6 @@ json: json | ||
case "end": | ||
return _context4.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee4, null, [[2, 16]]); | ||
}, _callee5, null, [[2, 16]]); | ||
})); | ||
@@ -1235,2 +1253,86 @@ return _handleResponse.apply(this, arguments); | ||
function subscription(path, opts) { | ||
var stopped = false; // let nextTry: any; // setting as `NodeJS.Timeout` causes compat issues, can probably be solved | ||
var currentPromise = null; | ||
var attemptIndex = 0; | ||
var unsubscribe = function unsubscribe() { | ||
var _currentPromise; | ||
stopped = true; | ||
(_currentPromise = currentPromise) == null ? void 0 : _currentPromise.cancel(); | ||
currentPromise = null; | ||
}; | ||
function exec(_x4) { | ||
return _exec2.apply(this, arguments); | ||
} | ||
function _exec2() { | ||
_exec2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(input) { | ||
var res, nextInput, err; | ||
return runtime_1.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.prev = 0; | ||
currentPromise = subscriptionOnce(path, input); | ||
_context4.next = 4; | ||
return currentPromise; | ||
case 4: | ||
res = _context4.sent; | ||
attemptIndex = 0; | ||
opts.onData && opts.onData(res); | ||
_context4.prev = 7; | ||
nextInput = opts.nextInput(res); | ||
exec(nextInput); | ||
_context4.next = 18; | ||
break; | ||
case 12: | ||
_context4.prev = 12; | ||
_context4.t0 = _context4["catch"](7); | ||
err = new NextInputError(_context4.t0); | ||
opts.onError && opts.onError(err); | ||
unsubscribe(); | ||
return _context4.abrupt("return"); | ||
case 18: | ||
_context4.next = 27; | ||
break; | ||
case 20: | ||
_context4.prev = 20; | ||
_context4.t1 = _context4["catch"](0); | ||
if (!stopped) { | ||
_context4.next = 24; | ||
break; | ||
} | ||
return _context4.abrupt("return"); | ||
case 24: | ||
opts.onError && opts.onError(_context4.t1); | ||
attemptIndex++; | ||
setTimeout(function () { | ||
exec(input); | ||
}, retryDelay(attemptIndex)); | ||
case 27: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, null, [[0, 20], [7, 12]]); | ||
})); | ||
return _exec2.apply(this, arguments); | ||
} | ||
exec(opts.initialInput); | ||
return unsubscribe; | ||
} | ||
return { | ||
@@ -1241,2 +1343,3 @@ request: request, | ||
subscriptionOnce: subscriptionOnce, | ||
subscription: subscription, | ||
transformer: transformer | ||
@@ -1246,3 +1349,3 @@ }; | ||
export { TRPCClientError, createTRPCClient }; | ||
export { NextInputError, TRPCClientError, createTRPCClient }; | ||
//# sourceMappingURL=client.esm.js.map |
@@ -1,2 +0,2 @@ | ||
import type { AnyRouter, DataTransformer, HTTPResponseEnvelope, HTTPSuccessResponseEnvelope, inferHandlerFn, inferRouteInput, inferSubscriptionOutput, Maybe } from '@trpc/server'; | ||
import type { AnyRouter, DataTransformer, HTTPResponseEnvelope, HTTPSuccessResponseEnvelope, inferHandlerFn, inferRouteInput, Maybe } from '@trpc/server'; | ||
declare type CancelFn = () => void; | ||
@@ -16,2 +16,6 @@ declare type CancellablePromise<T = unknown> = Promise<T> & { | ||
} | ||
export declare class NextInputError extends Error { | ||
readonly originalError: Error; | ||
constructor(originalError: Error); | ||
} | ||
export interface FetchOptions { | ||
@@ -41,3 +45,12 @@ fetch?: typeof fetch; | ||
mutate: inferHandlerFn<TRouter["_def"]["mutations"]>; | ||
subscriptionOnce: <TPath extends keyof TRouter["_def"]["subscriptions"] & string, TInput extends inferRouteInput<TRouter["_def"]["subscriptions"][TPath]>>(path: TPath, input: TInput) => CancellablePromise<inferSubscriptionOutput<TRouter, TPath>[]>; | ||
subscriptionOnce: <TPath extends keyof TRouter["_def"]["subscriptions"] & string, TInput extends inferRouteInput<TRouter["_def"]["subscriptions"][TPath]>, TOutput extends ReturnType<import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["subscriptions"][TPath]["resolve"]>>["output"]>>(path: TPath, input: TInput) => CancellablePromise<TOutput[]>; | ||
subscription: <TPath_1 extends keyof TRouter["_def"]["subscriptions"] & string, TInput_1 extends inferRouteInput<TRouter["_def"]["subscriptions"][TPath_1]>, TOutput_1 extends ReturnType<import("@trpc/server").ThenArg<ReturnType<TRouter["_def"]["subscriptions"][TPath_1]["resolve"]>>["output"]>>(path: TPath_1, opts: { | ||
initialInput: TInput_1; | ||
onError?: ((err: NextInputError | TRPCClientError) => void) | undefined; | ||
onData?: ((data: TOutput_1[]) => void) | undefined; | ||
/** | ||
* Input cursor for next call to subscription endpoint | ||
*/ | ||
nextInput: (data: TOutput_1[]) => TInput_1; | ||
}) => CancelFn; | ||
transformer: DataTransformer; | ||
@@ -44,0 +57,0 @@ }; |
{ | ||
"name": "@trpc/client", | ||
"version": "1.4.1", | ||
"version": "1.4.3", | ||
"description": "TRPC Client lib", | ||
@@ -30,3 +30,3 @@ "author": "KATT", | ||
"devDependencies": { | ||
"@trpc/server": "^1.4.1" | ||
"@trpc/server": "^1.4.3" | ||
}, | ||
@@ -36,3 +36,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "6c17f0dba789204be6386a2632f6073440e8604a" | ||
"gitHead": "3102802183e37cb08e1d48f636eb5bfef9a813e7" | ||
} |
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
274493
2332