Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
}
}
54 changes: 54 additions & 0 deletions app/controller/material-center/ComponentLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { Controller } from 'egg';
import { I_CreateComponentLibrary } from '../../lib/interface';

export default class ComponentController extends Controller {

/**
* @summary 注册组件库
* @router POST /api/componentLibrary/create
*/
async create(){
const data: I_CreateComponentLibrary = this.ctx.request.body;
this.ctx.body = await this.service.materialCenter.componentLibrary.create(data);
}

/**
* @summary 查询组件库
* @router GET
*/
async find(){
this.ctx.body = await this.service.materialCenter.componentLibrary.find(this.ctx.query);
}

/**
* @summary 更新组件库
* @router PUT
*/
async update(){
const { id } = this.ctx.params;
const params = this.ctx.request.body;
this.ctx.body = await this.service.materialCenter.componentLibrary.update({id, ...params });
}


/**
* @summary 删除组件库
* @router DELETE
*/
async delete(){
const { id } = this.ctx.params;
this.ctx.body = await this.service.materialCenter.componentLibrary.delete({ id });
}

}
26 changes: 26 additions & 0 deletions app/controller/material-center/UserComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { Controller } from 'egg';

export default class userComponentController extends Controller {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Follow TypeScript naming conventions

Class names should use PascalCase according to TypeScript conventions.

-export default class userComponentController extends Controller {
+export default class UserComponentController extends Controller {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export default class userComponentController extends Controller {
export default class UserComponentController extends Controller {


/**
* @summary 注册组件库
* @router POST /api/componentLibrary/create
*/
Comment on lines +17 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix incorrect route documentation

The JSDoc comment doesn't match the actual route defined in the router file.

/**
- * @summary 注册组件库
- * @router POST /api/componentLibrary/create
+ * @summary 创建组件库包
+ * @router POST /component/bundle/create
 */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
* @summary 注册组件库
* @router POST /api/componentLibrary/create
*/
/**
* @summary 创建组件库包
* @router POST /component/bundle/create
*/

async bundleCreate(){
const ctx = this.ctx;
const fileStream = await ctx.getFileStream();
this.ctx.body = await this.ctx.service.materialCenter.userComponents.bundleCreate(fileStream);
}
Comment on lines +20 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for file stream operations

The method lacks error handling for file upload operations, which can fail for various reasons (invalid file type, size limits, etc.).

Add proper error handling to gracefully manage file upload failures:

async bundleCreate(){
  const ctx = this.ctx;
-  const fileStream = await ctx.getFileStream();
-  this.ctx.body = await this.ctx.service.materialCenter.userComponents.bundleCreate(fileStream);
+  try {
+    const fileStream = await ctx.getFileStream();
+    this.ctx.body = await this.ctx.service.materialCenter.userComponents.bundleCreate(fileStream);
+  } catch (error) {
+    ctx.logger.error('[UserComponentController] bundleCreate error:', error);
+    ctx.status = error.status || 500;
+    ctx.body = {
+      success: false,
+      message: error.message || 'File upload failed',
+    };
+  }
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async bundleCreate(){
const ctx = this.ctx;
const fileStream = await ctx.getFileStream();
this.ctx.body = await this.ctx.service.materialCenter.userComponents.bundleCreate(fileStream);
}
async bundleCreate(){
const ctx = this.ctx;
try {
const fileStream = await ctx.getFileStream();
this.ctx.body = await this.ctx.service.materialCenter.userComponents.bundleCreate(fileStream);
} catch (error) {
ctx.logger.error('[UserComponentController] bundleCreate error:', error);
ctx.status = error.status || 500;
ctx.body = {
success: false,
message: error.message || 'File upload failed',
};
}
}


}
2 changes: 1 addition & 1 deletion app/lib/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,6 @@ export enum E_Public {
export enum E_FOUNDATION_MODEL {
GPT_35_TURBO = 'gpt-3.5-turbo', // openai
Local_GPT = 'loacl-compatible-gpt-3.5', //本地兼容opanai-api接口的 大语言模型,如chatGLM6b,通义千问 等。
ERNIE_BOT_TURBO = 'ERNIE-Bot-turbo', // 文心一言
ERNIE_BOT_TURBO = 'ERNIE-4.0-8K', // 文心一言
MOONSHOT_V1_8K = 'moonshot-v1-8k' // kimi
}
9 changes: 9 additions & 0 deletions app/router/materialCenter/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ export default (app: Application) => {
subRouter.get('/tasks/status', controller.materialCenter.task.status);
subRouter.get('/tasks/:id', controller.materialCenter.task.findById);

//组件库
subRouter.post('/component-library/update/:id',controller.materialCenter.componentLibrary.update);
subRouter.post('/component-library/create',controller.materialCenter.componentLibrary.create);
subRouter.delete('/component-library/delete/:id',controller.materialCenter.componentLibrary.delete);
subRouter.get('/component-library/find',controller.materialCenter.componentLibrary.find);
Comment on lines +65 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

API Design: Consider using correct RESTful HTTP methods

The component library endpoints follow RESTful naming but are inconsistent with their HTTP methods. For example, the update endpoint uses POST instead of PUT, while delete correctly uses DELETE.

Consider adjusting the router methods to align with RESTful conventions:

-subRouter.post('/component-library/update/:id',controller.materialCenter.componentLibrary.update);
+subRouter.put('/component-library/update/:id',controller.materialCenter.componentLibrary.update);

Also, add spacing after commas for consistency:

-subRouter.post('/component-library/update/:id',controller.materialCenter.componentLibrary.update);
+subRouter.put('/component-library/update/:id', controller.materialCenter.componentLibrary.update);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
//组件库
subRouter.post('/component-library/update/:id',controller.materialCenter.componentLibrary.update);
subRouter.post('/component-library/create',controller.materialCenter.componentLibrary.create);
subRouter.delete('/component-library/delete/:id',controller.materialCenter.componentLibrary.delete);
subRouter.get('/component-library/find',controller.materialCenter.componentLibrary.find);
//组件库
- subRouter.post('/component-library/update/:id',controller.materialCenter.componentLibrary.update);
+ subRouter.put('/component-library/update/:id', controller.materialCenter.componentLibrary.update);
subRouter.post('/component-library/create',controller.materialCenter.componentLibrary.create);
subRouter.delete('/component-library/delete/:id',controller.materialCenter.componentLibrary.delete);
subRouter.get('/component-library/find',controller.materialCenter.componentLibrary.find);



// 拆分bundle.json
subRouter.post('/component/bundle/create',controller.materialCenter.userComponents.bundleCreate);
};
18 changes: 17 additions & 1 deletion app/service/app-center/appSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class AppSchema extends SchemaService {
['dataSource', this.getSchemaDataSource],
['i18n', this.getSchemaI18n],
['componentsTree', this.getSchemaComponentsTree],
['componentsMap', this.getSchemaComponentsMap]
['componentsMap', this.getSchemaComponentsMap],
['packages', this.getPackages]
]);

// 获取schema数据
Expand Down Expand Up @@ -79,10 +80,25 @@ class AppSchema extends SchemaService {
// 获取应用信息
private async setMeta(query?): Promise<I_Response> {
const metaData: I_Response = await this.service.appCenter.apps.schemaMeta(this.appId, query);
const componentLibraryData = await this.service.materialCenter.componentLibrary.list();
this.meta = metaData.data;
this.meta.componentLibrary = componentLibraryData.data;
Comment on lines +83 to +85
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for component library fetch

The setMeta method now fetches component library data, but there's no error handling for cases where the fetch operation fails. This could lead to undefined behavior if the service is unavailable.

private async setMeta(query?): Promise<I_Response> {
  const metaData: I_Response = await this.service.appCenter.apps.schemaMeta(this.appId, query);
-  const componentLibraryData = await this.service.materialCenter.componentLibrary.list();
+  let componentLibraryData = { data: [] };
+  try {
+    componentLibraryData = await this.service.materialCenter.componentLibrary.list();
+  } catch (error) {
+    this.ctx.logger.error('Failed to fetch component library data', error);
+  }
  this.meta = metaData.data;
  this.meta.componentLibrary = componentLibraryData.data;
  return metaData;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const componentLibraryData = await this.service.materialCenter.componentLibrary.list();
this.meta = metaData.data;
this.meta.componentLibrary = componentLibraryData.data;
private async setMeta(query?): Promise<I_Response> {
const metaData: I_Response = await this.service.appCenter.apps.schemaMeta(this.appId, query);
let componentLibraryData = { data: [] };
try {
componentLibraryData = await this.service.materialCenter.componentLibrary.list();
} catch (error) {
this.ctx.logger.error('Failed to fetch component library data', error);
}
this.meta = metaData.data;
this.meta.componentLibrary = componentLibraryData.data;
return metaData;
}

return metaData;
}

private getPackages() {
const {componentLibrary} = this.meta;
const packages = componentLibrary.map((item) => ({
name: item.name,
package: item.packageName,
version: item.version,
script: item.script,
css: item.css,
others: item.others
}));
return this.ctx.helper.getResponseData(packages);
}
Comment on lines +89 to +100
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for component library data

The getPackages method assumes that componentLibrary always exists and has the expected structure. Add validation to handle cases where the data might be missing or malformed.

private getPackages() {
-  const {componentLibrary} = this.meta;
+  const componentLibrary = this.meta.componentLibrary || [];
  const packages = componentLibrary.map((item) => ({
    name: item.name,
    package: item.packageName,
    version: item.version,
    script: item.script,
    css: item.css,
    others: item.others
  }));
  return this.ctx.helper.getResponseData(packages);
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private getPackages() {
const {componentLibrary} = this.meta;
const packages = componentLibrary.map((item) => ({
name: item.name,
package: item.packageName,
version: item.version,
script: item.script,
css: item.css,
others: item.others
}));
return this.ctx.helper.getResponseData(packages);
}
private getPackages() {
- const {componentLibrary} = this.meta;
+ const componentLibrary = this.meta.componentLibrary || [];
const packages = componentLibrary.map((item) => ({
name: item.name,
package: item.packageName,
version: item.version,
script: item.script,
css: item.css,
others: item.others
}));
return this.ctx.helper.getResponseData(packages);
}


// 获取元数据
private getSchemaMeta(): I_Response {
const appData = this.meta.app;
Expand Down
53 changes: 53 additions & 0 deletions app/service/material-center/ComponentLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { E_Method } from '../../lib/enum';
import { I_CreateComponentLibrary } from '../../lib/interface';
import * as qs from 'querystring';
import DataService from '../dataService';

export default class ComponentLibrary extends DataService{

async create(param: I_CreateComponentLibrary) {
return this.query({
url: 'component-library' ,
method: E_Method.Post,
data: param
});
}

async find(param){
const query = qs.stringify(param);
return this.query({
url: `component-library?${query}`
});
}
Comment on lines +27 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve type safety with proper interfaces

The find method lacks type definitions for its parameters and return value, which reduces type safety.

Define an interface for the find parameters and specify the return type:

+interface FindParams {
+  name?: string;
+  description?: string;
+  [key: string]: any;
+}

-async find(param){
+async find(param: FindParams): Promise<any> {
  const query = qs.stringify(param);
  return this.query({
    url: `component-library?${query}`
  });
}


async list() {
return this.query({ url: 'component-library' });
}

async update(param) {
const { id } = param;
return this.query({
url: `component-library/${id}`,
method: E_Method.Put,
data: param
});
}
Comment on lines +38 to +45
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add type safety to the update method

Similar to the find method, the update method also lacks proper type definitions.

+interface UpdateComponentLibrary extends I_CreateComponentLibrary {
+  id: string | number;
+}

-async update(param) {
+async update(param: UpdateComponentLibrary): Promise<any> {
  const { id } = param;
  return this.query({
    url: `component-library/${id}`,
    method: E_Method.Put,
    data: param
  });
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async update(param) {
const { id } = param;
return this.query({
url: `component-library/${id}`,
method: E_Method.Put,
data: param
});
}
interface UpdateComponentLibrary extends I_CreateComponentLibrary {
id: string | number;
}
async update(param: UpdateComponentLibrary): Promise<any> {
const { id } = param;
return this.query({
url: `component-library/${id}`,
method: E_Method.Put,
data: param
});
}


delete({ id }){
return this.query({
url: `component-library/${id}`,
method: E_Method.Delete
});
}
Comment on lines +47 to +52
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use consistent method signatures and add async keyword

The delete method signature is inconsistent with other methods and missing the async keyword.

-delete({ id }){
+async delete({ id }: { id: string | number }): Promise<any> {
  return this.query({
    url: `component-library/${id}`,
    method: E_Method.Delete
  });
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
delete({ id }){
return this.query({
url: `component-library/${id}`,
method: E_Method.Delete
});
}
async delete({ id }: { id: string | number }): Promise<any> {
return this.query({
url: `component-library/${id}`,
method: E_Method.Delete
});
}

}
Comment on lines +17 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling to service methods

None of the methods include error handling, which is essential for robust API operations.

Add try-catch blocks to handle potential errors:

async create(param: I_CreateComponentLibrary) {
+  try {
    return this.query({ 
      url: 'component-library' ,
      method: E_Method.Post,
      data: param
    });
+  } catch (error) {
+    this.ctx.logger.error('[ComponentLibrary] create error:', error);
+    throw error;
+  }
}

Apply similar error handling to all other methods in the class.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export default class ComponentLibrary extends DataService{
async create(param: I_CreateComponentLibrary) {
return this.query({
url: 'component-library' ,
method: E_Method.Post,
data: param
});
}
async find(param){
const query = qs.stringify(param);
return this.query({
url: `component-library?${query}`
});
}
async list() {
return this.query({ url: 'component-library' });
}
async update(param) {
const { id } = param;
return this.query({
url: `component-library/${id}`,
method: E_Method.Put,
data: param
});
}
delete({ id }){
return this.query({
url: `component-library/${id}`,
method: E_Method.Delete
});
}
}
export default class ComponentLibrary extends DataService {
async create(param: I_CreateComponentLibrary) {
try {
return this.query({
url: 'component-library',
method: E_Method.Post,
data: param
});
} catch (error) {
this.ctx.logger.error('[ComponentLibrary] create error:', error);
throw error;
}
}
async find(param) {
try {
const query = qs.stringify(param);
return this.query({
url: `component-library?${query}`
});
} catch (error) {
this.ctx.logger.error('[ComponentLibrary] find error:', error);
throw error;
}
}
async list() {
try {
return this.query({ url: 'component-library' });
} catch (error) {
this.ctx.logger.error('[ComponentLibrary] list error:', error);
throw error;
}
}
async update(param) {
try {
const { id } = param;
return this.query({
url: `component-library/${id}`,
method: E_Method.Put,
data: param
});
} catch (error) {
this.ctx.logger.error('[ComponentLibrary] update error:', error);
throw error;
}
}
delete({ id }) {
try {
return this.query({
url: `component-library/${id}`,
method: E_Method.Delete
});
} catch (error) {
this.ctx.logger.error('[ComponentLibrary] delete error:', error);
throw error;
}
}
}

Loading