TypeScript front-end page generation method and system based on Swagger document

Through the method based on Swagger documents, the TypeScript front-end page generation method is implemented, which solves the problems of code duplication and poor applicability. Through automatic matching of tags and interfaces and template association, multiple page codes are generated, which improves work efficiency and code directness. Applicability.

CN118747069APending Publication Date: 2024-10-08SHENZHEN FENGYI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410808280.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-21
Publication Date
2024-10-08

AI Technical Summary

Technical Problem

Existing TypeScript front-end page generation methods have problems such as code duplication, easy vulnerability, poor applicability and flexibility. Especially in large projects, existing solutions cannot efficiently generate multiple page codes and associate them with interfaces.

Method used

Through the method based on Swagger documents, automatic matching of tags and interfaces is achieved, and multiple page codes are generated by combining templates and components, reducing code integration work and improving work efficiency.

Benefits of technology

It realizes the one-time generation of multiple page codes and related codes for multiple interfaces, avoiding the problem of generating a single page for a single interface. The code can be directly used in projects, reducing code integration work and improving work efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118747069A_ABST
    Figure CN118747069A_ABST
Patent Text Reader

Abstract

The invention discloses a TypeScript front-end page generation method based on a Swagger document, and the method comprises the following steps: S1, selecting a local item, and carrying out the label configuration; s2, a matching protocol of the interface labels and templates corresponding to the labels are configured, and each label can be correspondingly configured with a plurality of templates; s3, configuring a general protocol of the form component corresponding to the field, and configuring an address corresponding to an api-docs interface in the Swager; s4, selecting a Swagger interface group; and S5, on the basis of the Swagger interface group selected in the S4, obtaining related parameters of the interface group, and processing the related parameters to generate required data. According to the method, the label and the interface are automatically matched through the protocol, and the effect of generating a plurality of page codes and related codes for a plurality of interfaces at a time is achieved through the association of the label and the template and the association of the field and the component.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of TypeScript front-end page generation, and in particular to a TypeScript front-end page generation method and system based on a Swagger document. Background Art

[0002] JavaScript has some problems in large projects, such as difficult maintenance, difficult to detect type errors, lack of object-oriented programming, etc. In order to solve these problems, Microsoft developed TypeScript. TypeScript makes JavaScript code more robust, maintainable, and easier to develop large projects by adding features such as type system, interface, and generics.

[0003] With the introduction of TypeScript, the amount of code will increase suddenly. For large projects, there are many developers involved. Although there are restrictions on eslint, the styles will be different in the specific development details, which is not conducive to later maintenance. Taking the background management system as an example, list display, detail display, editing, adding, importing, exporting, reviewing, deleting, etc. are common functions. For these functions, the codes are similar, which leads to a lot of repetitive work in the code writing process, and manual writing is also prone to vulnerabilities.

[0004] In order to solve the above problems, code generation is needed. The existing solutions are mainly as follows:

[0005] 1) Generate front-end and back-end codes uniformly according to database design files. This solution is suitable for adding, deleting, modifying and checking a single data table. It is rigid and usually generates a list display, editing and deleting of a single data table. It is not very applicable.

[0006] 2) Drag and edit module components in the visual low-code platform to complete the definition of the front-end page. This solution only generates the front-end page, and the dragging and editing also require developers to operate one by one. The interaction with the interface is not achieved, and the developer needs to match the fields of the interface one by one later, which is not efficient;

[0007] 3) Read the interface document through node.js, generate the declaration file corresponding to the interface, and some pages (some solutions do not have a page generation method). This type of interface is without, and the operator needs to change the configuration and generate files through commands. In addition, the template is fixed, and only a single page of code can be generated according to the template at a time. The code integrity is insufficient (for example, only the code for generating a list page according to the list template is generated, but the editing and other related codes are not generated), the functional association is missing, and manual integration is required. It has poor flexibility, poor customizability, and is not easy to expand. Summary of the invention

[0008] The purpose of the present invention is to provide a TypeScript front-end page generation method and system based on Swagger documents. The method automatically matches tags and interfaces through protocols, and achieves the effect of generating multiple page codes and related codes for multiple interfaces at one time through the association of tags and templates, and the association of fields and components. The problem of only being able to generate a single page for a single interface each time can be avoided, and the code can be directly used in the project, reducing code integration work and improving work efficiency.

[0009] To achieve the above objectives, the following technical solutions are adopted:

[0010] A method for generating a TypeScript front-end page based on a Swagger document includes the following steps:

[0011] S1: Select a local project and configure labels;

[0012] S2: configure the matching protocol of the interface label and the template corresponding to each label, where each label can be configured with multiple templates;

[0013] S3: configure the general protocol of the form component corresponding to the field, and configure the address corresponding to the api-docs interface in Swagger;

[0014] S4: Select the Swagger interface group;

[0015] S5: Based on the Swagger interface group selected in S4, obtain relevant parameters of the interface group and process them to generate required data;

[0016] S6: Generates interface declaration files, interface call files, and page files based on the data generated by S5, the template configured by S2, and the general protocol configured by S3.

[0017] Furthermore, in S5, the relevant parameters of the interface group include the address of the interface, the input and output parameters of the interface, and the label corresponding to the interface.

[0018] Furthermore, in S6, generating the interface declaration file specifically includes the following steps:

[0019] Based on the selected Swagger interface group name, traverse the return value under the paths of api-docs, find each interface under the interface group, and obtain the object names of the input and output parameters of all interfaces to form an object array b;

[0020] Traverse the object array b and obtain the object c corresponding to all object names in the object array b in the return value definitions or components / schemas of api-docs;

[0021] Traverse all the attributes in object c and determine whether the attribute type is a custom object type. If so, return to the previous step. Otherwise, put all objects c into array d.

[0022] Traverse the array d, generate Typescript custom type codes and comments for all the obtained objects one by one, and put them in the interface declaration file.

[0023] Furthermore, the object c includes input and output parameter objects corresponding to the object name, and objects corresponding to the interface fields.

[0024] A TypeScript front-end page generation system based on Swagger documents, including

[0025] A project management module for managing the projects for which code generation is targeted;

[0026] Configuration module, used for label configuration, label and template relationship configuration, interface label matching protocol configuration, field and form component correspondence configuration, and interface address management;

[0027] Template management module, used to manage templates and template configurations corresponding to each tag;

[0028] A code generation module is used to generate code based on a selected interface group.

[0029] By adopting the above scheme, the beneficial effects of the present invention are:

[0030] This method automatically matches tags and interfaces through protocols, and by associating tags with templates, and fields with components, it can achieve the effect of generating multiple page codes and related codes for multiple interfaces at one time, which can avoid the problem of only generating a single page for a single interface each time, and the code can be directly used in the project, reducing code integration work and improving work efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] Figure 1 is a flow chart of the present invention;

[0032] Figure 2 The present invention is a flowchart of generating an interface declaration file. DETAILED DESCRIPTION

[0033] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments.

[0034] Reference Figures 1 to 2 As shown, the present invention provides a TypeScript front-end page generation method based on a Swagger document, comprising the following steps:

[0035] S1: Select a local project and configure tags.

[0036] In this step, since the directory structure, interface protocol, and template of each project under development may be different, you need to select a local project first to specify the project for which code generation is targeted. In the tag configuration, the default tags include "List", "Add", "Edit", "Delete", "Review", "Export", "Import", "Batch Delete", "Batch Other Operations", "Details", "Routing", etc. Tags can support adding custom tags, and custom tags can match templates.

[0037] S2: Configure the matching protocol of the interface label and the template corresponding to each label, wherein each label can be configured with multiple templates.

[0038] In this step, each interface has a description and an interface name. By setting the text in the description and the names in the interface name, they correspond to a certain label to establish the matching protocol of the interface label. For example, if the interface description contains "list", "table" or the interface name contains "list", "table", "page" fields, it corresponds to the label "list"; for labels with independent code files, set the corresponding template for code generation. Labels with independent code files: "Routing", "List", "Details", "Edit", "Add", "Import", "Export", etc., need to configure the corresponding template to generate the corresponding file. Each label can correspond to multiple templates, and the default template needs to be set for code generation. When configuring the template, you need to fill in the suffix of the file generation to support code generation in multiple front-end languages.

[0039] S3: Configure the common protocol of the form component corresponding to the field, and configure the address corresponding to the api-docs interface in Swagger.

[0040] In this step, configure the general protocol of the form component corresponding to the field, which is used to generate the corresponding code for the list search condition form, add and edit forms. The protocol is based on which keywords are contained in the field name or which keywords are contained in the field description or which field type is which. The component type is the component in the corresponding template, or the component type is "custom" and fill in the component-related code. The rule priority is sorted; at the same time, the address corresponding to the api-docs interface in swagger can be configured for different environments and different services to read the address of the interface, input and output parameters, etc.

[0041] S4: Select the Swagger interface group.

[0042] In this step, an interface group is selected to determine the interface correlation. You can also manually select multiple interfaces as a temporary interface group, replace the interface group, and continue with the subsequent logic.

[0043] S5: Based on the Swagger interface group selected in S4, relevant parameters of the interface group are obtained, and the parameters are processed to generate required data; in S5, the relevant parameters of the interface group include the address of the interface, the input and output parameters of the interface, and the label corresponding to the interface.

[0044] In this step, based on the interface group (or temporary interface group) selected in the previous step, you can obtain the address of the interface, the input and output parameters of the interface, and the label corresponding to the interface (the label corresponding to the interface can be adjusted), and process it according to the rules to generate the data required for the file.

[0045] S6: Generates interface declaration files, interface call files, and page files based on the data generated by S5, the template configured by S2, and the general protocol configured by S3.

[0046] In this step, declaration files, interface call files, interface mock files, etc. of all interfaces are generated, and the corresponding page code is generated for each interface under the interface group according to the template corresponding to the label.

[0047] Wherein, in said S6, generating an interface declaration file specifically comprises the following steps:

[0048] Based on the selected Swagger interface group name, traverse the return value under the paths of api-docs, find each interface under the interface group, and obtain the object names of the input and output parameters of all interfaces to form an object array b;

[0049] Traverse the object array b and obtain the object c corresponding to all object names in the object array b in the return value definitions or components / schemas of api-docs;

[0050] Traverse all the attributes in object c and determine whether the attribute type is a custom object type. If so, return to the previous step. Otherwise, put all objects c into array d.

[0051] Traverse the array d, generate Typescript custom type codes and comments for all the obtained objects one by one, and put them in the interface declaration file.

[0052] At the same time, the object c includes input and output parameter objects corresponding to the object name, and objects corresponding to the interface fields.

[0053] In this step, first select the Swagger interface group name (which can be named a), traverse the return value under the paths of api-docs, find each interface with the interface group name a, and find out their input and output parameter object names (named array b); then obtain the input and output parameter objects corresponding to all object names in array b in the return value definitions or components / schemas of api-docs, as well as the objects corresponding to the interface fields (if any), and generate typescript custom type codes for all the obtained objects one by one (with additional comments on the generated custom types and attributes) and put them in the interface declaration file.

[0054] The specific interface call file generation method can generate the corresponding interface call method for each interface under the selected swagger interface group according to a fixed template. In addition, for the specific page generation method, taking the most complex computer-side list page as an example, the list page is usually composed of a search module, a table header operation module, a table, and sub-pages. The table is divided into a display column and an operation column; the search template takes the input parameters of the list interface (except the paging and sorting related fields of the interface), stores them in the input parameter field array, traverses the input parameter field array, and creates search conditions according to the type and name of the input parameter field according to the rules to the code array A; the header of the table determines the same Check whether "Add", "Import" and "Export" interfaces exist under the interface group. If so, put them into array B. Determine whether the return parameters of the list interface have a page structure and related fields. If so, set F=true, otherwise set F=false. For the display columns of the table, take the return fields of the list interface (except for the page-related ones), format the fields according to the rules, and put them into array E. Determine whether there are other interfaces except "Import" and "Export" under the same interface group, use the label name as the button name, and put them into array D. According to the template corresponding to the list, combine array A, array B, array D, array E, and variable F to generate a complete page code.

[0055] There are also some alternatives:

[0056] Solution 1: Before code generation, you can add an operational interface to remove undisplayed fields or adjust the display order of fields;

[0057] Solution 2: The labels "Add", "Edit", and "Details" can share the same template and only generate one file;

[0058] Solution 3: Configure the interface address. You can configure the interface website address instead. Through the website interface, you can obtain all service-related interface groups and interfaces under the website.

[0059] It also provides a TypeScript front-end page generation system based on Swagger documents, including

[0060] A project management module for managing the projects for which code generation is targeted;

[0061] Configuration module, used for label configuration, label and template relationship configuration, interface label matching protocol configuration, field and form component correspondence configuration, and interface address management;

[0062] Template management module, used to manage templates and template configurations corresponding to each tag;

[0063] A code generation module is used to generate code based on a selected interface group.

[0064] The working principle of the system can refer to the above method and will not be repeated here. The system automatically matches labels and interfaces through protocols, and through the association of labels and templates, and the association of fields and components, it can achieve the effect of generating multiple page codes and related codes for multiple interfaces at one time, avoiding the problem of only being able to generate a single page for a single interface each time, and the code can be directly used in the project, reducing code integration work and improving work efficiency.

[0065] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A TypeScript front-end page generation method based on Swagger document, characterized in that: The steps include: S1: Select a local project and configure labels; S2: Configure the matching protocol for the interface tags and the templates corresponding to each tag. Each tag can be configured with multiple templates. S3: Configure the general protocol of the form component corresponding to the configuration field, and configure the address corresponding to the api-docs interface in Swagger; S4: Select the Swagger interface group; S5: Based on the Swagger interface group selected in S4, obtain the relevant parameters of the interface group and process them to generate the required data; S6: Generates interface declaration files, interface call files, and page files based on the data generated by S5, the template configured by S2, and the general protocol configured by S3.

2. The method for generating a TypeScript front-end page based on a Swagger document according to claim 1, wherein: In S5, the relevant parameters of the interface group include the address of the interface, the input and output parameters of the interface, and the label corresponding to the interface.

3. The method for generating a TypeScript front-end page based on a Swagger document according to claim 1, wherein: In S6, generating the interface declaration file specifically includes the following steps: Based on the selected Swagger interface group name, traverse the return value under the paths of api-docs, find each interface under the interface group, and obtain the object names of the input and output parameters of all interfaces to form an object array b; Traverse the object array b and obtain the object c corresponding to all object names in the object array b from the return value definitions or components / schemas of api-docs; Traverse all the properties in object c and determine whether the property type is a custom object type. If so, return to the previous step. Otherwise, put all objects c into array d. Traverse the array d, generate Typescript custom type codes and comments for all obtained objects one by one, and place them in the interface declaration file.

4. The method for generating a TypeScript front-end page based on a Swagger document according to claim 3, wherein: The object c includes input and output parameter objects corresponding to the object name, and objects corresponding to the interface fields.

5. A TypeScript front-end page generation system based on Swagger document, characterized in that: include Project management module, used to manage the projects for code generation; Configuration module, used for label configuration, label and template relationship configuration, interface label matching protocol configuration, field and form component correspondence configuration, and interface address management; Template management module, used to manage templates and template configurations corresponding to each tag; A code generation module is used to generate code based on a selected interface group.