apollo-logger
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -1,237 +0,246 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(factory()); | ||
}(this, (function () { 'use strict'; | ||
(function (factory) { | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
factory(); | ||
}(function () { 'use strict'; | ||
"use strict"; | ||
var __extends = (undefined && undefined.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
var __extends = (undefined && undefined.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
})(); | ||
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (undefined && undefined.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var apollo_link_1 = require("apollo-link"); | ||
var graphql_1 = require("graphql"); | ||
var iterall_1 = require("iterall"); | ||
exports.defaultFormatter = function (req) { | ||
return !req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
}; | ||
exports.defaultLogger = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return console.log.apply(null, args); | ||
}; | ||
var getDefaultLogOptions = function (options) { | ||
var result = options; | ||
if (!result.formatter) { | ||
result.formatter = exports.defaultFormatter; | ||
} | ||
if (!result.logger) { | ||
result.logger = exports.defaultLogger; | ||
} | ||
return result; | ||
}; | ||
var LoggedPubSub = (function () { | ||
function LoggedPubSub(pubsub, options) { | ||
this.pubsub = pubsub; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
LoggedPubSub.prototype.publish = function (triggerName, payload) { | ||
this.options.logger('pubsub publish', triggerName, payload); | ||
return this.pubsub.publish(triggerName, payload); | ||
}; | ||
LoggedPubSub.prototype.subscribe = function (triggerName, onMessage, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
var result, logHandler; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
logHandler = function () { | ||
var msgArgs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
msgArgs[_i] = arguments[_i]; | ||
} | ||
_this.options.logger('pubsub msg', triggerName + "(" + JSON.stringify(msgArgs) + ")"); | ||
onMessage(msgArgs); | ||
}; | ||
return [4, this.pubsub.subscribe(triggerName, logHandler, options)]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger('pubsub subscribe', triggerName, '=>', result); | ||
return [7]; | ||
case 3: return [2, result]; | ||
var __generator = (undefined && undefined.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
}); | ||
}); | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
LoggedPubSub.prototype.unsubscribe = function (subId) { | ||
this.options.logger('pubsub unsubscribe', subId); | ||
this.pubsub.unsubscribe(subId); | ||
}; | ||
LoggedPubSub.prototype.asyncIterator = function (triggers) { | ||
return new PubSubAsyncIterator(this.pubsub, triggers, this.options); | ||
}; | ||
return LoggedPubSub; | ||
}()); | ||
exports.LoggedPubSub = LoggedPubSub; | ||
var PubSubAsyncIterator = (function () { | ||
function PubSubAsyncIterator(pubsub, triggers, options) { | ||
this.asyncIter = pubsub.asyncIterator(triggers); | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.options = getDefaultLogOptions(options); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var apollo_link_1 = require("apollo-link"); | ||
var graphql_1 = require("graphql"); | ||
var iterall_1 = require("iterall"); | ||
function defaultFormatter(req) { | ||
return !req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
} | ||
PubSubAsyncIterator.prototype.next = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
exports.defaultFormatter = defaultFormatter; | ||
exports.defaultLogger = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return console.log.apply(null, args); | ||
}; | ||
PubSubAsyncIterator.prototype.return = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}; | ||
PubSubAsyncIterator.prototype.throw = function (e) { | ||
var result; | ||
try { | ||
result = this.asyncIter.throw(e); | ||
var getDefaultLogOptions = function (options) { | ||
var result = options; | ||
if (!result.formatter) { | ||
result.formatter = defaultFormatter; | ||
} | ||
finally { | ||
this.options.logger(JSON.stringify(this.triggers) + ("->throw('" + JSON.stringify(e) + "') =>"), JSON.stringify(result)); | ||
if (!result.logger) { | ||
result.logger = exports.defaultLogger; | ||
} | ||
return result; | ||
}; | ||
PubSubAsyncIterator.prototype[iterall_1.$$asyncIterator] = function () { | ||
return this; | ||
var LoggedPubSub = (function () { | ||
function LoggedPubSub(pubsub, options) { | ||
this.pubsub = pubsub; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
LoggedPubSub.prototype.publish = function (triggerName, payload) { | ||
this.options.logger('pubsub publish', triggerName, payload); | ||
return this.pubsub.publish(triggerName, payload); | ||
}; | ||
LoggedPubSub.prototype.subscribe = function (triggerName, onMessage, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, logHandler; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
logHandler = function () { | ||
var msgArgs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
msgArgs[_i] = arguments[_i]; | ||
} | ||
_this.options.logger('pubsub msg', triggerName + "(" + JSON.stringify(msgArgs) + ")"); | ||
onMessage(msgArgs); | ||
}; | ||
return [4, this.pubsub.subscribe(triggerName, logHandler, options)]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger('pubsub subscribe', triggerName, '=>', result); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}; | ||
LoggedPubSub.prototype.unsubscribe = function (subId) { | ||
this.options.logger('pubsub unsubscribe', subId); | ||
this.pubsub.unsubscribe(subId); | ||
}; | ||
LoggedPubSub.prototype.asyncIterator = function (triggers) { | ||
return new PubSubAsyncIterator(this.pubsub, triggers, this.options); | ||
}; | ||
return LoggedPubSub; | ||
}()); | ||
exports.LoggedPubSub = LoggedPubSub; | ||
var PubSubAsyncIterator = (function () { | ||
function PubSubAsyncIterator(pubsub, triggers, options) { | ||
this.asyncIter = pubsub.asyncIterator(triggers); | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
PubSubAsyncIterator.prototype.next = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}; | ||
PubSubAsyncIterator.prototype.return = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}; | ||
PubSubAsyncIterator.prototype.throw = function (e) { | ||
var result; | ||
try { | ||
result = this.asyncIter.throw(e); | ||
} | ||
finally { | ||
this.options.logger(JSON.stringify(this.triggers) + ("->throw('" + JSON.stringify(e) + "') =>"), JSON.stringify(result)); | ||
} | ||
return result; | ||
}; | ||
PubSubAsyncIterator.prototype[iterall_1.$$asyncIterator] = function () { | ||
return this; | ||
}; | ||
return PubSubAsyncIterator; | ||
}()); | ||
exports.wrapPubSub = function (pubsub, options) { | ||
return new LoggedPubSub(pubsub, options); | ||
}; | ||
return PubSubAsyncIterator; | ||
}()); | ||
exports.wrapPubSub = function (pubsub, options) { | ||
return new LoggedPubSub(pubsub, options); | ||
}; | ||
var LoggingLink = (function (_super) { | ||
__extends(LoggingLink, _super); | ||
function LoggingLink(options) { | ||
var _this = _super.call(this) || this; | ||
_this.options = getDefaultLogOptions(options); | ||
return _this; | ||
} | ||
LoggingLink.prototype.request = function (operation, forward) { | ||
var _this = this; | ||
var operationAST = graphql_1.getOperationAST(operation.query, operation.operationName); | ||
var isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
return new apollo_link_1.Observable(function (observer) { | ||
if (isSubscription) { | ||
_this.options.logger("subscribe <= " + _this.options.formatter(operation)); | ||
var LoggingLink = (function (_super) { | ||
__extends(LoggingLink, _super); | ||
function LoggingLink(options) { | ||
var _this = _super.call(this) || this; | ||
_this.options = getDefaultLogOptions(options); | ||
return _this; | ||
} | ||
LoggingLink.prototype.request = function (operation, forward) { | ||
var _this = this; | ||
var operationAST = graphql_1.getOperationAST(operation.query, operation.operationName); | ||
var isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
if (!isSubscription) { | ||
this.options.logger("loading <= " + this.options.formatter(operation)); | ||
} | ||
var sub = forward(operation).subscribe({ | ||
next: function (result) { | ||
_this.options.logger(JSON.stringify(result) + " <= " + _this.options.formatter(operation)); | ||
observer.next(result); | ||
}, | ||
error: function (error) { | ||
_this.options.logger(JSON.stringify(error) + " <=e " + _this.options.formatter(operation)); | ||
observer.error(error); | ||
}, | ||
complete: observer.complete.bind(observer) | ||
}); | ||
return function () { | ||
return new apollo_link_1.Observable(function (observer) { | ||
if (isSubscription) { | ||
_this.options.logger("unsubscribe <= " + _this.options.formatter(operation)); | ||
_this.options.logger("subscribe <= " + _this.options.formatter(operation)); | ||
} | ||
sub.unsubscribe(); | ||
}; | ||
}); | ||
}; | ||
return LoggingLink; | ||
}(apollo_link_1.ApolloLink)); | ||
exports.LoggingLink = LoggingLink; | ||
exports.formatResponse = function (logOptions, response, options) { | ||
var logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(JSON.stringify(response) + " <= " + logOpts.formatter(options)); | ||
return response; | ||
}; | ||
exports.default = function (options) { return ({ | ||
link: new LoggingLink(options), | ||
formatResponse: exports.formatResponse.bind(options), | ||
wrapPubSub: function (pubsub) { return exports.wrapPubSub(pubsub, options); } | ||
}); }; | ||
var sub = forward(operation).subscribe({ | ||
next: function (result) { | ||
_this.options.logger(JSON.stringify(result) + " <= " + _this.options.formatter(operation)); | ||
observer.next(result); | ||
}, | ||
error: function (error) { | ||
_this.options.logger(JSON.stringify(error) + " <=e " + _this.options.formatter(operation)); | ||
observer.error(error); | ||
}, | ||
complete: observer.complete.bind(observer) | ||
}); | ||
return function () { | ||
if (isSubscription) { | ||
_this.options.logger("unsubscribe <= " + _this.options.formatter(operation)); | ||
} | ||
sub.unsubscribe(); | ||
}; | ||
}); | ||
}; | ||
return LoggingLink; | ||
}(apollo_link_1.ApolloLink)); | ||
exports.LoggingLink = LoggingLink; | ||
function formatResponse(logOptions, response) { | ||
var options = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
options[_i - 2] = arguments[_i]; | ||
} | ||
var logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(JSON.stringify(response) + " <= " + logOpts.formatter(options[0])); | ||
return response; | ||
} | ||
exports.formatResponse = formatResponse; | ||
exports.default = (function (options) { return ({ | ||
link: new LoggingLink(options), | ||
formatResponse: formatResponse.bind(options), | ||
wrapPubSub: function (pubsub) { return exports.wrapPubSub(pubsub, options); } | ||
}); }); | ||
}))); | ||
//# sourceMappingURL=bundle.umd.js.map | ||
})); |
@@ -5,5 +5,5 @@ import { ApolloLink, Observable, Operation } from 'apollo-link'; | ||
logger?: (...args: any[]) => void; | ||
formatter?: (input: FormatterInput) => string; | ||
formatter?: (req: any) => string; | ||
} | ||
export declare const defaultFormatter: (req: any) => any; | ||
export declare function defaultFormatter(req: any): string; | ||
export declare const defaultLogger: (...args: any[]) => void; | ||
@@ -14,3 +14,3 @@ export declare class LoggedPubSub implements PubSubEngine { | ||
constructor(pubsub: PubSubEngine, options?: LogOptions); | ||
publish(triggerName: string, payload: any): boolean; | ||
publish(triggerName: string, payload: any): Promise<void>; | ||
subscribe(triggerName: string, onMessage: (...args: any[]) => void, options: any): Promise<number>; | ||
@@ -31,4 +31,4 @@ unsubscribe(subId: number): void; | ||
} | ||
export declare const formatResponse: (logOptions: LogOptions, response: any, options: any) => any; | ||
declare var _default: (options?: LogOptions) => { | ||
export declare function formatResponse(logOptions: LogOptions, response: any, ...options: any): any; | ||
declare const _default: (options?: LogOptions) => { | ||
link: LoggingLink; | ||
@@ -35,0 +35,0 @@ formatResponse: any; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
@@ -27,4 +30,4 @@ extendStatics(d, b); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
@@ -52,7 +55,25 @@ case 0: case 1: t = op; break; | ||
var iterall_1 = require("iterall"); | ||
exports.defaultFormatter = function (req) { | ||
var stripCircular = function (circularData, seen) { | ||
var notCircularData = Array.isArray(circularData) ? [] : {}; | ||
seen = seen || []; | ||
seen.push(circularData); | ||
Object.getOwnPropertyNames(circularData).forEach(function (key) { | ||
if (!circularData[key] || (typeof circularData[key] !== 'object' && !Array.isArray(circularData[key]))) { | ||
notCircularData[key] = circularData[key]; | ||
} | ||
else if (seen.indexOf(circularData[key]) < 0) { | ||
notCircularData[key] = stripCircular(circularData[key], seen.slice(0)); | ||
} | ||
else { | ||
notCircularData[key] = '[Circular]'; | ||
} | ||
}); | ||
return notCircularData; | ||
}; | ||
function defaultFormatter(req) { | ||
return !req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
}; | ||
} | ||
exports.defaultFormatter = defaultFormatter; | ||
exports.defaultLogger = function () { | ||
@@ -68,3 +89,3 @@ var args = []; | ||
if (!result.formatter) { | ||
result.formatter = exports.defaultFormatter; | ||
result.formatter = defaultFormatter; | ||
} | ||
@@ -87,4 +108,4 @@ if (!result.logger) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, logHandler; | ||
var _this = this; | ||
var result, logHandler; | ||
return __generator(this, function (_a) { | ||
@@ -197,2 +218,5 @@ switch (_a.label) { | ||
var isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
if (!isSubscription) { | ||
this.options.logger("loading <= " + this.options.formatter(operation)); | ||
} | ||
return new apollo_link_1.Observable(function (observer) { | ||
@@ -224,12 +248,17 @@ if (isSubscription) { | ||
exports.LoggingLink = LoggingLink; | ||
exports.formatResponse = function (logOptions, response, options) { | ||
function formatResponse(logOptions, response) { | ||
var options = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
options[_i - 2] = arguments[_i]; | ||
} | ||
var logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(JSON.stringify(response) + " <= " + logOpts.formatter(options)); | ||
logOpts.logger(JSON.stringify(response) + " <= " + logOpts.formatter(options[0])); | ||
return response; | ||
}; | ||
exports.default = function (options) { return ({ | ||
} | ||
exports.formatResponse = formatResponse; | ||
exports.default = (function (options) { return ({ | ||
link: new LoggingLink(options), | ||
formatResponse: exports.formatResponse.bind(options), | ||
formatResponse: formatResponse.bind(options), | ||
wrapPubSub: function (pubsub) { return exports.wrapPubSub(pubsub, options); } | ||
}); }; | ||
}); }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "apollo-logger", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Apollo GraphQL Logger", | ||
@@ -13,5 +13,5 @@ "main": "./lib/bundle.umd.js", | ||
"bundle": "rollup -c", | ||
"build": "tsc -p .", | ||
"build": "tsc", | ||
"watch": "tsc -w", | ||
"lint": "tslint --fix 'src/*.ts*' -p tsconfig.json --type-check && tslint 'test/*.ts*' -p tsconfig.json --type-check", | ||
"lint": "tslint -p tsconfig.json --fix", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
@@ -46,21 +46,22 @@ "minify:browser": "uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js", | ||
"@types/node": "^7.0.18", | ||
"@types/zen-observable": "^0.5.3", | ||
"apollo-link": "^1.0.0", | ||
"graphql": "^0.11.7", | ||
"graphql-subscriptions": "^0.5.8", | ||
"husky": "^0.14.3", | ||
"@types/zen-observable": "^0.8.0", | ||
"apollo-link": "^1.2.11", | ||
"graphql": "^14.2.0", | ||
"graphql-subscriptions": "^1.0.0", | ||
"husky": "^1.3.1", | ||
"iterall": "^1.2.2", | ||
"lint-staged": "^4.1.3", | ||
"prettier": "^1.6.1", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.50.0", | ||
"tslint": "^5.2.0", | ||
"tslint-config-prettier": "^1.5.0", | ||
"tslint-plugin-prettier": "^1.3.0", | ||
"typescript": "^2.3.2" | ||
"lint-staged": "^8.1.5", | ||
"prettier": "^1.16.4", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.7.3", | ||
"tslint": "^5.14.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-plugin-prettier": "^2.0.1", | ||
"typescript": "^3.3.3" | ||
}, | ||
"peerDependencies": { | ||
"apollo-link": ">= 0.5.0", | ||
"graphql": ">= 0.10.0", | ||
"iterall": ">= 1.0.0" | ||
"apollo-link": "*", | ||
"graphql-subscriptions": "*", | ||
"graphql": "*", | ||
"iterall": "*" | ||
}, | ||
@@ -83,4 +84,4 @@ "dependencies": { | ||
"singleQuote": true, | ||
"parser": "babylon" | ||
"parser": "babel" | ||
} | ||
} |
## Apollo GraphQL Logger | ||
[![npm version](https://badge.fury.io/js/apollo-logger.svg)](https://badge.fury.io/js/apollo-logger) | ||
[![Twitter Follow](https://img.shields.io/twitter/follow/sysgears.svg?style=social)](https://twitter.com/sysgears) | ||
@@ -4,0 +5,0 @@ |
import { ApolloLink, Observable, Operation } from 'apollo-link'; | ||
import { getOperationAST, print } from 'graphql'; | ||
import { getOperationAST } from 'graphql'; | ||
import { PubSubEngine } from 'graphql-subscriptions'; | ||
@@ -8,10 +8,29 @@ import { $$asyncIterator } from 'iterall'; | ||
logger?: (...args: any[]) => void; | ||
formatter?: (input: FormatterInput) => string; | ||
formatter?: (req: any) => string; | ||
} | ||
export const defaultFormatter = req => | ||
!req.variables || Object.keys(req.variables).length === 0 | ||
const stripCircular = (circularData: any, seen: any[] | null) => { | ||
const notCircularData = Array.isArray(circularData) ? [] : {}; | ||
seen = seen || []; | ||
seen.push(circularData); | ||
Object.getOwnPropertyNames(circularData).forEach(key => { | ||
if (!circularData[key] || (typeof circularData[key] !== 'object' && !Array.isArray(circularData[key]))) { | ||
notCircularData[key] = circularData[key]; | ||
} else if (seen.indexOf(circularData[key]) < 0) { | ||
notCircularData[key] = stripCircular(circularData[key], seen.slice(0)); | ||
} else { | ||
notCircularData[key] = '[Circular]'; | ||
} | ||
}); | ||
return notCircularData; | ||
}; | ||
export function defaultFormatter(req: any): string { | ||
return !req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: `${req.operationName}(${JSON.stringify(req.variables)})`; | ||
} | ||
// tslint:disable-next-line | ||
export const defaultLogger = (...args: any[]): void => console.log.apply(null, args); | ||
@@ -39,3 +58,3 @@ | ||
public publish(triggerName: string, payload: any): boolean { | ||
public publish(triggerName: string, payload: any) { | ||
this.options.logger('pubsub publish', triggerName, payload); | ||
@@ -129,2 +148,5 @@ return this.pubsub.publish(triggerName, payload); | ||
const isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
if (!isSubscription) { | ||
this.options.logger(`loading <= ${this.options.formatter(operation)}`); | ||
} | ||
return new Observable(observer => { | ||
@@ -163,8 +185,8 @@ if (isSubscription) { | ||
export const formatResponse = (logOptions: LogOptions, response: any, options: any) => { | ||
export function formatResponse(logOptions: LogOptions, response: any, ...options: any) { | ||
const logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(`${JSON.stringify(response)} <= ${logOpts.formatter(options)}`); | ||
logOpts.logger(`${JSON.stringify(response)} <= ${logOpts.formatter(options[0])}`); | ||
return response; | ||
}; | ||
} | ||
@@ -171,0 +193,0 @@ export default (options?: LogOptions) => ({ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
701
93
42209
5
8