Skip to content

Commit

Permalink
feat: support setting cURL host
Browse files Browse the repository at this point in the history
  • Loading branch information
lkqm committed Sep 27, 2023
1 parent 73eb9cf commit b17cfef
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 26 deletions.
27 changes: 10 additions & 17 deletions src/main/java/io/apidocx/config/ApidocxSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import io.apidocx.action.ActionType;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Yapix应用程序级别配置.
*/
@State(name = "YapixSettings", storages = @Storage("YapixSettings.xml"))
@Getter
@Setter
public class ApidocxSettings implements PersistentStateComponent<ApidocxSettings> {

private ActionType defaultAction = ActionType.YApi;

private String curlHost = "http://localhost:8080";

public static ApidocxSettings getInstance() {
return ServiceManager.getService(ApidocxSettings.class);
}
Expand All @@ -32,26 +39,12 @@ public void loadState(@NotNull ApidocxSettings state) {
XmlSerializerUtil.copyBean(state, this);
}

public ActionType getDefaultAction() {
return defaultAction;
}

public void setDefaultAction(ActionType defaultAction) {
this.defaultAction = defaultAction;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ApidocxSettings)) {
return false;
}

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ApidocxSettings that = (ApidocxSettings) o;

return defaultAction == that.defaultAction;
return defaultAction == that.defaultAction && Objects.equals(curlHost, that.curlHost);
}

}
31 changes: 27 additions & 4 deletions src/main/java/io/apidocx/config/ApidocxSettingsForm.form
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="72"/>
<xy x="20" y="20" width="508" height="91"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -81,11 +81,34 @@
</component>
</children>
</grid>
<vspacer id="65fa3">
<grid id="cf522" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<properties/>
<border type="none"/>
<children>
<component id="29515" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="cURL Host:"/>
</properties>
</component>
<component id="bcd8e" class="javax.swing.JFormattedTextField" binding="curlHostField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="http://localhost:8080"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<buttonGroups>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/apidocx/config/ApidocxSettingsForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ApidocxSettingsForm {
private JRadioButton eolinkRadioButton;
private JRadioButton showdocRadioButton;
private JRadioButton apifoxRadioButton;
private JFormattedTextField curlHostField;

public JPanel getPanel() {
return panel;
Expand All @@ -37,6 +38,7 @@ public void set(ApidocxSettings data) {
default:
yapiRadioButton.doClick();
}
curlHostField.setText(data.getCurlHost());
}

public ApidocxSettings get() {
Expand All @@ -55,6 +57,7 @@ public ApidocxSettings get() {

ApidocxSettings data = new ApidocxSettings();
data.setDefaultAction(defaultAction);
data.setCurlHost(curlHostField.getText());
return data;
}
}
6 changes: 5 additions & 1 deletion src/main/java/io/apidocx/handle/curl/CopyAsCurlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import io.apidocx.action.AbstractAction;
import io.apidocx.base.util.ClipboardUtils;
import io.apidocx.config.ApidocxConfig;
import io.apidocx.config.ApidocxSettings;
import io.apidocx.model.Api;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

/**
Expand Down Expand Up @@ -43,7 +45,9 @@ public void handle(AnActionEvent event, ApidocxConfig config, List<Api> apis) {
notifyWarning("Copy as cURL", "only support single api, please choose method in editor");
return;
}
String curl = new CurlGenerator().generate(apis.get(0));
ApidocxSettings settings = ApidocxSettings.getInstance();
String host = StringUtils.isNotEmpty(settings.getCurlHost()) ? settings.getCurlHost() : "";
String curl = new CurlGenerator(host).generate(apis.get(0));
ClipboardUtils.setClipboard(curl);
notifyInfo(ACTION_TEXT, "copied to clipboard");
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/apidocx/handle/curl/CurlGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import io.apidocx.model.Property;
import io.apidocx.model.RequestBodyType;
import java.util.List;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
* 接口信息生成curl命令
*/
@AllArgsConstructor
public class CurlGenerator {

private final String host;

/**
* 生成curl字符串
*/
Expand Down Expand Up @@ -53,7 +57,7 @@ public String generate(Api api) {
private String getUrl(Api api) {
List<Property> queries = api.getParametersByIn(ParameterIn.query);
StringBuilder sb = new StringBuilder();
sb.append("{{host}}");
sb.append(host);
sb.append(api.getPath());
if (queries.size() > 0) {
sb.append("?");
Expand Down
8 changes: 5 additions & 3 deletions src/main/resources/.yapix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ returnUnwrapTypes=\
parameterIgnoreTypes=\
javax.servlet.http.HttpServletRequest, \
javax.servlet.http.HttpServletResponse, \
org.springframework.validation.BindingResult
org.springframework.validation.BindingResult, \
com.wangxiaobao.infra.pierre.security.tenant.Tenant, \
com.wangxiaobao.infra.pierre.security.tenant.Project, \
com.wangxiaobao.infra.pierre.security.User

# 自定义bean配置
beans[org.springframework.data.domain.Pageable]= { \
Expand Down Expand Up @@ -103,8 +106,7 @@ beans[com.wangxiaobao.infra.pierre.common.model.Result]= { \
"fields": { \
"code": { "mock": "0", "description": "状态码: 0(成功)", "required": true }, \
"msg": { "mock": "ok", "description": "消息", "required": true }, \
"data": { "description": "数据", "required": true}, \
"timestamp": { "mock": "@now", "description": "时间", "required": true } \
"data": { "description": "数据", "required": true} \
} \
}
beans[com.wangxiaobao.infra.pierre.common.model.Pager]= { \
Expand Down

0 comments on commit b17cfef

Please sign in to comment.