UNPKG

use-query-rd

Version:

A drop in replacement for apollo client's `useQuery` hook with a return type that mimics Elm's RemoteData ADT

136 lines 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.andMap = exports.map2 = exports.map = exports.match = exports.fold = exports.isSuccess = exports.isFailure = exports.isLoading = exports.isInitialized = exports.success = exports.failure = exports.pending = exports.initialized = exports.Tags = void 0; var Tags; (function (Tags) { Tags["Initialized"] = "Initialized"; Tags["Pending"] = "Pending"; Tags["Failure"] = "Failure"; Tags["Success"] = "Success"; Tags["_"] = "_"; })(Tags = exports.Tags || (exports.Tags = {})); var initialized = function () { return ({ tag: Tags.Initialized }); }; exports.initialized = initialized; var pending = function () { return ({ tag: Tags.Pending }); }; exports.pending = pending; var failure = function (error) { return ({ tag: Tags.Failure, error: error }); }; exports.failure = failure; var success = function (data) { return ({ tag: Tags.Success, data: data }); }; exports.success = success; var isInitialized = function (rd) { return rd.tag === Tags.Initialized; }; exports.isInitialized = isInitialized; var isLoading = function (rd) { return rd.tag === Tags.Pending; }; exports.isLoading = isLoading; var isFailure = function (rd) { return rd.tag === Tags.Failure; }; exports.isFailure = isFailure; var isSuccess = function (rd) { return rd.tag === Tags.Success; }; exports.isSuccess = isSuccess; var fold = function (initialized, pending, failure, success) { return function (_) { switch (_.tag) { case Tags.Initialized: return initialized(); case Tags.Pending: return pending(); case Tags.Failure: return failure(_.error); case Tags.Success: return success(_.data); default: throw new TypeError('RemoteData case not matched'); } }; }; exports.fold = fold; var isComplete = function (matcher) { return matcher[Tags.Initialized] !== undefined && matcher[Tags.Pending] !== undefined && matcher[Tags.Failure] !== undefined && matcher[Tags.Success] !== undefined; }; var match = function (rd, matcher) { if (isComplete(matcher)) { return (0, exports.fold)(matcher[Tags.Initialized], matcher[Tags.Pending], matcher[Tags.Failure], matcher[Tags.Success])(rd); } switch (rd.tag) { case Tags.Initialized: { var initialized_1 = matcher[rd.tag]; if (initialized_1 !== undefined) { return initialized_1(); } else { return matcher[Tags._](); } } case Tags.Pending: { var pending_1 = matcher[rd.tag]; if (pending_1 !== undefined) { return pending_1(); } else { return matcher[Tags._](); } } case Tags.Failure: { var failure_1 = matcher[rd.tag]; if (failure_1 !== undefined) { return failure_1(rd.error); } else { return matcher[Tags._](); } } case Tags.Success: { var success_1 = matcher[rd.tag]; if (success_1 !== undefined) { return success_1(rd.data); } else { return matcher[Tags._](); } } default: throw new TypeError('RemoteData case not matched'); } }; exports.match = match; var map = function (f, fa) { return (0, exports.isSuccess)(fa) ? (0, exports.success)(f(fa.data)) : fa; }; exports.map = map; /** * Combine two remote data sources with the given function. The result will succeed when (and if) both sources succeed. */ var map2 = function (f, rd1, rd2) { return (0, exports.andMap)(rd2, (0, exports.map)(f, rd1)); }; exports.map2 = map2; /** * Put the results of two RemoteData calls together. * @see https://github.com/krisajenkins/remotedata/blob/6.0.1/src/RemoteData.elm#L361 */ var andMap = function (rd1, rd2) { return (0, exports.match)(rd2, { Initialized: function () { return (0, exports.initialized)(); }, Pending: function () { return (0, exports.pending)(); }, Success: function (f) { return (0, exports.map)(f, rd1); }, Failure: function (e) { return (0, exports.failure)(e); } }); }; exports.andMap = andMap; //# sourceMappingURL=rd.js.map