@apimatic/cli 1.0.1-alpha.1 → 1.0.1-alpha.5
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 +64 -49
- package/bin/run +3 -3
- package/lib/client-utils/sdk-client.d.ts +1 -1
- package/lib/client-utils/sdk-client.js +3 -3
- package/lib/commands/api/index.js +4 -5
- package/lib/commands/api/transform.d.ts +1 -2
- package/lib/commands/api/transform.js +23 -81
- package/lib/commands/api/validate.d.ts +0 -2
- package/lib/commands/api/validate.js +16 -37
- package/lib/commands/auth/index.js +5 -5
- package/lib/commands/auth/login.js +4 -2
- package/lib/commands/auth/logout.js +1 -1
- package/lib/commands/auth/status.js +1 -1
- package/lib/commands/portal/generate.d.ts +1 -11
- package/lib/commands/portal/generate.js +23 -60
- package/lib/commands/portal/index.js +3 -3
- package/lib/commands/sdk/generate.d.ts +1 -0
- package/lib/commands/sdk/generate.js +35 -83
- package/lib/commands/sdk/index.js +2 -2
- package/lib/controllers/api/transform.d.ts +7 -0
- package/lib/controllers/api/transform.js +59 -0
- package/lib/controllers/api/validate.d.ts +5 -0
- package/lib/controllers/api/validate.js +32 -0
- package/lib/controllers/portal/generate.d.ts +2 -0
- package/lib/controllers/portal/generate.js +49 -0
- package/lib/controllers/sdk/generate.d.ts +4 -0
- package/lib/controllers/sdk/generate.js +64 -0
- package/lib/types/api/transform.d.ts +33 -0
- package/lib/types/api/transform.js +17 -0
- package/lib/types/api/validate.d.ts +12 -0
- package/lib/types/api/validate.js +2 -0
- package/lib/types/portal/generate.d.ts +9 -0
- package/lib/types/portal/generate.js +2 -0
- package/lib/types/sdk/generate.d.ts +22 -0
- package/lib/types/sdk/generate.js +12 -0
- package/oclif.manifest.json +1 -1
- package/package.json +2 -2
- package/CHANGELOG.md +0 -6
|
@@ -44,14 +44,16 @@ class Login extends command_1.Command {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
exports.default = Login;
|
|
47
|
-
Login.description = "
|
|
47
|
+
Login.description = "Login using your APIMatic credentials or an API Key";
|
|
48
48
|
Login.examples = [
|
|
49
49
|
`$ apimatic auth:login
|
|
50
50
|
Please enter your registered email: apimatic-user@gmail.com
|
|
51
51
|
Please enter your password: *********
|
|
52
52
|
|
|
53
53
|
You have successfully logged into APIMatic
|
|
54
|
-
|
|
54
|
+
`,
|
|
55
|
+
`$ apimatic auth:login --auth-key=xxxxxx
|
|
56
|
+
Authentication key successfully set`
|
|
55
57
|
];
|
|
56
58
|
Login.flags = {
|
|
57
59
|
"auth-key": command_1.flags.string({ default: "", description: "Set authentication key for all commands" })
|
|
@@ -15,7 +15,7 @@ class Status extends command_1.Command {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.default = Status;
|
|
18
|
-
Status.description = "
|
|
18
|
+
Status.description = "View current authentication state";
|
|
19
19
|
Status.examples = [
|
|
20
20
|
`$ apimatic auth:status
|
|
21
21
|
Currently logged in as apimatic-client@gmail.com
|
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
import { DocsPortalManagementController } from "@apimatic/apimatic-sdk-for-js";
|
|
2
1
|
import { Command, flags } from "@oclif/command";
|
|
3
|
-
declare type GeneratePortalParams = {
|
|
4
|
-
zippedBuildFilePath: string;
|
|
5
|
-
generatedPortalFolderPath: string;
|
|
6
|
-
docsPortalController: DocsPortalManagementController;
|
|
7
|
-
overrideAuthKey: string | null;
|
|
8
|
-
zip: boolean;
|
|
9
|
-
};
|
|
10
2
|
export default class PortalGenerate extends Command {
|
|
11
3
|
static description: string;
|
|
12
4
|
static flags: {
|
|
13
5
|
folder: flags.IOptionFlag<string>;
|
|
14
6
|
destination: flags.IOptionFlag<string>;
|
|
7
|
+
force: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
|
|
15
8
|
zip: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
|
|
16
9
|
"auth-key": flags.IOptionFlag<string>;
|
|
17
10
|
};
|
|
18
11
|
static examples: string[];
|
|
19
|
-
downloadPortalAxios: (zippedBuildFilePath: string, overrideAuthKey: string | null) => Promise<any>;
|
|
20
|
-
downloadDocsPortal: ({ zippedBuildFilePath, generatedPortalFolderPath, overrideAuthKey, zip }: GeneratePortalParams) => Promise<string>;
|
|
21
12
|
run(): Promise<undefined>;
|
|
22
13
|
}
|
|
23
|
-
export {};
|
|
@@ -1,67 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const fs = require("fs-extra");
|
|
4
|
-
const FormData = require("form-data");
|
|
5
4
|
const path = require("path");
|
|
6
5
|
const cli_ux_1 = require("cli-ux");
|
|
7
|
-
const apimatic_sdk_for_js_1 = require("@apimatic/apimatic-sdk-for-js");
|
|
8
6
|
const command_1 = require("@oclif/command");
|
|
7
|
+
const sdk_1 = require("@apimatic/sdk");
|
|
9
8
|
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
10
|
-
const
|
|
9
|
+
const generate_1 = require("../../controllers/portal/generate");
|
|
11
10
|
const utils_1 = require("../../utils/utils");
|
|
12
|
-
const auth_manager_1 = require("../../client-utils/auth-manager");
|
|
13
|
-
const axios_1 = require("axios");
|
|
14
11
|
class PortalGenerate extends command_1.Command {
|
|
15
|
-
constructor() {
|
|
16
|
-
super(...arguments);
|
|
17
|
-
// TODO: Remove after SDK is patched
|
|
18
|
-
this.downloadPortalAxios = async (zippedBuildFilePath, overrideAuthKey) => {
|
|
19
|
-
const formData = new FormData();
|
|
20
|
-
const authInfo = await auth_manager_1.getAuthInfo(this.config.configDir);
|
|
21
|
-
formData.append("file", fs.createReadStream(zippedBuildFilePath));
|
|
22
|
-
const config = {
|
|
23
|
-
headers: Object.assign({ Authorization: authInfo ? `X-Auth-Key ${overrideAuthKey || authInfo.authKey.trim()}` : "", "Content-Type": "multipart/form-data" }, formData.getHeaders()),
|
|
24
|
-
responseType: "arraybuffer"
|
|
25
|
-
};
|
|
26
|
-
const { data } = await axios_1.default.post(`${env_1.baseURL}/portal`, formData, config);
|
|
27
|
-
return data;
|
|
28
|
-
};
|
|
29
|
-
// Download Docs Portal
|
|
30
|
-
this.downloadDocsPortal = async ({ zippedBuildFilePath, generatedPortalFolderPath, overrideAuthKey, zip }) => {
|
|
31
|
-
const zippedPortalPath = path.join(generatedPortalFolderPath, "generated_portal.zip");
|
|
32
|
-
const portalPath = path.join(generatedPortalFolderPath, "generated_portal");
|
|
33
|
-
cli_ux_1.default.action.start("Downloading portal");
|
|
34
|
-
// Check if the build file exists for the user or not
|
|
35
|
-
if (!(await fs.pathExists(zippedBuildFilePath))) {
|
|
36
|
-
throw new Error("Build file doesn't exist");
|
|
37
|
-
}
|
|
38
|
-
// TODO: ***CRITICAL*** Remove this call once the SDK is patched
|
|
39
|
-
const data = await this.downloadPortalAxios(zippedBuildFilePath, overrideAuthKey);
|
|
40
|
-
await utils_1.deleteFile(zippedBuildFilePath);
|
|
41
|
-
await fs.writeFile(zippedPortalPath, data);
|
|
42
|
-
// TODO: Uncomment this code block when the SDK is patched
|
|
43
|
-
// const file: FileWrapper = new FileWrapper(fs.createReadStream(zippedBuildFilePath));
|
|
44
|
-
// const { result }: ApiResponse<NodeJS.ReadableStream | Blob> =
|
|
45
|
-
// await docsPortalController.generateOnPremPortalViaBuildInput(file);
|
|
46
|
-
// if ((data as NodeJS.ReadableStream).readable) {
|
|
47
|
-
// await writeFileUsingReadableStream(data as NodeJS.ReadableStream, zippedPortalPath);
|
|
48
|
-
if (!zip) {
|
|
49
|
-
await utils_1.unzipFile(fs.createReadStream(zippedPortalPath), portalPath);
|
|
50
|
-
await utils_1.deleteFile(zippedPortalPath);
|
|
51
|
-
}
|
|
52
|
-
cli_ux_1.default.action.stop();
|
|
53
|
-
return portalPath;
|
|
54
|
-
// } else {
|
|
55
|
-
// throw new Error("Couldn't download the portal");
|
|
56
|
-
// }
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
12
|
async run() {
|
|
60
13
|
const { flags } = this.parse(PortalGenerate);
|
|
61
14
|
const zip = flags.zip;
|
|
62
|
-
const
|
|
63
|
-
const
|
|
15
|
+
const sourceFolderPath = flags.folder;
|
|
16
|
+
const portalFolderPath = path.join(flags.destination, "generated_portal");
|
|
17
|
+
const zippedPortalPath = path.join(flags.destination, "generated_portal.zip");
|
|
64
18
|
const overrideAuthKey = flags["auth-key"] ? flags["auth-key"] : null;
|
|
19
|
+
// Check if at destination, portal already exists and throw error if force flag is not set for both zip and extracted
|
|
20
|
+
if (fs.existsSync(portalFolderPath) && !flags.force && !zip) {
|
|
21
|
+
throw new Error(`Can't download portal to path ${portalFolderPath}, because it already exists`);
|
|
22
|
+
}
|
|
23
|
+
else if (fs.existsSync(zippedPortalPath) && !flags.force && zip) {
|
|
24
|
+
throw new Error(`Can't download portal to path ${zippedPortalPath}, because it already exists`);
|
|
25
|
+
}
|
|
65
26
|
try {
|
|
66
27
|
if (!(await fs.pathExists(flags.destination))) {
|
|
67
28
|
throw new Error(`Destination path ${flags.destination} does not exist`);
|
|
@@ -70,17 +31,18 @@ class PortalGenerate extends command_1.Command {
|
|
|
70
31
|
throw new Error(`Portal build folder ${flags.folder} does not exist`);
|
|
71
32
|
}
|
|
72
33
|
const client = await sdk_client_1.SDKClient.getInstance().getClient(overrideAuthKey, this.config.configDir);
|
|
73
|
-
const docsPortalController = new
|
|
74
|
-
const zippedBuildFilePath = await utils_1.zipDirectory(
|
|
34
|
+
const docsPortalController = new sdk_1.DocsPortalManagementController(client);
|
|
35
|
+
const zippedBuildFilePath = await utils_1.zipDirectory(sourceFolderPath, flags.destination);
|
|
75
36
|
const generatePortalParams = {
|
|
76
37
|
zippedBuildFilePath,
|
|
77
|
-
|
|
38
|
+
portalFolderPath,
|
|
39
|
+
zippedPortalPath,
|
|
78
40
|
docsPortalController,
|
|
79
41
|
overrideAuthKey,
|
|
80
42
|
zip
|
|
81
43
|
};
|
|
82
|
-
const generatedPortalPath = await
|
|
83
|
-
this.log(`Your portal has been generated at ${generatedPortalPath}
|
|
44
|
+
const generatedPortalPath = await generate_1.downloadDocsPortal(generatePortalParams, this.config.configDir);
|
|
45
|
+
this.log(`Your portal has been generated at ${generatedPortalPath}`);
|
|
84
46
|
}
|
|
85
47
|
catch (error) {
|
|
86
48
|
cli_ux_1.default.action.stop();
|
|
@@ -116,22 +78,23 @@ class PortalGenerate extends command_1.Command {
|
|
|
116
78
|
}
|
|
117
79
|
}
|
|
118
80
|
exports.default = PortalGenerate;
|
|
119
|
-
PortalGenerate.description = "Generate static
|
|
81
|
+
PortalGenerate.description = "Generate and download a static API Documentation portal. Requires an input directory containing API specifications, a config file and optionally, markdown guides. For details, refer to the [documentation](https://portal-api-docs.apimatic.io/#/http/generating-api-portal/build-file)";
|
|
120
82
|
PortalGenerate.flags = {
|
|
121
83
|
folder: command_1.flags.string({
|
|
122
84
|
parse: (input) => path.resolve(input),
|
|
123
85
|
default: "",
|
|
124
|
-
description: "
|
|
86
|
+
description: "path to the input directory containing API specifications and config files"
|
|
125
87
|
}),
|
|
126
88
|
destination: command_1.flags.string({
|
|
127
89
|
parse: (input) => path.resolve(input),
|
|
128
90
|
default: "./",
|
|
129
91
|
description: "path to the downloaded portal"
|
|
130
92
|
}),
|
|
131
|
-
|
|
93
|
+
force: command_1.flags.boolean({ char: "f", default: false, description: "overwrite if a portal exists in the destination" }),
|
|
94
|
+
zip: command_1.flags.boolean({ default: false, description: "download the generated portal as a .zip archive" }),
|
|
132
95
|
"auth-key": command_1.flags.string({
|
|
133
96
|
default: "",
|
|
134
|
-
description: "override current
|
|
97
|
+
description: "override current authentication state with an authentication key"
|
|
135
98
|
})
|
|
136
99
|
};
|
|
137
100
|
PortalGenerate.examples = [
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const command_1 = require("@oclif/command");
|
|
4
4
|
class SDK extends command_1.Command {
|
|
5
5
|
async run() {
|
|
6
|
-
this.log(`
|
|
6
|
+
this.log(`Manage API documentation portals
|
|
7
7
|
|
|
8
8
|
USAGE
|
|
9
9
|
$ apimatic portal
|
|
@@ -12,9 +12,9 @@ EXAMPLE
|
|
|
12
12
|
$apimatic portal --help
|
|
13
13
|
|
|
14
14
|
COMMANDS
|
|
15
|
-
portal:generate Generate static
|
|
15
|
+
portal:generate Generate and download a static API Documentation portal. Requires an input directory containing API specifications, a config file and optionally, markdown guides. For details, refer to the [documentation](https://portal-api-docs.apimatic.io/#/http/generating-api-portal/build-file)`);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
exports.default = SDK;
|
|
19
|
-
SDK.description = "
|
|
19
|
+
SDK.description = "Manage API documentation portals";
|
|
20
20
|
SDK.examples = ["$apimatic portal --help"];
|
|
@@ -6,6 +6,7 @@ export default class SdkGenerate extends Command {
|
|
|
6
6
|
file: flags.IOptionFlag<string>;
|
|
7
7
|
url: flags.IOptionFlag<string>;
|
|
8
8
|
destination: flags.IOptionFlag<string>;
|
|
9
|
+
force: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
|
|
9
10
|
zip: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
|
|
10
11
|
"auth-key": flags.IOptionFlag<string>;
|
|
11
12
|
};
|
|
@@ -3,78 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const fs = require("fs-extra");
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const cli_ux_1 = require("cli-ux");
|
|
6
|
-
const
|
|
6
|
+
const sdk_1 = require("@apimatic/sdk");
|
|
7
7
|
const command_1 = require("@oclif/command");
|
|
8
8
|
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
9
9
|
const utils_1 = require("../../utils/utils");
|
|
10
|
-
|
|
11
|
-
(function (SimplePlatforms) {
|
|
12
|
-
SimplePlatforms["CSHARP"] = "CS_NET_STANDARD_LIB";
|
|
13
|
-
SimplePlatforms["JAVA"] = "JAVA_ECLIPSE_JRE_LIB";
|
|
14
|
-
SimplePlatforms["PHP"] = "PHP_GENERIC_LIB";
|
|
15
|
-
SimplePlatforms["PYTHON"] = "PYTHON_GENERIC_LIB";
|
|
16
|
-
SimplePlatforms["RUBY"] = "RUBY_GENERIC_LIB";
|
|
17
|
-
SimplePlatforms["TYPESCRIPT"] = "TS_GENERIC_LIB";
|
|
18
|
-
})(SimplePlatforms || (SimplePlatforms = {}));
|
|
19
|
-
async function getSDKGenerationId({ file, url, platform }, sdkGenerationController) {
|
|
20
|
-
cli_ux_1.default.action.start("Generating SDK");
|
|
21
|
-
let generation;
|
|
22
|
-
const sdkPlatform = getSDKPlatform(platform);
|
|
23
|
-
if (file) {
|
|
24
|
-
const fileDescriptor = new apimatic_sdk_for_js_1.FileWrapper(fs.createReadStream(file));
|
|
25
|
-
generation = await sdkGenerationController.generateSDKViaFile(fileDescriptor, sdkPlatform);
|
|
26
|
-
}
|
|
27
|
-
else if (url) {
|
|
28
|
-
// If url to spec file is provided
|
|
29
|
-
const body = {
|
|
30
|
-
url: url,
|
|
31
|
-
template: sdkPlatform
|
|
32
|
-
};
|
|
33
|
-
generation = await sdkGenerationController.generateSDKViaURL(body);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
throw new Error("Please provide a specification file");
|
|
37
|
-
}
|
|
38
|
-
cli_ux_1.default.action.stop();
|
|
39
|
-
return generation.result.id;
|
|
40
|
-
}
|
|
41
|
-
// Get valid platform from user's input, convert simple platform to valid Platforms enum value
|
|
42
|
-
function getSDKPlatform(platform) {
|
|
43
|
-
if (Object.keys(SimplePlatforms).includes(platform)) {
|
|
44
|
-
return SimplePlatforms[platform];
|
|
45
|
-
}
|
|
46
|
-
else if (Object.values(apimatic_sdk_for_js_1.Platforms).includes(platform)) {
|
|
47
|
-
return platform;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
const platforms = Object.keys(SimplePlatforms).concat(Object.values(apimatic_sdk_for_js_1.Platforms)).join("|");
|
|
51
|
-
throw new Error(`Please provide a valid platform i.e. ${platforms}`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
// Download Platform
|
|
55
|
-
async function downloadGeneratedSDK({ codeGenId, zippedSDKPath, sdkFolderPath, zip }, sdkGenerationController) {
|
|
56
|
-
cli_ux_1.default.action.start("Downloading SDK");
|
|
57
|
-
const { result } = await sdkGenerationController.downloadSDK(codeGenId);
|
|
58
|
-
if (result.readable) {
|
|
59
|
-
if (!zip) {
|
|
60
|
-
await utils_1.unzipFile(result, sdkFolderPath);
|
|
61
|
-
cli_ux_1.default.action.stop();
|
|
62
|
-
return sdkFolderPath;
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
await utils_1.writeFileUsingReadableStream(result, zippedSDKPath);
|
|
66
|
-
cli_ux_1.default.action.stop();
|
|
67
|
-
return zippedSDKPath;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
throw new Error("Couldn't download the SDK");
|
|
72
|
-
}
|
|
73
|
-
}
|
|
10
|
+
const generate_1 = require("../../controllers/sdk/generate");
|
|
74
11
|
class SdkGenerate extends command_1.Command {
|
|
75
12
|
async run() {
|
|
76
13
|
const { flags } = this.parse(SdkGenerate);
|
|
77
14
|
const zip = flags.zip;
|
|
15
|
+
const fileName = flags.file ? utils_1.getFileNameFromPath(flags.file) : utils_1.getFileNameFromPath(flags.url);
|
|
16
|
+
const sdkFolderPath = path.join(flags.destination, `${fileName}_sdk_${flags.platform}`.toLowerCase());
|
|
17
|
+
const zippedSDKPath = path.join(flags.destination, `${fileName}_sdk_${flags.platform}.zip`.toLowerCase());
|
|
18
|
+
// Check if at destination, SDK already exists and throw error if force flag is not set for both zip and extracted
|
|
19
|
+
if (fs.existsSync(sdkFolderPath) && !flags.force && !zip) {
|
|
20
|
+
throw new Error(`Can't download SDK to path ${sdkFolderPath}, because it already exists`);
|
|
21
|
+
}
|
|
22
|
+
else if (fs.existsSync(zippedSDKPath) && !flags.force && zip) {
|
|
23
|
+
throw new Error(`Can't download SDK to path ${zippedSDKPath}, because it already exists`);
|
|
24
|
+
}
|
|
78
25
|
try {
|
|
79
26
|
if (!(await fs.pathExists(path.resolve(flags.destination)))) {
|
|
80
27
|
throw new Error(`Destination path ${flags.destination} does not exist`);
|
|
@@ -82,20 +29,11 @@ class SdkGenerate extends command_1.Command {
|
|
|
82
29
|
else if (!(await fs.pathExists(path.resolve(flags.file)))) {
|
|
83
30
|
throw new Error(`Specification file ${flags.file} does not exist`);
|
|
84
31
|
}
|
|
85
|
-
const fileName = flags.file ? utils_1.getFileNameFromPath(flags.file) : utils_1.getFileNameFromPath(flags.url);
|
|
86
|
-
const sdkFolderPath = path.join(flags.destination, `${fileName}_sdk_${flags.platform}`.toLowerCase());
|
|
87
|
-
const zippedSDKPath = path.join(flags.destination, `${fileName}_sdk_${flags.platform}.zip`.toLowerCase());
|
|
88
|
-
if (fs.existsSync(zippedSDKPath) && zip) {
|
|
89
|
-
throw new Error(`Can't generate SDK to path ${zippedSDKPath}, because it already exists`);
|
|
90
|
-
}
|
|
91
|
-
else if (fs.existsSync(sdkFolderPath) && !zip) {
|
|
92
|
-
throw new Error(`Can't generate SDK to path ${sdkFolderPath}, because it already exists`);
|
|
93
|
-
}
|
|
94
32
|
const overrideAuthKey = flags["auth-key"] ? flags["auth-key"] : null;
|
|
95
33
|
const client = await sdk_client_1.SDKClient.getInstance().getClient(overrideAuthKey, this.config.configDir);
|
|
96
|
-
const sdkGenerationController = new
|
|
34
|
+
const sdkGenerationController = new sdk_1.CodeGenerationExternalApisController(client);
|
|
97
35
|
// Get generation id for the specification and platform
|
|
98
|
-
const codeGenId = await getSDKGenerationId(flags, sdkGenerationController);
|
|
36
|
+
const codeGenId = await generate_1.getSDKGenerationId(flags, sdkGenerationController);
|
|
99
37
|
// If user wanted to download the SDK as well
|
|
100
38
|
const sdkDownloadParams = {
|
|
101
39
|
codeGenId,
|
|
@@ -103,7 +41,7 @@ class SdkGenerate extends command_1.Command {
|
|
|
103
41
|
sdkFolderPath,
|
|
104
42
|
zip
|
|
105
43
|
};
|
|
106
|
-
const sdkPath = await downloadGeneratedSDK(sdkDownloadParams, sdkGenerationController);
|
|
44
|
+
const sdkPath = await generate_1.downloadGeneratedSDK(sdkDownloadParams, sdkGenerationController);
|
|
107
45
|
this.log(`Success! Your SDK is located at ${sdkPath}`);
|
|
108
46
|
}
|
|
109
47
|
catch (error) {
|
|
@@ -157,22 +95,36 @@ Legacy: CS_NET_STANDARD_LIB|CS_PORTABLE_NET_LIB|CS_UNIVERSAL_WINDOWS_PLATFORM_LI
|
|
|
157
95
|
file: command_1.flags.string({
|
|
158
96
|
parse: (input) => path.resolve(input),
|
|
159
97
|
default: "",
|
|
160
|
-
description: "path to the API specification to generate
|
|
98
|
+
description: "path to the API specification to generate SDKs for"
|
|
99
|
+
}),
|
|
100
|
+
url: command_1.flags.string({
|
|
101
|
+
default: "",
|
|
102
|
+
description: "URL to the API specification to generate SDKs for. Can be used in place of the --file option if the API specification is publicly available."
|
|
161
103
|
}),
|
|
162
|
-
url: command_1.flags.string({ default: "", description: "URL to the API specification to generate SDK" }),
|
|
163
104
|
destination: command_1.flags.string({
|
|
164
105
|
parse: (input) => path.resolve(input),
|
|
165
106
|
default: "./",
|
|
166
|
-
description: "
|
|
107
|
+
description: "directory to download the generated SDK to"
|
|
108
|
+
}),
|
|
109
|
+
force: command_1.flags.boolean({
|
|
110
|
+
char: "f",
|
|
111
|
+
default: false,
|
|
112
|
+
description: "overwrite if an SDK already exists in the destination"
|
|
167
113
|
}),
|
|
168
|
-
zip: command_1.flags.boolean({ default: false, description: "
|
|
114
|
+
zip: command_1.flags.boolean({ default: false, description: "download the generated SDK as a .zip archive" }),
|
|
169
115
|
"auth-key": command_1.flags.string({
|
|
170
116
|
default: "",
|
|
171
|
-
description: "override current
|
|
117
|
+
description: "override current authentication state with an authentication key"
|
|
172
118
|
})
|
|
173
119
|
};
|
|
174
120
|
SdkGenerate.examples = [
|
|
175
121
|
`$ apimatic sdk:generate --platform="CSHARP" --file="./specs/sample.json"
|
|
176
|
-
SDK
|
|
122
|
+
Generating SDK... done
|
|
123
|
+
Downloading SDK... done
|
|
124
|
+
Success! Your SDK is located at swagger_sdk_csharp`,
|
|
125
|
+
`$ apimatic sdk:generate --platform="CSHARP" --url=https://petstore.swagger.io/v2/swagger.json
|
|
126
|
+
Generating SDK... done
|
|
127
|
+
Downloading SDK... done
|
|
128
|
+
Success! Your SDK is located at swagger_sdk_csharp
|
|
177
129
|
`
|
|
178
130
|
];
|
|
@@ -4,7 +4,7 @@ const command_1 = require("@oclif/command");
|
|
|
4
4
|
class SDK extends command_1.Command {
|
|
5
5
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
6
6
|
async run() {
|
|
7
|
-
this.log(`
|
|
7
|
+
this.log(`Generate and manage SDKs for APIs
|
|
8
8
|
|
|
9
9
|
USAGE
|
|
10
10
|
$ apimatic sdk
|
|
@@ -17,5 +17,5 @@ COMMANDS
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
exports.default = SDK;
|
|
20
|
-
SDK.description = "
|
|
20
|
+
SDK.description = "Generate and manage SDKs for APIs";
|
|
21
21
|
SDK.examples = ["$apimatic sdk --help"];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Command from "@oclif/command";
|
|
2
|
+
import { ApiValidationSummary, ExportFormats, Transformation, TransformationController } from "@apimatic/sdk";
|
|
3
|
+
import { DownloadTransformationParams, TransformationIdParams } from "../../types/api/transform";
|
|
4
|
+
export declare const getTransformationId: ({ file, url, format }: TransformationIdParams, transformationController: TransformationController) => Promise<Transformation>;
|
|
5
|
+
export declare const downloadTransformationFile: ({ id, destinationFilePath, transformationController }: DownloadTransformationParams) => Promise<string>;
|
|
6
|
+
export declare const getValidFormat: (format: string) => ExportFormats;
|
|
7
|
+
export declare const printValidationMessages: (apiValidationSummary: ApiValidationSummary | undefined, warn: Command["warn"], error: Command["error"]) => void;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.printValidationMessages = exports.getValidFormat = exports.downloadTransformationFile = exports.getTransformationId = void 0;
|
|
4
|
+
const cli_ux_1 = require("cli-ux");
|
|
5
|
+
const fs = require("fs-extra");
|
|
6
|
+
const sdk_1 = require("@apimatic/sdk");
|
|
7
|
+
const utils_1 = require("../../utils/utils");
|
|
8
|
+
exports.getTransformationId = async ({ file, url, format }, transformationController) => {
|
|
9
|
+
cli_ux_1.default.action.start("Transforming API specification");
|
|
10
|
+
let generation;
|
|
11
|
+
if (file) {
|
|
12
|
+
const fileDescriptor = new sdk_1.FileWrapper(fs.createReadStream(file));
|
|
13
|
+
generation = await transformationController.transformViaFile(fileDescriptor, format);
|
|
14
|
+
}
|
|
15
|
+
else if (url) {
|
|
16
|
+
const body = {
|
|
17
|
+
url: url,
|
|
18
|
+
exportFormat: format
|
|
19
|
+
};
|
|
20
|
+
generation = await transformationController.transformViaURL(body);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw new Error("Please provide a specification file");
|
|
24
|
+
}
|
|
25
|
+
cli_ux_1.default.action.stop();
|
|
26
|
+
return generation.result;
|
|
27
|
+
};
|
|
28
|
+
exports.downloadTransformationFile = async ({ id, destinationFilePath, transformationController }) => {
|
|
29
|
+
cli_ux_1.default.action.start("Downloading Transformed file");
|
|
30
|
+
const { result } = await transformationController.downloadTransformedFile(id);
|
|
31
|
+
if (result.readable) {
|
|
32
|
+
await utils_1.writeFileUsingReadableStream(result, destinationFilePath);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error("Couldn't save transformation file");
|
|
36
|
+
}
|
|
37
|
+
cli_ux_1.default.action.stop();
|
|
38
|
+
return destinationFilePath;
|
|
39
|
+
};
|
|
40
|
+
// Get valid platform from user's input, convert simple platform to valid Platforms enum value
|
|
41
|
+
exports.getValidFormat = (format) => {
|
|
42
|
+
if (Object.keys(sdk_1.ExportFormats).find((exportFormat) => exportFormat === format)) {
|
|
43
|
+
return sdk_1.ExportFormats[format];
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const formats = Object.keys(sdk_1.ExportFormats).join("|");
|
|
47
|
+
throw new Error(`Please provide a valid platform i.e. ${formats}`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
exports.printValidationMessages = (apiValidationSummary, warn, error) => {
|
|
51
|
+
const warnings = (apiValidationSummary === null || apiValidationSummary === void 0 ? void 0 : apiValidationSummary.warnings) || [];
|
|
52
|
+
const errors = (apiValidationSummary === null || apiValidationSummary === void 0 ? void 0 : apiValidationSummary.errors.join("\n")) || "";
|
|
53
|
+
warnings.forEach((warning) => {
|
|
54
|
+
warn(utils_1.replaceHTML(warning));
|
|
55
|
+
});
|
|
56
|
+
if (apiValidationSummary && apiValidationSummary.errors.length > 0) {
|
|
57
|
+
error(utils_1.replaceHTML(errors));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import Command from "@oclif/command";
|
|
2
|
+
import { APIValidationExternalApisController, ApiValidationSummary } from "@apimatic/sdk";
|
|
3
|
+
import { GetValidationParams } from "../../types/api/validate";
|
|
4
|
+
export declare const getValidation: ({ file, url }: GetValidationParams, apiValidationController: APIValidationExternalApisController) => Promise<ApiValidationSummary>;
|
|
5
|
+
export declare const printValidationMessages: ({ warnings, errors }: ApiValidationSummary, warn: Command["warn"], error: Command["error"]) => void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.printValidationMessages = exports.getValidation = void 0;
|
|
4
|
+
const cli_ux_1 = require("cli-ux");
|
|
5
|
+
const fs = require("fs-extra");
|
|
6
|
+
const core_1 = require("@apimatic/core");
|
|
7
|
+
const utils_1 = require("../../utils/utils");
|
|
8
|
+
exports.getValidation = async ({ file, url }, apiValidationController) => {
|
|
9
|
+
let validation;
|
|
10
|
+
cli_ux_1.default.action.start("Validating specification file");
|
|
11
|
+
if (file) {
|
|
12
|
+
const fileDescriptor = new core_1.FileWrapper(fs.createReadStream(file));
|
|
13
|
+
validation = await apiValidationController.validateAPIViaFile(fileDescriptor);
|
|
14
|
+
}
|
|
15
|
+
else if (url) {
|
|
16
|
+
validation = await apiValidationController.validateAPIViaURL(url);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
throw new Error("Please provide a specification file");
|
|
20
|
+
}
|
|
21
|
+
cli_ux_1.default.action.stop();
|
|
22
|
+
return validation.result;
|
|
23
|
+
};
|
|
24
|
+
exports.printValidationMessages = ({ warnings, errors }, warn, error) => {
|
|
25
|
+
warnings.forEach((warning) => {
|
|
26
|
+
warn(`${utils_1.replaceHTML(warning)}`);
|
|
27
|
+
});
|
|
28
|
+
if (errors.length > 0) {
|
|
29
|
+
const singleLineError = errors.join("\n");
|
|
30
|
+
error(utils_1.replaceHTML(singleLineError));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downloadDocsPortal = void 0;
|
|
4
|
+
const fs = require("fs-extra");
|
|
5
|
+
const FormData = require("form-data");
|
|
6
|
+
const cli_ux_1 = require("cli-ux");
|
|
7
|
+
const auth_manager_1 = require("../../client-utils/auth-manager");
|
|
8
|
+
const env_1 = require("../../config/env");
|
|
9
|
+
const utils_1 = require("../../utils/utils");
|
|
10
|
+
const axios_1 = require("axios");
|
|
11
|
+
// TODO: Remove after SDK is patched
|
|
12
|
+
const downloadPortalAxios = async (zippedBuildFilePath, overrideAuthKey, configDir) => {
|
|
13
|
+
const formData = new FormData();
|
|
14
|
+
const authInfo = await auth_manager_1.getAuthInfo(configDir);
|
|
15
|
+
formData.append("file", fs.createReadStream(zippedBuildFilePath));
|
|
16
|
+
const config = {
|
|
17
|
+
headers: Object.assign({ Authorization: authInfo ? `X-Auth-Key ${overrideAuthKey || authInfo.authKey.trim()}` : "", "Content-Type": "multipart/form-data" }, formData.getHeaders()),
|
|
18
|
+
responseType: "arraybuffer"
|
|
19
|
+
};
|
|
20
|
+
const { data } = await axios_1.default.post(`${env_1.baseURL}/portal`, formData, config);
|
|
21
|
+
return data;
|
|
22
|
+
};
|
|
23
|
+
// Download Docs Portal
|
|
24
|
+
exports.downloadDocsPortal = async ({ zippedBuildFilePath, portalFolderPath, zippedPortalPath, overrideAuthKey, zip }, configDir) => {
|
|
25
|
+
cli_ux_1.default.action.start("Downloading portal");
|
|
26
|
+
// Check if the build file exists for the user or not
|
|
27
|
+
if (!(await fs.pathExists(zippedBuildFilePath))) {
|
|
28
|
+
throw new Error("Build file doesn't exist");
|
|
29
|
+
}
|
|
30
|
+
// TODO: ***CRITICAL*** Remove this call once the SDK is patched
|
|
31
|
+
const data = await downloadPortalAxios(zippedBuildFilePath, overrideAuthKey, configDir);
|
|
32
|
+
await utils_1.deleteFile(zippedBuildFilePath);
|
|
33
|
+
await fs.writeFile(zippedPortalPath, data);
|
|
34
|
+
// TODO: Uncomment this code block when the SDK is patched
|
|
35
|
+
// const file: FileWrapper = new FileWrapper(fs.createReadStream(zippedBuildFilePath));
|
|
36
|
+
// const { result }: ApiResponse<NodeJS.ReadableStream | Blob> =
|
|
37
|
+
// await docsPortalController.generateOnPremPortalViaBuildInput(file);
|
|
38
|
+
// if ((data as NodeJS.ReadableStream).readable) {
|
|
39
|
+
// await writeFileUsingReadableStream(data as NodeJS.ReadableStream, zippedPortalPath);
|
|
40
|
+
if (!zip) {
|
|
41
|
+
await utils_1.unzipFile(fs.createReadStream(zippedPortalPath), portalFolderPath);
|
|
42
|
+
await utils_1.deleteFile(zippedPortalPath);
|
|
43
|
+
}
|
|
44
|
+
cli_ux_1.default.action.stop();
|
|
45
|
+
return zip ? zippedPortalPath : portalFolderPath;
|
|
46
|
+
// } else {
|
|
47
|
+
// throw new Error("Couldn't download the portal");
|
|
48
|
+
// }
|
|
49
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CodeGenerationExternalApisController } from "@apimatic/sdk";
|
|
2
|
+
import { GenerationIdParams, DownloadSDKParams } from "../../types/sdk/generate";
|
|
3
|
+
export declare const getSDKGenerationId: ({ file, url, platform }: GenerationIdParams, sdkGenerationController: CodeGenerationExternalApisController) => Promise<string>;
|
|
4
|
+
export declare const downloadGeneratedSDK: ({ codeGenId, zippedSDKPath, sdkFolderPath, zip }: DownloadSDKParams, sdkGenerationController: CodeGenerationExternalApisController) => Promise<string>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downloadGeneratedSDK = exports.getSDKGenerationId = void 0;
|
|
4
|
+
const fs = require("fs-extra");
|
|
5
|
+
const cli_ux_1 = require("cli-ux");
|
|
6
|
+
const sdk_1 = require("@apimatic/sdk");
|
|
7
|
+
const core_1 = require("@apimatic/core");
|
|
8
|
+
const generate_1 = require("../../types/sdk/generate");
|
|
9
|
+
const utils_1 = require("../../utils/utils");
|
|
10
|
+
exports.getSDKGenerationId = async ({ file, url, platform }, sdkGenerationController) => {
|
|
11
|
+
cli_ux_1.default.action.start("Generating SDK");
|
|
12
|
+
let generation;
|
|
13
|
+
const sdkPlatform = getSDKPlatform(platform);
|
|
14
|
+
if (file) {
|
|
15
|
+
const fileDescriptor = new core_1.FileWrapper(fs.createReadStream(file));
|
|
16
|
+
generation = await sdkGenerationController.generateSDKViaFile(fileDescriptor, sdkPlatform);
|
|
17
|
+
}
|
|
18
|
+
else if (url) {
|
|
19
|
+
// If url to spec file is provided
|
|
20
|
+
const body = {
|
|
21
|
+
url: url,
|
|
22
|
+
template: sdkPlatform
|
|
23
|
+
};
|
|
24
|
+
generation = await sdkGenerationController.generateSDKViaURL(body);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error("Please provide a specification file");
|
|
28
|
+
}
|
|
29
|
+
cli_ux_1.default.action.stop();
|
|
30
|
+
return generation.result.id;
|
|
31
|
+
};
|
|
32
|
+
// Get valid platform from user's input, convert simple platform to valid Platforms enum value
|
|
33
|
+
const getSDKPlatform = (platform) => {
|
|
34
|
+
if (Object.keys(generate_1.SimplePlatforms).includes(platform)) {
|
|
35
|
+
return generate_1.SimplePlatforms[platform];
|
|
36
|
+
}
|
|
37
|
+
else if (Object.values(sdk_1.Platforms).includes(platform)) {
|
|
38
|
+
return platform;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const platforms = Object.keys(generate_1.SimplePlatforms).concat(Object.values(sdk_1.Platforms)).join("|");
|
|
42
|
+
throw new Error(`Please provide a valid platform i.e. ${platforms}`);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
// Download Platform
|
|
46
|
+
exports.downloadGeneratedSDK = async ({ codeGenId, zippedSDKPath, sdkFolderPath, zip }, sdkGenerationController) => {
|
|
47
|
+
cli_ux_1.default.action.start("Downloading SDK");
|
|
48
|
+
const { result } = await sdkGenerationController.downloadSDK(codeGenId);
|
|
49
|
+
if (result.readable) {
|
|
50
|
+
if (!zip) {
|
|
51
|
+
await utils_1.unzipFile(result, sdkFolderPath);
|
|
52
|
+
cli_ux_1.default.action.stop();
|
|
53
|
+
return sdkFolderPath;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
await utils_1.writeFileUsingReadableStream(result, zippedSDKPath);
|
|
57
|
+
cli_ux_1.default.action.stop();
|
|
58
|
+
return zippedSDKPath;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw new Error("Couldn't download the SDK");
|
|
63
|
+
}
|
|
64
|
+
};
|