我的 Nswag 生成文件遇到了一个奇怪的问题。在运行用于生成代码的脚本后,它成功地完成了它,但在文件末尾它添加了文件本身的路径。
我已经尝试过:
npm install
npm install
npm install
这就是我生成文件后得到的
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
throw new SwaggerException(message, status, response, headers, result);
}
C:\Users\USER_NAME\source\repos\SOLUTION_NAME\src\backend\api-client-extentions.ts
感谢任何帮助!
我有一个 C# 项目作为我的后端运行,提供了所有需要用 Nswag 翻译的控制器
我有一个包含在 React 框架中的 Microsoft SharePoint Online WebPart 解决方案。
{
"runtime": "Default",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"url": "https://localhost:8081/swagger/BACKEND_PATH/swagger.json",
"output": null,
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToTypeScriptClient": {
"className": "ApiClient",
"moduleName": "",
"namespace": "",
"typeScriptVersion": 2.7,
"template": "Fetch",
"promiseType": "Promise",
"httpClass": "HttpClient",
"withCredentials": false,
"useSingletonProvider": false,
"injectionTokenType": "OpaqueToken",
"rxJsVersion": 6.0,
"dateTimeType": "Date",
"nullValue": "Undefined",
"generateClientClasses": true,
"generateClientInterfaces": false,
"generateOptionalParameters": false,
"exportTypes": true,
"wrapDtoExceptions": true,
"exceptionClass": "SwaggerException",
"clientBaseClass": "ApiClientBase",
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"protectedMethods": [],
"configurationClass": null,
"useTransformOptionsMethod": true,
"useTransformResultMethod": false,
"generateDtoTypes": true,
"operationGenerationMode": "SingleClientFromOperationId",
"markOptionalProperties": true,
"generateCloneMethod": false,
"typeStyle": "Interface",
"enumStyle": "Enum",
"useLeafType": false,
"classTypes": [],
"extendedClasses": [],
"extensionCode": "src/backend/api-client-extentions.ts",
"generateDefaultValues": true,
"excludedTypeNames": [],
"excludedParameterNames": [],
"handleReferences": false,
"generateConstructorInterface": true,
"convertConstructorInterfaceData": false,
"importRequiredTypes": true,
"useGetBaseUrlMethod": false,
"baseUrlTokenName": "API_BASE_URL",
"queryNullValue": "",
"inlineNamedDictionaries": false,
"inlineNamedAny": false,
"templateDirectory": null,
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"serviceHost": null,
"serviceSchemes": null,
"output": "src/backend/api-client-generated.ts",
"newLineBehavior": "Auto"
},
}
}
{
"name": "test-new-solution",
"version": "0.0.1",
"private": true,
"main": "lib/index.js",
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test",
"generate:nswag:api": "nswag run config.api.nswag"
},
"dependencies": {
"react": "16.13.1",
"react-dom": "16.13.1",
"office-ui-fabric-react": "7.174.1",
"@microsoft/sp-core-library": "1.14.0",
"@microsoft/sp-property-pane": "1.14.0",
"@microsoft/sp-webpart-base": "1.14.0",
"@microsoft/sp-lodash-subset": "1.14.0",
"@microsoft/sp-office-ui-fabric-core": "1.14.0",
"nswag": "13.6.1"
},
"devDependencies": {
"@types/react": "16.9.51",
"@types/react-dom": "16.9.8",
"@microsoft/sp-build-web": "1.14.0",
"@microsoft/sp-tslint-rules": "1.14.0",
"@microsoft/sp-module-interfaces": "1.14.0",
"@microsoft/rush-stack-compiler-3.9": "0.4.47",
"gulp": "~4.0.2",
"ajv": "~5.2.2",
"@types/webpack-env": "1.13.1",
"@types/node": "^17.0.33"
}
}
我们有同样的问题。正如 github 问题所暗示的那样,它仅在使用 extensionCode 属性时发生。从我们的测试中我们发现,如果 nswag 找不到扩展代码文件 relative 到命令运行的位置,就会发生这种情况。
您应该能够使用相对路径使其工作,例如,如果您的
package.json
位于顶层,这可能就像将 extensionCode 属性更改为一样简单:
"extensionCode": "./src/backend/api-client-extentions.ts",
我们选择将我们的扩展代码文件放在与我们生成的客户端相同的位置,并添加了一个 powershell 脚本来从相同的位置调用 nswag,即在
config.api.nswag
:
"extensionCode": "api-client-extentions.ts",
...
"output": "api-client-generated.ts",
在
src/backend
中我们会有一个generate.ps1:
Set-Location -Path $PSScriptRoot
& "C:\Program Files (x86)\Rico Suter\NSwagStudio\Win\nswag.exe" run config.api.nswag
只要我们想重新生成客户端,它就可以从 VS Code 即时运行。