Code generation method and device, equipment and storage medium

CN122985134APending Publication Date: 2026-10-27CHINA MERCHANTS BANK
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611380488.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-07
Publication Date
2026-10-27

AI Technical Summary

Technical Problem

然而,这类传统工具通常以命令行脚本或编辑器插件的形式存在,开发人员在编码过程中需要中断当前工作流,需要在编写过程中手动执行命令或者通过编辑器操作触发代码生成,生成后还需要手动复制粘贴到对应代码文件,使得接口代码编写的操作链路长、编码效率低下

Benefits of technology

[0017]本申请提供了一种代码生成方法,本申请监听代码文件的修改事件以得到实时修改事件,基于所述实时修改事件确定所述代码文件中的原始代码;判断所述原始代码是否包含预设的接口关键词,在所述原始代码包含所述接口关键词的情况下,提取所述原始代码中与所述接口关键词对应的接口标识信息,从预设接口文档平台确定与所述接口标识信息对应的目标接口定义信息;基于所述目标接口定义信息生成接口请求代码,并将所述接口请求代码补全至所述原始代码,得到目标代码。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122985134A_ABST
    Figure CN122985134A_ABST
Patent Text Reader

Abstract

The application discloses a code generation method and device, equipment and a storage medium, and relates to the technical field of computers. The method comprises the following steps: listening to a modification event of a code file to obtain a real-time modification event, determining original code in the code file based on the real-time modification event; determining whether the original code contains a preset interface keyword; in the case where the original code contains the interface keyword, extracting interface identification information corresponding to the interface keyword in the original code, determining target interface definition information corresponding to the interface identification information from a preset interface document platform; generating an interface request code based on the target interface definition information, and completing the interface request code to the original code to obtain target code. The application can simplify the operation link of interface code writing and improve the efficiency of interface code writing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a code generation method, apparatus, device, and storage medium. Background Technology

[0002] In modern front-end engineering development, calling backend APIs (Application Programming Interfaces) is a core step in the interaction between front-end applications and backend services. Each API interface requires a corresponding TypeScript (TS) type declaration in the front-end code to ensure the correctness of request parameters and the type safety of response data.

[0003] Currently, the common method for generating API code mainly involves developers parsing the structured API definitions provided by API documentation platforms or backend services, and then using code generation tools to batch convert the API definitions into TypeScript type declarations and API request code. However, these traditional tools usually exist in the form of command-line scripts or editor plugins. Developers need to interrupt their current workflow during coding, manually execute commands or trigger code generation through editor operations, and then manually copy and paste the generated code into the corresponding code file. This results in a long workflow and low coding efficiency in API code writing.

[0004] Therefore, how to improve the efficiency of writing interface code is a technical problem that urgently needs to be solved. Summary of the Invention

[0005] The main purpose of this application is to provide a code generation method, apparatus, device, and storage medium, which aims to improve the efficiency of interface code writing.

[0006] To achieve the above objectives, this application provides a code generation method, the code generation method comprising: Listen for modification events in the code file to obtain real-time modification events, and determine the original code in the code file based on the real-time modification events; Determine whether the original code contains a preset interface keyword. If the original code contains the interface keyword, extract the interface identifier information corresponding to the interface keyword from the original code, and determine the target interface definition information corresponding to the interface identifier information from a preset interface document platform. Based on the target interface definition information, an interface request code is generated, and the interface request code is then added to the original code to obtain the target code.

[0007] In one embodiment, the step of monitoring modification events of a code file to obtain real-time modification events, and determining the original code in the code file based on the real-time modification events, includes: Monitor each sub-code file in the directory where the specified code file is located; When a modification is detected in any of the sub-code files under the directory, the modification event of the sub-code file is taken as the real-time modification event, and the code in the sub-code file is taken as the original code.

[0008] In one embodiment, the step of extracting the interface identifier information corresponding to the interface keyword in the original code includes: The original code is matched using regular expressions based on the interface keywords, and the matched strings are used as the content to be replaced. Extract a preset length of characters from the content to be replaced after the interface keyword, and use it as the interface identification information.

[0009] In one embodiment, the step of determining the target interface definition information corresponding to the interface identification information from a preset interface documentation platform includes: Obtain login credentials for logging into the preset interface document platform; Based on the login credentials, a login authentication request is initiated to the preset interface document platform, and the system monitors whether an authentication credential for successfully logging into the preset interface document platform is received. If an authentication credential for successful login to the preset interface documentation platform is received, an interface query request is initiated to the preset interface documentation platform based on the authentication credential and the interface identification information; The interface definition information corresponding to the interface identifier information in the preset interface documentation platform is used as the target interface definition information.

[0010] In one embodiment, the target interface definition information includes a request parameter field, a return parameter field, and an interface path. The step of generating interface request code based on the target interface definition information and completing the interface request code into the original code to obtain the target code includes: Extract the portion following the last path separator from the interface path and use it as the base name; Remove the separators from the base name and convert the first letter of each word obtained by splitting the base name by the separators to uppercase to obtain the interface name; Convert the first letter of the interface name to lowercase to obtain the request function name; Generate a type name based on the interface name; Read the preset code template, and fill the code template with the interface path, the interface name, the request function name and the type name to obtain the interface request code; The target code is obtained by replacing the content to be replaced in the original code with the interface request code.

[0011] In one embodiment, the type name includes a request parameter type name and a return parameter type name, and the step of generating a type name based on the interface name includes: Add a first preset suffix to the interface name to obtain the request parameter type name; Add a second preset suffix to the interface name to obtain the return parameter type name.

[0012] In one embodiment, after the step of completing the interface request code into the original code, the method further includes: The content that is replaced in the original code is taken as the replaced content. Preset start and end markers are added to the start and end positions of the replaced content to identify that the replaced content is automatically generated interface code. Update the timestamp of the code file to trigger the code editor containing the code file to reload the code file.

[0013] Furthermore, to achieve the above objectives, this application also provides a code generation apparatus, the code generation apparatus comprising: The monitoring module is used to monitor modification events of code files to obtain real-time modification events, and to determine the original code in the code file based on the real-time modification events; The extraction module is used to determine whether the original code contains preset interface keywords. If the original code contains the interface keywords, the module extracts the interface identifier information corresponding to the interface keywords in the original code and determines the target interface definition information corresponding to the interface identifier information from the preset interface document platform. The completion module is used to generate interface request code based on the target interface definition information, and to complete the interface request code into the original code to obtain the target code.

[0014] In addition, to achieve the above objectives, this application also proposes a code generation device, the device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the code generation method as described above.

[0015] In addition, to achieve the above objectives, this application also provides a storage medium, which is a computer-readable storage medium, on which a program implementing the code generation method is stored, and the program implementing the code generation method is executed by a processor to implement the steps of the code generation method as described above.

[0016] In addition, to achieve the above objectives, this application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the code generation method described above.

[0017] This application provides a code generation method. The method involves monitoring modification events of code files to obtain real-time modification events, determining the original code in the code file based on these real-time modification events, determining whether the original code contains preset interface keywords, and if so, extracting interface identifier information corresponding to the interface keywords from the original code, determining target interface definition information corresponding to the interface identifier information from a preset interface documentation platform, generating interface request code based on the target interface definition information, and then completing the interface request code into the original code to obtain the target code.

[0018] In summary, this application monitors code file modification events. Upon detecting a modification, it identifies the original code within the file, determines whether it contains preset interface keywords, extracts interface identifier information from the original code if so, retrieves the target interface definition information corresponding to the interface identifier information from a preset interface documentation platform, generates interface request code based on the target interface definition information, and completes the original code with the interface request code to obtain the target code. Compared to traditional solutions that require manually executing generation commands or triggering code generation through editor operations, followed by manual copying and pasting into the code file, this application, by monitoring file modification events in real time and automatically extracting the corresponding interface definition information from a preset interface documentation platform when the original code contains preset interface keywords, generates interface request code to complete the original code. This eliminates the need for developers to interrupt their workflow for tedious copy-and-paste operations during coding, simplifying the interface code writing process and improving efficiency. Attached Figure Description

[0019] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0020] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0021] Figure 1 This is a flowchart illustrating the first embodiment of the code generation method of this application; Figure 2 This is a schematic diagram illustrating the interface information acquisition process involved in one embodiment of the code generation method of this application; Figure 3 This is a schematic diagram of the interface request code generation process according to an embodiment of the code generation method of this application; Figure 4 This is a schematic diagram of the module structure of the code generation device for this application; Figure 5 This is a schematic diagram of the device structure of the hardware operating environment involved in the code generation method in this application embodiment.

[0022] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0023] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.

[0024] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.

[0025] Currently, the common method for generating API code mainly involves developers parsing the structured API definitions provided by API documentation platforms or backend services, and then using code generation tools to convert the API definitions into TypeScript type declarations and API request code in batches. However, these traditional tools usually exist in the form of command-line scripts or editor plugins. Developers need to interrupt their current workflow during coding, manually execute commands or trigger code generation through editor operations, and then manually copy and paste the generated code into the corresponding code file. This makes the API code writing process lengthy and inefficient.

[0026] Therefore, how to improve the efficiency of writing interface code is a technical problem that urgently needs to be solved.

[0027] The main solution of this application is as follows: listen for modification events of code files to obtain real-time modification events, determine the original code in the code files based on the real-time modification events; determine whether the original code contains preset interface keywords, and if the original code contains interface keywords, extract the interface identifier information corresponding to the interface keywords in the original code, determine the target interface definition information corresponding to the interface identifier information from the preset interface documentation platform; generate interface request code based on the target interface definition information, and complete the interface request code into the original code to obtain the target code.

[0028] This application monitors code file modification events. When a modification is detected, it identifies the original code within the file, determines whether it contains preset interface keywords, extracts interface identifier information from the original code if so, retrieves the corresponding target interface definition information from a preset interface documentation platform, generates interface request code based on the target interface definition information, and completes the original code with the interface request code to obtain the target code. Compared to traditional solutions that require manually executing generation commands or triggering code generation through editor operations, followed by manual copying and pasting into the code file, this application, by monitoring file modification events in real time and automatically extracting the corresponding interface definition information from a preset interface documentation platform when the original code contains preset interface keywords, generates interface request code to complete the original code. This eliminates the need for developers to interrupt their workflow for tedious copy-and-paste operations, simplifying the interface code writing process and improving efficiency.

[0029] It should be noted that the execution subject of the code generation method in various embodiments of this application can be a code generation device, or a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or a code generation device capable of performing the above functions. This embodiment does not specifically limit this. The following uses a code generation device as the execution subject as an example to describe this embodiment and the following embodiments.

[0030] Based on this, this application proposes a code generation method according to the first embodiment, please refer to... Figure 1 The code generation method of this application includes steps S10 to S30: Step S10: Listen for modification events in the code file to obtain real-time modification events, and determine the original code in the code file based on the real-time modification events.

[0031] A code file refers to the source code file written by developers during front-end application development, used to carry API request code and type definitions, such as a source code file written in TypeScript. A modification event is an event triggered when the content of a code file changes. A real-time modification event is a modification event that is captured immediately when the content of a code file is modified during development. The original code refers to the source code included in the code file after modification, before the API request code has been completed.

[0032] After the code generation device starts up, it monitors the target directory and its subdirectories for each code file through a file monitoring subsystem. When any code file in the target directory is detected to have been modified, the code generation device immediately captures the modification event as a real-time modification event and uses the code in the modified file as the original code.

[0033] For example, the code generation device uses chokidar (a file monitoring library) in Node.js (a JavaScript server-side runtime environment) to monitor the source code directory of the front-end project configured by the developer in real time. When the developer saves a TS code file in the code editor, the chokidar library immediately captures the modification event of the code file. The code generation device treats this modification event as a real-time modification event and reads the current content of the code file as the source code.

[0034] Step S20: Determine whether the original code contains preset interface keywords. If the original code contains interface keywords, extract the interface identifier information corresponding to the interface keywords in the original code, and determine the target interface definition information corresponding to the interface identifier information from the preset interface document platform.

[0035] Interface keywords are pre-defined placeholder keywords used in code files to mark the interfaces that will be used to generate interface request code. Interface identification information refers to the identifier carried within the interface keywords, used to uniquely identify a specific interface or group of interfaces, such as the interface's ID (Identifier). A pre-defined interface documentation platform refers to a pre-specified platform for centrally managing and providing interface definition information, such as the YAPI (Yet Another Practice Interface, a visual interface management platform) interface documentation platform. Target interface definition information refers to the definition information in the pre-defined interface documentation platform that corresponds to the interface identification information and describes the structure of the interface, including request parameters and return parameters.

[0036] After determining the original code, the code generation device checks whether the original code contains preset interface keywords. If the original code does not contain interface keywords, it means that the modification of the code file does not involve interface code generation, and the code generation device ends the current processing. If the original code contains interface keywords, the code generation device extracts the interface identifier information corresponding to the interface keywords in the original code, and queries and determines the corresponding target interface definition information from the preset interface document platform based on the interface identifier information.

[0037] For example, a developer might write an interface keyword like "yapi@123" in a code file, where "yapi@" is a predefined interface keyword prefix and the number "123" is the interface identifier. When the code generation device detects a modification to the code file, it matches "yapi@123" using a regular expression, determines that the original code contains the interface keyword, extracts the interface identifier "123," and then queries the YAPI interface documentation platform for the interface definition information with interface ID 123 as the target interface definition information.

[0038] Step S30: Generate interface request code based on the target interface definition information, and complete the interface request code into the original code to obtain the target code.

[0039] API request code refers to the code used to initiate API requests in a code file, including the API request function and its corresponding type declaration. Completion refers to the process of replacing the placeholder content corresponding to API keywords in the original code with the generated API request code. Target code refers to the complete code obtained after completing the API request code into the original code, which is directly usable by developers.

[0040] After obtaining the target interface definition information, the code generation device parses the request and return parameters in the target interface definition information and generates interface request code accordingly. Subsequently, the code generation device completes the generated interface request code into the position of the interface keyword in the original code, that is, replaces the interface keyword in the original code with the interface request code, thereby obtaining the target code.

[0041] For example, the code generation device parses the request parameter field, return parameter field, and interface path of the interface based on the interface definition information with interface ID 123, and generates the corresponding TypeScript type declaration and interface request function accordingly. Then, it replaces "yapi@123" in the original code with the type declaration and interface request function to obtain the target code.

[0042] This embodiment of the application monitors code file modification events, determines the original code based on real-time modification events, extracts interface identifier information when the original code contains preset interface keywords, and determines target interface definition information from a preset interface documentation platform. Then, based on the target interface definition information, it generates interface request code and completes it into the original code to obtain the target code. Thus, compared to traditional solutions that require manually executing generation commands or triggering code generation through editor operations followed by manual copying and pasting, this embodiment of the application automatically generates and completes interface request code by monitoring file modification events in real time and when the original code contains interface keywords. This eliminates the need for developers to interrupt their current workflow for tedious copying and pasting, simplifying the interface code writing process and improving efficiency.

[0043] Based on the first embodiment of this application, in the second embodiment of this application, the content that is the same as or similar to that in the first embodiment described above can be referred to the above description and will not be repeated hereafter. Based on this, step S10 above may include: Step A10: Monitor each sub-code file in the directory where the code file is located; Step A20: When a modification is detected in any sub-code file under the directory, the modification event of the sub-code file is treated as a real-time modification event, and the code in the sub-code file is treated as the original code.

[0044] Sub-code files refer to the individual code files contained within the directory containing the main code file and its subdirectories. Directories are folders configured by developers to store the source code of front-end projects.

[0045] The code generation device recursively monitors all sub-code files in the directory containing the code file and its subdirectories through a file monitoring subsystem, meaning it monitors every code file in the directory simultaneously. When any sub-code file in the directory is detected to have been modified, the code generation device treats that modification event as a real-time modification event and uses the code in the modified sub-code file as the original code, thus enabling real-time response to modifications to any code file in the directory.

[0046] For example, a developer configures the `src` directory as a monitoring target in a front-end project. The code generation device recursively monitors all TypeScript code files in the `src` directory and its subdirectories using the `chokidar` library. When a developer modifies and saves a code file in the `src / services` directory, the code generation device captures the modification event of that sub-file, treats it as a real-time modification event, and uses the code in that sub-file as the source code.

[0047] In this embodiment, the step of "extracting interface identifier information corresponding to interface keywords in the original code" in step S20 above may include: Step B10: Perform regular expression matching on the original code based on the interface keywords, and use the matched strings as the content to be replaced; Step B20: Extract the characters of a preset length following the interface keyword from the content to be replaced, and use them as interface identification information.

[0048] The content to be replaced refers to the string obtained by matching the interface keywords and their accompanying interface identifier information in the original code. This string will be replaced by the generated interface request code in subsequent steps. Regular expression matching refers to the process of using regular expressions to find strings in the original code that match the interface keyword format. Preset length refers to the pre-defined character length of the interface identifier information.

[0049] The code generation device performs a global match on the original code based on a preset regular expression, searching for all strings that match the interface keyword format, and uses the matched strings as the content to be replaced; then, the code generation device extracts characters of a preset length after the interface keyword from the content to be replaced, and uses the extracted characters as the interface identification information.

[0050] For example, the preset interface keyword is "yapi@", the preset regular expression is / yapi@\d+ / g, and the preset length is the number of digits in the interface ID. When the original code contains "yapi@123", the code generation device uses the regular expression to match and obtain the content to be replaced, "yapi@123", and then extracts the characters "123" after "yapi@" as the interface identifier information. For batch interface scenarios, the preset regular expression is / yapi@cat_\d+ / g, the interface keyword is "yapi@cat_", and the interface identifier information "45" extracted from the content to be replaced, "yapi@cat_45", is the interface directory ID.

[0051] In this embodiment, the step of "determining the target interface definition information corresponding to the interface identifier information from the preset interface documentation platform" in step S20 above may include: Step C10: Obtain login credentials for logging into the preset interface documentation platform; Step C20: Initiate a login authentication request to the preset interface documentation platform based on the login credentials, and monitor whether the authentication credentials for successfully logging into the preset interface documentation platform have been received; Step C30: If an authentication credential for successfully logging into the preset interface documentation platform is received, an interface query request is initiated to the preset interface documentation platform based on the authentication credential and interface identification information. Step C40: Use the interface definition information corresponding to the interface identifier information in the preset interface document platform as the target interface definition information.

[0052] Login credentials refer to account credentials used to log in to the pre-defined interface documentation platform, such as a user's email address and password. Authentication credentials refer to credentials returned by the pre-defined interface documentation platform after successful user login authentication, used to identify the user's login identity, such as cookies (credentials used by website servers to identify user session identities). An interface query request refers to a request initiated to the pre-defined interface documentation platform based on interface identifier information to query interface definition information.

[0053] The code generation device reads the login credentials used to log in to the preset interface documentation platform from the configuration file; it initiates a login authentication request to the preset interface documentation platform based on the login credentials, and monitors whether it receives a successful login authentication credential returned by the preset interface documentation platform; if a successful login authentication credential is received, the code generation device initiates an interface query request to the preset interface documentation platform based on the authentication credential and interface identification information to query the interface definition information corresponding to the interface identification information; subsequently, the code generation device uses the interface definition information returned by the preset interface documentation platform that corresponds to the interface identification information as the target interface definition information.

[0054] For example, the code generation device reads the user's email address and password from the configuration file as login credentials, calls the login interface of the YAPI interface documentation platform, and obtains the returned cookie credentials as authentication credentials. Then, it calls the YAPI interface query interface (e.g., the ` / interface / get` interface) with this cookie credentials, passing in the interface identifier "123", to obtain the complete definition information of the interface with interface ID 123, and uses it as the target interface definition information. When the login credentials expire, the code generation device prompts the user to log in again via a dialog box. For batch interface scenarios, the code generation device first calls the ` / list_cat` interface to obtain a list of IDs of all interfaces under the specified interface directory, and then calls the ` / interface / get` interface one by one to obtain the detailed information of each interface.

[0055] For example, such as Figure 2The diagram illustrates the interface information acquisition process. First, the code generation device listens for code file modification events. When any sub-code file in the code file directory is modified, the modification event of the sub-code file is treated as a real-time modification event, and the code in the sub-code file is used as the original code. Regular expression matching is performed on the original code based on the interface keywords. If the match fails, code generation is not performed. If the match succeeds, a pre-defined length of characters following the interface keywords in the matched string is extracted as the interface identifier information. Next, the device obtains the login credentials for logging into the pre-defined interface documentation platform and initiates a login authentication request, monitoring whether a successful login credentials for the pre-defined interface documentation platform have been received. If no successful login credentials for the pre-defined interface documentation platform have been received, code generation is not performed. If successful login credentials for the pre-defined interface documentation platform have been received, an interface query request is initiated, and the interface definition information corresponding to the interface identifier information in the pre-defined interface documentation platform is used as the target interface definition information.

[0056] In this embodiment, step S30, "generating interface request code based on target interface definition information and completing the interface request code into the original code to obtain the target code," may include: Step D10: Extract the part after the last path separator from the interface path as the base name; Step D20: Remove the separator from the base name and convert the first letter of each word obtained by splitting the base name by the separator to uppercase to obtain the interface name; Step D30: Convert the first letter of the interface name to lowercase to obtain the request function name; Step D40: Generate type name based on interface name; Step D50: Read the preset code template, fill in the interface path, interface name, request function name and type name into the code template to obtain the interface request code; Step D60: Replace the content to be replaced in the original code with the interface request code to obtain the target code.

[0057] Request parameter fields refer to the fields in the interface definition information used to describe the structure of request parameters, such as the `req_body_other` field in the YAPI interface documentation. Return parameter fields refer to the fields in the interface definition information used to describe the structure of return parameters, such as the `res_body` field in the YAPI interface documentation. Interface path refers to the access path of the interface, such as " / user / get-info". Base name refers to the part of the interface path after the last path separator. Interface name refers to the interface name generated according to the Big CamelCase naming convention based on the base name. Request function name refers to the request function name generated according to the Little CamelCase naming convention based on the interface name. Type name refers to the type name generated based on the interface name and used for type declaration. Code template refers to the pre-configured template string used to generate the interface request code.

[0058] After obtaining the target interface definition information, the code generation device extracts the part after the last path separator from the interface path as the base name; removes the separator from the base name and converts the first letter of each word obtained by splitting the base name by the separator to uppercase to obtain the interface name; converts the first letter of the interface name to lowercase to obtain the request function name; generates a type name based on the interface name; then reads a preset code template and fills the interface path, interface name, request function name, and type name into the corresponding placeholder positions in the code template to obtain the interface request code; finally, replaces the content to be replaced in the original code with the interface request code to obtain the target code.

[0059] For example, if the interface path is " / user / get-info", the code generation device extracts the part "get-info" after the last path separator " / " as the base name; removes the separator "-" from the base name to get "getinfo", and converts the first letter of each word to uppercase to get the interface name "GetInfo"; converts the first letter of "GetInfo" to lowercase to get the request function name "getInfo"; generates the type names "GetInfoRequest" and "GetInfoResponse" based on the interface name "GetInfo"; reads the preset code template, fills the code template with the interface path, interface name, request function name, and type name, and generates the interface request code; finally, replaces the content to be replaced in the original code "yapi@123" with the interface request code to obtain the target code.

[0060] In this embodiment, the type name includes the request parameter type name and the return parameter type name. The step D40 "Generate type name based on interface name" mentioned above may include: Step D41: Add the first preset suffix to the interface name to obtain the request parameter type name; Step D42: Add a second preset suffix to the interface name to obtain the return parameter type name.

[0061] The request parameter type name refers to the type name used to declare the structure of interface request parameters. The return parameter type name refers to the type name used to declare the structure of interface return parameters. The first preset suffix is ​​a pre-defined suffix used to identify the request parameter type, such as "Request". The second preset suffix is ​​a pre-defined suffix used to identify the return parameter type, such as "Response".

[0062] The code generation device adds a first preset suffix to the interface name to obtain the request parameter type name; it adds a second preset suffix to the interface name to obtain the return parameter type name.

[0063] For example, if the interface name is "GetInfo", the first preset suffix is ​​"Request", and the second preset suffix is ​​"Response", then the request parameter type name is "GetInfoRequest" and the return parameter type name is "GetInfoResponse".

[0064] For example, such as Figure 3 This is a flowchart illustrating the interface request code generation process. The target interface definition information includes request parameter fields, return parameter fields, and the interface path. First, the part after the last path separator is extracted from the interface path as the base name. The separators are removed from the base name, and the first letter of each word obtained by splitting the base name by the separator is converted to uppercase to obtain the interface name. The first letter of the interface name is converted to lowercase to obtain the request function name. A first preset suffix is ​​added to the interface name to obtain the request parameter type name. A second preset suffix is ​​added to the interface name to obtain the return parameter type name. Next, a preset code template is read, and the interface path, interface name, request function name, and type name are filled into the code template to obtain the interface request code.

[0065] In this embodiment, after the step of "completing the interface request code to the original code" in step S30 above, the code generation method of this application further includes: Step E10: The content that has been replaced in the original code is taken as the replaced content. Preset start and end markers are added to the start and end positions of the replaced content to identify that the replaced content is automatically generated interface code. Step E20 updates the timestamp of the code file, triggering the code editor containing the code file to reload the code file.

[0066] The replaced content refers to the automatically generated interface code obtained after replacing the interface keywords in the original code with the interface request code. Start and end markers are pre-defined symbols added to the beginning and end of the replaced content, respectively, to identify the code segment as automatically generated interface code, facilitating developer identification and subsequent maintenance. The timestamp refers to the last modification time attribute of the code file.

[0067] After the code generation device completes the interface request code into the original code, it will take the content that was replaced in the original code as the replaced content and add preset start and end markers at the start and end positions of the replaced content to identify that the replaced content is automatically generated interface code. Then, the code generation device updates the timestamp of the code file, triggering the code editor where the code file is located to reload the code file, so that the code editor can display the completed target code.

[0068] For example, the code generation device adds comment markers " / / yapi:begin" and " / / yapi:end" at the start and end positions of the replaced content, respectively, to identify the code segment as automatically generated interface code; then it updates the timestamp of the code file through the fs.utimes interface, triggering the code editor to detect the file change and reload it, thereby displaying the completed target code in the editor.

[0069] This embodiment of the application monitors modification events of each sub-code file in the directory, uses regular expression matching to accurately capture interface keywords and extract interface identification information, and combines login authentication and credential management of the preset interface documentation platform to ensure the secure acquisition of interface definition information. Simultaneously, it automatically derives the base name, interface name, request function name, and type name from the interface path, and uses configurable code templates to generate complete interface request code. Finally, the generated code replaces the original placeholder content, adds start and end markers, and updates the timestamp to trigger an editor reload. Thus, this embodiment not only inherits the advantages of the overall solution—no workflow interruption and no manual copy-pasting—but further refines the complete automated chain from file modification to code implementation, ensuring the accuracy of code generation, consistency of naming conventions, and the immediacy of editor loading. This significantly shortens the waiting and switching time for developers during the coding process, making it particularly suitable for development scenarios with frequent interface changes and multi-person collaboration, effectively improving the overall efficiency and quality of interface code writing in front-end engineering development.

[0070] Based on the first or second embodiment of this application, in the third embodiment of this application, the content that is the same as or similar to the first or second embodiment described above can be referred to the above description and will not be repeated hereafter. On this basis, the code generation method of this application may also include extended implementations such as background running startup, recursive generation of interface type definitions, code generation of configurable templates, and batch interface code generation, which will be described below.

[0071] In this embodiment, prior to step S10, the code generation method of this application further includes: Step F10: When the system starts, run the code generation program in the background with a hidden window and configure the code generation program to start automatically on boot. Step F20: Initialize the HTTP service listening port; Step F30: Start the file monitoring subsystem.

[0072] Hidden window mode refers to the code generation program running as a background process without displaying a main window after startup. Automatic startup on boot refers to the configuration where the code generation program starts automatically when the operating system boots. The HTTP (Hypertext Transfer Protocol) service listening port refers to the local network port that the code generation program initializes and listens for to receive file modification notifications.

[0073] The code generation program is built on the Electron framework (a development framework for building desktop applications) and adopts a desktop application architecture. Upon system startup, the code generation program configures itself to start automatically on boot and is set to a hidden state by calling Electron's App.setLoginItemSettings interface, running in the background as a hidden window without consuming foreground user resources. Subsequently, the code generation program initializes an HTTP service listening port to receive file modification notifications and simultaneously starts a file monitoring subsystem to achieve real-time monitoring of code files in the target directory.

[0074] For example, after a developer logs into the operating system, the code generation program automatically starts with the system and runs in the background in a hidden state, requiring no manual operation from the developer. After the program starts, it listens to a local preset port and simultaneously starts a file listening subsystem to monitor the front-end project source code directory in real time. Developers can automatically obtain the generation and completion of interface code without being aware of the program's existence during the coding process.

[0075] In this embodiment, after step D40 "generating type name based on interface name" described above, the code generation method of this application further includes: Step G10: Parse the request parameter structure and return parameter structure in the target interface definition information, and extract the attribute name, type, required field identifier and comment information; Step G20: Recursively generate interface (interface declaration) type definitions for nested object types; Step G30 generates the corresponding array type declaration for the array type.

[0076] The request parameter structure refers to the structured data in the target interface definition information used to describe request parameters, such as the `req_body_other` field in the YAPI interface documentation. The return parameter structure refers to the structured data in the target interface definition information used to describe return parameters, such as the `res_body` field in the YAPI interface documentation. Attribute names refer to the names of each parameter in the parameter structure. Required field identifiers are markers used to indicate whether a parameter is required. Comment information refers to descriptive text carried in the parameter structure to explain the meaning of the parameters. Interface type definitions refer to the interface declarations in TypeScript used to describe object structures. Nested object types refer to composite types in the parameter structure that contain sub-objects. Array types refer to collection types in the parameter structure composed of elements of the same type.

[0077] The code generation device parses the request parameter structure and return parameter structure in the target interface definition information, and iterates to extract the attribute name, type, required field identifier and comment information of each parameter. When the parameter type is a nested object type, the code generation device recursively generates an interface type definition for the nested object. When the parameter type is an array type, the code generation device generates the corresponding array type declaration, thereby realizing the automatic generation of type declarations under complex type structures.

[0078] For example, the request parameter structure of the interface contains a nested object property named userInfo, which in turn contains two properties: name and age. The code generation device recursively generates an interface type definition for userInfo and references this interface in the request parameter type. If the request parameter structure contains an array property named ids, the code generation device generates an array type declaration such as number[] or string[] for it.

[0079] In this embodiment, step D50, "reading a preset code template, filling the code template with the interface path, interface name, request function name, and type name to obtain the interface request code," may include: Step H10: Read the code template string in the configuration file. The code template string contains multiple placeholder variables. Step H20: Replace the corresponding placeholder variables in the code template string with the interface title, request function name, request type, return type, and interface path to obtain the interface request code.

[0080] The code template string refers to the template text stored in the configuration file used to generate the API request code. Placeholder variables are predefined markers in the code template string that are used to replace actual variable values, such as API title, request function name (reqName), request type (reqTypeValue), return type (resTypeValue), and API path (url).

[0081] The code generation device reads a code template string from a configuration file. This template string supports multiple placeholder variables, such as the interface title, request function name, request type, return type, and interface path. Then, the device uses JavaScript's `eval` function to replace the corresponding placeholder variables in the template string with the actual variable values ​​for the interface title, request function name, request parameter type name, return parameter type name, and interface path, thereby generating the final interface request code. Through configurable templates, the code generation device can adapt to the coding styles and framework requirements of different projects.

[0082] For example, the code template string configured in the configuration file contains placeholder variables {{title}}, {{reqName}}, {{reqTypeValue}}, {{resTypeValue}}, and {{url}}. The code generation device replaces these placeholder variables with the interface title, request function name, request parameter type name, return parameter type name, and interface path respectively to generate an interface request function that conforms to the project code specifications.

[0083] In this embodiment, when the original code contains interface keywords, the code generation method of this application further includes: Step I10: If the interface identifier information corresponds to the interface directory, call the preset interface documentation platform to obtain the identifier list of all interfaces in the interface directory; Step I20: For each interface in the identifier list, obtain its corresponding interface definition information and generate the interface request code; Step I30: After concatenating the generated multiple interface request codes, replace the content to be replaced in the original code.

[0084] The interface directory refers to the directory used for classifying and managing interfaces within the pre-defined interface documentation platform. The identifier list is the collection of identifiers for all interfaces under the interface directory.

[0085] When the interface identifier information corresponds to an interface directory, the code generation device calls a preset interface documentation platform to obtain a list of identifiers for all interfaces in that interface directory. For each interface in the identifier list, the code generation device obtains its corresponding interface definition information and generates the corresponding interface request code. Finally, the code generation device concatenates the generated multiple interface request codes and replaces the content to be replaced in the original code with the concatenated result, thereby realizing the generation of batch interface codes.

[0086] For example, the code file contains "yapi@cat_45", where "cat_" identifies the interface directory and "45" is the interface directory ID. The code generation device calls YAPI's / list_cat interface to obtain a list of IDs for all interfaces under directory 45, then calls the / interface / get interface one by one to obtain the definition information of each interface and generate the corresponding interface request code. Finally, the multiple interface request codes are concatenated and replaced with "yapi@cat_45".

[0087] This application embodiment further expands the implementation details of the code generation method through background silent operation, recursive generation of interface type definitions, configurable template code generation, and batch interface code generation. This enables the code generation device to adapt to the code specifications and complex type structures of different projects and supports batch interface scenarios, thereby further improving the efficiency of interface code writing.

[0088] This application also provides a code generation apparatus, please refer to... Figure 4 The code generation apparatus of this application includes: Listening module 10 is used to listen for modification events in code files to obtain real-time modification events, and to determine the original code in the code file based on the real-time modification events; Extraction module 20 is used to determine whether the original code contains preset interface keywords. If the original code contains interface keywords, it extracts the interface identifier information corresponding to the interface keywords in the original code and determines the target interface definition information corresponding to the interface identifier information from the preset interface document platform. The completion module 30 is used to generate interface request code based on the target interface definition information, and complete the interface request code into the original code to obtain the target code.

[0089] Optionally, the listening module 10 is also used for: Monitor all sub-files in the directory containing the main code file; When a modification is detected in any sub-file of the directory, the modification event of the sub-file is treated as a real-time modification event, and the code in the sub-file is treated as the original code.

[0090] Optionally, the extraction module 20 is also used for: The original code is matched using regular expressions based on the API keywords, and the matched strings are used as the content to be replaced. Extract a pre-defined length of characters from the content to be replaced after the API keyword, and use them as API identifier information.

[0091] Optionally, the extraction module 20 is also used for: Obtain login credentials for logging into the preset interface documentation platform; Based on the login credentials, a login authentication request is initiated to the preset interface documentation platform, and the system monitors whether the authentication credentials for successfully logging into the preset interface documentation platform are received. If an authentication credential confirming successful login to the preset interface documentation platform is received, an interface query request will be initiated to the preset interface documentation platform based on the authentication credential and interface identification information. Use the interface definition information corresponding to the interface identifier information in the preset interface documentation platform as the target interface definition information.

[0092] Optionally, the completion module 30 is also used for: Extract the part after the last path separator from the interface path and use it as the base name; Remove the separators from the base name and convert the first letter of each word obtained by splitting the base name by the separators to uppercase to get the interface name; Convert the first letter of the interface name to lowercase to get the request function name; Generate type names based on interface names; Read the preset code template, fill in the interface path, interface name, request function name and type name into the code template to obtain the interface request code; Replace the content to be replaced in the original code with the API request code to obtain the target code.

[0093] Optionally, the completion module 30 is also used for: Add a first preset suffix to the interface name to get the request parameter type name; Add a second preset suffix to the interface name to get the return parameter type name.

[0094] Optionally, the completion module 30 is also used for: The content that is replaced in the original code will be used as the replaced content. Preset start and end markers will be added to the start and end positions of the replaced content to identify that the replaced content is automatically generated interface code. Update the timestamp of the code file to trigger the code editor containing the code file to reload the code file.

[0095] The code generation apparatus provided in this application, employing the code generation method described in the above embodiments, can solve the technical problem of low efficiency in writing interface code. Compared with the prior art, the beneficial effects of the code generation apparatus provided in this application are the same as those of the code generation method described in the above embodiments, and other technical features in the code generation apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0096] This application provides a code generation device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the code generation method in Embodiment 1 above.

[0097] The following is for reference. Figure 5 It shows a schematic diagram of the structure of a code generation device suitable for implementing the embodiments of this application. Figure 5 The code generation device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.

[0098] like Figure 5 As shown, the code generation device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory 1002 or a program loaded from a storage device 1003 into a random access memory 1004. The random access memory 1004 also stores various programs and data required for the operation of the code generation device. The processing unit 1001, the read-only memory 1002, and the random access memory 1004 are interconnected via a bus 1005. An input / output interface 1006 is also connected to the bus. Typically, the following systems can be connected to the input / output interface 1006: input devices 1007 including, for example, a touchscreen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage devices 1003 including, for example, magnetic tape, hard disk, etc.; and communication devices 1009. The communication device 1009 allows the code generation device to communicate wirelessly or wiredly with other devices to exchange data. Although the figure shows code generation devices with various systems, it should be understood that implementation or possession of all the systems shown is not required. More or fewer systems may be implemented alternatively.

[0099] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from read-only memory 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.

[0100] The code generation device provided in this application, employing the code generation method described in the above embodiments, can solve the technical problem of low efficiency in writing interface code. Compared with the prior art, the beneficial effects of the code generation device provided in this application are the same as those of the code generation method described in the above embodiments, and other technical features of the code generation device are the same as those disclosed in the method of the previous embodiment, and will not be repeated here.

[0101] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.

[0102] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

[0103] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, the computer-readable program instructions being used to execute the code generation method in the above embodiments.

[0104] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: electrical connections having one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.

[0105] The aforementioned computer-readable storage medium may be included in the code generation device; or it may exist independently and not assembled into the code generation device.

[0106] The aforementioned computer-readable storage medium carries one or more programs. When the one or more programs are executed by the code generation device, the code generation device: listens for modification events of the code file to obtain real-time modification events, and determines the original code in the code file based on the real-time modification events; determines whether the original code contains preset interface keywords, and if the original code contains interface keywords, extracts the interface identifier information corresponding to the interface keywords from the original code, and determines the target interface definition information corresponding to the interface identifier information from a preset interface document platform; generates interface request code based on the target interface definition information, and completes the interface request code into the original code to obtain the target code.

[0107] Computer program code for performing the operations of this application can be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, and C++, and conventional procedural programming languages ​​such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0108] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0109] The modules described in the embodiments of this application can be implemented in software or hardware. The names of the modules do not necessarily limit the functionality of the unit itself.

[0110] The readable storage medium provided in this application is a computer-readable storage medium that stores computer-readable program instructions (i.e., a computer program) for executing the above-described code generation method, thereby solving the technical problem of low efficiency in writing interface code. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the code generation method provided in the above embodiments, and will not be repeated here.

[0111] This application provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the code generation method described above.

[0112] The computer program product provided in this application can solve the technical problem of low efficiency in writing interface code. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as the beneficial effects of the code generation method provided in the above embodiments, and will not be repeated here.

[0113] The above are merely preferred embodiments of this application and do not limit the patent scope of this application. Any equivalent structural or procedural transformations made using the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent scope of this application.

Claims

1. A code generation method, characterized in that, The code generation method includes: Listen for modification events in the code file to obtain real-time modification events, and determine the original code in the code file based on the real-time modification events; Determine whether the original code contains a preset interface keyword. If the original code contains the interface keyword, extract the interface identifier information corresponding to the interface keyword from the original code, and determine the target interface definition information corresponding to the interface identifier information from a preset interface document platform. Based on the target interface definition information, an interface request code is generated, and the interface request code is then added to the original code to obtain the target code.

2. The code generation method as described in claim 1, characterized in that, The steps of monitoring modification events in the code file to obtain real-time modification events, and determining the original code in the code file based on the real-time modification events, include: Monitor each sub-code file in the directory where the specified code file is located; When a modification is detected in any of the sub-code files under the directory, the modification event of the sub-code file is taken as the real-time modification event, and the code in the sub-code file is taken as the original code.

3. The code generation method as described in claim 1, characterized in that, The step of extracting the interface identifier information corresponding to the interface keyword in the original code includes: The original code is matched using regular expressions based on the interface keywords, and the matched strings are used as the content to be replaced. Extract a preset length of characters from the content to be replaced after the interface keyword, and use it as the interface identification information.

4. The code generation method as described in claim 1, characterized in that, The step of determining the target interface definition information corresponding to the interface identifier information from the preset interface document platform includes: Obtain login credentials for logging into the preset interface document platform; Based on the login credentials, a login authentication request is initiated to the preset interface document platform, and the system monitors whether an authentication credential for successfully logging into the preset interface document platform is received. If an authentication credential for successful login to the preset interface documentation platform is received, an interface query request is initiated to the preset interface documentation platform based on the authentication credential and the interface identification information; The interface definition information corresponding to the interface identifier information in the preset interface documentation platform is used as the target interface definition information.

5. The code generation method as described in claim 3, characterized in that, The target interface definition information includes request parameter fields, return parameter fields, and interface path. The step of generating interface request code based on the target interface definition information and completing the interface request code into the original code to obtain the target code includes: Extract the portion following the last path separator from the interface path and use it as the base name; Remove the separators from the base name and convert the first letter of each word obtained by splitting the base name by the separators to uppercase to obtain the interface name; Convert the first letter of the interface name to lowercase to obtain the request function name; Generate a type name based on the interface name; Read the preset code template, and fill the code template with the interface path, the interface name, the request function name and the type name to obtain the interface request code; The target code is obtained by replacing the content to be replaced in the original code with the interface request code.

6. The code generation method as described in claim 5, characterized in that, The type name includes the request parameter type name and the return parameter type name. The step of generating the type name based on the interface name includes: Add a first preset suffix to the interface name to obtain the request parameter type name; Add a second preset suffix to the interface name to obtain the return parameter type name.

7. The code generation method as described in claim 5, characterized in that, After the step of completing the interface request code into the original code, the method further includes: The content that is replaced in the original code is taken as the replaced content. Preset start and end markers are added to the start and end positions of the replaced content to identify that the replaced content is automatically generated interface code. Update the timestamp of the code file to trigger the code editor containing the code file to reload the code file.

8. A code generation device, characterized in that, The code generation device includes: The monitoring module is used to monitor modification events of code files to obtain real-time modification events, and to determine the original code in the code file based on the real-time modification events; The extraction module is used to determine whether the original code contains preset interface keywords. If the original code contains the interface keywords, the module extracts the interface identifier information corresponding to the interface keywords in the original code and determines the target interface definition information corresponding to the interface identifier information from the preset interface document platform. The completion module is used to generate interface request code based on the target interface definition information, and to complete the interface request code into the original code to obtain the target code.

9. A code generation device, characterized in that, The device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the code generation method as described in any one of claims 1 to 7.

10. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, it implements the steps of the code generation method as described in any one of claims 1 to 7.