mage-remote-run 0.24.0 → 0.25.0
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/lib/command-registry.js +4 -2
- package/lib/commands/rest.js +136 -0
- package/package.json +1 -1
package/lib/command-registry.js
CHANGED
|
@@ -16,8 +16,9 @@ import { registerImportCommands } from './commands/import.js';
|
|
|
16
16
|
import { registerModulesCommands } from './commands/modules.js';
|
|
17
17
|
import { registerConsoleCommand } from './commands/console.js';
|
|
18
18
|
import { registerShipmentCommands } from './commands/shipments.js';
|
|
19
|
+
import { registerRestCommands } from './commands/rest.js';
|
|
19
20
|
|
|
20
|
-
export { registerConnectionCommands, registerConsoleCommand, registerShipmentCommands };
|
|
21
|
+
export { registerConnectionCommands, registerConsoleCommand, registerShipmentCommands, registerRestCommands };
|
|
21
22
|
|
|
22
23
|
const GROUPS = {
|
|
23
24
|
CORE: [
|
|
@@ -31,7 +32,8 @@ const GROUPS = {
|
|
|
31
32
|
registerTaxCommands,
|
|
32
33
|
registerInventoryCommands,
|
|
33
34
|
registerShipmentCommands,
|
|
34
|
-
registerConsoleCommand
|
|
35
|
+
registerConsoleCommand,
|
|
36
|
+
registerRestCommands
|
|
35
37
|
],
|
|
36
38
|
COMMERCE: [
|
|
37
39
|
registerCompanyCommands,
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { createClient } from '../api/factory.js';
|
|
2
|
+
import { handleError } from '../utils.js';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { input, select, editor } from '@inquirer/prompts';
|
|
5
|
+
|
|
6
|
+
export function registerRestCommands(program) {
|
|
7
|
+
program.command('rest [path]')
|
|
8
|
+
.description('Execute a manual REST API request')
|
|
9
|
+
.option('-m, --method <method>', 'HTTP Method (GET, POST, PUT, DELETE)')
|
|
10
|
+
.option('-d, --data <data>', 'Request body data (JSON)')
|
|
11
|
+
.option('-q, --query <string>', 'Query parameters (e.g. "a=1&b=2")')
|
|
12
|
+
.option('--page-size <number>', 'Search Criteria Page Size')
|
|
13
|
+
.option('--current-page <number>', 'Search Criteria Current Page')
|
|
14
|
+
.option('-c, --content-type <type>', 'Content-Type', 'application/json')
|
|
15
|
+
.option('-f, --format <type>', 'Output format (json, xml)')
|
|
16
|
+
.addHelpText('after', `
|
|
17
|
+
Examples:
|
|
18
|
+
$ mage-remote-run rest V1/store/websites
|
|
19
|
+
$ mage-remote-run rest V1/customers/1 -m GET
|
|
20
|
+
$ mage-remote-run rest V1/customers -m POST -d '{"customer": {"email": "test@example.com", ...}}'
|
|
21
|
+
$ mage-remote-run rest V1/products -m GET -q "searchCriteria[pageSize]=10&fields=items[sku,name]"
|
|
22
|
+
$ mage-remote-run rest V1/products -m GET --page-size 10 --current-page 1
|
|
23
|
+
`)
|
|
24
|
+
.action(async (path, options) => {
|
|
25
|
+
try {
|
|
26
|
+
const client = await createClient();
|
|
27
|
+
|
|
28
|
+
// 1. Path
|
|
29
|
+
let requestPath = path;
|
|
30
|
+
if (!requestPath) {
|
|
31
|
+
requestPath = await input({
|
|
32
|
+
message: 'Enter the endpoint path (relative to base URL):',
|
|
33
|
+
validate: (value) => value.length > 0 ? true : 'Path is required'
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 2. Method
|
|
38
|
+
let method = options.method;
|
|
39
|
+
if (!method) {
|
|
40
|
+
method = await select({
|
|
41
|
+
message: 'Select HTTP Method:',
|
|
42
|
+
choices: [
|
|
43
|
+
{ name: 'GET', value: 'GET' },
|
|
44
|
+
{ name: 'POST', value: 'POST' },
|
|
45
|
+
{ name: 'PUT', value: 'PUT' },
|
|
46
|
+
{ name: 'DELETE', value: 'DELETE' }
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
method = method.toUpperCase();
|
|
51
|
+
|
|
52
|
+
// 3. Data (Body)
|
|
53
|
+
let data = options.data;
|
|
54
|
+
const contentType = options.contentType || 'application/json';
|
|
55
|
+
|
|
56
|
+
if ((method === 'POST' || method === 'PUT') && data === undefined) {
|
|
57
|
+
data = await editor({
|
|
58
|
+
message: 'Enter request body:',
|
|
59
|
+
default: contentType === 'application/json' ? '{\n \n}' : ''
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Validate and Parse JSON
|
|
64
|
+
let parsedData = data;
|
|
65
|
+
if (contentType === 'application/json' && data) {
|
|
66
|
+
if (typeof data === 'string') {
|
|
67
|
+
try {
|
|
68
|
+
parsedData = JSON.parse(data);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
throw new Error('Invalid JSON data provided.');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 4. Execution
|
|
76
|
+
if (!options.format) {
|
|
77
|
+
console.log(chalk.gray(`Executing ${method} ${requestPath}...`));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const config = {
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': contentType
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if (options.format === 'json') {
|
|
87
|
+
config.headers['Accept'] = 'application/json';
|
|
88
|
+
} else if (options.format === 'xml') {
|
|
89
|
+
config.headers['Accept'] = 'application/xml';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Parse query options
|
|
93
|
+
let params = {};
|
|
94
|
+
if (options.query) {
|
|
95
|
+
const searchParams = new URLSearchParams(options.query);
|
|
96
|
+
for (const [key, value] of searchParams) {
|
|
97
|
+
params[key] = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (options.pageSize) {
|
|
102
|
+
params['searchCriteria[pageSize]'] = options.pageSize;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (options.currentPage) {
|
|
106
|
+
params['searchCriteria[currentPage]'] = options.currentPage;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const response = await client.request(method, requestPath, parsedData, params, config);
|
|
110
|
+
|
|
111
|
+
// 5. Output
|
|
112
|
+
if (options.format === 'json') {
|
|
113
|
+
// Ensure we output valid JSON even if response is already an object
|
|
114
|
+
if (typeof response === 'object') {
|
|
115
|
+
console.log(JSON.stringify(response, null, 2));
|
|
116
|
+
} else {
|
|
117
|
+
// Attempt to parse if string, otherwise output as is (or error?)
|
|
118
|
+
// Usually response.data is parsed by axios if json.
|
|
119
|
+
console.log(response);
|
|
120
|
+
}
|
|
121
|
+
} else if (options.format === 'xml') {
|
|
122
|
+
console.log(response);
|
|
123
|
+
} else {
|
|
124
|
+
// Default behavior
|
|
125
|
+
if (typeof response === 'object') {
|
|
126
|
+
console.log(JSON.stringify(response, null, 2));
|
|
127
|
+
} else {
|
|
128
|
+
console.log(response);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
} catch (e) {
|
|
133
|
+
handleError(e);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|