Skip to content

Commit 4817492

Browse files
committed
feat: support configuration of output request logger options in .httpyac config (AnWeber/httpyac#467)
1 parent c2e0c88 commit 4817492

8 files changed

+137
-35
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## [unreleased]
2+
3+
### Features
4+
5+
- support configuration of output request logger options in .httpyac config (Anweber/httpyac#467)
6+
- calculate average of timings for repeat (httpyac/httpyac.github.io#69)
7+
8+
### Fixes
9+
10+
- use `env` as default env dirname (Anweber/vscode-httpyac#198)
11+
112
## [6.4.6] (2023-06-09)
213

314
### Fixes

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,11 @@
711711
"default": true,
712712
"description": "log response headers"
713713
},
714+
"timings": {
715+
"type": "boolean",
716+
"default": false,
717+
"description": "log timings"
718+
},
714719
"responseBodyPrettyPrint": {
715720
"type": "boolean",
716721
"default": false,
@@ -1360,6 +1365,10 @@
13601365
"fileMatch": ".httpyac.json",
13611366
"url": "./schemas/httpyac-schema.json"
13621367
},
1368+
{
1369+
"fileMatch": "httpyac.config.json",
1370+
"url": "./schemas/httpyac-schema.json"
1371+
},
13631372
{
13641373
"fileMatch": "http-client.env.json",
13651374
"url": "./schemas/http-client.env-schema.json"

schemas/httpyac-schema.json

+59-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,57 @@
4242
},
4343
"supportAnsiColors": {
4444
"type": "boolean"
45+
},
46+
"options": {
47+
"type": "object",
48+
"properties": {
49+
"useShort": {
50+
"type": "boolean",
51+
"default": false,
52+
"description": "log only request line and status line"
53+
},
54+
"requestOutput": {
55+
"type": "boolean",
56+
"default": true,
57+
"description": "log request"
58+
},
59+
"requestHeaders": {
60+
"type": "boolean",
61+
"default": true,
62+
"description": "log request headers"
63+
},
64+
"requestBodyLength": {
65+
"type": "number",
66+
"default": 1024,
67+
"description": "log request body character length"
68+
},
69+
"responseHeaders": {
70+
"type": "boolean",
71+
"default": true,
72+
"description": "log response headers"
73+
},
74+
"timings": {
75+
"type": "boolean",
76+
"default": false,
77+
"description": "log timings"
78+
},
79+
"responseBodyPrettyPrint": {
80+
"type": "boolean",
81+
"default": false,
82+
"description": "use pretty print output if available"
83+
},
84+
"responseBodyLength": {
85+
"type": "number",
86+
"default": 1024,
87+
"description": "log response body character length"
88+
},
89+
"onlyFailed": {
90+
"type": "boolean",
91+
"default": false,
92+
"description": "log only requests with failed tests"
93+
}
94+
},
95+
"additionalProperties": false
4596
}
4697
},
4798
"additionalProperties": false
@@ -141,7 +192,13 @@
141192
},
142193
"LogLevel": {
143194
"type": "number",
144-
"enum": [0, 2, 5, 10, 100]
195+
"enum": [
196+
0,
197+
2,
198+
5,
199+
10,
200+
100
201+
]
145202
},
146203
"Variables": {
147204
"type": "object",
@@ -166,4 +223,4 @@
166223
"additionalProperties": false
167224
}
168225
}
169-
}
226+
}

src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface ResourceConfig {
4545
envDirName?: string;
4646
rootDir?: string;
4747
logLevel?: string;
48-
logOutputChannelOptions?: httpyac.utils.RequestLoggerFactoryOptions;
48+
logOutputChannelOptions?: httpyac.RequestLoggerFactoryOptions;
4949
logRequest?: boolean;
5050
useRegionScopedVariables?: boolean;
5151
}

src/documentStore.ts

+1-31
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {
22
getConfigSetting,
33
getEnvironmentConfig,
4-
getResourceConfig,
54
allHttpDocumentSelector,
65
outputDocumentSelector,
76
watchConfigSettings,
87
} from './config';
98
import { DocumentStore as IDocumentStore } from './extensionApi';
10-
import { getOutputChannel, logToOutputChannelFactory, logStream } from './io';
9+
import { logToOutputChannelFactory } from './io';
1110
import * as utils from './utils';
1211
import * as httpyac from 'httpyac';
1312
import * as vscode from 'vscode';
@@ -227,35 +226,6 @@ export class DocumentStore extends utils.DisposeProvider implements IDocumentSto
227226
logMethod: logToOutputChannelFactory('Console'),
228227
});
229228
}
230-
const resourceConfig = getResourceConfig(context.httpFile.fileName);
231-
if (resourceConfig.logRequest) {
232-
const outputChannelLogResponse = httpyac.utils.requestLoggerFactory(
233-
(arg: string) => {
234-
const requestChannel = getOutputChannel('Request', 'http');
235-
requestChannel.appendLine(arg);
236-
},
237-
resourceConfig.logOutputChannelOptions || {
238-
requestOutput: true,
239-
requestHeaders: true,
240-
requestBodyLength: 1024,
241-
responseHeaders: true,
242-
responseBodyLength: 1024,
243-
}
244-
);
245-
const logContextStream = context.logStream;
246-
context.logStream = async (type, message) => {
247-
await logStream(type, message);
248-
if (logContextStream) {
249-
await logContextStream?.(type, message);
250-
}
251-
};
252-
const logResponse = context.logResponse;
253-
context.logResponse = async (response, httpRegion) => {
254-
await outputChannelLogResponse(response, httpRegion);
255-
await logResponse?.(response, httpRegion);
256-
};
257-
}
258-
259229
if (!context.config) {
260230
context.config = config;
261231
}

src/plugin/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './bailOnFailedTestInterceptor';
2-
export * from './vscodeHttpyacPLugin';
2+
export * from './outputChannelProvider';
3+
export * from './vscodeHttpyacPlugin';

src/plugin/outputChannelProvider.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as httpyac from 'httpyac';
2+
import { ResourceConfig, getResourceConfig } from '../config';
3+
import { getOutputChannel, logStream } from '../io';
4+
5+
6+
export async function provideOutputChannelLogger(_env: string[] | undefined, context: httpyac.VariableProviderContext): Promise<httpyac.Variables> {
7+
8+
if (context.config && isProcessorContext(context)) {
9+
const resourceConfig = getResourceConfig(context.httpFile.fileName);
10+
if (resourceConfig.logRequest) {
11+
const outputChannelLogResponse = httpyac.utils.requestLoggerFactory((arg: string) => {
12+
const requestChannel = getOutputChannel('Request', 'http');
13+
requestChannel.appendLine(arg);
14+
}, getRequestLoggerOptions(resourceConfig, context.config));
15+
const logContextStream = context.logStream;
16+
context.logStream = async (type, message) => {
17+
await logStream(type, message);
18+
if (logContextStream) {
19+
await logContextStream?.(type, message);
20+
}
21+
};
22+
const logResponse = context.logResponse;
23+
context.logResponse = async (response, httpRegion) => {
24+
await outputChannelLogResponse(response, httpRegion);
25+
await logResponse?.(response, httpRegion);
26+
};
27+
}
28+
}
29+
30+
return {}
31+
}
32+
33+
function isProcessorContext(context: unknown): context is httpyac.ProcessorContext {
34+
const guard = context as httpyac.ProcessorContext;
35+
return !!guard?.config;
36+
}
37+
38+
function getRequestLoggerOptions(
39+
resourceConfig: ResourceConfig,
40+
config: httpyac.EnvironmentConfig
41+
): httpyac.RequestLoggerFactoryOptions | undefined {
42+
if (resourceConfig.logOutputChannelOptions || config.log?.options) {
43+
return Object.assign({}, resourceConfig.logOutputChannelOptions, config.log?.options);
44+
}
45+
return {
46+
requestOutput: true,
47+
requestHeaders: true,
48+
requestBodyLength: 1024,
49+
responseHeaders: true,
50+
responseBodyLength: 1024,
51+
};
52+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { HttpyacHooksApi, io } from 'httpyac';
22
import { bailOnFailedTestInterceptor } from './bailOnFailedTestInterceptor';
3+
import { provideOutputChannelLogger } from './outputChannelProvider';
34
import * as vscode from 'vscode';
45

56
export function registerVscodePlugins(api: HttpyacHooksApi) {
67
api.hooks.execute.addInterceptor(bailOnFailedTestInterceptor);
8+
api.hooks.provideVariables.addHook('provideOutputChannelLogger', provideOutputChannelLogger);
79
io.javascriptProvider.require.vscode = vscode;
810
}

0 commit comments

Comments
 (0)