Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@decathlon/moon

Package Overview
Dependencies
Maintainers
4
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@decathlon/moon - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

125

dist/hooks.js
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const shallow_equal_1 = require("./utils/shallow-equal");
const moon_provider_1 = require("./moon-provider");
var React = require("react");
var shallow_equal_1 = require("./utils/shallow-equal");
var moon_provider_1 = require("./moon-provider");
function useQueryResult(queryId, resultToProps) {
const { store } = useMoon();
const queryResult = store.readQuery(queryId).data;
const [state, setState] = React.useState(queryResult);
var store = useMoon().store;
var queryResult = store.readQuery(queryId).data;
var _a = React.useState(queryResult), state = _a[0], setState = _a[1];
store.subscribeToQueryResult(queryId, setState);
React.useEffect(() => {
return () => store.unsubscribeFromQueryResult(queryId, setState);
React.useEffect(function () {
return function () { return store.unsubscribeFromQueryResult(queryId, setState); };
}, [queryId]);

@@ -18,24 +29,29 @@ return resultToProps ? resultToProps(state) : state;

function useQueriesResults(queriesIds, resultsToProps) {
const { store } = useMoon();
const { value: currentQueriesIds } = usePrevValue(queriesIds);
const queriesStates = currentQueriesIds.reduce((result, queryId) => {
var store = useMoon().store;
var currentQueriesIds = usePrevValue(queriesIds).value;
var queriesStates = currentQueriesIds.reduce(function (result, queryId) {
result[queryId] = store.readQuery(queryId).data;
return result;
}, {});
const [states, setStates] = React.useState(queriesStates);
const setState = (queryId) => (result) => {
setStates(Object.assign(Object.assign({}, states), { [queryId]: result }));
};
const queriesHandlers = React.useMemo(() => currentQueriesIds.reduce((handlers, queryId) => {
handlers[queryId] = setState(queryId);
return handlers;
}, {}), [currentQueriesIds]);
const { prevValue: prevQueriesHandlers } = usePrevValue(queriesHandlers);
React.useEffect(() => {
currentQueriesIds.forEach(queryId => {
var _a = React.useState(queriesStates), states = _a[0], setStates = _a[1];
var setState = function (queryId) { return function (result) {
var _a;
setStates(__assign(__assign({}, states), (_a = {}, _a[queryId] = result, _a)));
}; };
var queriesHandlers = React.useMemo(function () {
return currentQueriesIds.reduce(function (handlers, queryId) {
handlers[queryId] = setState(queryId);
return handlers;
}, {});
}, [currentQueriesIds]);
var prevQueriesHandlers = usePrevValue(queriesHandlers).prevValue;
React.useEffect(function () {
currentQueriesIds.forEach(function (queryId) {
store.subscribeToQueryResult(queryId, queriesHandlers[queryId]);
});
return () => queriesIds.forEach(queryId => {
store.unsubscribeFromQueryResult(queryId, prevQueriesHandlers[queryId]);
});
return function () {
return queriesIds.forEach(function (queryId) {
store.unsubscribeFromQueryResult(queryId, prevQueriesHandlers[queryId]);
});
};
}, [currentQueriesIds]);

@@ -46,8 +62,8 @@ return resultsToProps ? resultsToProps(states) : states;

function useQueryState(queryId, stateToProps) {
const { store } = useMoon();
const queryState = store.readQuery(queryId);
const [state, setState] = React.useState(queryState);
var store = useMoon().store;
var queryState = store.readQuery(queryId);
var _a = React.useState(queryState), state = _a[0], setState = _a[1];
store.subscribeToQuery(queryId, setState);
React.useEffect(() => {
return () => store.unsubscribeFromQuery(queryId, setState);
React.useEffect(function () {
return function () { return store.unsubscribeFromQuery(queryId, setState); };
}, [queryId]);

@@ -58,24 +74,29 @@ return stateToProps ? stateToProps(state) : state;

function useQueriesStates(queriesIds, statesToProps) {
const { store } = useMoon();
const { value: currentQueriesIds } = usePrevValue(queriesIds);
const queriesStates = currentQueriesIds.reduce((result, queryId) => {
var store = useMoon().store;
var currentQueriesIds = usePrevValue(queriesIds).value;
var queriesStates = currentQueriesIds.reduce(function (result, queryId) {
result[queryId] = store.readQuery(queryId);
return result;
}, {});
const [states, setStates] = React.useState(queriesStates);
const setState = (queryId) => (state) => {
setStates(Object.assign(Object.assign({}, states), { [queryId]: state }));
};
const queriesHandlers = React.useMemo(() => currentQueriesIds.reduce((handlers, queryId) => {
handlers[queryId] = setState(queryId);
return handlers;
}, {}), [currentQueriesIds]);
const { prevValue: prevQueriesHandlers } = usePrevValue(queriesHandlers);
React.useEffect(() => {
currentQueriesIds.forEach(queryId => {
var _a = React.useState(queriesStates), states = _a[0], setStates = _a[1];
var setState = function (queryId) { return function (state) {
var _a;
setStates(__assign(__assign({}, states), (_a = {}, _a[queryId] = state, _a)));
}; };
var queriesHandlers = React.useMemo(function () {
return currentQueriesIds.reduce(function (handlers, queryId) {
handlers[queryId] = setState(queryId);
return handlers;
}, {});
}, [currentQueriesIds]);
var prevQueriesHandlers = usePrevValue(queriesHandlers).prevValue;
React.useEffect(function () {
currentQueriesIds.forEach(function (queryId) {
store.subscribeToQuery(queryId, queriesHandlers[queryId]);
});
return () => queriesIds.forEach(queryId => {
store.unsubscribeFromQuery(queryId, prevQueriesHandlers[queryId]);
});
return function () {
return queriesIds.forEach(function (queryId) {
store.unsubscribeFromQuery(queryId, prevQueriesHandlers[queryId]);
});
};
}, [currentQueriesIds]);

@@ -86,4 +107,4 @@ return statesToProps ? statesToProps(states) : states;

function usePrevValue(value) {
const valueRef = React.useRef(value);
const prevValue = valueRef.current;
var valueRef = React.useRef(value);
var prevValue = valueRef.current;
//@ts-ignore prevValue is an object

@@ -93,8 +114,8 @@ if (typeof prevValue === "object" && typeof value === "object" && !shallow_equal_1.default(prevValue, value)) {

}
return { value: valueRef.current, prevValue };
return { value: valueRef.current, prevValue: prevValue };
}
exports.usePrevValue = usePrevValue;
function useMoon() {
const moonContext = React.useContext(moon_provider_1.MoonContext);
const { client, store } = moonContext;
var moonContext = React.useContext(moon_provider_1.MoonContext);
var client = moonContext.client, store = moonContext.store;
if (!client || !store) {

@@ -101,0 +122,0 @@ throw new Error("Invariant Violation: Please wrap the root component in a <MoonProvider>");

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

@@ -11,4 +22,31 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("./utils/axios");
var axios_1 = require("./utils/axios");
var MutateType;

@@ -20,38 +58,66 @@ (function (MutateType) {

})(MutateType = exports.MutateType || (exports.MutateType = {}));
class MoonClient {
constructor(links) {
this.query = (source, endPoint, variables = {}, deserialize, options = {}) => __awaiter(this, void 0, void 0, function* () {
const client = this.clients[source];
let response;
if (client) {
response = yield client.get(endPoint, Object.assign(Object.assign({}, options), { params: Object.assign({}, variables) }));
response = deserialize ? deserialize(response) : response;
}
return response;
});
this.mutate = (source, endPoint, type = MutateType.Post, variables = {}, options = {}) => __awaiter(this, void 0, void 0, function* () {
const client = this.clients[source];
let response;
if (client) {
switch (type) {
case MutateType.Delete:
{
const mutationOptions = Object.assign(Object.assign({}, options), { params: Object.assign({}, variables) });
response = yield client.delete(endPoint, mutationOptions);
}
break;
case MutateType.Put:
response = yield client.put(endPoint, variables, options);
break;
default:
response = yield client.post(endPoint, variables, options);
break;
}
}
return response;
});
var MoonClient = /** @class */ (function () {
function MoonClient(links) {
var _this = this;
this.query = function (source, endPoint, variables, deserialize, options) {
if (variables === void 0) { variables = {}; }
if (options === void 0) { options = {}; }
return __awaiter(_this, void 0, void 0, function () {
var client, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
client = this.clients[source];
if (!client) return [3 /*break*/, 2];
return [4 /*yield*/, client.get(endPoint, __assign(__assign({}, options), { params: __assign({}, variables) }))];
case 1:
response = _a.sent();
response = deserialize ? deserialize(response) : response;
_a.label = 2;
case 2: return [2 /*return*/, response];
}
});
});
};
this.mutate = function (source, endPoint, type, variables, options) {
if (type === void 0) { type = MutateType.Post; }
if (variables === void 0) { variables = {}; }
if (options === void 0) { options = {}; }
return __awaiter(_this, void 0, void 0, function () {
var client, response, _a, mutationOptions;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
client = this.clients[source];
if (!client) return [3 /*break*/, 7];
_a = type;
switch (_a) {
case MutateType.Delete: return [3 /*break*/, 1];
case MutateType.Put: return [3 /*break*/, 3];
}
return [3 /*break*/, 5];
case 1:
mutationOptions = __assign(__assign({}, options), { params: __assign({}, variables) });
return [4 /*yield*/, client.delete(endPoint, mutationOptions)];
case 2:
response = _b.sent();
return [3 /*break*/, 7];
case 3: return [4 /*yield*/, client.put(endPoint, variables, options)];
case 4:
response = _b.sent();
return [3 /*break*/, 7];
case 5: return [4 /*yield*/, client.post(endPoint, variables, options)];
case 6:
response = _b.sent();
return [3 /*break*/, 7];
case 7: return [2 /*return*/, response];
}
});
});
};
this.clients = axios_1.getClients(links);
}
}
return MoonClient;
}());
exports.default = MoonClient;
//# sourceMappingURL=moon-client.js.map
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {

@@ -14,5 +38,5 @@ var t = {};

Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const moon_client_1 = require("./moon-client");
const store_1 = require("./store");
var React = require("react");
var moon_client_1 = require("./moon-client");
var store_1 = require("./store");
exports.MoonContext = React.createContext({

@@ -22,28 +46,37 @@ client: null,

});
class MoonProvider extends React.Component {
constructor(props) {
super(props);
const { links, initialStore } = this.props;
this.store = store_1.default(initialStore);
this.client = new moon_client_1.default(links);
var MoonProvider = /** @class */ (function (_super) {
__extends(MoonProvider, _super);
function MoonProvider(props) {
var _this = _super.call(this, props) || this;
var _a = _this.props, links = _a.links, initialStore = _a.initialStore;
_this.store = store_1.default(initialStore);
_this.client = new moon_client_1.default(links);
return _this;
}
render() {
const { children } = this.props;
MoonProvider.prototype.render = function () {
var children = this.props.children;
return React.createElement(exports.MoonContext.Provider, { value: { client: this.client, store: this.store } }, children);
}
}
};
return MoonProvider;
}(React.Component));
function withMoon(WrappedComponent) {
class WithMoonComponent extends React.Component {
render() {
const _a = this.props, { forwardedRef } = _a, rest = __rest(_a, ["forwardedRef"]);
return (React.createElement(exports.MoonContext.Consumer, null, ({ client, store }) => {
var WithMoonComponent = /** @class */ (function (_super) {
__extends(WithMoonComponent, _super);
function WithMoonComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
WithMoonComponent.prototype.render = function () {
var _a = this.props, forwardedRef = _a.forwardedRef, rest = __rest(_a, ["forwardedRef"]);
return (React.createElement(exports.MoonContext.Consumer, null, function (_a) {
var client = _a.client, store = _a.store;
return (
// @ts-ignore I don't know how to implement this without breaking out of the types.
React.createElement(WrappedComponent, Object.assign({ ref: forwardedRef, client: client, store: store }, rest)));
React.createElement(WrappedComponent, __assign({ ref: forwardedRef, client: client, store: store }, rest)));
}));
}
}
return React.forwardRef((props, ref) => {
};
return WithMoonComponent;
}(React.Component));
return React.forwardRef(function (props, ref) {
// @ts-ignore I don't know how to implement this without breaking out of the types.
return React.createElement(WithMoonComponent, Object.assign({ forwardedRef: ref }, props));
return React.createElement(WithMoonComponent, __assign({ forwardedRef: ref }, props));
});

@@ -50,0 +83,0 @@ }

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

@@ -11,49 +22,90 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const axios_1 = require("axios");
const hooks_1 = require("./hooks");
function useMutation({ source, type, endPoint, variables, options, onResponse, onError }) {
const { client } = hooks_1.useMoon();
const { value } = hooks_1.usePrevValue(variables);
const [state, setState] = React.useState({
var React = require("react");
var axios_1 = require("axios");
var hooks_1 = require("./hooks");
function useMutation(_a) {
var _this = this;
var source = _a.source, type = _a.type, endPoint = _a.endPoint, variables = _a.variables, options = _a.options, onResponse = _a.onResponse, onError = _a.onError;
var client = hooks_1.useMoon().client;
var value = hooks_1.usePrevValue(variables).value;
var _b = React.useState({
response: undefined,
loading: false,
error: null
});
const cancelSourceRef = React.useRef();
const { response, loading, error } = state;
React.useEffect(() => {
}), state = _b[0], setState = _b[1];
var cancelSourceRef = React.useRef();
var response = state.response, loading = state.loading, error = state.error;
React.useEffect(function () {
cancelSourceRef.current = axios_1.default.CancelToken.source();
// @ts-ignore can't be undefined
return () => cancelSourceRef.current.cancel();
return function () { return cancelSourceRef.current.cancel(); };
}, [value, endPoint, source]);
const cancel = () => {
var cancel = function () {
if (cancelSourceRef.current) {
cancelSourceRef.current.cancel();
setState(Object.assign(Object.assign({}, state), { loading: false }));
setState(__assign(__assign({}, state), { loading: false }));
}
};
const mutate = () => __awaiter(this, void 0, void 0, function* () {
setState(Object.assign(Object.assign({}, state), { loading: true, error: null, response: undefined }));
try {
const response = (yield client.mutate(source, endPoint, type, variables, Object.assign(Object.assign({}, options), { cancelToken: cancelSourceRef.current && cancelSourceRef.current.token })));
setState(Object.assign(Object.assign({}, state), { loading: false, response }));
if (onResponse) {
onResponse(response);
var mutate = function () { return __awaiter(_this, void 0, void 0, function () {
var response_1, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
setState(__assign(__assign({}, state), { loading: true, error: null, response: undefined }));
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, client.mutate(source, endPoint, type, variables, __assign(__assign({}, options), { cancelToken: cancelSourceRef.current && cancelSourceRef.current.token }))];
case 2:
response_1 = (_a.sent());
setState(__assign(__assign({}, state), { loading: false, response: response_1 }));
if (onResponse) {
onResponse(response_1);
}
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
if (axios_1.default.isCancel(err_1)) {
return [2 /*return*/];
}
setState(__assign(__assign({}, state), { loading: false, error: err_1 }));
if (onError) {
onError(err_1);
}
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
}
catch (err) {
if (axios_1.default.isCancel(err)) {
return;
}
setState(Object.assign(Object.assign({}, state), { loading: false, error: err }));
if (onError) {
onError(err);
}
}
});
});
}); };
return [
{ response, loading, error },
{ mutate, cancel }
{ response: response, loading: loading, error: error },
{ mutate: mutate, cancel: cancel }
];

@@ -60,0 +112,0 @@ }

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {

@@ -14,14 +25,14 @@ var t = {};

Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const moon_client_1 = require("./moon-client");
const mutation_hook_1 = require("./mutation-hook");
var React = require("react");
var moon_client_1 = require("./moon-client");
var mutation_hook_1 = require("./mutation-hook");
// @ts-ignore ignore children type
exports.DumbMutation = React.memo(function DumbMutation(props) {
const { children } = props, childrenProps = __rest(props, ["children"]);
return children ? children(Object.assign({}, childrenProps)) : null;
var children = props.children, childrenProps = __rest(props, ["children"]);
return children ? children(__assign({}, childrenProps)) : null;
});
function Mutation(props) {
const { children } = props, mutationProps = __rest(props, ["children"]);
const [state, actions] = mutation_hook_1.default(mutationProps);
return (React.createElement(exports.DumbMutation, Object.assign({ actions: actions }, state), children));
var children = props.children, mutationProps = __rest(props, ["children"]);
var _a = mutation_hook_1.default(mutationProps), state = _a[0], actions = _a[1];
return (React.createElement(exports.DumbMutation, __assign({ actions: actions }, state), children));
}

@@ -28,0 +39,0 @@ Mutation.defaultProps = {

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

@@ -11,8 +22,35 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const axios_1 = require("axios");
const hooks_1 = require("./hooks");
const store_1 = require("./store");
const utils_1 = require("./utils");
var React = require("react");
var axios_1 = require("axios");
var hooks_1 = require("./hooks");
var store_1 = require("./store");
var utils_1 = require("./utils");
var FetchPolicy;

@@ -27,11 +65,13 @@ (function (FetchPolicy) {

})(FetchPolicy = exports.FetchPolicy || (exports.FetchPolicy = {}));
function useQuery({ id, source, endPoint, options, variables, fetchPolicy = FetchPolicy.CacheAndNetwork, deserialize, onResponse, onError, fetchOnMount = true, autoRefetchOnUpdate = true }) {
const { client, store } = hooks_1.useMoon();
const isInitialMount = React.useRef(true);
const cancelSourceRef = React.useRef();
const { value: currentVariables, prevValue: prevVariables } = hooks_1.usePrevValue(variables);
const queryId = React.useMemo(() => utils_1.getQueryId(id, source, endPoint, variables), [client, id, source, endPoint, variables]);
const state = hooks_1.useQueryState(queryId);
const { data, loading, error, networkStatus } = state;
React.useEffect(() => {
function useQuery(_a) {
var _this = this;
var id = _a.id, source = _a.source, endPoint = _a.endPoint, options = _a.options, variables = _a.variables, _b = _a.fetchPolicy, fetchPolicy = _b === void 0 ? FetchPolicy.CacheAndNetwork : _b, deserialize = _a.deserialize, onResponse = _a.onResponse, onError = _a.onError, _c = _a.fetchOnMount, fetchOnMount = _c === void 0 ? true : _c, _d = _a.autoRefetchOnUpdate, autoRefetchOnUpdate = _d === void 0 ? true : _d;
var _e = hooks_1.useMoon(), client = _e.client, store = _e.store;
var isInitialMount = React.useRef(true);
var cancelSourceRef = React.useRef();
var _f = hooks_1.usePrevValue(variables), currentVariables = _f.value, prevVariables = _f.prevValue;
var queryId = React.useMemo(function () { return utils_1.getQueryId(id, source, endPoint, variables); }, [client, id, source, endPoint, variables]);
var state = hooks_1.useQueryState(queryId);
var data = state.data, loading = state.loading, error = state.error, networkStatus = state.networkStatus;
React.useEffect(function () {
cancelSourceRef.current = axios_1.default.CancelToken.source();

@@ -48,41 +88,52 @@ if (isInitialMount.current) {

// @ts-ignore cancelSourceRef.current can't be undefined
return () => cancelSourceRef.current.cancel();
return function () { return cancelSourceRef.current.cancel(); };
}, [currentVariables, endPoint, source]);
const cancel = () => {
var cancel = function () {
if (cancelSourceRef.current) {
cancelSourceRef.current.cancel();
store.setQueryState(queryId, Object.assign(Object.assign({}, state), { loading: false, networkStatus: store_1.MoonNetworkStatus.Finished }));
store.setQueryState(queryId, __assign(__assign({}, state), { loading: false, networkStatus: store_1.MoonNetworkStatus.Finished }));
}
};
const fetch = () => __awaiter(this, void 0, void 0, function* () {
if (data && currentVariables === prevVariables && FetchPolicy.CacheFirst === fetchPolicy) {
store.setQueryState(queryId, Object.assign(Object.assign({}, state), { loading: false, networkStatus: store_1.MoonNetworkStatus.Finished, error: null }));
if (onResponse) {
onResponse(data);
var fetch = function () { return __awaiter(_this, void 0, void 0, function () {
var reponse, deserializedResponse, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(data && currentVariables === prevVariables && FetchPolicy.CacheFirst === fetchPolicy)) return [3 /*break*/, 1];
store.setQueryState(queryId, __assign(__assign({}, state), { loading: false, networkStatus: store_1.MoonNetworkStatus.Finished, error: null }));
if (onResponse) {
onResponse(data);
}
return [3 /*break*/, 5];
case 1:
reponse = FetchPolicy.NetworkOnly === fetchPolicy ? undefined : data;
store.setQueryState(queryId, __assign(__assign({}, state), { loading: true, networkStatus: store_1.MoonNetworkStatus.Fetch, error: null, data: reponse }));
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, client.query(source, endPoint, variables, deserialize, __assign(__assign({}, options), { cancelToken: cancelSourceRef.current && cancelSourceRef.current.token }))];
case 3:
deserializedResponse = _a.sent();
store.setQueryState(queryId, __assign(__assign({}, state), { data: deserializedResponse, loading: false, networkStatus: store_1.MoonNetworkStatus.Finished }));
if (onResponse) {
onResponse(deserializedResponse);
}
return [3 /*break*/, 5];
case 4:
err_1 = _a.sent();
if (axios_1.default.isCancel(err_1)) {
return [2 /*return*/];
}
store.setQueryState(queryId, __assign(__assign({}, state), { loading: false, networkStatus: store_1.MoonNetworkStatus.Finished, error: err_1 }));
if (onError) {
onError(err_1);
}
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
}
else {
const reponse = FetchPolicy.NetworkOnly === fetchPolicy ? undefined : data;
store.setQueryState(queryId, Object.assign(Object.assign({}, state), { loading: true, networkStatus: store_1.MoonNetworkStatus.Fetch, error: null, data: reponse }));
try {
const deserializedResponse = yield client.query(source, endPoint, variables, deserialize, Object.assign(Object.assign({}, options), { cancelToken: cancelSourceRef.current && cancelSourceRef.current.token }));
store.setQueryState(queryId, Object.assign(Object.assign({}, state), { data: deserializedResponse, loading: false, networkStatus: store_1.MoonNetworkStatus.Finished }));
if (onResponse) {
onResponse(deserializedResponse);
}
}
catch (err) {
if (axios_1.default.isCancel(err)) {
return;
}
store.setQueryState(queryId, Object.assign(Object.assign({}, state), { loading: false, networkStatus: store_1.MoonNetworkStatus.Finished, error: err }));
if (onError) {
onError(err);
}
}
}
});
});
}); };
return [
{ loading, data, error, networkStatus },
{ refetch: fetch, cancel }
{ loading: loading, data: data, error: error, networkStatus: networkStatus },
{ refetch: fetch, cancel: cancel }
];

@@ -89,0 +140,0 @@ }

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {

@@ -14,14 +25,14 @@ var t = {};

Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const query_hook_1 = require("./query-hook");
const hooks_1 = require("./hooks");
var React = require("react");
var query_hook_1 = require("./query-hook");
var hooks_1 = require("./hooks");
// @ts-ignore ignore children type
exports.DumbQuery = React.memo(function DumbQuery(props) {
const { children } = props, childrenProps = __rest(props, ["children"]);
return children ? children(Object.assign({}, childrenProps)) : null;
var children = props.children, childrenProps = __rest(props, ["children"]);
return children ? children(__assign({}, childrenProps)) : null;
});
function Query(props) {
const { children } = props, queryProps = __rest(props, ["children"]);
const [state, actions] = query_hook_1.default(queryProps);
return (React.createElement(exports.DumbQuery, Object.assign({ actions: actions }, state), children));
var children = props.children, queryProps = __rest(props, ["children"]);
var _a = query_hook_1.default(queryProps), state = _a[0], actions = _a[1];
return (React.createElement(exports.DumbQuery, __assign({ actions: actions }, state), children));
}

@@ -34,12 +45,13 @@ Query.defaultProps = {

function withQueryResult(queryId, resutToProps) {
return (WrappedComponent) => {
const WithQueryComponent = props => {
const { forwardedRef } = props, rest = __rest(props, ["forwardedRef"]);
const queryResult = hooks_1.useQueryResult(queryId, resutToProps);
const queryProps = resutToProps ? queryResult : { [queryId]: queryResult };
return function (WrappedComponent) {
var WithQueryComponent = function (props) {
var _a;
var forwardedRef = props.forwardedRef, rest = __rest(props, ["forwardedRef"]);
var queryResult = hooks_1.useQueryResult(queryId, resutToProps);
var queryProps = resutToProps ? queryResult : (_a = {}, _a[queryId] = queryResult, _a);
// @ts-ignore I don't know how to implement this without breaking out of the types.
return React.createElement(WrappedComponent, Object.assign({ ref: forwardedRef }, queryProps, rest));
return React.createElement(WrappedComponent, __assign({ ref: forwardedRef }, queryProps, rest));
};
return React.forwardRef((props, ref) => {
return React.createElement(WithQueryComponent, Object.assign({ forwardedRef: ref }, props));
return React.forwardRef(function (props, ref) {
return React.createElement(WithQueryComponent, __assign({ forwardedRef: ref }, props));
});

@@ -50,11 +62,11 @@ };

function withQueriesResults(queriesIds, resultsToProps) {
return (WrappedComponent) => {
const WithQueryComponent = props => {
const { forwardedRef } = props, rest = __rest(props, ["forwardedRef"]);
const queryResult = hooks_1.useQueriesResults(queriesIds, resultsToProps);
return function (WrappedComponent) {
var WithQueryComponent = function (props) {
var forwardedRef = props.forwardedRef, rest = __rest(props, ["forwardedRef"]);
var queryResult = hooks_1.useQueriesResults(queriesIds, resultsToProps);
// @ts-ignore I don't know how to implement this without breaking out of the types.
return React.createElement(WrappedComponent, Object.assign({ ref: forwardedRef }, queryResult, rest));
return React.createElement(WrappedComponent, __assign({ ref: forwardedRef }, queryResult, rest));
};
return React.forwardRef((props, ref) => {
return React.createElement(WithQueryComponent, Object.assign({ forwardedRef: ref }, props));
return React.forwardRef(function (props, ref) {
return React.createElement(WithQueryComponent, __assign({ forwardedRef: ref }, props));
});

@@ -61,0 +73,0 @@ };

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
const shallow_equal_1 = require("./utils/shallow-equal");
var shallow_equal_1 = require("./utils/shallow-equal");
var MoonNetworkStatus;

@@ -10,4 +21,4 @@ (function (MoonNetworkStatus) {

})(MoonNetworkStatus = exports.MoonNetworkStatus || (exports.MoonNetworkStatus = {}));
exports.getDefaultQuery = (queryId) => {
const defaultQueryState = {
exports.getDefaultQuery = function (queryId) {
var defaultQueryState = {
loading: false,

@@ -25,41 +36,43 @@ error: null,

};
class Store {
constructor(initialStore = {}) {
this.subscribeToQueryResult = (queryId, onResultChangeHandler) => {
const currentQuery = this.queries[queryId] || exports.getDefaultQuery(queryId);
var Store = /** @class */ (function () {
function Store(initialStore) {
var _this = this;
if (initialStore === void 0) { initialStore = {}; }
this.subscribeToQueryResult = function (queryId, onResultChangeHandler) {
var currentQuery = _this.queries[queryId] || exports.getDefaultQuery(queryId);
if (currentQuery.onResultChangeHandlers.indexOf(onResultChangeHandler) < 0) {
currentQuery.onResultChangeHandlers.push(onResultChangeHandler);
this.queries[queryId] = currentQuery;
_this.queries[queryId] = currentQuery;
}
};
this.unsubscribeFromQueryResult = (queryId, onResultChangeHandler) => {
const currentQuery = this.queries[queryId];
this.unsubscribeFromQueryResult = function (queryId, onResultChangeHandler) {
var currentQuery = _this.queries[queryId];
if (currentQuery) {
const { onResultChangeHandlers } = currentQuery;
var onResultChangeHandlers = currentQuery.onResultChangeHandlers;
onResultChangeHandlers.splice(onResultChangeHandlers.indexOf(onResultChangeHandler), 1);
}
};
this.subscribeToQuery = (queryId, onStateChangeHandler) => {
const currentQuery = this.queries[queryId] || exports.getDefaultQuery(queryId);
this.subscribeToQuery = function (queryId, onStateChangeHandler) {
var currentQuery = _this.queries[queryId] || exports.getDefaultQuery(queryId);
if (currentQuery.onStateChangeHandlers.indexOf(onStateChangeHandler) < 0) {
currentQuery.onStateChangeHandlers.push(onStateChangeHandler);
this.queries[queryId] = currentQuery;
_this.queries[queryId] = currentQuery;
}
};
this.unsubscribeFromQuery = (queryId, onStateChangeHandler) => {
const currentQuery = this.queries[queryId];
this.unsubscribeFromQuery = function (queryId, onStateChangeHandler) {
var currentQuery = _this.queries[queryId];
if (currentQuery) {
const { onStateChangeHandlers } = currentQuery;
var onStateChangeHandlers = currentQuery.onStateChangeHandlers;
onStateChangeHandlers.splice(onStateChangeHandlers.indexOf(onStateChangeHandler), 1);
}
};
this.setQueryState = (queryId, queryState) => {
const currentQuery = this.queries[queryId] || exports.getDefaultQuery(queryId);
const newState = Object.assign(Object.assign({}, currentQuery.state), queryState);
this.setQueryState = function (queryId, queryState) {
var currentQuery = _this.queries[queryId] || exports.getDefaultQuery(queryId);
var newState = __assign(__assign({}, currentQuery.state), queryState);
if (!shallow_equal_1.default(newState, currentQuery.state)) {
currentQuery.onStateChangeHandlers.forEach(onStateChangeHandler => {
currentQuery.onStateChangeHandlers.forEach(function (onStateChangeHandler) {
onStateChangeHandler(newState);
});
if (!shallow_equal_1.default(newState.data, currentQuery.state.data)) {
currentQuery.onResultChangeHandlers.forEach(onResultChangeHandler => {
currentQuery.onResultChangeHandlers.forEach(function (onResultChangeHandler) {
onResultChangeHandler(newState.data);

@@ -70,23 +83,24 @@ });

}
this.queries[queryId] = currentQuery;
_this.queries[queryId] = currentQuery;
};
this.writeQuery = (queryId, data) => {
const currentQuery = this.queries[queryId] || exports.getDefaultQuery(queryId);
this.queries[queryId] = currentQuery;
const newState = Object.assign(Object.assign({}, currentQuery.state), { data });
this.setQueryState(queryId, newState);
this.writeQuery = function (queryId, data) {
var currentQuery = _this.queries[queryId] || exports.getDefaultQuery(queryId);
_this.queries[queryId] = currentQuery;
var newState = __assign(__assign({}, currentQuery.state), { data: data });
_this.setQueryState(queryId, newState);
};
this.readQuery = (queryId) => {
const currentQuery = this.queries[queryId] || exports.getDefaultQuery(queryId);
this.queries[queryId] = currentQuery;
this.readQuery = function (queryId) {
var currentQuery = _this.queries[queryId] || exports.getDefaultQuery(queryId);
_this.queries[queryId] = currentQuery;
return currentQuery.state;
};
this.getQueries = () => {
return this.queries;
this.getQueries = function () {
return _this.queries;
};
this.queries = initialStore;
}
}
return Store;
}());
exports.Store = Store;
let store = null;
var store = null;
function createMoonStore(initialStore) {

@@ -93,0 +107,0 @@ store = new Store(initialStore);

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const qs = require("qs");
const axios_1 = require("axios");
const paramsSerializer = (params) => qs.stringify(params, { arrayFormat: "repeat" });
function createHttpClient(baseURL, interceptors = {}) {
var qs = require("qs");
var axios_1 = require("axios");
var paramsSerializer = function (params) { return qs.stringify(params, { arrayFormat: "repeat" }); };
function createHttpClient(baseURL, interceptors) {
if (interceptors === void 0) { interceptors = {}; }
// create Axios client with custom params serializer
const client = axios_1.default.create({
baseURL,
paramsSerializer
var client = axios_1.default.create({
baseURL: baseURL,
paramsSerializer: paramsSerializer
});
// create interceptors
if (interceptors && interceptors.request && interceptors.request.length > 0) {
interceptors.request.forEach(interceptor => client.interceptors.request.use(interceptor.onFulfilled, interceptor.onRejected));
interceptors.request.forEach(function (interceptor) { return client.interceptors.request.use(interceptor.onFulfilled, interceptor.onRejected); });
}
if (interceptors && interceptors.response && interceptors.response.length > 0) {
interceptors.response.forEach(interceptor => client.interceptors.response.use(interceptor.onFulfilled, interceptor.onRejected));
interceptors.response.forEach(function (interceptor) {
return client.interceptors.response.use(interceptor.onFulfilled, interceptor.onRejected);
});
}

@@ -23,3 +26,3 @@ return client;

function getClients(links) {
return links.reduce((clients, link) => {
return links.reduce(function (clients, link) {
clients[link.id] = createHttpClient(link.baseUrl, link.interceptors);

@@ -26,0 +29,0 @@ return clients;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hash = require("object-hash");
exports.getQueryId = (id, source, endPoint, variables) => {
var hash = require("object-hash");
exports.getQueryId = function (id, source, endPoint, variables) {
return id || hash([source, endPoint, variables]);
};
//# sourceMappingURL=common.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hasOwn = Object.prototype.hasOwnProperty;
var hasOwn = Object.prototype.hasOwnProperty;
function is(x, y) {

@@ -16,7 +16,7 @@ if (x === y) {

}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
var keysA = Object.keys(objA);
var keysB = Object.keys(objB);
if (keysA.length !== keysB.length)
return false;
for (let i = 0; i < keysA.length; i++) {
for (var i = 0; i < keysA.length; i++) {
if (!hasOwn.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {

@@ -23,0 +23,0 @@ return false;

{
"name": "@decathlon/moon",
"version": "2.0.1",
"version": "2.0.2",
"description": "A featured, production ready caching REST client for every React UI",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc