43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
|
|
import {
|
||
|
|
createEnvConfig,
|
||
|
|
requestApiWithAutoRefresh,
|
||
|
|
type ApiResponse,
|
||
|
|
} from '@clawd/auth-runtime';
|
||
|
|
|
||
|
|
export type Command = 'run'; // TODO: add your commands
|
||
|
|
|
||
|
|
export interface RunResult {
|
||
|
|
status: 'success' | 'failed';
|
||
|
|
command: Command;
|
||
|
|
dryRun: boolean;
|
||
|
|
data?: unknown;
|
||
|
|
error?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function run(
|
||
|
|
command: Command,
|
||
|
|
args: string[],
|
||
|
|
dryRun: boolean,
|
||
|
|
): Promise<RunResult> {
|
||
|
|
const config = createEnvConfig();
|
||
|
|
const apiBase = (process.env.API_BASE ?? 'https://api-gw-test.yuanwei-lnc.com').replace(/\/$/, '');
|
||
|
|
|
||
|
|
if (command === 'run') {
|
||
|
|
const response: ApiResponse = await requestApiWithAutoRefresh(
|
||
|
|
'POST',
|
||
|
|
`${apiBase}/your/endpoint`,
|
||
|
|
dryRun,
|
||
|
|
config,
|
||
|
|
JSON.stringify({ param: args[0] }),
|
||
|
|
);
|
||
|
|
|
||
|
|
if (response.status < 200 || response.status >= 300) {
|
||
|
|
return { status: 'failed', command, dryRun, error: `HTTP ${response.status}: ${response.body}` };
|
||
|
|
}
|
||
|
|
|
||
|
|
return { status: 'success', command, dryRun, data: JSON.parse(response.body) };
|
||
|
|
}
|
||
|
|
|
||
|
|
return { status: 'failed', command, dryRun, error: `unknown command: ${command}` };
|
||
|
|
}
|