@zendesk/laika
Version:
Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!
37 lines • 1.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLaikaSingleton = void 0;
exports.createGlobalLaikaLink = createGlobalLaikaLink;
const memoize_1 = __importDefault(require("lodash/memoize"));
const constants_1 = require("./constants");
const laika_1 = require("./laika");
exports.getLaikaSingleton = (0, memoize_1.default)((globalPropertyName = constants_1.DEFAULT_GLOBAL_PROPERTY_NAME, startLoggingImmediately = false, onLaikaReady) => {
var _a;
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access,no-multi-assign
const singleton = (globalThis[globalPropertyName] = new laika_1.Laika({
referenceName: globalPropertyName,
}));
if (startLoggingImmediately) {
singleton.log.startLogging();
}
onLaikaReady === null || onLaikaReady === void 0 ? void 0 : onLaikaReady(singleton);
(_a = globalThis[`${globalPropertyName}ReadyCallbacks`]) === null || _a === void 0 ? void 0 : _a.forEach((fn) => void fn(singleton));
return singleton;
});
/**
* Creates an instance of ApolloLink with intercepting functionality.
* @param options
*/
function createGlobalLaikaLink({ clientName = '__unknown__', globalPropertyName, startLoggingImmediately = false, onLaikaReady, }) {
if (clientName === '__unknown__') {
throw new Error('LaikaLink: clientName is required');
}
const laika = (0, exports.getLaikaSingleton)(globalPropertyName, startLoggingImmediately, onLaikaReady);
return laika.createLink((operation) => {
operation.setContext({ clientName });
});
}
//# sourceMappingURL=createGlobalLaikaLink.js.map