@xano/cli 1.0.4-beta.8 → 1.0.4-beta.9
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 +28 -1
- package/dist/commands/function/run/index.d.ts +40 -0
- package/dist/commands/function/run/index.js +244 -0
- package/dist/utils/function-input.d.ts +77 -0
- package/dist/utils/function-input.js +148 -0
- package/oclif.manifest.json +2944 -2794
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -255,7 +255,34 @@ xano function edit <function_id> -f new.xs # Update from file
|
|
|
255
255
|
xano function edit <function_id> -f new.xs --edit # Open in $EDITOR before updating
|
|
256
256
|
cat function.xs | xano function edit <function_id> --stdin # Update from stdin
|
|
257
257
|
xano function edit <function_id> --no-publish # Edit without publishing
|
|
258
|
-
|
|
258
|
+
|
|
259
|
+
# Run (execute) a function by name
|
|
260
|
+
xano function run <name> # Prompts for declared inputs (on a TTY)
|
|
261
|
+
xano function run <name> --data email=jo@x.com --data age:=30 --data active:=true
|
|
262
|
+
xano function run <name> --json @payload.json --data env=staging # base payload + override
|
|
263
|
+
echo '{"email":"jo@x.com"}' | xano function run <name> --stdin -o json | jq .result
|
|
264
|
+
xano function run <name> --branch dev --logs # run on a branch, show execution logs
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Input flexibility for `function run` (assembled into one JSON `input` object):
|
|
268
|
+
|
|
269
|
+
| Form | Meaning |
|
|
270
|
+
| --- | --- |
|
|
271
|
+
| `--data key=value` | string field |
|
|
272
|
+
| `--data key:=<json>` | raw JSON field (`age:=30`, `active:=true`, `tags:='["a","b"]'`) |
|
|
273
|
+
| `--data key@file` | field value read from a file |
|
|
274
|
+
| `--json '<inline>'` / `--json @file.json` / `--json -` | a base JSON object (stdin with `-`) |
|
|
275
|
+
| `--stdin` | read the JSON object from stdin (same as `--json -`) |
|
|
276
|
+
|
|
277
|
+
Merge order is JSON base first, then `--data` overrides. Missing required inputs are
|
|
278
|
+
prompted for on an interactive terminal; in a non-TTY (CI) context the command fails
|
|
279
|
+
listing them. Output defaults to the raw `result` (`-o json`); the exit code is non-zero
|
|
280
|
+
when the function returns an error status.
|
|
281
|
+
|
|
282
|
+
> **Permissions:** running a function requires **Function** (read) plus the **run/debug**
|
|
283
|
+
> action on your workspace role. If your access token's role lacks run/debug, the call is
|
|
284
|
+
> denied with an access error — grant run/debug to the role (or use a token whose role has
|
|
285
|
+
> it). `function list`/`get`/`create`/`edit` only need the Function permission.
|
|
259
286
|
|
|
260
287
|
### Releases
|
|
261
288
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import BaseCommand from '../../../base-command.js';
|
|
2
|
+
export default class FunctionRun extends BaseCommand {
|
|
3
|
+
static args: {
|
|
4
|
+
name: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
|
|
5
|
+
};
|
|
6
|
+
static description: string;
|
|
7
|
+
static examples: string[];
|
|
8
|
+
static flags: {
|
|
9
|
+
branch: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
data: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
json: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
logs: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
13
|
+
name: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
'no-input-check': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
15
|
+
output: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
16
|
+
stdin: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
17
|
+
workspace: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
18
|
+
config: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
19
|
+
profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
20
|
+
verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
21
|
+
};
|
|
22
|
+
run(): Promise<void>;
|
|
23
|
+
private emitResult;
|
|
24
|
+
/**
|
|
25
|
+
* Fetch a single function record by name via the list endpoint's search,
|
|
26
|
+
* returning the exact-name match (so `calc` doesn't accidentally match
|
|
27
|
+
* `calcScore`). Best-effort: returns undefined and warns on any failure so a
|
|
28
|
+
* transient introspection problem never blocks an otherwise-valid run.
|
|
29
|
+
*/
|
|
30
|
+
private fetchFunctionByName;
|
|
31
|
+
/**
|
|
32
|
+
* Prompt for each missing required input on an interactive TTY. Values are
|
|
33
|
+
* read as raw JSON when they parse as such (so numbers/bools/objects work),
|
|
34
|
+
* otherwise treated as a string — mirroring the `key:=`/`key=` distinction.
|
|
35
|
+
* Non-TTY: fail fast with the list of missing params (never hang on a prompt).
|
|
36
|
+
*/
|
|
37
|
+
private promptForMissing;
|
|
38
|
+
private readStdinSync;
|
|
39
|
+
private resolveWorkspaceId;
|
|
40
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { Args, Flags } from '@oclif/core';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import BaseCommand from '../../../base-command.js';
|
|
4
|
+
import { assembleInput, loadJsonSource, parseDataPairs, validateAgainstSchema, } from '../../../utils/function-input.js';
|
|
5
|
+
export default class FunctionRun extends BaseCommand {
|
|
6
|
+
static args = {
|
|
7
|
+
name: Args.string({
|
|
8
|
+
description: 'Name of the function to run (interactive if omitted with --name unset)',
|
|
9
|
+
required: false,
|
|
10
|
+
}),
|
|
11
|
+
};
|
|
12
|
+
static description = 'Run (execute) a named function in a workspace and print its result';
|
|
13
|
+
static examples = [
|
|
14
|
+
`$ xano function:run calcScore -w 40
|
|
15
|
+
# Prompts for any declared inputs, then runs the function`,
|
|
16
|
+
`$ xano function:run calcScore --data email=jo@x.com --data age:=30 --data active:=true`,
|
|
17
|
+
`$ xano function:run calcScore --json @payload.json --data env=staging`,
|
|
18
|
+
`$ echo '{"email":"jo@x.com"}' | xano function:run calcScore --stdin -o json | jq .result`,
|
|
19
|
+
`$ xano function:run calcScore --branch dev --logs`,
|
|
20
|
+
];
|
|
21
|
+
static flags = {
|
|
22
|
+
...BaseCommand.baseFlags,
|
|
23
|
+
branch: Flags.string({
|
|
24
|
+
description: 'Branch to run from (defaults to profile branch, then main)',
|
|
25
|
+
required: false,
|
|
26
|
+
}),
|
|
27
|
+
data: Flags.string({
|
|
28
|
+
char: 'd',
|
|
29
|
+
description: 'Input field as key=value (string), key:=json (raw JSON), or key@file (file contents). Repeatable.',
|
|
30
|
+
multiple: true,
|
|
31
|
+
required: false,
|
|
32
|
+
}),
|
|
33
|
+
json: Flags.string({
|
|
34
|
+
description: "Input as a JSON object: inline, @file.json, or '-' for stdin",
|
|
35
|
+
exclusive: ['stdin'],
|
|
36
|
+
required: false,
|
|
37
|
+
}),
|
|
38
|
+
logs: Flags.boolean({
|
|
39
|
+
default: false,
|
|
40
|
+
description: 'Print the execution logs returned by the debugger',
|
|
41
|
+
required: false,
|
|
42
|
+
}),
|
|
43
|
+
'name': Flags.string({
|
|
44
|
+
char: 'n',
|
|
45
|
+
description: 'Name of the function to run (alternative to the positional argument)',
|
|
46
|
+
required: false,
|
|
47
|
+
}),
|
|
48
|
+
'no-input-check': Flags.boolean({
|
|
49
|
+
default: false,
|
|
50
|
+
description: 'Skip local schema validation and interactive prompting; send the payload as-is',
|
|
51
|
+
required: false,
|
|
52
|
+
}),
|
|
53
|
+
output: Flags.string({
|
|
54
|
+
char: 'o',
|
|
55
|
+
default: 'json',
|
|
56
|
+
description: 'Output format',
|
|
57
|
+
options: ['json', 'summary'],
|
|
58
|
+
required: false,
|
|
59
|
+
}),
|
|
60
|
+
stdin: Flags.boolean({
|
|
61
|
+
char: 's',
|
|
62
|
+
default: false,
|
|
63
|
+
description: "Read the input JSON object from stdin (same as --json -)",
|
|
64
|
+
exclusive: ['json'],
|
|
65
|
+
required: false,
|
|
66
|
+
}),
|
|
67
|
+
workspace: Flags.string({
|
|
68
|
+
char: 'w',
|
|
69
|
+
description: 'Workspace ID (optional if set in profile)',
|
|
70
|
+
required: false,
|
|
71
|
+
}),
|
|
72
|
+
};
|
|
73
|
+
async run() {
|
|
74
|
+
const { args, flags } = await this.parse(FunctionRun);
|
|
75
|
+
const { profile, profileName } = this.resolveProfile(flags);
|
|
76
|
+
const functionName = args.name ?? flags.name;
|
|
77
|
+
if (!functionName) {
|
|
78
|
+
this.error('A function name is required. Provide it as an argument or with --name.');
|
|
79
|
+
}
|
|
80
|
+
const workspaceId = this.resolveWorkspaceId(flags, profile, profileName);
|
|
81
|
+
const branch = flags.branch ?? profile.branch ?? '';
|
|
82
|
+
// 1) Assemble the input object from JSON base + --data overrides.
|
|
83
|
+
const jsonSource = flags.stdin ? '-' : flags.json;
|
|
84
|
+
const jsonBase = loadJsonSource(jsonSource, { stdin: () => this.readStdinSync() });
|
|
85
|
+
let input = assembleInput({ dataPairs: parseDataPairs(flags.data ?? []), jsonBase });
|
|
86
|
+
// 2) Introspect the function's declared inputs (best-effort) to validate,
|
|
87
|
+
// warn on obvious mismatches, and prompt for missing required params.
|
|
88
|
+
if (!flags['no-input-check']) {
|
|
89
|
+
const fn = await this.fetchFunctionByName({
|
|
90
|
+
branch,
|
|
91
|
+
name: functionName,
|
|
92
|
+
profile,
|
|
93
|
+
verbose: flags.verbose,
|
|
94
|
+
workspaceId,
|
|
95
|
+
});
|
|
96
|
+
const schema = fn?.input;
|
|
97
|
+
const { missingRequired, warnings } = validateAgainstSchema(input, schema);
|
|
98
|
+
for (const w of warnings)
|
|
99
|
+
this.warn(w);
|
|
100
|
+
if (missingRequired.length > 0) {
|
|
101
|
+
input = await this.promptForMissing(input, missingRequired);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// 3) Execute via the meta run endpoint.
|
|
105
|
+
const apiUrl = `${profile.instance_origin}/api:meta/workspace/${workspaceId}/function/run`;
|
|
106
|
+
const body = { input, name: functionName };
|
|
107
|
+
if (branch)
|
|
108
|
+
body.branch = branch;
|
|
109
|
+
const response = await this.verboseFetch(apiUrl, {
|
|
110
|
+
body: JSON.stringify(body),
|
|
111
|
+
headers: {
|
|
112
|
+
'accept': 'application/json',
|
|
113
|
+
'Authorization': `Bearer ${profile.access_token}`,
|
|
114
|
+
'Content-Type': 'application/json',
|
|
115
|
+
},
|
|
116
|
+
method: 'POST',
|
|
117
|
+
}, flags.verbose, profile.access_token);
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
const message = await this.parseApiError(response, 'Failed to run function');
|
|
120
|
+
this.error(message);
|
|
121
|
+
}
|
|
122
|
+
const result = (await response.json());
|
|
123
|
+
this.emitResult(result, flags.output, flags.logs);
|
|
124
|
+
// 4) Compose in scripts/CI: non-zero exit when the function errored.
|
|
125
|
+
if (result.status && result.status !== 'ok') {
|
|
126
|
+
this.exit(1);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
emitResult(result, output, showLogs) {
|
|
130
|
+
if (output === 'json') {
|
|
131
|
+
// Raw result object so `| jq .result` works predictably.
|
|
132
|
+
this.log(JSON.stringify(result.result ?? null, null, 2));
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
const status = result.status ?? 'ok';
|
|
136
|
+
this.log(`Status: ${status}`);
|
|
137
|
+
this.log('Result:');
|
|
138
|
+
this.log(JSON.stringify(result.result ?? null, null, 2));
|
|
139
|
+
}
|
|
140
|
+
if (showLogs && Array.isArray(result.logs) && result.logs.length > 0) {
|
|
141
|
+
this.log('');
|
|
142
|
+
this.log('Logs:');
|
|
143
|
+
for (const entry of result.logs) {
|
|
144
|
+
this.log(typeof entry === 'string' ? entry : JSON.stringify(entry));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Fetch a single function record by name via the list endpoint's search,
|
|
150
|
+
* returning the exact-name match (so `calc` doesn't accidentally match
|
|
151
|
+
* `calcScore`). Best-effort: returns undefined and warns on any failure so a
|
|
152
|
+
* transient introspection problem never blocks an otherwise-valid run.
|
|
153
|
+
*/
|
|
154
|
+
async fetchFunctionByName(opts) {
|
|
155
|
+
const { branch, name, profile, verbose, workspaceId } = opts;
|
|
156
|
+
// eslint-disable-next-line camelcase -- external Metadata API query param
|
|
157
|
+
const params = new URLSearchParams({ per_page: '100', search: name });
|
|
158
|
+
if (branch)
|
|
159
|
+
params.set('branch', branch);
|
|
160
|
+
const url = `${profile.instance_origin}/api:meta/workspace/${workspaceId}/function?${params.toString()}`;
|
|
161
|
+
try {
|
|
162
|
+
const response = await this.verboseFetch(url, { headers: { accept: 'application/json', Authorization: `Bearer ${profile.access_token}` }, method: 'GET' }, verbose, profile.access_token);
|
|
163
|
+
if (!response.ok)
|
|
164
|
+
return undefined;
|
|
165
|
+
const page = (await response.json());
|
|
166
|
+
const items = Array.isArray(page) ? page : (page.items ?? []);
|
|
167
|
+
const exact = items.filter((f) => f.name === name);
|
|
168
|
+
if (exact.length > 1) {
|
|
169
|
+
this.warn(`Multiple functions named '${name}' found; skipping input validation.`);
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
return exact[0];
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
// Introspection is a convenience, not a requirement.
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Prompt for each missing required input on an interactive TTY. Values are
|
|
181
|
+
* read as raw JSON when they parse as such (so numbers/bools/objects work),
|
|
182
|
+
* otherwise treated as a string — mirroring the `key:=`/`key=` distinction.
|
|
183
|
+
* Non-TTY: fail fast with the list of missing params (never hang on a prompt).
|
|
184
|
+
*/
|
|
185
|
+
async promptForMissing(input, missing) {
|
|
186
|
+
if (!process.stdout.isTTY || !process.stdin.isTTY) {
|
|
187
|
+
const names = missing.map((p) => `${p.name}${p.type ? ` (${p.type})` : ''}`).join(', ');
|
|
188
|
+
this.error(`Missing required input: ${names}. ` +
|
|
189
|
+
`Provide via --data/--json, or run interactively in a terminal.`);
|
|
190
|
+
}
|
|
191
|
+
const readline = await import('node:readline');
|
|
192
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
193
|
+
const ask = (message) => new Promise((resolve) => {
|
|
194
|
+
rl.question(message, resolve);
|
|
195
|
+
});
|
|
196
|
+
const filled = { ...input };
|
|
197
|
+
try {
|
|
198
|
+
for (const param of missing) {
|
|
199
|
+
const hint = [param.type, param.description].filter(Boolean).join(' — ');
|
|
200
|
+
// eslint-disable-next-line no-await-in-loop
|
|
201
|
+
const raw = await ask(`${param.name}${hint ? ` (${hint})` : ''}: `);
|
|
202
|
+
filled[param.name] = coercePromptValue(raw, param);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
rl.close();
|
|
207
|
+
}
|
|
208
|
+
return filled;
|
|
209
|
+
}
|
|
210
|
+
readStdinSync() {
|
|
211
|
+
// Synchronous read of all of stdin (fd 0); used for --json - / --stdin.
|
|
212
|
+
try {
|
|
213
|
+
return readFileSync(0, 'utf8');
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
this.error(`Failed to read from stdin: ${error instanceof Error ? error.message : String(error)}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
resolveWorkspaceId(flags, profile, profileName) {
|
|
220
|
+
if (flags.workspace)
|
|
221
|
+
return flags.workspace;
|
|
222
|
+
if (profile.workspace)
|
|
223
|
+
return profile.workspace;
|
|
224
|
+
this.error(`Workspace ID is required. Either:\n` +
|
|
225
|
+
` 1. Provide it as a flag: xano function:run <name> -w <workspace_id>\n` +
|
|
226
|
+
` 2. Set it in your profile using: xano profile:edit ${profileName} -w <workspace_id>`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Coerce a prompted string into the value the parameter likely expects. Non-text
|
|
231
|
+
* params attempt a JSON parse (so `30`, `true`, `["a"]` become typed values);
|
|
232
|
+
* text params keep the raw string. Falls back to the raw string on parse errors.
|
|
233
|
+
*/
|
|
234
|
+
function coercePromptValue(raw, param) {
|
|
235
|
+
const isTextLike = !param.type || param.type === 'text' || param.type === 'uuid';
|
|
236
|
+
if (isTextLike)
|
|
237
|
+
return raw;
|
|
238
|
+
try {
|
|
239
|
+
return JSON.parse(raw);
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return raw;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Input assembly for `xano function run`.
|
|
3
|
+
*
|
|
4
|
+
* A run's `input` is a single JSON object sent to the Metadata API. Users can
|
|
5
|
+
* build that object from several sources, which this module normalizes and
|
|
6
|
+
* merges:
|
|
7
|
+
*
|
|
8
|
+
* --json <inline | @file | -> a base JSON object
|
|
9
|
+
* --data key=value a string field (httpie convention)
|
|
10
|
+
* --data key:=<json> a raw-JSON field (numbers, bools, arrays, ...)
|
|
11
|
+
* --data key@<file> a field whose value is a file's contents (string)
|
|
12
|
+
*
|
|
13
|
+
* Merge order is: JSON base first, then --data pairs override — so the common
|
|
14
|
+
* "reuse a payload file, tweak one field" flow works without editing the file.
|
|
15
|
+
*
|
|
16
|
+
* Everything here is pure and free of oclif/network deps so it is unit-testable
|
|
17
|
+
* in isolation; the command layer handles fetching, prompting and output.
|
|
18
|
+
*/
|
|
19
|
+
export type JsonObject = Record<string, unknown>;
|
|
20
|
+
/** A single declared function input parameter, as returned by the meta API. */
|
|
21
|
+
export interface FunctionInputParam {
|
|
22
|
+
children?: FunctionInputParam[];
|
|
23
|
+
default?: unknown;
|
|
24
|
+
description?: string;
|
|
25
|
+
name: string;
|
|
26
|
+
nullable?: boolean;
|
|
27
|
+
required?: boolean;
|
|
28
|
+
style?: {
|
|
29
|
+
type?: string;
|
|
30
|
+
};
|
|
31
|
+
type?: string;
|
|
32
|
+
values?: unknown[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Split a `--data` token into its key, operator and value.
|
|
36
|
+
* Recognizes, in priority order: `:=` (raw json), `@` (file), `=` (string).
|
|
37
|
+
* The operator is the FIRST occurrence scanned left-to-right, but `:=` is
|
|
38
|
+
* detected before a bare `=` at the same position so `k:=1` is raw-json, not
|
|
39
|
+
* a string key `k:` set to `=1`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function splitDataToken(token: string): {
|
|
42
|
+
key: string;
|
|
43
|
+
op: ':=' | '=' | '@';
|
|
44
|
+
value: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Parse `--data` tokens into an object, applying httpie-style typing.
|
|
48
|
+
* Later tokens override earlier ones with the same key.
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseDataPairs(pairs: string[], readFile?: (p: string) => string): JsonObject;
|
|
51
|
+
/**
|
|
52
|
+
* Load the `--json` source: inline JSON, `@file`, or `-` (stdin, supplied by
|
|
53
|
+
* the caller via `stdin`). Must parse to a plain object; arrays/scalars are
|
|
54
|
+
* rejected because a function's input is always a keyed object.
|
|
55
|
+
*/
|
|
56
|
+
export declare function loadJsonSource(source: string | undefined, opts?: {
|
|
57
|
+
readFile?: (p: string) => string;
|
|
58
|
+
stdin?: () => string;
|
|
59
|
+
}): JsonObject | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Merge a JSON base with --data overrides into the final input object.
|
|
62
|
+
* Precedence: base first, then dataPairs override (shallow).
|
|
63
|
+
*/
|
|
64
|
+
export declare function assembleInput(opts: {
|
|
65
|
+
dataPairs?: JsonObject;
|
|
66
|
+
jsonBase?: JsonObject;
|
|
67
|
+
}): JsonObject;
|
|
68
|
+
/**
|
|
69
|
+
* Validate a supplied input object against a function's declared input schema.
|
|
70
|
+
* Non-destructive: returns lists the caller can act on (prompt for missing
|
|
71
|
+
* required params on a TTY, warn on enum mismatches, etc.). The server remains
|
|
72
|
+
* the source of truth, so type mismatches are warnings, not hard failures.
|
|
73
|
+
*/
|
|
74
|
+
export declare function validateAgainstSchema(input: JsonObject, schema: FunctionInputParam[] | undefined): {
|
|
75
|
+
missingRequired: FunctionInputParam[];
|
|
76
|
+
warnings: string[];
|
|
77
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
/**
|
|
3
|
+
* Split a `--data` token into its key, operator and value.
|
|
4
|
+
* Recognizes, in priority order: `:=` (raw json), `@` (file), `=` (string).
|
|
5
|
+
* The operator is the FIRST occurrence scanned left-to-right, but `:=` is
|
|
6
|
+
* detected before a bare `=` at the same position so `k:=1` is raw-json, not
|
|
7
|
+
* a string key `k:` set to `=1`.
|
|
8
|
+
*/
|
|
9
|
+
export function splitDataToken(token) {
|
|
10
|
+
// Find the earliest `=` and `@`; `:=` is an `=` preceded by `:`.
|
|
11
|
+
const eq = token.indexOf('=');
|
|
12
|
+
const at = token.indexOf('@');
|
|
13
|
+
// Neither operator present -> invalid.
|
|
14
|
+
if (eq === -1 && at === -1) {
|
|
15
|
+
throw new Error(`Invalid --data '${token}'. Expected key=value, key:=json, or key@file.`);
|
|
16
|
+
}
|
|
17
|
+
// Choose whichever operator appears first. On a tie an '=' can't tie with '@'
|
|
18
|
+
// at the same index, so a simple "smallest index wins" is unambiguous.
|
|
19
|
+
const useAt = at !== -1 && (eq === -1 || at < eq);
|
|
20
|
+
if (useAt) {
|
|
21
|
+
const key = token.slice(0, at);
|
|
22
|
+
if (!key)
|
|
23
|
+
throw new Error(`Invalid --data '${token}': missing key before '@'.`);
|
|
24
|
+
return { key, op: '@', value: token.slice(at + 1) };
|
|
25
|
+
}
|
|
26
|
+
// `=` path — check for the `:=` raw-json form (a ':' immediately before '=').
|
|
27
|
+
if (eq > 0 && token[eq - 1] === ':') {
|
|
28
|
+
const key = token.slice(0, eq - 1);
|
|
29
|
+
if (!key)
|
|
30
|
+
throw new Error(`Invalid --data '${token}': missing key before ':='.`);
|
|
31
|
+
return { key, op: ':=', value: token.slice(eq + 1) };
|
|
32
|
+
}
|
|
33
|
+
const key = token.slice(0, eq);
|
|
34
|
+
if (!key)
|
|
35
|
+
throw new Error(`Invalid --data '${token}': missing key before '='.`);
|
|
36
|
+
return { key, op: '=', value: token.slice(eq + 1) };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Parse `--data` tokens into an object, applying httpie-style typing.
|
|
40
|
+
* Later tokens override earlier ones with the same key.
|
|
41
|
+
*/
|
|
42
|
+
export function parseDataPairs(pairs, readFile = defaultReadFile) {
|
|
43
|
+
const out = {};
|
|
44
|
+
for (const token of pairs) {
|
|
45
|
+
const { key, op, value } = splitDataToken(token);
|
|
46
|
+
switch (op) {
|
|
47
|
+
case ':=': {
|
|
48
|
+
try {
|
|
49
|
+
out[key] = JSON.parse(value);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
throw new Error(`Invalid JSON for --data '${key}:=': ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case '@': {
|
|
57
|
+
out[key] = readFile(value);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
default: {
|
|
61
|
+
out[key] = value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Load the `--json` source: inline JSON, `@file`, or `-` (stdin, supplied by
|
|
69
|
+
* the caller via `stdin`). Must parse to a plain object; arrays/scalars are
|
|
70
|
+
* rejected because a function's input is always a keyed object.
|
|
71
|
+
*/
|
|
72
|
+
export function loadJsonSource(source, opts = {}) {
|
|
73
|
+
if (source === undefined)
|
|
74
|
+
return undefined;
|
|
75
|
+
const readFile = opts.readFile ?? defaultReadFile;
|
|
76
|
+
let raw;
|
|
77
|
+
if (source === '-') {
|
|
78
|
+
if (!opts.stdin)
|
|
79
|
+
throw new Error('stdin reader not available for --json -');
|
|
80
|
+
raw = opts.stdin();
|
|
81
|
+
}
|
|
82
|
+
else if (source.startsWith('@')) {
|
|
83
|
+
raw = readFile(source.slice(1));
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
raw = source;
|
|
87
|
+
}
|
|
88
|
+
if (raw.trim() === '')
|
|
89
|
+
return undefined;
|
|
90
|
+
let parsed;
|
|
91
|
+
try {
|
|
92
|
+
parsed = JSON.parse(raw);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw new Error(`Invalid JSON input: ${error.message}`);
|
|
96
|
+
}
|
|
97
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
98
|
+
throw new Error('Function input must be a JSON object (e.g. {"name":"..."}), not an array or scalar.');
|
|
99
|
+
}
|
|
100
|
+
return parsed;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Merge a JSON base with --data overrides into the final input object.
|
|
104
|
+
* Precedence: base first, then dataPairs override (shallow).
|
|
105
|
+
*/
|
|
106
|
+
export function assembleInput(opts) {
|
|
107
|
+
return { ...opts.jsonBase, ...opts.dataPairs };
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Validate a supplied input object against a function's declared input schema.
|
|
111
|
+
* Non-destructive: returns lists the caller can act on (prompt for missing
|
|
112
|
+
* required params on a TTY, warn on enum mismatches, etc.). The server remains
|
|
113
|
+
* the source of truth, so type mismatches are warnings, not hard failures.
|
|
114
|
+
*/
|
|
115
|
+
export function validateAgainstSchema(input, schema) {
|
|
116
|
+
const missingRequired = [];
|
|
117
|
+
const warnings = [];
|
|
118
|
+
if (!schema)
|
|
119
|
+
return { missingRequired, warnings };
|
|
120
|
+
for (const param of schema) {
|
|
121
|
+
const present = Object.hasOwn(input, param.name);
|
|
122
|
+
if (!present) {
|
|
123
|
+
// A required param with no default must be supplied.
|
|
124
|
+
if (param.required && param.default === undefined) {
|
|
125
|
+
missingRequired.push(param);
|
|
126
|
+
}
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const value = input[param.name];
|
|
130
|
+
if (value === null && param.nullable === false) {
|
|
131
|
+
warnings.push(`Input '${param.name}' is null but the parameter is not nullable.`);
|
|
132
|
+
}
|
|
133
|
+
// Enum membership check (only for non-null scalar values).
|
|
134
|
+
if (Array.isArray(param.values) && param.values.length > 0 && value !== null && !param.values.includes(value)) {
|
|
135
|
+
warnings.push(`Input '${param.name}' = ${JSON.stringify(value)} is not one of the allowed values: ` +
|
|
136
|
+
param.values.map((v) => JSON.stringify(v)).join(', '));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { missingRequired, warnings };
|
|
140
|
+
}
|
|
141
|
+
function defaultReadFile(p) {
|
|
142
|
+
try {
|
|
143
|
+
return fs.readFileSync(p, 'utf8');
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
throw new Error(`Failed to read file '${p}': ${error.message}`);
|
|
147
|
+
}
|
|
148
|
+
}
|