Request function generation method, device, equipment and storage medium
By generating type declarations and interface configuration files, type-safe request functions are automatically created, which solves the problems of low efficiency and poor type safety for front-end developers to manually write request functions, and improves the efficiency and maintainability of software development.
Patent Information
- Application Number
- CN202211407033.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-10
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2042-11-10
AI Technical Summary
During the software development process, front-end developers need to manually write request functions for calling back-end interfaces, which is inefficient and prone to type safety issues, affecting code readability and subsequent operation and maintenance.
By obtaining the interface description file, the type declaration file and the interface configuration file are generated, and the request function is automatically generated based on these files to ensure that the request function refers to the type declaration and achieve type safety.
It improves the efficiency of request function generation, avoids manual writing errors, ensures type safety and code readability, and simplifies subsequent operation and maintenance.
Smart Images

Figure CN116028028B
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of data processing technology, in particular to the field of software technology, and further to a request function generation method, device, equipment and storage medium. Background Art
[0002] In the current software development process, a front-end and back-end separation development method is often adopted, that is, back-end developers write back-end programs, and front-end developers write front-end programs. The front-end program contains a request function for calling the back-end program. Summary of the invention
[0003] The present disclosure provides a request function generation method, apparatus, device and storage medium.
[0004] According to one aspect of the present disclosure, a request function generation method is provided, comprising:
[0005] Obtain an interface description file for describing an interface;
[0006] Generate a type declaration file of the interface according to the interface definition information of the interface recorded in the interface description file and a preset type template file;
[0007] Generate an interface configuration file of the target interface according to the information storage path of the target interface recorded in the interface description file and a preset interface configuration template;
[0008] Determining a target type declaration to be referenced from a type declaration file of the target interface based on a request data field and a response information field included in an interface configuration file of the target interface;
[0009] A request function of the target interface is generated according to an interface configuration file of the target interface and a preset request function template, wherein the request function of the target interface references a target type declaration of the target interface.
[0010] According to another aspect of the present disclosure, there is provided a request function generating device, comprising:
[0011] A file acquisition module, used to acquire an interface description file used to describe an interface;
[0012] A declaration file generating module, used to generate a type declaration file of an interface according to the interface definition information of the interface recorded in the interface description file and a preset type template file;
[0013] A configuration file generating module, used to generate an interface configuration file of the target interface according to the information storage path of the target interface recorded in the interface description file and a preset interface configuration template;
[0014] A type declaration determining module, configured to determine a target type declaration to be referenced from a type declaration file of the target interface based on a request data field and a response information field contained in an interface configuration file of the target interface;
[0015] The request function generation module is used to generate the request function of the target interface according to the interface configuration file of the target interface and a preset request function template, wherein the request function of the target interface references the target type declaration of the target interface.
[0016] According to another aspect of the present disclosure, there is provided an electronic device, comprising:
[0017] at least one processor; and
[0018] a memory communicatively connected to the at least one processor; wherein,
[0019] The memory stores instructions that can be executed 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 any method according to the first aspect.
[0020] According to another aspect of the present disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to cause the computer to execute the method according to any one of the first aspect.
[0021] According to another aspect of the present disclosure, a computer program product is provided, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements the method according to any one of the first aspects.
[0022] It can be seen from the above that the solution provided by the embodiment of the present disclosure can directly and conveniently generate a request function for calling an interface based on an interface description file, and the generated request function references a type declaration, thereby ensuring the type safety of the request function.
[0023] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.
[0025] Figure 1 A flowchart of a first request function generation method provided in an embodiment of the present disclosure;
[0026] Figure 2A schematic diagram of a swagger file reading process provided in an embodiment of the present disclosure;
[0027] Figure 3 A flowchart of a second request function generation method provided in an embodiment of the present disclosure;
[0028] Figure 4 A flowchart of a third method for generating a request file provided in an embodiment of the present disclosure;
[0029] Figure 5 A flowchart of a fourth request file generation method provided in an embodiment of the present disclosure;
[0030] Figure 6 A flowchart of a fifth request file generation method provided in an embodiment of the present disclosure;
[0031] Figure 7 A schematic diagram of a flow chart for generating a type declaration file provided in an embodiment of the present disclosure;
[0032] Figure 8 A schematic diagram of a software architecture for generating a type declaration file provided in an embodiment of the present disclosure;
[0033] Fig. 9 A schematic diagram of the structure of a first request function generating device provided in an embodiment of the present disclosure;
[0034] Fig.10 A schematic diagram of the structure of a second request function generating device provided in an embodiment of the present disclosure;
[0035] Fig.11 A schematic diagram of the structure of a third request function generating device provided in an embodiment of the present disclosure;
[0036] Fig.12 A schematic diagram of the structure of a fourth request function generating device provided in an embodiment of the present disclosure;
[0037] Fig.13 It is a block diagram of an electronic device used to implement the request function generation method of the embodiment of the present disclosure. DETAILED DESCRIPTION
[0038] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0039] First, the application scenarios of the present disclosure are described:
[0040] After the back-end developer completes the back-end program writing, the interface for generating interface description files can be used to generate an interface description file. The above interface description file can be a swagger file, which is an interactive interface document. The interface description file records the interface information of each interface written by the back-end developer. The front-end developer can determine the interface information of each interface by viewing the above interface description file, and manually write a request function for calling the above interface when writing the front-end program, but the efficiency of manually writing request functions is low.
[0041] The above-mentioned interface description file can respectively record the interface definition information and information storage path of each interface. The information storage path of each interface is the storage path of the interface information of the interface. The storage path records the configuration information of the request data and the configuration information of the response data of the interface. The above-mentioned interface definition information can be called definitions information, and the information storage path can be called paths information.
[0042] The above definitions information includes the name information, type information, properties information, etc. of the interface. The above properties information records the name information, type information, description information, etc. of the request data of the interface. The above properties information also records the name information, type information, description information, etc. of the response data of the interface.
[0043] The paths information records the path of the interface, the name of the interface, the definition of the request data of the interface, and the definition of the response data of the interface.
[0044] Specifically, the definition of the request data includes the type of the request data, and the definition of the response data includes the type of the response data.
[0045] Each parameters (range) field contained in the paths field contained in the above interface description file corresponds to a request data of the interface, the value of the schema (synopsis) field contained in the parameters field indicates request data with type constraints, each response (response) field contained in the paths field corresponds to a response data of the interface, and the value of the schema field in the "200" state in the response field indicates response data with type constraints.
[0046] The request function generation method provided by the embodiment of the present disclosure is described below.
[0047] See also Figure 1 , which is a flow chart of a first request function generation method provided in an embodiment of the present disclosure, the method includes the following steps S101-S105.
[0048] S101: Acquire an interface description file for describing an interface.
[0049] In one embodiment of the present disclosure, the interface description file may be obtained through the following step A or step B.
[0050] Step A: Acquire the above interface description file based on the access address of the interface description file.
[0051] Specifically, the user can input a command line to control the electronic device to obtain an interface description file, where the interface description file can be a swagger file. The user can input the -s or -swagger command to control the electronic device to import the above swagger file based on the access address. The command line parameter is the swagger json url (swagger JavaScript ObjectNotation uniform resource locator, swagger JavaScript object notation uniform resource locator) address, which is the access address of the swagger file.
[0052] The electronic device can download the interface description file through an http (hypertext transfer protocol) software module based on the access address.
[0053] Step B: Read the interface description file stored in the preset project directory.
[0054] In one embodiment of the present disclosure, the interface description file is stored in the preset project directory, and the user can use the command line to input a -file command to control the electronic device to read the interface description file from the project directory.
[0055] It can be seen that the solution provided by the embodiment of the present disclosure can obtain the interface description file in two different ways. The way to obtain the interface description file is relatively flexible, and the user can choose different ways to obtain the interface description file.
[0056] Specifically, the above interface description file is a swagger file. The process of obtaining the swagger file can be seen below. Figure 2 The illustrated embodiment will not be described in detail here.
[0057] S102: Generate an interface type declaration file according to the interface definition information of the interface recorded in the above interface description file and a preset type template file.
[0058] In one embodiment of the present disclosure, the interface description file can be read and the information recorded in the interface description file can be traversed to obtain the interface definition information of each interface. And for each interface, the embodiment of the present disclosure can generate a type declaration file of the interface based on the interface definition information of the interface through step S102.
[0059] Specifically, the electronic device can read the interface description file with the help of a file software module for reading files, and convert the interface description file into a JSON document stream. Then, the JSON document stream is traversed to search for the keywords "definitions" and "paths" to obtain the definitions information and paths information of each interface.
[0060] In addition, the above-mentioned preset type template file may be written based on the page template library Handlebars.js, and the above-mentioned type template file is a file in the hbs format.
[0061] In one embodiment of the present disclosure, the type template file records the fields contained in the type declaration file to be generated, and the information recorded in the definitions information is used as the value of the field contained in the type template file to obtain the type declaration file. The type declaration file is a file in ts format and can be named type.d.ts.
[0062] When generating the above type declaration file, the above type declaration file may be written into a preset type declaration file directory.
[0063] In addition, the above type declaration file contains various interface fields, each interface field corresponds to an interface, and the above interface field records the interface name and the interface attribute description. The above attribute description includes the name and data type of the interface request data and the name and data type of the response data.
[0064] In one embodiment of the present disclosure, the above step S102 can be implemented by following steps C-D.
[0065] Step C: determining the target interface name and target attribute description contained in the interface definition information of the interface recorded in the above interface description file;
[0066] Step D: Generate an interface type declaration file according to the interface definition information of the interface recorded in the above interface description file and the preset type template file.
[0067] Among them, the interface name recorded in the interface type declaration file is the above-mentioned target interface name, and the recorded property description is the above-mentioned target property description. The above-mentioned target interface name is: the information contained in the name field recorded in the definitions information of the interface, and the above-mentioned target property description is: the information contained in the properties field recorded in the definitions information of the interface.
[0068] It can be seen that, through the solution provided by the embodiment of the present disclosure, a detailed type declaration file recording the interface name and property description can be generated directly based on the definitions information of the interface without manual operation.
[0069] S103: Generate an interface configuration file of the target interface according to the information storage path of the target interface recorded in the interface description file and a preset interface configuration template.
[0070] Among them, the above interface configuration template can be called an apiconfig template.
[0071] In one embodiment of the present disclosure, the above-mentioned preset apiconfig template includes fields contained in the interface configuration file to be generated. The above-mentioned interface configuration file can be called an apiconfig file. The information recorded in the above-mentioned paths information is used as the value of the field contained in the apiconfig file to obtain the above-mentioned apiconfig file.
[0072] Specifically, the apiconfig file includes a request field for recording the definition of the request data of the interface, wherein the bodyDefinition field included in the request field indicates the request data with type constraints. The apiconfig file also includes a response field for recording the definition of the response data of the interface, wherein the definitions field included in the response field indicates the response data with type constraints.
[0073] In another embodiment of the present disclosure, all paths information contained in the above interface description file can be traversed, each path information corresponds to an interface, all interfaces corresponding to each path information are used as target interfaces, and apiconfig files of each target interface are generated based on each path information.
[0074] In another embodiment of the present disclosure, the above step S103 can be implemented by following steps E-F.
[0075] Step E: Receive path parameters input by the user.
[0076] Step F: Based on the information storage path of the target interface indicated by the path parameter, the value of each parameter in the preset interface configuration template is determined, and an interface configuration file of the target interface is generated.
[0077] Specifically, the above path parameter can be called a path parameter, and the path parameter can indicate one or more paths information. The interface corresponding to the paths information indicated by the path parameter is used as the target interface, and only the interface configuration file of the target interface corresponding to the paths information indicated by the path parameter is generated.
[0078] It can be seen that in the solution provided by the embodiment of the present disclosure, the electronic device can determine the target interface for which the apiconfig file needs to be generated indicated by the user based on the path parameters input by the user, so that only the paths information of the specific target interface indicated by the user can be processed.
[0079] S104: Based on the request data field and the response information field included in the interface configuration file of the target interface, determine the target type declaration to be referenced from the type declaration file of the target interface.
[0080] In one embodiment of the present disclosure, the above-mentioned request data field may be the bodyDefinition field contained in the above-mentioned request field, and the request data recorded in the bodyDefinition field is determined as request data requiring a reference type declaration; the above-mentioned response information field may be the definition field contained in the above-mentioned response field, and the response data recorded in the definition field is determined as request data requiring a reference type declaration.
[0081] In addition, the data type of the request data can be determined from the type declaration file based on the name of the request data recorded in the bodyDefinition field, thereby determining the type declaration of the request data; and the data type of the response data can be determined from the type declaration file based on the name of the response data recorded in the definition field, thereby determining the type declaration of the response data.
[0082] S105: Generate a request function of the target interface according to the interface configuration file of the target interface and a preset request function template.
[0083] The request function of the target interface refers to the target type declaration of the target interface. The preset request function template may be written based on the page template library Handlebars.js. The request function template is a file in hbs format.
[0084] In one embodiment of the present disclosure, the fields included in the request function to be generated are recorded in the request function template, and the information recorded in the apiconfig file is used as the value of the field included in the request function template to obtain the request function. The request function is a file in ts format and can be named api / request.ts.
[0085] In addition, the generated request function may be stored in a preset target path.
[0086] The detailed process of generating request functions can be found below Figure 3 The illustrated embodiment will not be described in detail here.
[0087] In another embodiment of the present disclosure, the above step S105 can be implemented through steps G to H, which will not be described in detail here.
[0088] As can be seen from the above, the solution provided by the embodiment of the present disclosure can directly and conveniently generate a request function for calling an interface based on an interface description file, without the need for manual writing of the request function, thereby saving human resources and avoiding the problem of writing errors caused by manual writing of the request function. In addition, in order to save writing time when writing manually, people tend not to limit the types of request data and response data, resulting in type safety issues in the interface, and the code readability is low, thus affecting subsequent operation and maintenance. However, the request function generated by the solution provided by the embodiment of the present disclosure includes a type declaration, thereby ensuring the type safety of the request function, improving the readability of the code, and facilitating subsequent operation and maintenance.
[0089] In one embodiment of the present disclosure, the above step S105 can be implemented through steps G to H.
[0090] Step G: Determine the interface name and interface path recorded in the interface configuration file of the target interface.
[0091] Step H: According to the interface configuration file of the target interface and the preset request function template, a request function whose name of the target interface is the interface name and whose path is the interface path is generated.
[0092] In one embodiment of the present disclosure, the interface name recorded in the apiconfig file is the name of the interface recorded at the storage path indicated by the paths information used when generating the apiconfig file. When generating a request function, the interface name recorded in the apiconfig file is used as the name of the request function, which is equivalent to using the name of the interface recorded at the storage path indicated by the paths information in the interface description file as the name of the request function.
[0093] In another embodiment of the present disclosure, the path recorded in the above-mentioned apiconfig file is the path of the interface recorded at the storage path indicated by the paths information used when generating the apiconfig file. When generating a request function, the path recorded in the apiconfig file is used as the path of the request function, which is equivalent to using the path of the interface recorded at the storage path indicated by the paths information in the interface description file as the path of the request function.
[0094] It can be seen that the name of the request function generated by the solution provided by the embodiment of the present disclosure can be consistent with the name of the interface recorded in the interface description file. The path of the request function generated by the solution provided by the embodiment of the present disclosure can also be consistent with the path of the interface recorded in the interface description file.
[0095] In another embodiment of the present disclosure, the request function may also be generated through the following steps I-J.
[0096] Step I: Get the interface name input by the user.
[0097] Specifically, the user can call the formatter function to input the interface name.
[0098] Step J: Generate a request function whose name is the interface name input by the user according to the interface configuration file of the target interface and a preset request function template.
[0099] Compared with the aforementioned steps G to H, the name of the request function generated by the embodiment of the present disclosure is the interface name input by the user.
[0100] It can be seen that the solution provided by the embodiment of the present disclosure supports users to customize the name of the request function so that the generated request function matches the user's needs.
[0101] See also Figure 2 , which is a flow chart of a swagger file reading process provided in an embodiment of the present disclosure, including the following steps S201-S206.
[0102] S201: Determine whether the current electronic device provides swagger configuration.
[0103] If the swagger configuration is not provided, the swagger file cannot be read and an exception is thrown, otherwise step S202 is executed.
[0104] S202: Determine whether to obtain a swagger file based on the URL.
[0105] If yes, you need to access the URL to obtain the swagger file and execute step S203. Otherwise, you can read the stored swagger file from the preset project directory and execute step S205.
[0106] S203: Establish an http link based on the url.
[0107] S204: Download the swagger file through the established http link.
[0108] S205: Read the swagger file from the project directory.
[0109] S206: Convert the swagger file into a json document stream.
[0110] See also Figure 3 , which is a flow chart of the second request function generation method provided in an embodiment of the present disclosure.
[0111] As can be seen from the figure, in the process of generating the request function, the swagger json document stream is first obtained;
[0112] After that, traverse and obtain each path information in the swagger json document stream, or only obtain the path information indicated by the path parameter entered by the user;
[0113] Generate apiconfig file based on apiconfig template;
[0114] Traverse each item in the apiconfig file, parse the request.bodyDefinition field and parse the response.definition field to determine the type declaration that needs to be referenced;
[0115] Parse the request function template request.hbs;
[0116] Generate a request function and store it in the preset target path.
[0117] The apiconfig file, target path, and request.hbs outlined in the dotted box in the figure represent the information required in the process of generating the request function.
[0118] See also Figure 4 , is a flow chart of a third method for generating a request file provided in an embodiment of the present disclosure, which is similar to the aforementioned Figure 1 Compared with the embodiment shown, the following step S106 is included before step S105, and S105 can be implemented by the following step S105A.
[0119] S106: Generate a basic request function for intercepting user requests and / or intercepting responses.
[0120] Specifically, the basic request function can be used to uniformly implement functions such as request interception and response interception. The basic request function can be an axios request file, and the generated basic request function can be stored in the same path as the request function. The basic request function is a file in ts format and can be named api / request.ts.
[0121] In one embodiment of the present disclosure, the above step S106 can be executed by default, or it can be determined based on the -instance parameter passed in by the user whether to execute step S106. If the value of the above -instance parameter is true (yes), step S106 is executed; if the value of the above -instance parameter is false (no), step S106 is not executed.
[0122] S105A: Generate a request function of the target interface for calling the basic request function according to the interface configuration file of the target interface and a preset request function template.
[0123] Specifically, compared with the request function generated in the aforementioned step S105, the request function generated in the aforementioned step S105A references a basic request function for intercepting user requests and / or intercepting responses, so that the request function can call the referenced basic request function to intercept user requests and / or responses during operation.
[0124] As can be seen from the above, the request function generated by the solution provided by the embodiment of the present disclosure can call the basic request function to implement the interception of user requests and / or the interception of responses, so that developers do not need to manually write programs for implementing user request interception and / or response interception, thereby improving development efficiency. In particular, in the case of generating request functions of multiple interfaces, the multiple request functions generated can uniformly reference the above-mentioned basic request function, and by generating a basic request function, each request function can implement user request interception and / or response interception.
[0125] See also Figure 5 , is a flow chart of a fourth method for generating a request file provided in an embodiment of the present disclosure, which is similar to the aforementioned Figure 1 Compared with the embodiment shown, the following step S107 is also included after step S105.
[0126] S107: Generate an index file for introducing and exposing the request function.
[0127] Specifically, the index file is used to introduce and expose the generated request function so that other projects can directly call the request function. The index file can be a file in ts format and can be named api / index.ts.
[0128] In one embodiment of the present disclosure, the above step S107 can be executed by default, or it can be determined whether to execute step S107 based on the -index parameter passed in by the user. If the value of the above -index parameter is true, step S107 is executed, and if the value of the above -index parameter is false, step S107 is not executed.
[0129] As can be seen from the above, the solution provided by the embodiment of the present disclosure introduces and exposes the generated request function by generating an index file, so that other projects can easily call the above request function, which is convenient for users to write code files that call the above request function.
[0130] See also Figure 6 , is a flowchart of a fifth method for generating a request file provided in an embodiment of the present disclosure, which is similar to the aforementioned Figure 1 Compared with the embodiment shown, before the above step S102, at least one of the following steps S108-S110 is performed for the interface definition information of the interface recorded in the above interface description file.
[0131] S108: According to a preset character conversion relationship, characters not supported by the front-end language and included in the interface definition information are converted into characters supported by the front-end language.
[0132] Specifically, if the definitions information contains characters that are not supported by the front-end language, when a type declaration file is generated directly based on the definitions information, the type declaration file may contain characters that are not supported by the front-end language, causing the generated type declaration file to fail to run normally. Therefore, step S108 may be first performed to convert the characters that are not supported by the front-end language contained in the definitions information.
[0133] In one embodiment of the present disclosure, each character in the above definitions information can be traversed to find the characters not supported by the front-end language recorded in the above character conversion relationship, and then based on the above character conversion relationship, the found characters can be replaced with the corresponding characters supported by the front-end language.
[0134] For example, the character that the front-end language does not support is “《”, and the character that the front-end language supports corresponding to “《” in the above character conversion relationship is “<”. The character that the front-end language does not support is “》”, and the character that the front-end language supports corresponding to “》” in the above character conversion relationship is ">”.
[0135] S109: Convert the characters included in the interface definition information into characters of a preset language supported by the front-end language.
[0136] The above interface definition information may contain characters of a language not supported by the front-end language. In this case, such characters can be converted into characters of a preset language supported by the front-end language, and then a type declaration file can be generated so that the type declaration file does not contain characters of a language not supported by the front-end language, thereby avoiding the problem that the type declaration file cannot run.
[0137] For example, the languages not supported by the front-end language may be Chinese, Korean, Japanese, etc., and the default language may be English.
[0138] In one embodiment of the present disclosure, a translation program may be called to translate characters into characters of a preset language, or when Chinese characters are included in definitions information and the preset language is English, Chinese characters may be converted into pinyin based on a pinyin conversion program.
[0139] S110: Convert the data type of the parameter included in the interface definition information according to a preset data type conversion relationship.
[0140] Specifically, since the front-end language and the back-end language have different representation forms for some data types, some data types included in the interface definition information may be converted before generating the type declaration file used by the front-end.
[0141] For example, the front-end language and the back-end language have different representations of generics. Therefore, if the data type of the generic parameter contained in the definitions information is not converted, the data type of the generic parameter recorded in the subsequently generated type declaration file cannot be successfully read, causing the type declaration file to fail to run. To this end, the data type of the generic parameter contained in the definitions information can be converted to the T type that the front-end language can handle.
[0142] From the above, it can be seen that the solution provided by the embodiment of the present disclosure can first convert the characters not supported by the front-end language contained in the definitions information into characters supported by the front-end language, so that the type declaration file subsequently generated based on the definitions information does not contain characters not supported by the front-end language, so as to avoid the failure of the type declaration file to run.
[0143] In addition, the solution provided by the embodiment of the present disclosure can also first convert the characters contained in the front-end language contained in the definitions information into characters of the language supported by the front-end language, so that the type declaration file subsequently generated based on the definitions information only contains language characters supported by the front-end language, so as to avoid the failure of the type declaration file to run.
[0144] Furthermore, in the solution provided in the embodiment of the present disclosure, the data types of the parameters contained in the definitions information are converted, so that the type declaration file generated based on the definitions information after the data type conversion can be successfully executed.
[0145] See also Figure 7 , which is a flow chart of generating a type declaration file provided in an embodiment of the present disclosure.
[0146] As can be seen from the figure, in the process of generating the type declaration file, the swagger file is first obtained;
[0147] Traverse the definitions information in the swagger file;
[0148] Generate definitions information of fixed structures and perform generic processing;
[0149] Specifically, generating the definitions information of the fixed structure is to process the definitions information based on at least one of the aforementioned steps S108 - S110 .
[0150] After that, the type template file is parsed, and the parameters contained in the definitions information are written into the type template file;
[0151] Output type declaration file.
[0152] The definitions, directory of type declaration files and type template files in the dotted box in the figure are the information used in the process of generating type declaration files. Among them, definitions represent definitions of fixed structures after generic processing, and the directory of type declaration files is the storage path of the generated type declaration files.
[0153] See also Figure 8 , which is a schematic diagram of a software architecture for generating a type declaration file provided in an embodiment of the present disclosure.
[0154] The above software architecture includes four parts: dependencies, command line parameters, technical solution process and engineering.
[0155] The above dependencies include gulp-related dependencies for packaging programs, bable and typescript-related dependencies for compiling programs, jest dependencies for program testing, and types-related dependencies. It also includes commander dependencies, handlebars dependencies for writing templates, fs-extra dependencies for file management, and pinyin dependencies for converting Chinese characters to pinyin.
[0156] Exposing package.json / bin means exposing dependencies and calling this solution, and then writing a command line tool CLI (Command-Line Interface).
[0157] The written command lines include the -formatter command line for obtaining the interface name input by the user, the -s or -swagger command line for controlling the electronic device to obtain the swagger file based on the URL, the -index command line for controlling whether to generate an index file, the -instance command line for controlling whether to generate a basic request function, the -d or -dir command line for obtaining the storage path of the request function input by the user, the -file command line for reading the swagger file stored in the project directory, the -p or -path command line for obtaining the path parameter input by the user, the -template command line for calling the template, and the -override command line for overriding the function.
[0158] After calling the tool through the command line, you can read the swagger file, create request functions and related files (such as basic request functions and index files), and create type declaration files.
[0159] Then, the generated request function and other files are engineered, including jest unit testing, bable compilation, gulp packaging, and npm publishing. The engineering process can call the aforementioned dependencies.
[0160] Corresponding to the above-mentioned request function generation method, the embodiment of the present disclosure also provides a request function generation device.
[0161] See also Fig. 9 , is a structural diagram of a first request function generating device provided by an embodiment of the present disclosure, the device comprising:
[0162] A file acquisition module 901 is used to acquire an interface description file for describing an interface;
[0163] A declaration file generating module 902, configured to generate a type declaration file of an interface according to the interface definition information of the interface recorded in the interface description file and a preset type template file;
[0164] The configuration file generating module 903 is used to generate an interface configuration file of the target interface according to the information storage path of the target interface recorded in the interface description file and a preset interface configuration template;
[0165] A type declaration determining module 904 is used to determine a target type declaration to be referenced from a type declaration file of the target interface based on a request data field and a response information field contained in an interface configuration file of the target interface;
[0166] The request function generation module 905 is used to generate a request function of the target interface according to the interface configuration file of the target interface and a preset request function template, wherein the request function of the target interface references the target type declaration of the target interface.
[0167] As can be seen from the above, the solution provided by the embodiment of the present disclosure can directly and conveniently generate a request function for calling an interface based on an interface description file, without the need for manual writing of the request function, thereby saving human resources and avoiding the problem of writing errors caused by manual writing of the request function. In addition, in order to save writing time when writing manually, people tend not to limit the types of request data and response data, resulting in type safety issues in the interface, and the code readability is low, thus affecting subsequent operation and maintenance. However, the request function generated by the solution provided by the embodiment of the present disclosure includes a type declaration, thereby ensuring the type safety of the request function, improving the readability of the code, and facilitating subsequent operation and maintenance.
[0168] See also Fig.10 , which is a schematic diagram of the structure of a second request function generating device provided in an embodiment of the present disclosure, and is similar to the aforementioned Fig. 9 Compared with the embodiment shown, it also includes:
[0169] A basic request function generating module 906, used to generate a basic request function for intercepting user requests and / or intercepting responses;
[0170] The request function generation module 905 is specifically used for:
[0171] A request function of the target interface for calling the basic request function is generated according to the interface configuration file of the target interface and a preset request function template.
[0172] As can be seen from the above, the request function generated by the solution provided by the embodiment of the present disclosure can call the basic request function to implement the interception of user requests and / or the interception of responses, so that developers do not need to manually write programs for implementing user request interception and / or response interception, thereby improving development efficiency. In particular, in the case of generating request functions of multiple interfaces, the multiple request functions generated can uniformly reference the above-mentioned basic request function, and by generating a basic request function, each request function can implement user request interception and / or response interception.
[0173] See also Fig.11 , is a schematic diagram of the structure of a third request function generating device provided in an embodiment of the present disclosure, which is similar to the aforementioned Fig. 9 Compared with the embodiment shown, it also includes:
[0174] The index file generating module 907 is used to generate an index file for introducing and exposing the request function.
[0175] As can be seen from the above, the solution provided by the embodiment of the present disclosure introduces and exposes the generated request function by generating an index file, so that other projects can easily call the above request function, which is convenient for users to write code files that call the above request function.
[0176] In one embodiment of the present disclosure, the configuration file generating module 903 is specifically used to:
[0177] Receive path parameters entered by the user;
[0178] Based on the information storage path of the target interface indicated by the path parameter, the value of each parameter in the preset interface configuration template is determined, and the interface configuration file of the target interface is generated.
[0179] It can be seen that in the solution provided by the embodiment of the present disclosure, the electronic device can determine the target interface for which the apiconfig file needs to be generated indicated by the user based on the path parameters input by the user, so that only the paths information of the specific target interface indicated by the user can be processed.
[0180] In one embodiment of the present disclosure, the file acquisition module 901 is specifically used to:
[0181] Acquire the interface description file based on the access address of the interface description file;
[0182] or
[0183] Read the interface description file stored in the preset project directory.
[0184] It can be seen that the solution provided by the embodiment of the present disclosure can obtain the interface description file in two different ways. The way to obtain the interface description file is relatively flexible, and the user can choose different ways to obtain the interface description file.
[0185] In one embodiment of the present disclosure, the request function generation module 905 is specifically used to:
[0186] Determine the interface name and interface path recorded in the interface configuration file of the target interface;
[0187] According to the interface configuration file of the target interface and the preset request function template, a request function of the target interface is generated, the name of which is the interface name and the path of which is the interface path.
[0188] It can be seen that the name of the request function generated by the solution provided by the embodiment of the present disclosure can be consistent with the name of the interface recorded in the interface description file. The path of the request function generated by the solution provided by the embodiment of the present disclosure can also be consistent with the path of the interface recorded in the interface description file.
[0189] In one embodiment of the present disclosure, the declaration file generation module 902 is specifically used to:
[0190] Determine the target interface name and target attribute description included in the interface definition information of the interface recorded in the interface description file;
[0191] Generate an interface type declaration file based on the interface definition information of the interface recorded in the interface description file and a preset type template file, wherein the interface name recorded in the interface type declaration file is the target interface name, and the attribute description recorded is the target attribute description.
[0192] It can be seen that, through the solution provided by the embodiment of the present disclosure, a detailed type declaration file recording the interface name and property description can be generated directly based on the definitions information of the interface without manual operation.
[0193] See also Fig.12 , which is a schematic diagram of the structure of the fourth request function generating device provided in the embodiment of the present disclosure, and is similar to the aforementioned Fig. 9 Compared to the embodiment shown, the device further comprises:
[0194] The information conversion module 908 is used to perform at least one of the following steps for the interface definition information of the interface recorded in the interface description file:
[0195] According to a preset character conversion relationship, the characters not supported by the front-end language contained in the interface definition information are converted into characters supported by the front-end language;
[0196] Convert characters contained in the interface definition information into characters of a preset language supported by the front-end language;
[0197] According to the preset data type conversion relationship, the data type of the parameter included in the interface definition information is converted.
[0198] From the above, it can be seen that the solution provided by the embodiment of the present disclosure can first convert the characters not supported by the front-end language contained in the definitions information into characters supported by the front-end language, so that the type declaration file subsequently generated based on the definitions information does not contain characters not supported by the front-end language, so as to avoid the failure of the type declaration file to run.
[0199] In addition, the solution provided by the embodiment of the present disclosure can also first convert the characters contained in the front-end language contained in the definitions information into characters of the language supported by the front-end language, so that the type declaration file subsequently generated based on the definitions information only contains language characters supported by the front-end language, so as to avoid the failure of the type declaration file to run.
[0200] Furthermore, in the solution provided in the embodiment of the present disclosure, the data types of the parameters contained in the definitions information are converted, so that the type declaration file generated based on the definitions information after the data type conversion can be successfully executed.
[0201] In one embodiment of the present disclosure, the request function generation module 905 is specifically used to:
[0202] Get the interface name entered by the user;
[0203] A request function whose name of the target interface is the interface name input by the user is generated according to the interface configuration file of the target interface and a preset request function template.
[0204] It can be seen that the solution provided by the embodiment of the present disclosure supports users to customize the name of the request function so that the generated request function matches the user's needs.
[0205] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.
[0206] Fig.13A schematic block diagram of an example electronic device 1300 that can be used to implement an embodiment of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present disclosure described and / or required herein.
[0207] like Fig.13 As shown, the device 1300 includes a computing unit 1301, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 1302 or a computer program loaded from a storage unit 1308 into a random access memory (RAM) 1303. In the RAM 1303, various programs and data required for the operation of the device 1300 can also be stored. The computing unit 1301, the ROM 1302, and the RAM 1303 are connected to each other via a bus 1304. An input / output (I / O) interface 1305 is also connected to the bus 1304.
[0208] A number of components in the device 1300 are connected to the I / O interface 1305, including: an input unit 1306, such as a keyboard, a mouse, etc.; an output unit 1307, such as various types of displays, speakers, etc.; a storage unit 1308, such as a disk, an optical disk, etc.; and a communication unit 1309, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 1309 allows the device 1300 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0209] The computing unit 1301 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 1301 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 1301 performs the various methods and processes described above, such as the request function generation method. For example, in some embodiments, the request function generation method may be implemented as a computer software program, which is tangibly included in a machine-readable medium, such as a storage unit 1308. In some embodiments, part or all of the computer program may be loaded and / or installed on the device 1300 via the ROM 1302 and / or the communication unit 1309. When the computer program is loaded into the RAM 1303 and executed by the computing unit 1301, one or more steps of the request function generation method described above may be performed. Alternatively, in other embodiments, the computing unit 1301 may be configured to execute the request function generation method in any other appropriate manner (eg, by means of firmware).
[0210] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0211] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0212] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0213] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0214] The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.
[0215] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship with each other. The server may be a cloud server, a server of a distributed system, or a server combined with a blockchain.
[0216] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.
[0217] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A request function generation method, comprising: Obtain an interface description file for describing an interface; Generate a type declaration file of the interface according to the interface definition information of the interface recorded in the interface description file and a preset type template file, wherein the type declaration file includes the name and property description of the interface, wherein the property description includes the name and data type of the request data of the interface and the name and data type of the response data, wherein the name of the interface included in the type declaration file is: the information included in the name field recorded in the interface definition definitions information of the interface in the interface description file, and the property description of the interface included in the type declaration file is: the information included in the description properties field recorded in the definitions information of the interface in the interface description file; Generate an interface configuration file of the target interface according to the information storage path of the target interface recorded in the interface description file and a preset interface configuration template, wherein the information storage path includes: information for indicating request data with type constraints and information for indicating response data with type constraints; Determining a target type declaration to be referenced from a type declaration file of the target interface based on a request data field and a response information field included in an interface configuration file of the target interface; A request function of the target interface is generated according to an interface configuration file of the target interface and a preset request function template, wherein the request function of the target interface references a target type declaration of the target interface.
2. The method according to claim 1, wherein: Before generating the request function of the target interface according to the interface configuration file of the target interface and the preset request function template, the method further includes: Generate a basic request function for intercepting user requests and / or intercepting responses; The step of generating a request function of the target interface according to the interface configuration file of the target interface and a preset request function template comprises: A request function for the target interface that calls the basic request function is generated according to the interface configuration file of the target interface and a preset request function template.
3. The method according to claim 1, wherein: After generating the request function of the target interface according to the interface configuration file of the target interface and the preset request function template, the method further includes: Generate an index file for introducing and exposing the request function.
4. The method according to any one of claims 1 to 3, wherein: The step of generating an interface configuration file of the target interface according to the information storage path of the target interface recorded in the interface description file and a preset interface configuration template comprises: Receive path parameters entered by the user; Based on the information storage path of the target interface indicated by the path parameter, the value of each parameter in the preset interface configuration template is determined, and the interface configuration file of the target interface is generated.
5. The method according to any one of claims 1 to 3, wherein: The obtaining of an interface description file for describing an interface comprises: Acquire the interface description file based on the access address of the interface description file; or Read the interface description file stored in the preset project directory.
6. The method according to any one of claims 1 to 3, wherein: The step of generating a request function of the target interface according to the interface configuration file of the target interface and a preset request function template comprises: Determine the interface name and interface path recorded in the interface configuration file of the target interface; According to the interface configuration file of the target interface and the preset request function template, a request function of the target interface is generated, the name of which is the interface name and the path of which is the interface path.
7. The method according to any one of claims 1 to 3, wherein: The generating of the type declaration file of the interface according to the interface definition information of the interface recorded in the interface description file and the preset type template file comprises: Determine the target interface name and target attribute description included in the interface definition information of the interface recorded in the interface description file; Generate an interface type declaration file based on the interface definition information of the interface recorded in the interface description file and a preset type template file, wherein the interface name recorded in the interface type declaration file is the target interface name, and the attribute description recorded is the target attribute description.
8. The method according to any one of claims 1 to 3, wherein: Before generating the type declaration file of the interface according to the interface definition information of the interface recorded in the interface description file and the preset type template file, the method further includes: For the interface definition information of the interface recorded in the interface description file, perform at least one of the following steps: According to a preset character conversion relationship, the characters not supported by the front-end language contained in the interface definition information are converted into characters supported by the front-end language; Convert characters contained in the interface definition information into characters of a preset language supported by the front-end language; According to the preset data type conversion relationship, the data type of the parameter included in the interface definition information is converted.
9. The method according to any one of claims 1 to 3, wherein: The step of generating a request function of the target interface according to the interface configuration file of the target interface and a preset request function template comprises: Get the interface name entered by the user; A request function whose name of the target interface is the interface name input by the user is generated according to the interface configuration file of the target interface and a preset request function template.
10. A request function generating device, comprising: A file acquisition module, used to acquire an interface description file used to describe an interface; A declaration file generation module, used to generate a type declaration file of an interface according to the interface definition information of the interface recorded in the interface description file and a preset type template file, wherein the type declaration file includes the name and property description of the interface, wherein the property description includes the name and data type of the request data of the interface and the name and data type of the response data, wherein the name of the interface included in the type declaration file is: the information included in the name field recorded in the interface definition definitions information of the interface in the interface description file, and the property description of the interface included in the type declaration file is: the information included in the description properties field recorded in the definitions information of the interface in the interface description file; a configuration file generating module, configured to generate an interface configuration file of the target interface according to an information storage path of the target interface recorded in the interface description file and a preset interface configuration template, wherein the information storage path includes: information of request data indicating the existence of type constraints and information of response data indicating the existence of type constraints; A type declaration determining module, configured to determine a target type declaration to be referenced from a type declaration file of the target interface based on a request data field and a response information field contained in an interface configuration file of the target interface; The request function generation module is used to generate the request function of the target interface according to the interface configuration file of the target interface and a preset request function template, wherein the request function of the target interface references the target type declaration of the target interface.
11. The device according to claim 10, wherein: The device also includes: A basic request function generation module, used to generate a basic request function for intercepting user requests and / or intercepting responses; The request function generation module is specifically used for: A request function of the target interface for calling the basic request function is generated according to the interface configuration file of the target interface and a preset request function template.
12. The device according to claim 10, wherein: The device also includes: The index file generation module is used to generate an index file for introducing and exposing the request function.
13. The device according to any one of claims 10 to 12, wherein: The configuration file generation module is specifically used for: Receive path parameters entered by the user; Based on the information storage path of the target interface indicated by the path parameter, the value of each parameter in the preset interface configuration template is determined, and the interface configuration file of the target interface is generated.
14. The device according to any one of claims 10 to 12, wherein: The file acquisition module is specifically used for: Acquire the interface description file based on the access address of the interface description file; or Read the interface description file stored in the preset project directory.
15. The device according to any one of claims 10 to 12, wherein: The request function generation module is specifically used for: Determine the interface name and interface path recorded in the interface configuration file of the target interface; According to the interface configuration file of the target interface and the preset request function template, a request function of the target interface is generated, the name of which is the interface name and the path of which is the interface path.
16. The device according to any one of claims 10 to 12, wherein: The declaration file generation module is specifically used for: Determine the target interface name and target attribute description included in the interface definition information of the interface recorded in the interface description file; Generate an interface type declaration file based on the interface definition information of the interface recorded in the interface description file and a preset type template file, wherein the interface name recorded in the interface type declaration file is the target interface name, and the attribute description recorded is the target attribute description.
17. The device according to any one of claims 10 to 12, wherein: The device also includes: The information conversion module is used to perform at least one of the following steps for the interface definition information of the interface recorded in the interface description file: According to a preset character conversion relationship, the characters not supported by the front-end language contained in the interface definition information are converted into characters supported by the front-end language; Convert characters contained in the interface definition information into characters of a preset language supported by the front-end language; According to the preset data type conversion relationship, the data type of the parameter included in the interface definition information is converted.
18. The device according to any one of claims 10 to 12, wherein: The request function generation module is specifically used for: Get the interface name entered by the user; A request function whose name of the target interface is the interface name input by the user is generated according to the interface configuration file of the target interface and a preset request function template.
19. An electronic device comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed 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 method according to any one of claims 1 to 9.
20. A non-transitory computer-readable storage medium storing computer instructions, wherein: The computer instructions are used to cause the computer to execute the method according to any one of claims 1-9.
21. A computer program product comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 9.
Citation Information
Patent Citations
Document generation method and device, interface development method and device, server and storage medium
CN112148356A
Method and device for realizing front-end request sending processing based on swagger, processor and computer readable storage medium thereof
CN114168366A
Systems and Methods for Automated Generation of Interactive Documentation Based on Web Application Description Language Files
US20150317156A1
