@zendesk/laika
Version:
Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!
30 lines • 1.46 kB
JavaScript
import memoize from 'lodash/memoize';
import { DEFAULT_GLOBAL_PROPERTY_NAME } from './constants';
import { Laika } from './laika';
export const getLaikaSingleton = memoize((globalPropertyName = 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({
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
*/
export function createGlobalLaikaLink({ clientName = '__unknown__', globalPropertyName, startLoggingImmediately = false, onLaikaReady, }) {
if (clientName === '__unknown__') {
throw new Error('LaikaLink: clientName is required');
}
const laika = getLaikaSingleton(globalPropertyName, startLoggingImmediately, onLaikaReady);
return laika.createLink((operation) => {
operation.setContext({ clientName });
});
}
//# sourceMappingURL=createGlobalLaikaLink.js.map