@adobe/aio-cli-plugin-api-mesh 1.1.0 → 1.2.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/README.md CHANGED
@@ -62,7 +62,7 @@ If you want to have custom configuration instead, please follow the steps below:
62
62
  2. Perform the following command to update the configuration
63
63
 
64
64
  ```
65
- aio config:set api-mesh.configPath <path_to_json_file>
65
+ aio config:set api-mesh.cliConfig <path_to_json_file>
66
66
  ```
67
67
 
68
68
  # Commands
@@ -1 +1 @@
1
- {"version":"1.1.0","commands":{"PLUGINNAME":{"id":"PLUGINNAME","description":"Your description here","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"examples":["$ aio PLUGINNAME:some_command"],"flags":{"someflag":{"name":"someflag","type":"option","char":"f","description":"this is some flag"}},"args":[]},"api-mesh:create":{"id":"api-mesh:create","description":"Create a mesh with the given config.","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false},"autoConfirmAction":{"name":"autoConfirmAction","type":"boolean","char":"c","description":"Auto confirm action prompt. CLI will not check for user approval before executing the action.","allowNo":false}},"args":[{"name":"file"}]},"api-mesh:delete":{"id":"api-mesh:delete","description":"Delete the config of a given mesh","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false},"autoConfirmAction":{"name":"autoConfirmAction","type":"boolean","char":"c","description":"Auto confirm action prompt. CLI will not check for user approval before executing the action.","allowNo":false}},"args":[]},"api-mesh:describe":{"id":"api-mesh:describe","description":"Get details of a mesh","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false}},"args":[]},"api-mesh:get":{"id":"api-mesh:get","description":"Get the config of a given mesh","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false}},"args":[{"name":"file"}]},"api-mesh:update":{"id":"api-mesh:update","description":"Update a mesh with the given config.","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false},"autoConfirmAction":{"name":"autoConfirmAction","type":"boolean","char":"c","description":"Auto confirm action prompt. CLI will not check for user approval before executing the action.","allowNo":false}},"args":[{"name":"file"}]},"api-mesh:source:discover":{"id":"api-mesh:source:discover","description":"Return the list of avaliable sources","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{},"args":[]},"api-mesh:source:get":{"id":"api-mesh:source:get","description":"Command returns the content of a specific source.","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"examples":["$ aio api-mesh:source:get <version>@<source_name>","$ aio api-mesh:source:get <source_name>","$ aio api-mesh:source:get -m"],"flags":{"source":{"name":"source","type":"option","char":"s","description":"Source name"},"multiple":{"name":"multiple","type":"boolean","char":"m","description":"Select multiple sources","allowNo":false}},"args":[]}}}
1
+ {"version":"1.2.0","commands":{"PLUGINNAME":{"id":"PLUGINNAME","description":"Your description here","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"examples":["$ aio PLUGINNAME:some_command"],"flags":{"someflag":{"name":"someflag","type":"option","char":"f","description":"this is some flag"}},"args":[]},"api-mesh:create":{"id":"api-mesh:create","description":"Create a mesh with the given config.","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false},"autoConfirmAction":{"name":"autoConfirmAction","type":"boolean","char":"c","description":"Auto confirm action prompt. CLI will not check for user approval before executing the action.","allowNo":false}},"args":[{"name":"file"}]},"api-mesh:delete":{"id":"api-mesh:delete","description":"Delete the config of a given mesh","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false},"autoConfirmAction":{"name":"autoConfirmAction","type":"boolean","char":"c","description":"Auto confirm action prompt. CLI will not check for user approval before executing the action.","allowNo":false}},"args":[]},"api-mesh:describe":{"id":"api-mesh:describe","description":"Get details of a mesh","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false}},"args":[]},"api-mesh:get":{"id":"api-mesh:get","description":"Get the config of a given mesh","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false}},"args":[{"name":"file"}]},"api-mesh:update":{"id":"api-mesh:update","description":"Update a mesh with the given config.","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{"ignoreCache":{"name":"ignoreCache","type":"boolean","char":"i","description":"Ignore cache and force manual org -> project -> workspace selection","allowNo":false},"autoConfirmAction":{"name":"autoConfirmAction","type":"boolean","char":"c","description":"Auto confirm action prompt. CLI will not check for user approval before executing the action.","allowNo":false}},"args":[{"name":"file"}]},"api-mesh:source:discover":{"id":"api-mesh:source:discover","description":"Return the list of avaliable sources","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"flags":{},"args":[]},"api-mesh:source:get":{"id":"api-mesh:source:get","description":"Command returns the content of a specific source.","pluginName":"@adobe/aio-cli-plugin-api-mesh","pluginType":"core","aliases":[],"examples":["$ aio api-mesh:source:get <version>@<source_name>","$ aio api-mesh:source:get <source_name>","$ aio api-mesh:source:get -m"],"flags":{"source":{"name":"source","type":"option","char":"s","description":"Source name"},"multiple":{"name":"multiple","type":"boolean","char":"m","description":"Select multiple sources","allowNo":false}},"args":[]}}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/aio-cli-plugin-api-mesh",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -89,26 +89,19 @@ describe('describe command tests', () => {
89
89
  });
90
90
 
91
91
  test('should error if describe api has failed', async () => {
92
- describeMesh.mockRejectedValueOnce(new Error('describe api failed'));
92
+ describeMesh.mockRejectedValueOnce(new Error('Describe api failed'));
93
93
 
94
94
  const runResult = DescribeCommand.run();
95
95
 
96
96
  await expect(runResult).rejects.toEqual(
97
97
  new Error(
98
- 'Unable to get mesh details. Please check the details and try again. If the error persists please contact support. RequestId: dummy_request_id',
98
+ 'Describe api failed Please check the details and try again. If the error persists please contact support. RequestId: dummy_request_id',
99
99
  ),
100
100
  );
101
- expect(logSpy.mock.calls).toMatchInlineSnapshot(`
102
- Array [
103
- Array [
104
- "describe api failed",
105
- ],
106
- ]
107
- `);
108
101
  expect(errorLogSpy.mock.calls).toMatchInlineSnapshot(`
109
102
  Array [
110
103
  Array [
111
- "Unable to get mesh details. Please check the details and try again. If the error persists please contact support. RequestId: dummy_request_id",
104
+ "Describe api failed Please check the details and try again. If the error persists please contact support. RequestId: dummy_request_id",
112
105
  ],
113
106
  ]
114
107
  `);
@@ -124,13 +117,6 @@ describe('describe command tests', () => {
124
117
  'Unable to get mesh details. Please check the details and try again. If the error persists please contact support. RequestId: dummy_request_id',
125
118
  ),
126
119
  );
127
- expect(logSpy.mock.calls).toMatchInlineSnapshot(`
128
- Array [
129
- Array [
130
- "Unable to get mesh details",
131
- ],
132
- ]
133
- `);
134
120
  expect(errorLogSpy.mock.calls).toMatchInlineSnapshot(`
135
121
  Array [
136
122
  Array [
@@ -68,13 +68,15 @@ class DescribeCommand extends Command {
68
68
  );
69
69
  }
70
70
  } else {
71
- throw new Error(`Unable to get mesh details`);
71
+ throw new Error(`Unable to get mesh details.`);
72
72
  }
73
73
  } catch (error) {
74
- this.log(error.message);
75
-
76
74
  this.error(
77
- `Unable to get mesh details. Please check the details and try again. If the error persists please contact support. RequestId: ${global.requestId}`,
75
+ `${
76
+ error.message || 'Unable to get mesh details.'
77
+ } Please check the details and try again. If the error persists please contact support. RequestId: ${
78
+ global.requestId
79
+ }`,
78
80
  );
79
81
  }
80
82
  }
package/src/helpers.js CHANGED
@@ -26,49 +26,103 @@ const { objToString } = require('./utils');
26
26
 
27
27
  const { DEV_CONSOLE_BASE_URL, DEV_CONSOLE_API_KEY, AIO_CLI_API_KEY } = CONSTANTS;
28
28
 
29
+ async function getDevConsoleConfigFromFile(configFilePath) {
30
+ try {
31
+ if (!fs.existsSync(configFilePath)) {
32
+ throw new Error(
33
+ `Config file does not exist. Please run the command: aio config:set api-mesh.configPath <path_to_json_file> with a valid file.`,
34
+ );
35
+ }
36
+
37
+ const data = JSON.parse(fs.readFileSync(configFilePath, { encoding: 'utf8', flag: 'r' }));
38
+
39
+ if (!data.baseUrl || !data.apiKey) {
40
+ throw new Error(
41
+ 'Invalid config file. Please validate the file contents and try again. Config file must contain baseUrl and apiKey.',
42
+ );
43
+ }
44
+
45
+ const baseUrl = data.baseUrl.endsWith('/')
46
+ ? data.baseUrl.slice(0, data.baseUrl.length - 1)
47
+ : data.baseUrl;
48
+
49
+ const config = {
50
+ baseUrl: baseUrl,
51
+ accessToken: (await getLibConsoleCLI()).accessToken,
52
+ apiKey: data.apiKey,
53
+ };
54
+
55
+ logger.debug(`Using cli config from ${configFilePath}: ${objToString(config)}`);
56
+
57
+ return config;
58
+ } catch (error) {
59
+ logger.error(
60
+ 'Please run the command: aio config:set api-mesh.configPath <path_to_json_file> with a valid config file.',
61
+ );
62
+
63
+ throw new Error(error);
64
+ }
65
+ }
66
+
67
+ async function getDevConsoleConfigFromObject(configObject) {
68
+ const { baseUrl, apiKey } = configObject;
69
+ const config = {
70
+ baseUrl,
71
+ accessToken: (await getLibConsoleCLI()).accessToken,
72
+ apiKey,
73
+ };
74
+
75
+ logger.debug(`Using cli config: ${objToString(config)}`);
76
+
77
+ return config;
78
+ }
79
+
29
80
  /**
30
81
  * @returns {any} Returns a config object or null
31
82
  */
32
83
  async function getDevConsoleConfig() {
33
- const configFile = Config.get('api-mesh.configPath');
84
+ const configFileOrObject = Config.get('api-mesh.cliConfig');
85
+
86
+ /**
87
+ * Old legacy option, needs to be deprecated
88
+ */
89
+ const configPath = Config.get('api-mesh.configPath');
90
+ if (configPath) {
91
+ if (configFileOrObject) {
92
+ throw new Error(
93
+ 'Found both cliConfig and configPath in api-mesh config. Please consider using only cliConfig.',
94
+ );
95
+ } else {
96
+ console.warn(
97
+ 'Please consider using cliConfig instead of configPath on api-mesh config. configPath will be deprecated soon.',
98
+ );
99
+ logger.warn(
100
+ 'Please consider using cliConfig instead of configPath on api-mesh config. configPath will be deprecated soon.',
101
+ );
34
102
 
35
- if (!configFile) {
36
- return {
103
+ return await getDevConsoleConfigFromFile(configPath);
104
+ }
105
+ }
106
+
107
+ if (!configFileOrObject) {
108
+ const config = {
37
109
  baseUrl: DEV_CONSOLE_BASE_URL,
38
110
  accessToken: (await getLibConsoleCLI()).accessToken,
39
111
  apiKey: DEV_CONSOLE_API_KEY,
40
112
  };
41
- } else {
42
- try {
43
- if (!fs.existsSync(configFile)) {
44
- throw new Error(
45
- `Config file does not exist. Please run the command: aio config:set api-mesh.configPath <path_to_json_file> with a valid file.`,
46
- );
47
- }
48
-
49
- const data = JSON.parse(fs.readFileSync(configFile, { encoding: 'utf8', flag: 'r' }));
50
-
51
- if (!data.baseUrl || !data.apiKey) {
52
- throw new Error(
53
- 'Invalid config file. Please validate the file contents and try again. Config file must contain baseUrl and apiKey.',
54
- );
55
- }
56
113
 
57
- const baseUrl = data.baseUrl.endsWith('/')
58
- ? data.baseUrl.slice(0, data.baseUrl.length - 1)
59
- : data.baseUrl;
114
+ logger.debug(`No cli config found. Using defaults: ${objToString(config)}`);
60
115
 
61
- return {
62
- baseUrl: baseUrl,
63
- accessToken: (await getLibConsoleCLI()).accessToken,
64
- apiKey: data.apiKey,
65
- };
66
- } catch (error) {
67
- logger.error(
68
- 'Please run the command: aio config:set api-mesh.configPath <path_to_json_file> with a valid config file.',
116
+ return config;
117
+ } else {
118
+ if (typeof configFileOrObject === 'object') {
119
+ return getDevConsoleConfigFromObject(configFileOrObject);
120
+ } else if (typeof configFileOrObject === 'string') {
121
+ return await getDevConsoleConfigFromFile(configFileOrObject);
122
+ } else {
123
+ throw new Error(
124
+ 'Invalid config. Please validate and try again. Config should be a JSON object or a JSON file with baseUrl and apiKey.',
69
125
  );
70
-
71
- throw new Error(error);
72
126
  }
73
127
  }
74
128
  }