-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathopenapi-watcher.ts
More file actions
54 lines (44 loc) · 1.48 KB
/
openapi-watcher.ts
File metadata and controls
54 lines (44 loc) · 1.48 KB
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
52
53
54
import { type FSWatcher, watch } from "chokidar";
import createDebug from "debug";
import { waitForEvent } from "../util/wait-for-event.js";
import { CHOKIDAR_OPTIONS } from "./constants.js";
import type { Dispatcher } from "./dispatcher.js";
import { loadOpenApiDocument } from "./load-openapi-document.js";
const debug = createDebug("counterfact:server:openapi-watcher");
export class OpenApiWatcher {
private readonly openApiPath: string;
private readonly dispatcher: Dispatcher;
private watcher: FSWatcher | undefined;
public constructor(openApiPath: string, dispatcher: Dispatcher) {
this.openApiPath = openApiPath;
this.dispatcher = dispatcher;
}
public async watch(): Promise<void> {
if (this.openApiPath === "_" || this.openApiPath.startsWith("http")) {
return;
}
this.watcher = watch(this.openApiPath, CHOKIDAR_OPTIONS).on(
"change",
() => {
void (async () => {
try {
this.dispatcher.openApiDocument = await loadOpenApiDocument(
this.openApiPath,
);
debug("reloaded OpenAPI document from %s", this.openApiPath);
} catch (error: unknown) {
debug(
"failed to reload OpenAPI document from %s: %o",
this.openApiPath,
error,
);
}
})();
},
);
await waitForEvent(this.watcher, "ready");
}
public async stopWatching(): Promise<void> {
await this.watcher?.close();
}
}