fix: hide missing token warning when disabled (#260)

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>
This commit is contained in:
JYC 2022-08-03 11:39:57 +02:00 committed by GitHub
parent 3f1b6edc97
commit c0e08216b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'@graphql-hive/client': patch
---
fix: enabled false will log in debug that hive is disabled

View file

@ -4,10 +4,15 @@ import type { HivePluginOptions, HiveClient } from './internal/types';
import { createUsage } from './internal/usage';
import { createReporting } from './internal/reporting';
import { createOperationsStore } from './internal/operations-store';
import { logIf } from './internal/utils';
export function createHive(options: HivePluginOptions): HiveClient {
const logger = options?.agent?.logger ?? console;
if (!options.enabled) {
logIf(options.debug === true, '[hive] is not enabled.', logger.info);
}
if (!options.token && options.enabled) {
options.enabled = false;
logger.info('[hive] Missing token, disabling.');

View file

@ -11,7 +11,7 @@ export interface SchemaReporter {
}
export function createReporting(pluginOptions: HivePluginOptions): SchemaReporter {
if (!pluginOptions.reporting) {
if (!pluginOptions.reporting || pluginOptions.enabled === false) {
return {
report() {},
async dispose() {},

View file

@ -36,7 +36,7 @@ interface UsageCollector {
}
export function createUsage(pluginOptions: HivePluginOptions): UsageCollector {
if (!pluginOptions.usage) {
if (!pluginOptions.usage || pluginOptions.enabled === false) {
return {
collect() {
return () => {};

View file

@ -0,0 +1,49 @@
import { createHive } from '../src/client';
test("should log that it's not enabled", async () => {
const logger = {
error: jest.fn(),
info: jest.fn(),
};
const hive = createHive({
enabled: false,
debug: true,
agent: {
logger,
},
token: '',
});
const result = await hive
.info()
.then(() => 'OK')
.catch(() => 'ERROR');
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining(`[hive] is not enabled.`));
expect(result).toBe('OK');
});
test("should not log that it's not enabled", async () => {
const logger = {
error: jest.fn(),
info: jest.fn(),
};
const hive = createHive({
enabled: false,
debug: false,
agent: {
logger,
},
token: '',
});
const result = await hive
.info()
.then(() => 'OK')
.catch(() => 'ERROR');
expect(logger.info).not.toBeCalled();
expect(result).toBe('OK');
});