-
Notifications
You must be signed in to change notification settings - Fork 180
/
server.ts
51 lines (41 loc) · 1.56 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import * as vscode from 'vscode';
import { ZodiosPathsByMethod, ZodiosResponseByPath } from '@zodios/core/lib/zodios.types';
import { ResponseResolver, http } from 'msw';
import { setupServer, SetupServerApi } from 'msw/node';
import { apiClient, TerraformCloudAPIUrl } from '../../../../api/terraformCloud';
import { handlers } from './handlers';
type Api = typeof apiClient.api;
export function mockGetApiClient<Path extends ZodiosPathsByMethod<Api, 'get'>>(
path: Path,
// eslint-disable-next-line @typescript-eslint/ban-types
resolver: ResponseResolver<{}, never, Awaited<ZodiosResponseByPath<Api, 'get', Path>>>,
) {
return http.get(`${TerraformCloudAPIUrl}${path}`, resolver);
}
let server: SetupServerApi;
export let debugChannel: vscode.OutputChannel;
export function setupMockServer() {
debugChannel = vscode.window.createOutputChannel('MSW Debug Channel');
server = setupServer(...handlers);
server.events.on('request:unhandled', ({ request }) => {
debugChannel.appendLine(
`Intercepted a request without a matching request handler: ${request.method} ${request.url}`,
);
});
server.events.on('response:mocked', ({ request, response }) => {
debugChannel.appendLine(
`Outgoing request "${request.method} ${request.url}" received mock response: ${response.status} ${response.statusText}`,
);
});
server.listen();
}
export function stopMockServer() {
server.close();
}
export function appendLine(line: string) {
debugChannel?.appendLine(line);
}