use-invariant
Version:
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-in-nextjs-nn9fm'>Example - Next.js</a> - <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/embed/km04k9k9x5'>Examples - create-react-app</a>
51 lines • 1.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const use_ssr_1 = __importDefault(require("use-ssr"));
function useURLRequiredInvariant(condition, method) {
const exampleURL = exports.useExampleURL();
react_1.useEffect(() => {
invariant(condition, `${method} requires a URL to be set as the 1st argument,\n
unless you wrap your app like:\n
<Provider url="${exampleURL}"><App /></Provider>`);
}, []);
}
exports.useURLRequiredInvariant = useURLRequiredInvariant;
exports.useExampleURL = () => {
const { isBrowser } = use_ssr_1.default();
return react_1.useMemo(() => isBrowser ? window.location.origin : 'https://example.com', [isBrowser]);
};
/**
* Determines if the given param is an object. {}
* @param obj
*/
exports.isObject = (obj) => Object.prototype.toString.call(obj) === '[object Object]';
/**
* Used for error checking. If the condition is false, throw an error
*/
function invariant(condition, format, a = '', b = '', c = '', d = '', e = '', f = '') {
if (process.env.NODE_ENV !== 'production') {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}
if (!condition) {
var error;
if (format === undefined) {
error = new Error('Minified exception occurred; use the non-minified dev environment ' +
'for the full error message and additional helpful warnings.');
}
else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(format.replace(/%s/g, () => args[argIndex++]));
error.name = 'Invariant Violation';
}
throw error;
}
}
exports.invariant = invariant;
//# sourceMappingURL=utils.js.map