Skip to content

Commit 024babd

Browse files
committed
response size, multiline
1 parent a4f9834 commit 024babd

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ new ConsoleLoggerLink({
4444
response: '#A5006F',
4545
},
4646
},
47-
timings: true,
47+
multiline: false,
48+
responseSize: true,
49+
responseTime: true,
4850
});
4951
```
5052

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@freewall/apollo-console-logger",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"license": "MIT",
55
"author": "Michal Vaněk",
66
"private": false,

src/index.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from 'graphql';
1313
import { print } from 'graphql/language/printer';
1414

15-
const defaultOptions: ConsoleLoggerLinkOptions = {
15+
const defaultOptions: Required<ConsoleLoggerLinkOptions> = {
1616
colors: {
1717
[OperationTypeNode.QUERY]: {
1818
request: '#E17E00',
@@ -27,7 +27,9 @@ const defaultOptions: ConsoleLoggerLinkOptions = {
2727
response: '#A68600',
2828
},*/
2929
},
30-
timings: true,
30+
multiline: false,
31+
responseSize: true,
32+
responseTime: true,
3133
};
3234

3335
interface ConsoleLoggerLinkOptions {
@@ -41,12 +43,14 @@ interface ConsoleLoggerLinkOptions {
4143
response: string;
4244
};
4345
};
44-
timings?: boolean;
46+
multiline?: boolean;
47+
responseSize?: boolean;
48+
responseTime?: boolean;
4549
}
4650

4751
export class ConsoleLoggerLink extends ApolloLink {
4852
private logId = 0;
49-
private options: ConsoleLoggerLinkOptions;
53+
private options: Required<ConsoleLoggerLinkOptions>;
5054

5155
constructor(options?: ConsoleLoggerLinkOptions) {
5256
super();
@@ -60,7 +64,9 @@ export class ConsoleLoggerLink extends ApolloLink {
6064
options?.colors?.[OperationTypeNode.MUTATION] ??
6165
defaultOptions?.colors?.[OperationTypeNode.MUTATION],
6266
},
63-
timings: options?.timings ?? true,
67+
multiline: options?.multiline ?? defaultOptions.multiline,
68+
responseSize: options?.responseSize ?? defaultOptions.responseSize,
69+
responseTime: options?.responseTime ?? defaultOptions.responseTime,
6470
};
6571
}
6672

@@ -84,7 +90,7 @@ export class ConsoleLoggerLink extends ApolloLink {
8490
this._logRequest(operation, operationAst, operationId);
8591
}
8692

87-
const startTime = this.options.timings ? Date.now() : 0;
93+
const startTime = this.options.responseTime ? Date.now() : 0;
8894

8995
return forward(operation).map((result) => {
9096
if (
@@ -115,7 +121,8 @@ export class ConsoleLoggerLink extends ApolloLink {
115121
operationId +
116122
' %c %c' +
117123
operation.operationName +
118-
'%c',
124+
'%c' +
125+
(this.options.multiline ? '\n' : ''),
119126
'background: ' +
120127
this.options.colors?.[
121128
operationAst.operation as
@@ -155,7 +162,15 @@ export class ConsoleLoggerLink extends ApolloLink {
155162
' %c ' +
156163
(!success ? '⚠️ ' : '') +
157164
`%c${operation.operationName}%c` +
158-
(this.options.timings ? ` %c${Date.now() - startTime} ms` : '%c'),
165+
(this.options.responseSize
166+
? ` %c${
167+
Math.round((JSON.stringify(results)?.length / 1024) * 10) / 10
168+
} kB`
169+
: '%c') +
170+
'%c' +
171+
(this.options.responseTime ? ` %c${Date.now() - startTime} ms` : '%c') +
172+
'%c' +
173+
(this.options.multiline ? '\n' : ''),
159174
'background: ' +
160175
this.options.colors?.[
161176
operationAst.operation as
@@ -169,9 +184,14 @@ export class ConsoleLoggerLink extends ApolloLink {
169184
? 'color: #008000;'
170185
: 'color: #CC0000; text-decoration: underline; text-decoration-style: dotted'),
171186
undefined,
172-
this.options.timings
187+
this.options.responseSize
173188
? 'background: #e4e4e4; padding: 2px 4px; border-radius: 3px; font-size: 11px;'
174189
: undefined,
190+
undefined,
191+
this.options.responseTime
192+
? 'background: #e4e4e4; padding: 2px 4px; border-radius: 3px; font-size: 11px;'
193+
: undefined,
194+
undefined,
175195
results,
176196
);
177197
}
@@ -186,5 +206,5 @@ function isEmpty(value: unknown) {
186206
return !Object.keys(value).length;
187207
}
188208

189-
return true;
209+
return false;
190210
}

0 commit comments

Comments
 (0)