swytchcode-runtime 0.1.1 → 0.1.3
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/README.md +26 -8
- package/dist/exec.d.ts +6 -3
- package/dist/exec.js +4 -1
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +15 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# swytchcode-runtime
|
|
2
2
|
|
|
3
3
|
Thin runtime wrapper around the Swytchcode CLI. Calls `swytchcode exec` for you so you can stay in TypeScript/JavaScript without shell boilerplate.
|
|
4
4
|
|
|
@@ -9,7 +9,7 @@ By default, the runtime runs Swytchcode in **JSON mode**: the CLI is invoked wit
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
npm install
|
|
12
|
+
npm install swytchcode-runtime
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Use
|
|
@@ -17,22 +17,40 @@ npm install @swytchcode/runtime
|
|
|
17
17
|
### JSON mode (default)
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
|
-
import { exec } from "
|
|
20
|
+
import { exec } from "swytchcode-runtime";
|
|
21
21
|
|
|
22
22
|
const result = await exec("api.account.create", {
|
|
23
|
-
|
|
23
|
+
body: { name: "my-cluster" },
|
|
24
|
+
Authorization: "Bearer token123",
|
|
24
25
|
});
|
|
25
26
|
// result is parsed JSON (unknown)
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
Equivalent to: `swytchcode exec api.account.create --json` with
|
|
29
|
+
Equivalent to: `swytchcode exec api.account.create --json` with args on stdin.
|
|
30
|
+
|
|
31
|
+
**Request input (args):** The second argument is the kernel **args** object (sent as JSON on stdin). Use this shape so the kernel builds the request correctly:
|
|
32
|
+
- **`body`** — Request body (object).
|
|
33
|
+
- **`params`** — Query/path params (object, e.g. `{ id: "cluster-123" }`).
|
|
34
|
+
- **`Authorization`** — Auth header value (e.g. `"Bearer token123"`).
|
|
35
|
+
- **`headers`** — Additional request headers (e.g. `{ "X-Request-Id": "abc-123" }`).
|
|
36
|
+
- Other top-level keys are passed as query params.
|
|
37
|
+
|
|
38
|
+
Example with body, params, and headers:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
await exec("api.cluster.get", {
|
|
42
|
+
params: { id: "cluster-123" },
|
|
43
|
+
Authorization: "Bearer token123",
|
|
44
|
+
headers: { "X-Request-Id": "abc-123" },
|
|
45
|
+
});
|
|
46
|
+
```
|
|
29
47
|
|
|
30
48
|
### Raw mode
|
|
31
49
|
|
|
32
50
|
Get stdout as a string instead of parsing JSON:
|
|
33
51
|
|
|
34
52
|
```ts
|
|
35
|
-
import { exec } from "
|
|
53
|
+
import { exec } from "swytchcode-runtime";
|
|
36
54
|
|
|
37
55
|
const output = await exec("api.report.export", { id: "123" }, { raw: true });
|
|
38
56
|
// output is the raw stdout string
|
|
@@ -65,7 +83,7 @@ Or in code: `exec("shell.integration.list", undefined, { debug: true })`.
|
|
|
65
83
|
Failures throw `SwytchcodeError` (spawn error, non-zero exit, signal, empty stdout in JSON mode, invalid JSON). Use the type guard when you need to narrow:
|
|
66
84
|
|
|
67
85
|
```ts
|
|
68
|
-
import { exec, isSwytchcodeError } from "
|
|
86
|
+
import { exec, isSwytchcodeError } from "swytchcode-runtime";
|
|
69
87
|
|
|
70
88
|
try {
|
|
71
89
|
await exec("api.account.create", { email: "test@example.com" });
|
|
@@ -103,7 +121,7 @@ Generate code that uses this runtime instead of subprocess boilerplate.
|
|
|
103
121
|
**Preferred:**
|
|
104
122
|
|
|
105
123
|
```ts
|
|
106
|
-
import { exec } from "
|
|
124
|
+
import { exec } from "swytchcode-runtime";
|
|
107
125
|
|
|
108
126
|
export async function createAccount(input: { email: string }) {
|
|
109
127
|
return exec("api.account.create", input);
|
package/dist/exec.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { ExecOptions, ExecResult } from "./types.js";
|
|
1
|
+
import type { ExecArgs, ExecOptions, ExecResult } from "./types.js";
|
|
2
2
|
/**
|
|
3
|
-
* Run `swytchcode exec <canonicalId>` with optional JSON
|
|
3
|
+
* Run `swytchcode exec <canonicalId>` with optional JSON args on stdin.
|
|
4
4
|
* Default is JSON mode (stdout must be valid JSON; empty or parse failure throws).
|
|
5
5
|
* Use output: "raw" or raw: true for raw stdout string. Stream mode is not supported.
|
|
6
6
|
*
|
|
7
|
+
* The second argument (input) is the kernel args object sent on stdin: use body, params,
|
|
8
|
+
* Authorization, and headers so the kernel builds the request correctly. See ExecArgs.
|
|
9
|
+
*
|
|
7
10
|
* Enable logs: pass `{ debug: true }` or set env SWYTCHCODE_RUNTIME_DEBUG=1
|
|
8
11
|
*/
|
|
9
|
-
export declare function exec(canonicalId: string, input?: unknown, options?: ExecOptions): Promise<ExecResult>;
|
|
12
|
+
export declare function exec(canonicalId: string, input?: ExecArgs | unknown, options?: ExecOptions): Promise<ExecResult>;
|
package/dist/exec.js
CHANGED
|
@@ -16,10 +16,13 @@ function log(debug, msg, detail) {
|
|
|
16
16
|
process.stderr.write(line);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Run `swytchcode exec <canonicalId>` with optional JSON
|
|
19
|
+
* Run `swytchcode exec <canonicalId>` with optional JSON args on stdin.
|
|
20
20
|
* Default is JSON mode (stdout must be valid JSON; empty or parse failure throws).
|
|
21
21
|
* Use output: "raw" or raw: true for raw stdout string. Stream mode is not supported.
|
|
22
22
|
*
|
|
23
|
+
* The second argument (input) is the kernel args object sent on stdin: use body, params,
|
|
24
|
+
* Authorization, and headers so the kernel builds the request correctly. See ExecArgs.
|
|
25
|
+
*
|
|
23
26
|
* Enable logs: pass `{ debug: true }` or set env SWYTCHCODE_RUNTIME_DEBUG=1
|
|
24
27
|
*/
|
|
25
28
|
function exec(canonicalId, input, options = {}) {
|
package/dist/index.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -21,3 +21,18 @@ export interface ExecOptions {
|
|
|
21
21
|
}
|
|
22
22
|
/** Result of `exec()` in JSON mode: parsed stdout. In raw mode the result is a string. */
|
|
23
23
|
export type ExecResult = unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Tool arguments sent to the kernel on stdin (matches `swytchcode exec` JSON stdin).
|
|
26
|
+
* - `body`: Request body (object).
|
|
27
|
+
* - `params`: Query/path params (object).
|
|
28
|
+
* - `Authorization`: Auth header value (e.g. "Bearer token").
|
|
29
|
+
* - `headers`: Additional request headers (map of header name to value).
|
|
30
|
+
* - Other top-level keys are passed as query params.
|
|
31
|
+
*/
|
|
32
|
+
export interface ExecArgs {
|
|
33
|
+
body?: unknown;
|
|
34
|
+
params?: Record<string, string>;
|
|
35
|
+
Authorization?: string;
|
|
36
|
+
headers?: Record<string, string>;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|