bw-web-service-shared 0.0.1-test.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface HealthResponse {
|
|
2
|
+
status: "ok";
|
|
3
|
+
buildId: string;
|
|
4
|
+
env: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface InvokeCliRequestBody {
|
|
8
|
+
argv: string[];
|
|
9
|
+
terminalWidth: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface InvokeCliError {
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface InvokeCliResponseChunk {
|
|
17
|
+
terminalOutput: string;
|
|
18
|
+
streamName: "stdout" | "stderr";
|
|
19
|
+
isDone: boolean;
|
|
20
|
+
errors: InvokeCliError[];
|
|
21
|
+
warnings: InvokeCliError[];
|
|
22
|
+
exitCode: number | null;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./contract";
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type HealthResponse,
|
|
3
|
+
type InvokeCliRequestBody,
|
|
4
|
+
type InvokeCliResponseChunk,
|
|
5
|
+
} from "../contract";
|
|
6
|
+
|
|
7
|
+
export interface HttpClient {
|
|
8
|
+
health: () => Promise<HealthResponse>;
|
|
9
|
+
invoke: (
|
|
10
|
+
request: InvokeCliRequestBody,
|
|
11
|
+
) => AsyncIterable<InvokeCliResponseChunk>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class _BwWebServiceHttpClient implements HttpClient {
|
|
15
|
+
constructor(private readonly baseUrl: string) {}
|
|
16
|
+
|
|
17
|
+
async health(): Promise<HealthResponse> {
|
|
18
|
+
const res = await fetch(`${this.baseUrl}/v1/health`);
|
|
19
|
+
if (!res.ok) throw new Error(`Health check failed: ${res.status}`);
|
|
20
|
+
return res.json() as Promise<HealthResponse>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async *invoke(
|
|
24
|
+
request: InvokeCliRequestBody,
|
|
25
|
+
): AsyncIterable<InvokeCliResponseChunk> {
|
|
26
|
+
const res = await fetch(`${this.baseUrl}/v1/web-cli`, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: { "Content-Type": "application/json" },
|
|
29
|
+
body: JSON.stringify(request),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (!res.ok || !res.body) {
|
|
33
|
+
throw new Error(`Invoke failed: ${res.status}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const reader = res.body.getReader();
|
|
37
|
+
const decoder = new TextDecoder();
|
|
38
|
+
let buffer = "";
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
while (true) {
|
|
42
|
+
const { done, value } = await reader.read();
|
|
43
|
+
if (done) break;
|
|
44
|
+
|
|
45
|
+
buffer += decoder.decode(value, { stream: true });
|
|
46
|
+
const lines = buffer.split("\n");
|
|
47
|
+
buffer = lines.pop() ?? "";
|
|
48
|
+
|
|
49
|
+
for (const line of lines) {
|
|
50
|
+
if (line.trim()) {
|
|
51
|
+
yield JSON.parse(line) as InvokeCliResponseChunk;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (buffer.trim()) {
|
|
57
|
+
yield JSON.parse(buffer) as InvokeCliResponseChunk;
|
|
58
|
+
}
|
|
59
|
+
} finally {
|
|
60
|
+
reader.releaseLock();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const createHttpClient = (baseUrl: string): HttpClient =>
|
|
66
|
+
new _BwWebServiceHttpClient(baseUrl);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./httpClient";
|
package/src/index.ts
ADDED