@apimatic/cli 1.0.1-alpha.1
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/CHANGELOG.md +6 -0
- package/README.md +292 -0
- package/bin/run +5 -0
- package/bin/run.cmd +3 -0
- package/lib/client-utils/auth-manager.d.ts +17 -0
- package/lib/client-utils/auth-manager.js +33 -0
- package/lib/client-utils/sdk-client.d.ts +23 -0
- package/lib/client-utils/sdk-client.js +119 -0
- package/lib/commands/api/index.d.ts +6 -0
- package/lib/commands/api/index.js +22 -0
- package/lib/commands/api/transform.d.ts +15 -0
- package/lib/commands/api/transform.js +157 -0
- package/lib/commands/api/validate.d.ts +13 -0
- package/lib/commands/api/validate.js +85 -0
- package/lib/commands/auth/index.d.ts +6 -0
- package/lib/commands/auth/index.js +25 -0
- package/lib/commands/auth/login.d.ts +9 -0
- package/lib/commands/auth/login.js +58 -0
- package/lib/commands/auth/logout.d.ts +6 -0
- package/lib/commands/auth/logout.js +23 -0
- package/lib/commands/auth/status.d.ts +6 -0
- package/lib/commands/auth/status.js +23 -0
- package/lib/commands/portal/generate.d.ts +23 -0
- package/lib/commands/portal/generate.js +141 -0
- package/lib/commands/portal/index.d.ts +6 -0
- package/lib/commands/portal/index.js +20 -0
- package/lib/commands/sdk/generate.d.ts +14 -0
- package/lib/commands/sdk/generate.js +178 -0
- package/lib/commands/sdk/index.d.ts +6 -0
- package/lib/commands/sdk/index.js +21 -0
- package/lib/config/env.d.ts +1 -0
- package/lib/config/env.js +4 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/utils/utils.d.ts +18 -0
- package/lib/utils/utils.js +107 -0
- package/oclif.manifest.json +1 -0
- package/package.json +107 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fs = require("fs-extra");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const cli_ux_1 = require("cli-ux");
|
|
6
|
+
const apimatic_sdk_for_js_1 = require("@apimatic/apimatic-sdk-for-js");
|
|
7
|
+
const command_1 = require("@oclif/command");
|
|
8
|
+
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
9
|
+
const utils_1 = require("../../utils/utils");
|
|
10
|
+
const DestinationFormats = {
|
|
11
|
+
OpenApi3Json: "json",
|
|
12
|
+
OpenApi3Yaml: "yaml",
|
|
13
|
+
APIMATIC: "json",
|
|
14
|
+
WADL2009: "xml",
|
|
15
|
+
WSDL: "xml",
|
|
16
|
+
Swagger10: "json",
|
|
17
|
+
Swagger20: "json",
|
|
18
|
+
SwaggerYaml: "yaml",
|
|
19
|
+
RAML: "yaml",
|
|
20
|
+
RAML10: "yaml",
|
|
21
|
+
Postman10: "json",
|
|
22
|
+
Postman20: "json"
|
|
23
|
+
};
|
|
24
|
+
async function getTransformationId({ file, url, format }, transformationController) {
|
|
25
|
+
cli_ux_1.default.action.start("Transforming API specification");
|
|
26
|
+
let generation;
|
|
27
|
+
if (file) {
|
|
28
|
+
const fileDescriptor = new apimatic_sdk_for_js_1.FileWrapper(fs.createReadStream(file));
|
|
29
|
+
generation = await transformationController.transformViaFile(fileDescriptor, format);
|
|
30
|
+
}
|
|
31
|
+
else if (url) {
|
|
32
|
+
const body = {
|
|
33
|
+
url: url,
|
|
34
|
+
exportFormat: format
|
|
35
|
+
};
|
|
36
|
+
generation = await transformationController.transformViaURL(body);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw new Error("Please provide a specification file");
|
|
40
|
+
}
|
|
41
|
+
cli_ux_1.default.action.stop();
|
|
42
|
+
return generation.result;
|
|
43
|
+
}
|
|
44
|
+
async function downloadTransformationFile({ id, destinationFilePath, transformationController }) {
|
|
45
|
+
cli_ux_1.default.action.start("Downloading Transformed file");
|
|
46
|
+
const { result } = await transformationController.downloadTransformedFile(id);
|
|
47
|
+
if (result.readable) {
|
|
48
|
+
await utils_1.writeFileUsingReadableStream(result, destinationFilePath);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
throw new Error("Couldn't save transformation file");
|
|
52
|
+
}
|
|
53
|
+
cli_ux_1.default.action.stop();
|
|
54
|
+
return destinationFilePath;
|
|
55
|
+
}
|
|
56
|
+
// Get valid platform from user's input, convert simple platform to valid Platforms enum value
|
|
57
|
+
function getValidFormat(format) {
|
|
58
|
+
if (Object.keys(apimatic_sdk_for_js_1.ExportFormats).find((exportFormat) => exportFormat === format)) {
|
|
59
|
+
return apimatic_sdk_for_js_1.ExportFormats[format];
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const formats = Object.keys(apimatic_sdk_for_js_1.ExportFormats).join("|");
|
|
63
|
+
throw new Error(`Please provide a valid platform i.e. ${formats}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
class Transform extends command_1.Command {
|
|
67
|
+
constructor() {
|
|
68
|
+
super(...arguments);
|
|
69
|
+
this.printValidationMessages = (apiValidationSummary) => {
|
|
70
|
+
const warnings = (apiValidationSummary === null || apiValidationSummary === void 0 ? void 0 : apiValidationSummary.warnings) || [];
|
|
71
|
+
const errors = (apiValidationSummary === null || apiValidationSummary === void 0 ? void 0 : apiValidationSummary.errors.join("\n")) || "";
|
|
72
|
+
warnings.forEach((warning) => {
|
|
73
|
+
this.warn(utils_1.replaceHTML(warning));
|
|
74
|
+
});
|
|
75
|
+
if (apiValidationSummary && apiValidationSummary.errors.length > 0) {
|
|
76
|
+
this.error(utils_1.replaceHTML(errors));
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async run() {
|
|
81
|
+
const { flags } = this.parse(Transform);
|
|
82
|
+
const fileName = flags.file ? utils_1.getFileNameFromPath(flags.file) : utils_1.getFileNameFromPath(flags.url);
|
|
83
|
+
const destinationFormat = DestinationFormats[flags.format];
|
|
84
|
+
const destinationFilePath = path.join(flags.destination, `${fileName}_${flags.format}.${destinationFormat}`.toLowerCase());
|
|
85
|
+
if (fs.existsSync(destinationFilePath)) {
|
|
86
|
+
throw new Error(`Can't download transformed file to path ${destinationFilePath}, because it already exists`);
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
if (flags.file && !(await fs.pathExists(flags.file))) {
|
|
90
|
+
throw new Error(`Transformation file: ${flags.file} does not exist`);
|
|
91
|
+
}
|
|
92
|
+
else if (!(await fs.pathExists(flags.destination))) {
|
|
93
|
+
throw new Error(`Destination path: ${flags.destination} does not exist`);
|
|
94
|
+
}
|
|
95
|
+
const overrideAuthKey = flags["auth-key"] ? flags["auth-key"] : null;
|
|
96
|
+
const client = await sdk_client_1.SDKClient.getInstance().getClient(overrideAuthKey, this.config.configDir);
|
|
97
|
+
const transformationController = new apimatic_sdk_for_js_1.TransformationController(client);
|
|
98
|
+
const { id, apiValidationSummary } = await getTransformationId(flags, transformationController);
|
|
99
|
+
this.printValidationMessages(apiValidationSummary);
|
|
100
|
+
const savedTransformationFile = await downloadTransformationFile({
|
|
101
|
+
id,
|
|
102
|
+
destinationFilePath,
|
|
103
|
+
transformationController
|
|
104
|
+
});
|
|
105
|
+
this.log(`Success! Your transformed file is located at ${savedTransformationFile}`);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
cli_ux_1.default.action.stop();
|
|
109
|
+
if (error.result) {
|
|
110
|
+
const apiError = error;
|
|
111
|
+
// TODO: Hopefully, this type-cast won't be necessary when the SDK is
|
|
112
|
+
// updated to throw the right exception type for this status code.
|
|
113
|
+
const result = apiError.result;
|
|
114
|
+
if (apiError.statusCode === 422 && result && "errors" in result && Array.isArray(result.errors)) {
|
|
115
|
+
this.error(utils_1.replaceHTML(`${result.errors}`));
|
|
116
|
+
}
|
|
117
|
+
else if (apiError.statusCode === 422 && apiError.body && typeof apiError.body === "string") {
|
|
118
|
+
this.error(JSON.parse(apiError.body)["dto.FileUrl"][0]);
|
|
119
|
+
}
|
|
120
|
+
else if (apiError.statusCode === 401 && apiError.body && typeof apiError.body === "string") {
|
|
121
|
+
this.error(apiError.body);
|
|
122
|
+
}
|
|
123
|
+
else if (apiError.statusCode === 500) {
|
|
124
|
+
this.error(apiError.message);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (error.statusCode === 401 &&
|
|
128
|
+
error.body &&
|
|
129
|
+
typeof error.body === "string") {
|
|
130
|
+
this.error(error.body);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
this.error(`${error.message}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.default = Transform;
|
|
139
|
+
Transform.description = `Transforms your API specification to any supported format of your choice from amongst[10+ different formats](https://www.apimatic.io/transformer/#supported-formats).`;
|
|
140
|
+
Transform.examples = [
|
|
141
|
+
`$ apimatic api:transform --format="OpenApi3Json" --file="./specs/sample.json" --destination="D:/"
|
|
142
|
+
Success! Your transformed file is located at D:/Transformed_OpenApi3Json.json
|
|
143
|
+
`
|
|
144
|
+
];
|
|
145
|
+
Transform.flags = {
|
|
146
|
+
format: command_1.flags.string({
|
|
147
|
+
parse: (format) => getValidFormat(format.toUpperCase()),
|
|
148
|
+
required: true,
|
|
149
|
+
description: `specification format to transform API specification into
|
|
150
|
+
(OpenApi3Json|OpenApi3Yaml|APIMATIC|WADL2009|WADL2006|WSDL|
|
|
151
|
+
Swagger10|Swagger20|SwaggerYaml|RAML|RAML10|Postman10|Postman20)`
|
|
152
|
+
}),
|
|
153
|
+
file: command_1.flags.string({ default: "", description: "path to the API specification file to transform" }),
|
|
154
|
+
url: command_1.flags.string({ default: "", description: "URL to the API specification file to transform" }),
|
|
155
|
+
destination: command_1.flags.string({ default: __dirname, description: "path to transformed file" }),
|
|
156
|
+
"auth-key": command_1.flags.string({ description: "override current auth-key" })
|
|
157
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ApiValidationSummary } from "@apimatic/apimatic-sdk-for-js";
|
|
2
|
+
import { flags, Command } from "@oclif/command";
|
|
3
|
+
export default class Validate extends Command {
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
file: flags.IOptionFlag<string>;
|
|
8
|
+
url: flags.IOptionFlag<string>;
|
|
9
|
+
"auth-key": flags.IOptionFlag<string | undefined>;
|
|
10
|
+
};
|
|
11
|
+
printValidationMessages: ({ warnings, errors }: ApiValidationSummary) => void;
|
|
12
|
+
run(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fs = require("fs-extra");
|
|
4
|
+
const cli_ux_1 = require("cli-ux");
|
|
5
|
+
const apimatic_sdk_for_js_1 = require("@apimatic/apimatic-sdk-for-js");
|
|
6
|
+
const command_1 = require("@oclif/command");
|
|
7
|
+
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
8
|
+
const utils_1 = require("../../utils/utils");
|
|
9
|
+
async function getValidation({ file, url }, apiValidationController) {
|
|
10
|
+
let validation;
|
|
11
|
+
cli_ux_1.default.action.start("Validating specification file");
|
|
12
|
+
if (file) {
|
|
13
|
+
const fileDescriptor = new apimatic_sdk_for_js_1.FileWrapper(fs.createReadStream(file));
|
|
14
|
+
validation = await apiValidationController.validateAPIViaFile(fileDescriptor);
|
|
15
|
+
}
|
|
16
|
+
else if (url) {
|
|
17
|
+
validation = await apiValidationController.validateAPIViaURL(url);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
throw new Error("Please provide a specification file");
|
|
21
|
+
}
|
|
22
|
+
cli_ux_1.default.action.stop();
|
|
23
|
+
return validation.result;
|
|
24
|
+
}
|
|
25
|
+
class Validate extends command_1.Command {
|
|
26
|
+
constructor() {
|
|
27
|
+
super(...arguments);
|
|
28
|
+
this.printValidationMessages = ({ warnings, errors }) => {
|
|
29
|
+
warnings.forEach((warning) => {
|
|
30
|
+
this.warn(`${utils_1.replaceHTML(warning)}`);
|
|
31
|
+
});
|
|
32
|
+
if (errors.length > 0) {
|
|
33
|
+
const singleLineError = errors.join("\n");
|
|
34
|
+
this.error(utils_1.replaceHTML(singleLineError));
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async run() {
|
|
39
|
+
const { flags } = this.parse(Validate);
|
|
40
|
+
try {
|
|
41
|
+
if (flags.file && !(await fs.pathExists(flags.file))) {
|
|
42
|
+
throw new Error(`Validation file: ${flags.file} does not exist`);
|
|
43
|
+
}
|
|
44
|
+
const overrideAuthKey = flags["auth-key"] ? flags["auth-key"] : null;
|
|
45
|
+
const client = await sdk_client_1.SDKClient.getInstance().getClient(overrideAuthKey, this.config.configDir);
|
|
46
|
+
const apiValidationController = new apimatic_sdk_for_js_1.APIValidationExternalApisController(client);
|
|
47
|
+
const validationSummary = await getValidation(flags, apiValidationController);
|
|
48
|
+
this.printValidationMessages(validationSummary);
|
|
49
|
+
validationSummary.success
|
|
50
|
+
? this.log("Specification file provided is valid")
|
|
51
|
+
: this.error("Specification file provided is invalid");
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (error.result) {
|
|
55
|
+
const apiError = error;
|
|
56
|
+
const result = apiError.result;
|
|
57
|
+
if (result.modelState["exception Error"] && apiError.statusCode === 400) {
|
|
58
|
+
this.error(utils_1.replaceHTML(result.modelState["exception Error"][0]));
|
|
59
|
+
}
|
|
60
|
+
else if (error.body && apiError.statusCode === 401) {
|
|
61
|
+
this.error(error.body);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.error(error.message);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.error(`${error.message}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.default = Validate;
|
|
74
|
+
Validate.description = "Validates the provided API specification file for any syntactical and semantic errors";
|
|
75
|
+
Validate.examples = [
|
|
76
|
+
`$ apimatic api:validate --file="./specs/sample.json"
|
|
77
|
+
Specification file provided is valid
|
|
78
|
+
`
|
|
79
|
+
];
|
|
80
|
+
Validate.flags = {
|
|
81
|
+
file: command_1.flags.string({ default: "", description: "path to the API specification file to validate" }),
|
|
82
|
+
url: command_1.flags.string({ default: "", description: "URL to the specification file to validate" }),
|
|
83
|
+
// docs: flags.boolean({ default: false, description: "Validate specification for docs generation" }), // Next tier, not included in API spec
|
|
84
|
+
"auth-key": command_1.flags.string({ description: "override current auth-key" })
|
|
85
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const command_1 = require("@oclif/command");
|
|
4
|
+
class Auth extends command_1.Command {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.run = async () => {
|
|
8
|
+
this.log(`invokes subcommands related to authentication.
|
|
9
|
+
|
|
10
|
+
USAGE
|
|
11
|
+
$ apimatic auth
|
|
12
|
+
|
|
13
|
+
EXAMPLE
|
|
14
|
+
$ apimatic auth --help
|
|
15
|
+
|
|
16
|
+
COMMANDS
|
|
17
|
+
auth:login login to your APIMatic account
|
|
18
|
+
auth:logout logout of APIMatic
|
|
19
|
+
auth:status checks current logged-in account`);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.default = Auth;
|
|
24
|
+
Auth.description = "invokes subcommands related to authentication.";
|
|
25
|
+
Auth.examples = ["$ apimatic auth --help"];
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const command_1 = require("@oclif/command");
|
|
4
|
+
const cli_ux_1 = require("cli-ux");
|
|
5
|
+
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
6
|
+
const utils_1 = require("../../utils/utils");
|
|
7
|
+
class Login extends command_1.Command {
|
|
8
|
+
async run() {
|
|
9
|
+
const { flags } = this.parse(Login);
|
|
10
|
+
const configDir = this.config.configDir;
|
|
11
|
+
try {
|
|
12
|
+
const client = sdk_client_1.SDKClient.getInstance();
|
|
13
|
+
// If user is setting auth key
|
|
14
|
+
if (flags["auth-key"]) {
|
|
15
|
+
const response = client.setAuthKey(flags["auth-key"], configDir);
|
|
16
|
+
return this.log(response);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
// If user logs in with email and password
|
|
20
|
+
const email = await cli_ux_1.cli.prompt("Please enter your registered email");
|
|
21
|
+
const password = await cli_ux_1.cli.prompt("Please enter your password", {
|
|
22
|
+
type: "hide"
|
|
23
|
+
});
|
|
24
|
+
const response = await client.login(email, password, configDir);
|
|
25
|
+
this.log(response);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error && error.response) {
|
|
30
|
+
const apiError = error;
|
|
31
|
+
const apiResponse = apiError.response;
|
|
32
|
+
if (apiResponse) {
|
|
33
|
+
const responseData = apiResponse.data;
|
|
34
|
+
if (apiResponse.status === 403 && responseData) {
|
|
35
|
+
return this.error(utils_1.replaceHTML(responseData));
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return this.error(apiError.message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.error(error.message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.default = Login;
|
|
47
|
+
Login.description = "login to your APIMatic account";
|
|
48
|
+
Login.examples = [
|
|
49
|
+
`$ apimatic auth:login
|
|
50
|
+
Please enter your registered email: apimatic-user@gmail.com
|
|
51
|
+
Please enter your password: *********
|
|
52
|
+
|
|
53
|
+
You have successfully logged into APIMatic
|
|
54
|
+
`
|
|
55
|
+
];
|
|
56
|
+
Login.flags = {
|
|
57
|
+
"auth-key": command_1.flags.string({ default: "", description: "Set authentication key for all commands" })
|
|
58
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const command_1 = require("@oclif/command");
|
|
4
|
+
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
5
|
+
class Login extends command_1.Command {
|
|
6
|
+
async run() {
|
|
7
|
+
try {
|
|
8
|
+
const client = sdk_client_1.SDKClient.getInstance();
|
|
9
|
+
const response = await client.logout(this.config.configDir);
|
|
10
|
+
this.log(response);
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
this.error(error);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = Login;
|
|
18
|
+
Login.description = "logout of APIMatic";
|
|
19
|
+
Login.examples = [
|
|
20
|
+
`$ apimatic auth:logout
|
|
21
|
+
Logged out
|
|
22
|
+
`
|
|
23
|
+
];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const command_1 = require("@oclif/command");
|
|
4
|
+
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
5
|
+
class Status extends command_1.Command {
|
|
6
|
+
async run() {
|
|
7
|
+
try {
|
|
8
|
+
const client = sdk_client_1.SDKClient.getInstance();
|
|
9
|
+
const response = await client.status(this.config.configDir);
|
|
10
|
+
this.log(response);
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
this.error(error);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = Status;
|
|
18
|
+
Status.description = "checks current logged-in account";
|
|
19
|
+
Status.examples = [
|
|
20
|
+
`$ apimatic auth:status
|
|
21
|
+
Currently logged in as apimatic-client@gmail.com
|
|
22
|
+
`
|
|
23
|
+
];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DocsPortalManagementController } from "@apimatic/apimatic-sdk-for-js";
|
|
2
|
+
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
|
+
export default class PortalGenerate extends Command {
|
|
11
|
+
static description: string;
|
|
12
|
+
static flags: {
|
|
13
|
+
folder: flags.IOptionFlag<string>;
|
|
14
|
+
destination: flags.IOptionFlag<string>;
|
|
15
|
+
zip: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
|
|
16
|
+
"auth-key": flags.IOptionFlag<string>;
|
|
17
|
+
};
|
|
18
|
+
static examples: string[];
|
|
19
|
+
downloadPortalAxios: (zippedBuildFilePath: string, overrideAuthKey: string | null) => Promise<any>;
|
|
20
|
+
downloadDocsPortal: ({ zippedBuildFilePath, generatedPortalFolderPath, overrideAuthKey, zip }: GeneratePortalParams) => Promise<string>;
|
|
21
|
+
run(): Promise<undefined>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fs = require("fs-extra");
|
|
4
|
+
const FormData = require("form-data");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const cli_ux_1 = require("cli-ux");
|
|
7
|
+
const apimatic_sdk_for_js_1 = require("@apimatic/apimatic-sdk-for-js");
|
|
8
|
+
const command_1 = require("@oclif/command");
|
|
9
|
+
const sdk_client_1 = require("../../client-utils/sdk-client");
|
|
10
|
+
const env_1 = require("../../config/env");
|
|
11
|
+
const utils_1 = require("../../utils/utils");
|
|
12
|
+
const auth_manager_1 = require("../../client-utils/auth-manager");
|
|
13
|
+
const axios_1 = require("axios");
|
|
14
|
+
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
|
+
async run() {
|
|
60
|
+
const { flags } = this.parse(PortalGenerate);
|
|
61
|
+
const zip = flags.zip;
|
|
62
|
+
const portalFolderPath = flags.folder;
|
|
63
|
+
const generatedPortalFolderPath = flags.destination;
|
|
64
|
+
const overrideAuthKey = flags["auth-key"] ? flags["auth-key"] : null;
|
|
65
|
+
try {
|
|
66
|
+
if (!(await fs.pathExists(flags.destination))) {
|
|
67
|
+
throw new Error(`Destination path ${flags.destination} does not exist`);
|
|
68
|
+
}
|
|
69
|
+
else if (!(await fs.pathExists(flags.folder))) {
|
|
70
|
+
throw new Error(`Portal build folder ${flags.folder} does not exist`);
|
|
71
|
+
}
|
|
72
|
+
const client = await sdk_client_1.SDKClient.getInstance().getClient(overrideAuthKey, this.config.configDir);
|
|
73
|
+
const docsPortalController = new apimatic_sdk_for_js_1.DocsPortalManagementController(client);
|
|
74
|
+
const zippedBuildFilePath = await utils_1.zipDirectory(portalFolderPath, generatedPortalFolderPath);
|
|
75
|
+
const generatePortalParams = {
|
|
76
|
+
zippedBuildFilePath,
|
|
77
|
+
generatedPortalFolderPath,
|
|
78
|
+
docsPortalController,
|
|
79
|
+
overrideAuthKey,
|
|
80
|
+
zip
|
|
81
|
+
};
|
|
82
|
+
const generatedPortalPath = await this.downloadDocsPortal(generatePortalParams);
|
|
83
|
+
this.log(`Your portal has been generated at ${generatedPortalPath}${flags.zip ? ".zip" : ""}`);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
cli_ux_1.default.action.stop();
|
|
87
|
+
if (error && error.response) {
|
|
88
|
+
const apiError = error;
|
|
89
|
+
const apiResponse = apiError.response;
|
|
90
|
+
if (apiResponse) {
|
|
91
|
+
const responseData = apiResponse.data.toString();
|
|
92
|
+
if (apiResponse.status === 422 && responseData.length > 0 && utils_1.isJSONParsable(responseData)) {
|
|
93
|
+
const nestedErrors = JSON.parse(responseData);
|
|
94
|
+
if (nestedErrors.error) {
|
|
95
|
+
return this.error(utils_1.replaceHTML(nestedErrors.error));
|
|
96
|
+
}
|
|
97
|
+
else if (nestedErrors.message) {
|
|
98
|
+
return this.error(utils_1.replaceHTML(nestedErrors.message));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (apiResponse.status === 401 && responseData.length > 0 && utils_1.isJSONParsable(responseData)) {
|
|
102
|
+
this.error(utils_1.replaceHTML(responseData));
|
|
103
|
+
}
|
|
104
|
+
else if (apiResponse.status === 403 && apiResponse.statusText) {
|
|
105
|
+
return this.error(utils_1.replaceHTML(apiResponse.statusText));
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
return this.error(apiError.message);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
this.error(`${error.message}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.default = PortalGenerate;
|
|
119
|
+
PortalGenerate.description = "Generate static docs portal on premise";
|
|
120
|
+
PortalGenerate.flags = {
|
|
121
|
+
folder: command_1.flags.string({
|
|
122
|
+
parse: (input) => path.resolve(input),
|
|
123
|
+
default: "",
|
|
124
|
+
description: "folder to generate the portal with"
|
|
125
|
+
}),
|
|
126
|
+
destination: command_1.flags.string({
|
|
127
|
+
parse: (input) => path.resolve(input),
|
|
128
|
+
default: "./",
|
|
129
|
+
description: "path to the downloaded portal"
|
|
130
|
+
}),
|
|
131
|
+
zip: command_1.flags.boolean({ default: false, description: "zip the portal" }),
|
|
132
|
+
"auth-key": command_1.flags.string({
|
|
133
|
+
default: "",
|
|
134
|
+
description: "override current auth-key"
|
|
135
|
+
})
|
|
136
|
+
};
|
|
137
|
+
PortalGenerate.examples = [
|
|
138
|
+
`$ apimatic portal:generate --folder="./portal/" --destination="D:/"
|
|
139
|
+
Your portal has been generated at D:/
|
|
140
|
+
`
|
|
141
|
+
];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const command_1 = require("@oclif/command");
|
|
4
|
+
class SDK extends command_1.Command {
|
|
5
|
+
async run() {
|
|
6
|
+
this.log(`invokes subcommands related to the API Portal.
|
|
7
|
+
|
|
8
|
+
USAGE
|
|
9
|
+
$ apimatic portal
|
|
10
|
+
|
|
11
|
+
EXAMPLE
|
|
12
|
+
$apimatic portal --help
|
|
13
|
+
|
|
14
|
+
COMMANDS
|
|
15
|
+
portal:generate Generate static docs portal on premise`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.default = SDK;
|
|
19
|
+
SDK.description = "invokes subcommands related to the API Portal.";
|
|
20
|
+
SDK.examples = ["$apimatic portal --help"];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Command, flags } from "@oclif/command";
|
|
2
|
+
export default class SdkGenerate extends Command {
|
|
3
|
+
static description: string;
|
|
4
|
+
static flags: {
|
|
5
|
+
platform: flags.IOptionFlag<string>;
|
|
6
|
+
file: flags.IOptionFlag<string>;
|
|
7
|
+
url: flags.IOptionFlag<string>;
|
|
8
|
+
destination: flags.IOptionFlag<string>;
|
|
9
|
+
zip: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>;
|
|
10
|
+
"auth-key": flags.IOptionFlag<string>;
|
|
11
|
+
};
|
|
12
|
+
static examples: string[];
|
|
13
|
+
run(): Promise<void>;
|
|
14
|
+
}
|