@opra/cli 0.20.1 → 0.20.2
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/cjs/api-exporter/api-exporter.js +4 -4
- package/cjs/api-exporter/process-resources.js +18 -6
- package/cjs/api-exporter/process-types.js +1 -1
- package/esm/api-exporter/api-exporter.js +4 -4
- package/esm/api-exporter/process-resources.js +18 -6
- package/esm/api-exporter/process-types.js +1 -1
- package/package.json +2 -2
|
@@ -31,9 +31,9 @@ class ApiExporter {
|
|
|
31
31
|
// this.nsMap = nsMap || new ResponsiveMap(); // implement references later
|
|
32
32
|
}
|
|
33
33
|
async execute() {
|
|
34
|
-
this.logger.log(chalk_1.default.
|
|
34
|
+
this.logger.log(chalk_1.default.cyan('Fetching service metadata from'), chalk_1.default.whiteBright(this.client.serviceUrl));
|
|
35
35
|
this.document = await this.client.getMetadata();
|
|
36
|
-
this.logger.log(chalk_1.default.
|
|
36
|
+
this.logger.log(chalk_1.default.cyan('Retrieved service info:\n'), chalk_1.default.white('Title:'), chalk_1.default.whiteBright(this.document.info.title), '\n', chalk_1.default.white('Version:'), chalk_1.default.whiteBright(this.document.info.version), '\n', chalk_1.default.white('Resources:'), chalk_1.default.whiteBright(this.document.resources.size), 'resources found\n', chalk_1.default.white('Types:'), chalk_1.default.whiteBright(this.document.types.size), 'types found\n');
|
|
37
37
|
this.serviceClassName = (this.serviceClassName || this.document.info.title || 'Service1').replace(/[^\w_$]*/g, '');
|
|
38
38
|
this.serviceClassName = this.serviceClassName.charAt(0).toUpperCase() + this.serviceClassName.substring(1);
|
|
39
39
|
this.fileHeader += `/*
|
|
@@ -41,9 +41,9 @@ class ApiExporter {
|
|
|
41
41
|
* Version ${this.document.info.version}
|
|
42
42
|
* ${this.client.serviceUrl}
|
|
43
43
|
*/`;
|
|
44
|
-
this.logger.log(chalk_1.default.
|
|
44
|
+
this.logger.log(chalk_1.default.cyan('Removing old files..'));
|
|
45
45
|
this.cleanDirectory(this.outDir);
|
|
46
|
-
this.logger.log(chalk_1.default.
|
|
46
|
+
this.logger.log(chalk_1.default.cyan(`Generating service interface ( ${chalk_1.default.whiteBright(this.serviceClassName)} )`));
|
|
47
47
|
node_fs_1.default.mkdirSync(this.outDir, { recursive: true });
|
|
48
48
|
await this.processTypes();
|
|
49
49
|
await this.processResources();
|
|
@@ -11,7 +11,7 @@ const string_utils_js_1 = require("../utils/string-utils.js");
|
|
|
11
11
|
* @param targetDir
|
|
12
12
|
*/
|
|
13
13
|
async function processResources(targetDir = '') {
|
|
14
|
-
this.logger.log(chalk_1.default.
|
|
14
|
+
this.logger.log(chalk_1.default.cyan('Processing resources'));
|
|
15
15
|
const { document } = this;
|
|
16
16
|
const serviceTs = this.addFile(node_path_1.default.join(targetDir, this.serviceClassName + '.ts'));
|
|
17
17
|
serviceTs.addImportPackage('@opra/client', ['HttpServiceBase']);
|
|
@@ -19,16 +19,23 @@ async function processResources(targetDir = '') {
|
|
|
19
19
|
indexTs.addExportFile(serviceTs.filename);
|
|
20
20
|
serviceTs.content = `\nexport class ${this.serviceClassName} extends HttpServiceBase {\n`;
|
|
21
21
|
for (const resource of document.resources.values()) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const jsDoc = `
|
|
23
|
+
/**
|
|
24
|
+
* ${(0, string_utils_js_1.wrapJSDocString)(resource.description || resource.name)}
|
|
25
|
+
* @url ${(0, common_1.joinPath)(this.client.serviceUrl, '$metadata#resources/' + resource.name)}
|
|
26
|
+
*/`;
|
|
25
27
|
if (resource instanceof common_1.Collection) {
|
|
26
28
|
const typeName = resource.type.name || '';
|
|
27
29
|
serviceTs.addImportPackage('@opra/client', ['HttpCollectionNode']);
|
|
28
30
|
serviceTs.addImportFile(`types/${typeName}-type`, [typeName]);
|
|
29
31
|
const operations = Object.keys(resource.operations)
|
|
30
32
|
.map(x => `'${x}'`).join(' | ');
|
|
31
|
-
|
|
33
|
+
if (!operations.length) {
|
|
34
|
+
this.logger.warn(chalk_1.default.yellow('WARN: ') +
|
|
35
|
+
`Ignoring "${chalk_1.default.whiteBright(resource.name)}" resource. No operations available.`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
serviceTs.content += jsDoc + `
|
|
32
39
|
get ${resource.name}(): Pick<HttpCollectionNode<${typeName}>, ${operations}> {
|
|
33
40
|
return this.$client.collection('${resource.name}');
|
|
34
41
|
}\n`;
|
|
@@ -39,7 +46,12 @@ async function processResources(targetDir = '') {
|
|
|
39
46
|
serviceTs.addImportFile(`types/${typeName}-type`, [typeName]);
|
|
40
47
|
const operations = Object.keys(resource.operations)
|
|
41
48
|
.map(x => `'${x}'`).join(' | ');
|
|
42
|
-
|
|
49
|
+
if (!operations.length) {
|
|
50
|
+
this.logger.warn(chalk_1.default.yellow('WARN: ') +
|
|
51
|
+
`Ignoring "${chalk_1.default.whiteBright(resource.name)}" resource. No operations available.`);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
serviceTs.content += jsDoc + `
|
|
43
55
|
get ${resource.name}(): Pick<HttpSingletonNode<${typeName}>, ${operations}> {
|
|
44
56
|
return this.$client.singleton('${resource.name}');
|
|
45
57
|
}\n`;
|
|
@@ -12,7 +12,7 @@ const internalTypeNames = ['any', 'boolean', 'bigint', 'number', 'null', 'string
|
|
|
12
12
|
* @param targetDir
|
|
13
13
|
*/
|
|
14
14
|
async function processTypes(targetDir = '') {
|
|
15
|
-
this.logger.log(chalk_1.default.
|
|
15
|
+
this.logger.log(chalk_1.default.cyan('Processing types'));
|
|
16
16
|
const { document } = this;
|
|
17
17
|
const typesTs = this.addFile(node_path_1.default.join(targetDir, 'types.ts'));
|
|
18
18
|
for (const dataType of document.types.values()) {
|
|
@@ -27,9 +27,9 @@ export class ApiExporter {
|
|
|
27
27
|
// this.nsMap = nsMap || new ResponsiveMap(); // implement references later
|
|
28
28
|
}
|
|
29
29
|
async execute() {
|
|
30
|
-
this.logger.log(chalk.
|
|
30
|
+
this.logger.log(chalk.cyan('Fetching service metadata from'), chalk.whiteBright(this.client.serviceUrl));
|
|
31
31
|
this.document = await this.client.getMetadata();
|
|
32
|
-
this.logger.log(chalk.
|
|
32
|
+
this.logger.log(chalk.cyan('Retrieved service info:\n'), chalk.white('Title:'), chalk.whiteBright(this.document.info.title), '\n', chalk.white('Version:'), chalk.whiteBright(this.document.info.version), '\n', chalk.white('Resources:'), chalk.whiteBright(this.document.resources.size), 'resources found\n', chalk.white('Types:'), chalk.whiteBright(this.document.types.size), 'types found\n');
|
|
33
33
|
this.serviceClassName = (this.serviceClassName || this.document.info.title || 'Service1').replace(/[^\w_$]*/g, '');
|
|
34
34
|
this.serviceClassName = this.serviceClassName.charAt(0).toUpperCase() + this.serviceClassName.substring(1);
|
|
35
35
|
this.fileHeader += `/*
|
|
@@ -37,9 +37,9 @@ export class ApiExporter {
|
|
|
37
37
|
* Version ${this.document.info.version}
|
|
38
38
|
* ${this.client.serviceUrl}
|
|
39
39
|
*/`;
|
|
40
|
-
this.logger.log(chalk.
|
|
40
|
+
this.logger.log(chalk.cyan('Removing old files..'));
|
|
41
41
|
this.cleanDirectory(this.outDir);
|
|
42
|
-
this.logger.log(chalk.
|
|
42
|
+
this.logger.log(chalk.cyan(`Generating service interface ( ${chalk.whiteBright(this.serviceClassName)} )`));
|
|
43
43
|
fs.mkdirSync(this.outDir, { recursive: true });
|
|
44
44
|
await this.processTypes();
|
|
45
45
|
await this.processResources();
|
|
@@ -7,7 +7,7 @@ import { wrapJSDocString } from '../utils/string-utils.js';
|
|
|
7
7
|
* @param targetDir
|
|
8
8
|
*/
|
|
9
9
|
export async function processResources(targetDir = '') {
|
|
10
|
-
this.logger.log(chalk.
|
|
10
|
+
this.logger.log(chalk.cyan('Processing resources'));
|
|
11
11
|
const { document } = this;
|
|
12
12
|
const serviceTs = this.addFile(path.join(targetDir, this.serviceClassName + '.ts'));
|
|
13
13
|
serviceTs.addImportPackage('@opra/client', ['HttpServiceBase']);
|
|
@@ -15,16 +15,23 @@ export async function processResources(targetDir = '') {
|
|
|
15
15
|
indexTs.addExportFile(serviceTs.filename);
|
|
16
16
|
serviceTs.content = `\nexport class ${this.serviceClassName} extends HttpServiceBase {\n`;
|
|
17
17
|
for (const resource of document.resources.values()) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const jsDoc = `
|
|
19
|
+
/**
|
|
20
|
+
* ${wrapJSDocString(resource.description || resource.name)}
|
|
21
|
+
* @url ${joinPath(this.client.serviceUrl, '$metadata#resources/' + resource.name)}
|
|
22
|
+
*/`;
|
|
21
23
|
if (resource instanceof Collection) {
|
|
22
24
|
const typeName = resource.type.name || '';
|
|
23
25
|
serviceTs.addImportPackage('@opra/client', ['HttpCollectionNode']);
|
|
24
26
|
serviceTs.addImportFile(`types/${typeName}-type`, [typeName]);
|
|
25
27
|
const operations = Object.keys(resource.operations)
|
|
26
28
|
.map(x => `'${x}'`).join(' | ');
|
|
27
|
-
|
|
29
|
+
if (!operations.length) {
|
|
30
|
+
this.logger.warn(chalk.yellow('WARN: ') +
|
|
31
|
+
`Ignoring "${chalk.whiteBright(resource.name)}" resource. No operations available.`);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
serviceTs.content += jsDoc + `
|
|
28
35
|
get ${resource.name}(): Pick<HttpCollectionNode<${typeName}>, ${operations}> {
|
|
29
36
|
return this.$client.collection('${resource.name}');
|
|
30
37
|
}\n`;
|
|
@@ -35,7 +42,12 @@ export async function processResources(targetDir = '') {
|
|
|
35
42
|
serviceTs.addImportFile(`types/${typeName}-type`, [typeName]);
|
|
36
43
|
const operations = Object.keys(resource.operations)
|
|
37
44
|
.map(x => `'${x}'`).join(' | ');
|
|
38
|
-
|
|
45
|
+
if (!operations.length) {
|
|
46
|
+
this.logger.warn(chalk.yellow('WARN: ') +
|
|
47
|
+
`Ignoring "${chalk.whiteBright(resource.name)}" resource. No operations available.`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
serviceTs.content += jsDoc + `
|
|
39
51
|
get ${resource.name}(): Pick<HttpSingletonNode<${typeName}>, ${operations}> {
|
|
40
52
|
return this.$client.singleton('${resource.name}');
|
|
41
53
|
}\n`;
|
|
@@ -8,7 +8,7 @@ const internalTypeNames = ['any', 'boolean', 'bigint', 'number', 'null', 'string
|
|
|
8
8
|
* @param targetDir
|
|
9
9
|
*/
|
|
10
10
|
export async function processTypes(targetDir = '') {
|
|
11
|
-
this.logger.log(chalk.
|
|
11
|
+
this.logger.log(chalk.cyan('Processing types'));
|
|
12
12
|
const { document } = this;
|
|
13
13
|
const typesTs = this.addFile(path.join(targetDir, 'types.ts'));
|
|
14
14
|
for (const dataType of document.types.values()) {
|