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

react-magnetic-di

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-magnetic-di - npm Package Compare versions

Comparing version 2.3.1 to 2.3.2

4

lib/cjs/react/global.js

@@ -25,4 +25,4 @@ "use strict";

deps.forEach(function (d) {
_stats.stats.set(d);
replacementMap.set(d[_constants.KEY], d);
if (d[_constants.KEY].track) _stats.stats.set(d);
replacementMap.set(d[_constants.KEY].from, d);
});

@@ -29,0 +29,0 @@ },

@@ -30,4 +30,4 @@ "use strict";

var replacementMap = use.reduce(function (m, d) {
_stats.stats.set(d);
return m.set(d[_constants.KEY], d);
if (d[_constants.KEY].track) _stats.stats.set(d);
return m.set(d[_constants.KEY].from, d);
}, new Map());

@@ -43,2 +43,3 @@ // support single or multiple targets

return dependencies.map(function (dep) {
var _dep$KEY;
// dep can be either the original or a replacement

@@ -48,3 +49,3 @@ // if another provider at the top has already swapped it

// or return the original / parent replacement
var real = dep[_constants.KEY] || dep;
var real = ((_dep$KEY = dep[_constants.KEY]) === null || _dep$KEY === void 0 ? void 0 : _dep$KEY.from) || dep;
var replacedDep = replacementMap.get(real);

@@ -51,0 +52,0 @@ _stats.stats.track(replacedDep, dep);

@@ -10,6 +10,7 @@ "use strict";

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var createState = function createState() {

@@ -26,5 +27,21 @@ return {

set: function set(replacedDep) {
this.state.unused.set(replacedDep, new Error("Unused di injectable: ".concat(replacedDep.displayName || replacedDep)));
// allow injectable override without flagging as unused
var _iterator = _createForOfIteratorHelper(this.state.unused.keys()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var injectable = _step.value;
if (injectable[_constants.KEY].from === replacedDep[_constants.KEY].from) this.state.unused["delete"](injectable);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
this.state.unused.set(replacedDep, new Error("Unused \"di\" injectable: ".concat(replacedDep.displayName || replacedDep, "."), {
cause: replacedDep[_constants.KEY].cause
}));
},
track: function track(replacedDep, dep) {
var _dep$KEY;
if (replacedDep) {

@@ -35,3 +52,3 @@ this.state.unused["delete"](replacedDep);

this.state.provided.add(dep);
} else if (!dep[_constants.KEY] && !this.state.provided.has(dep)) {
} else if (!((_dep$KEY = dep[_constants.KEY]) !== null && _dep$KEY !== void 0 && _dep$KEY.from) && !this.state.provided.has(dep)) {
this.state.missing.set(dep, new Error("Unreplaced di dependency: ".concat(dep.displayName || dep)));

@@ -38,0 +55,0 @@ }

@@ -25,7 +25,20 @@ "use strict";

function injectable(from, implementation) {
implementation.displayName = getDisplayName(implementation) || getDisplayName(from, 'di');
if (implementation[_constants.KEY] && implementation[_constants.KEY] !== from) {
var _implementation$KEY, _implementation$KEY2;
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
displayName = _ref.displayName,
_ref$track = _ref.track,
track = _ref$track === void 0 ? true : _ref$track;
implementation.displayName = displayName || getDisplayName(implementation) || getDisplayName(from, 'di');
if ((_implementation$KEY = implementation[_constants.KEY]) !== null && _implementation$KEY !== void 0 && _implementation$KEY.from && ((_implementation$KEY2 = implementation[_constants.KEY]) === null || _implementation$KEY2 === void 0 ? void 0 : _implementation$KEY2.from) !== from) {
warnOnce("You are trying to use replacement \"".concat(implementation.displayName, "\" on multiple injectables. ") + "That will override only the last dependency, as each replacement is uniquely linked.");
}
implementation[_constants.KEY] = from;
Object.defineProperty(implementation, _constants.KEY, {
writable: true,
// ideally this should be false, but sometimes devs reuse mocks
value: {
from: from,
track: track,
cause: new Error('Injectable created but not used. If this is on purpose, add "{track: false}"')
}
});
return implementation;

@@ -32,0 +45,0 @@ }

@@ -17,4 +17,4 @@ import { KEY, PACKAGE_NAME } from './constants';

deps.forEach(d => {
stats.set(d);
replacementMap.set(d[KEY], d);
if (d[KEY].track) stats.set(d);
replacementMap.set(d[KEY].from, d);
});

@@ -21,0 +21,0 @@ },

@@ -22,4 +22,4 @@ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const replacementMap = use.reduce((m, d) => {
stats.set(d);
return m.set(d[KEY], d);
if (d[KEY].track) stats.set(d);
return m.set(d[KEY].from, d);
}, new Map());

@@ -35,2 +35,3 @@ // support single or multiple targets

return dependencies.map(dep => {
var _dep$KEY;
// dep can be either the original or a replacement

@@ -40,3 +41,3 @@ // if another provider at the top has already swapped it

// or return the original / parent replacement
const real = dep[KEY] || dep;
const real = ((_dep$KEY = dep[KEY]) == null ? void 0 : _dep$KEY.from) || dep;
const replacedDep = replacementMap.get(real);

@@ -43,0 +44,0 @@ stats.track(replacedDep, dep);

@@ -11,5 +11,10 @@ import { KEY } from './constants';

set(replacedDep) {
this.state.unused.set(replacedDep, new Error(`Unused di injectable: ${replacedDep.displayName || replacedDep}`));
// allow injectable override without flagging as unused
for (let injectable of this.state.unused.keys()) if (injectable[KEY].from === replacedDep[KEY].from) this.state.unused.delete(injectable);
this.state.unused.set(replacedDep, new Error(`Unused "di" injectable: ${replacedDep.displayName || replacedDep}.`, {
cause: replacedDep[KEY].cause
}));
},
track(replacedDep, dep) {
var _dep$KEY;
if (replacedDep) {

@@ -20,3 +25,3 @@ this.state.unused.delete(replacedDep);

this.state.provided.add(dep);
} else if (!dep[KEY] && !this.state.provided.has(dep)) {
} else if (!((_dep$KEY = dep[KEY]) != null && _dep$KEY.from) && !this.state.provided.has(dep)) {
this.state.missing.set(dep, new Error(`Unreplaced di dependency: ${dep.displayName || dep}`));

@@ -23,0 +28,0 @@ }

@@ -17,8 +17,21 @@ import { KEY } from './constants';

}
export function injectable(from, implementation) {
implementation.displayName = getDisplayName(implementation) || getDisplayName(from, 'di');
if (implementation[KEY] && implementation[KEY] !== from) {
export function injectable(from, implementation, _temp) {
var _implementation$KEY, _implementation$KEY2;
let {
displayName,
track = true
} = _temp === void 0 ? {} : _temp;
implementation.displayName = displayName || getDisplayName(implementation) || getDisplayName(from, 'di');
if ((_implementation$KEY = implementation[KEY]) != null && _implementation$KEY.from && ((_implementation$KEY2 = implementation[KEY]) == null ? void 0 : _implementation$KEY2.from) !== from) {
warnOnce(`You are trying to use replacement "${implementation.displayName}" on multiple injectables. ` + `That will override only the last dependency, as each replacement is uniquely linked.`);
}
implementation[KEY] = from;
Object.defineProperty(implementation, KEY, {
writable: true,
// ideally this should be false, but sometimes devs reuse mocks
value: {
from,
track,
cause: new Error('Injectable created but not used. If this is on purpose, add "{track: false}"')
}
});
return implementation;

@@ -25,0 +38,0 @@ }

{
"name": "react-magnetic-di",
"version": "2.3.1",
"version": "2.3.2",
"description": "Context driven dependency injection",

@@ -5,0 +5,0 @@ "keywords": [

@@ -90,3 +90,3 @@ <p align="center">

In the unit/integration tests or storybooks we can create a new injectable implementation and wrap the component with `DiProvider` to override such dependency:
In the unit/integration tests or storybooks we can create a new `injectable` implementation and wrap the component with `DiProvider` to override such dependency:

@@ -189,6 +189,3 @@ ```jsx

it('should call the API', async () => {
const fetchApiDi = injectable(
fetchApi,
jest.mock().mockResolvedValue('mock')
);
const fetchApiDi = injectable(fetchApi, jest.fn().mockResolvedValue('mock'));

@@ -202,2 +199,17 @@ const result = await runWithDi(() => myApiFetcher(), [fetchApiDi]);

### injectables configuration
When creating injectables you can provide a configuration object to customise some of its behaviour.
For instance, you can provide a custom `displayName` to make debugging easier:
```js
const fetchApiDi = injectable(fetchApi, jest.fn(), { displayName: 'fetchApi' });
```
Or you can skip reporting it in `stats.unused()` (handy if you provide default injectables across tests):
```js
const fetchApiDi = injectable(fetchApi, jest.fn(), { track: false });
```
### Tracking unused injectables

@@ -204,0 +216,0 @@

@@ -48,5 +48,10 @@ declare module 'react-magnetic-di' {

from: T,
implementation: ComponentOrFunction<T>
implementation: ComponentOrFunction<T>,
options?: { displayName?: string; track?: boolean }
): T;
function injectable<T extends Dependency>(from: T, implementation: T): T;
function injectable<T extends Dependency>(
from: T,
implementation: T,
options?: { displayName?: string; track?: boolean }
): T;

@@ -53,0 +58,0 @@ function di(...dependencies: Dependency[]): void;

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