如何以命名属性的形式使用 NSwag 生成 TypeScript 客户端?
原标题:How to generate TypeScript client using NSwag in form of named properties?
We generate TypeScript client from swagger interface using NSwag.
The generated client looks like following:
client.EndPointFoo(arg1, arg2, arg3, ...)
We deal with issues that NSWag change order of arugments in case of some changes in Swagger interface and compiler sometime does not catch the problem in case that those parameters are of same type. This finally leads to runtime issues.
I would like to generate client in form of named properties, for example this way:
client.EndPointFoo(
property1: arg1,
property2: arg2,
property3: arg3,
...)
Is this possible? Do I have write my own Liquid template or is there easier solution?
最佳回答
Disclaimer: I am a maintainer of Fern
Hey @tomas, you can give Fern s TypeScript generator a spin which will generate named properties for the request so that it s easier to get compile breaks when your API changes.
Here are our docs for importing an existing OpenAPI https://buildwithfern.com/docs/spec/openapi.
npm install -g fern-api
fern init --openapi
fern generate
问题回答
Unfortunately there is no configuration for that, and the only possible solution is to change the template. Here are the modifications you need to make:
In AngularClient.liquid replace line 42
{{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false or IncludeHttpContext == true%}, {% endif %}{% endfor %}{% if IncludeHttpContext%}httpContext?: HttpContext{% endif %}): Observable<{{ operation.ResultType }}> {
with:
{{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}(props: { {% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false or IncludeHttpContext == true%}, {% endif %}{% endfor %}{% if IncludeHttpContext%}httpContext?: HttpContext{% endif %} }): Observable<{{ operation.ResultType }}> {
const { {% for parameter in operation.Parameters %}{{ parameter.VariableName }}, {% endfor %} } = props;
Update the method documentation to match the new signature in Client.Method.Documentation.liquid
{% for parameter in operation.Parameters -%}
{% if parameter.HasDescriptionOrIsOptional -%}
* @param {{ parameter.VariableName }}{% if parameter.IsOptional %} (optional){% endif %} {{ parameter.Description }}
replace with:
* @param {Object} props
{% for parameter in operation.Parameters -%}
{% if parameter.HasDescriptionOrIsOptional -%}
* @param props.{{ parameter.VariableName }}{% if parameter.IsOptional %} (optional){% endif %} {{ parameter.Description }}
You will find the necessary templates here: https://github.com/RicoSuter/NSwag/tree/master/src/NSwag.CodeGeneration.TypeScript/Templates Please note that you do not need to copy the whole templates folder locally. The "templateDirectory" in the config needs to contain the modified files, and NSwag will use the default templates for the rest.
相关问题
Using liquid with haml
I m planning to develop a CMS with ruby/rails. One of the main key features that I m planning is to give the user to edit their layout (I m planning to do this through liquid)
meanwhile i have red ...
Extending Jekyll and Liquid to parse post s content
My blog, powerred by Jekyll, serves out a Atom feed.
---
layout: nill
rooturi: http://stefan.artspace44.com
---
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/...
Liquid templates - accessing members by name
I m using Jekyll to create a new blog. It uses Liquid underneath.
Jekyll defines certain "variables": site, content, page, post and paginator. These "variables" have several "members". For instance, ...
How to make will_paginate work with liquid
I managed to make a little hack to make will_paginate work with liquid:
http://gist.github.com/426737
I wonder if this is safe? Ideas?
undefined method `call for LiquidView:Class
I trying to use Liquid template engine plugin , but I m getting the following error while controller tries to render a .liquid template .
"undefined method `call for LiquidView:Class"
...
Liquid Templates Not Parsing!
Im trying to use Liquid Template Language in my Rails Application, i ve watched Ryan Bates video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work!
When I try ...
Having difficulties with Jekyll / Liquid
I m tring to do a loop for Nav links below my posts. This is going into the _layout of posts.html
I can t get the link to not show if the post is the last or the first. Any help would be awesome.
{% ...
Is there a way to easily parse the year from the [site.time] property in Jekyll?
Is there a way to just pull the year (or any other element... month, day, etc) from the site.time property that is available to your template file in Jekyll?
Right now it returns, for example: Sat ...