|
| 1 | +## Communication with the backend |
| 2 | + |
| 3 | +The `window.ddClient.backend` object can be used to communicate with the backend |
| 4 | +defined in the [vm section](../../extensions/METADATA.md#vm-section) in the |
| 5 | +extensions metadata. The client is already connected to the backend. |
| 6 | + |
| 7 | +It implements the following interface |
| 8 | + |
| 9 | +```typescript |
| 10 | +interface Backend { |
| 11 | + get(url: string): Promise<unknown>; |
| 12 | + post(url: string, data: any): Promise<unknown>; |
| 13 | + put(url: string, data: any): Promise<unknown>; |
| 14 | + patch(url: string, data: any): Promise<unknown>; |
| 15 | + delete(url: string): Promise<unknown>; |
| 16 | + head(url: string): Promise<unknown>; |
| 17 | + request(config: RequestConfig): Promise<unknown>; |
| 18 | + execInContainer(container: string, cmd: string): Promise<execResult>; |
| 19 | + execInVMExtension(cmd: string): Promise<execResult>; |
| 20 | +} |
| 21 | + |
| 22 | +interface RequestConfig { |
| 23 | + url: string; |
| 24 | + method: string; |
| 25 | + headers: Record<string, string>; |
| 26 | + data: any; |
| 27 | +} |
| 28 | + |
| 29 | +interface nodeExecResult { |
| 30 | + readonly cmd?: string; |
| 31 | + readonly killed?: boolean; |
| 32 | + readonly signal?: string; |
| 33 | + readonly code?: number; |
| 34 | + readonly stdout: string; |
| 35 | + readonly stderr: string; |
| 36 | +} |
| 37 | + |
| 38 | +interface execResult extends nodeExecResult { |
| 39 | + lines(): string[]; |
| 40 | + parseJsonLines(): any[]; |
| 41 | + parseJsonObject(): any; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Example usages: |
| 46 | + |
| 47 | +```typescript |
| 48 | +window.ddClient.backend |
| 49 | + .get("/some/service") |
| 50 | + .then((value: any) => console.log(value)); |
| 51 | + |
| 52 | +window.ddClient.backend |
| 53 | + .post("/some/service", { ... }) |
| 54 | + .then((value: any) => console.log(value)); |
| 55 | + |
| 56 | +window.ddClient.backend |
| 57 | + .put("/some/service", { ... }) |
| 58 | + .then((value: any) => console.log(value)); |
| 59 | + |
| 60 | +window.ddClient.backend |
| 61 | + .patch("/some/service", { ... }) |
| 62 | + .then((value: any) => console.log(value)); |
| 63 | + |
| 64 | +window.ddClient.backend |
| 65 | + .delete("/some/service") |
| 66 | + .then((value: any) => console.log(value)); |
| 67 | + |
| 68 | +window.ddClient.backend |
| 69 | + .head("/some/service") |
| 70 | + .then((value: any) => console.log(value)); |
| 71 | + |
| 72 | +window.ddClient.backend |
| 73 | + .request({ url: "/url", method: "GET", headers: { 'header-key': 'header-value' }, data: { ... }}) |
| 74 | + .then((value: any) => console.log(value)); |
| 75 | +``` |
| 76 | + |
| 77 | +## Running a command in the backend container |
| 78 | + |
| 79 | +If your extensions ships with additional binaries that should be run inside the |
| 80 | +backend container you can use the `execInVMExtension` function |
| 81 | + |
| 82 | +```typescript |
| 83 | +const output = await window.ddClient.backend.execInVMExtension( |
| 84 | + `cliShippedInTheVm xxx` |
| 85 | +); |
| 86 | +console.log(output); |
| 87 | +``` |
| 88 | + |
| 89 | +## Invoking an extension binary |
| 90 | + |
| 91 | +You can run binaries defined in the [host section](../../extensions/METADATA.md#host-section) |
| 92 | +in the exension metadata. |
| 93 | + |
| 94 | +```typescript |
| 95 | +window.ddClient.execHostCmd(`cliShippedOnHost xxx`).then((cmdResult: any) => { |
| 96 | + console.log(cmdResult); |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +Invoking an extension binary on your host and getting the output stream: |
| 101 | + |
| 102 | +```typescript |
| 103 | +window.ddClient.spawnHostCmd( |
| 104 | + `cliShippedOnHost`, |
| 105 | + [`arg1`, `arg2`], |
| 106 | + (data: any, err: any) => { |
| 107 | + console.log(data.stdout, data.stderr); |
| 108 | + // Once the command exits we get the status code |
| 109 | + if (data.code) { |
| 110 | + console.log(data.code); |
| 111 | + } |
| 112 | + } |
| 113 | +); |
| 114 | +``` |
0 commit comments