Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: Migrate dynamic icon API contracts https://github.com/siyuan-note/siyuan/issues/19378

Daniel84c6556f8821
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 734b32640d..de0962613b 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -808,6 +808,8 @@ export type DownloadInstallPkgRequestInput = { "downloadInstallPkg": boolean; };
export type DuplicateAttributeViewBlockRequestInput = { "avID": string; };
+export type DynamicIconRequestInput = { "color"?: string; "content"?: string; "date"?: string; "id"?: string; "lang"?: string; "type"?: string; "weekdayType"?: string; };
+
export type EditorReadOnlyRequestInput = { "readonly": boolean; };
export type EmbedBlock = { "allowChildOperation": boolean; "block": SearchBlock | null; "blockPaths": Array<BlockPath | null> | null; };
@@ -2005,7 +2007,6 @@ export type WorkspaceInfoData = { "siyuanVer": string; "workspaceDir": string; }
export type ZipRequestInput = { "path": string; "zipPath": string; };
export type APILegacyGETPath =
- "/api/icon/getDynamicIcon" |
"/api/system/bootProgressSSE" |
"/api/system/getBootAppearance" |
"/api/system/getCaptcha" |
@@ -2021,6 +2022,14 @@ export interface APIGETRoutes {
output: "binary";
contentVariants: [{"status":200,"contentType":"text/html"},{"status":400,"contentType":"text/html"},{"status":403,"contentType":"text/plain"}];
};
+ "/api/icon/getDynamicIcon": {
+ request: DynamicIconRequestInput;
+ response: Blob | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "none";
+ output: "binary";
+ emptyResponseStatuses: [500];
+ contentVariants: [{"status":200,"contentType":"image/svg+xml"}];
+ };
"/api/network/echo": {
request: Blob;
response: { "code": 0; "data": NetworkEchoData; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/icon.go b/kernel/api/icon.go
index 2aa798b898..136e1b35aa 100644
--- a/kernel/api/icon.go
+++ b/kernel/api/icon.go
@@ -27,6 +27,7 @@ import (
"time"
"github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -113,7 +114,7 @@ func darkenColor(hexColor string, factor float64) string {
return fmt.Sprintf("#%02X%02X%02X", r, g, b)
}
-func getDynamicIcon(c *gin.Context) {
+var getDynamicIcon = contractHandler(apicontract.GetDynamicIcon, func(c *gin.Context, request apicontract.DynamicIconRequest) apicontract.Response[apicontract.BinaryContent] {
// Add internal kernel API `/api/icon/getDynamicIcon` https://github.com/siyuan-note/siyuan/pull/12939
iconType := c.Query("type")
@@ -170,8 +171,7 @@ func getDynamicIcon(c *gin.Context) {
var err error
svg, err = util.SanitizeSVG(svg)
if err != nil {
- c.Status(http.StatusInternalServerError)
- return
+ return apicontract.EmptyHTTPResponse[apicontract.BinaryContent](http.StatusInternalServerError)
}
}
@@ -180,8 +180,8 @@ func getDynamicIcon(c *gin.Context) {
c.Header("X-Content-Type-Options", "nosniff")
c.Header("Cache-Control", "no-cache")
c.Header("Pragma", "no-cache")
- c.String(http.StatusOK, svg)
-}
+ return apicontract.SuccessBinary("image/svg+xml", []byte(svg))
+})
func getDateInfo(dateStr string, lang string, weekdayType string) map[string]any {
// 设置默认值
diff --git a/kernel/api/icon_test.go b/kernel/api/icon_test.go
index fa9c745de4..838fd8da53 100644
--- a/kernel/api/icon_test.go
+++ b/kernel/api/icon_test.go
@@ -50,6 +50,7 @@ func TestGetDynamicIconSetsSecurityHeaders(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "/api/icon/getDynamicIcon?type=8&content=%3Cscript%3Ealert(1)%3C%2Fscript%3E", nil)
context.Request = request
getDynamicIcon(context)
+ requireAPIContract(t, http.MethodGet, "/api/icon/getDynamicIcon", recorder)
if recorder.Code != http.StatusOK {
t.Fatalf("unexpected status code %d", recorder.Code)
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index d513fc3932..8604b952bd 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -946,3 +946,5 @@ var NetworkEventSourceProxy = define[EmptyRequest, ProxyFailure]("esProxy", "/es
var NetworkWebSocketProxy = define[EmptyRequest, ProxyFailure]("wsProxy", "/ws/network/proxy", NoBody, ProxyOptions(WebSocketProxy), "GET")
var PluginPrivateService = define[EmptyRequest, PluginServiceContent]("pluginPrivateWebServer", "/plugin/private/:name/*path", RawBody, PluginServiceOptions(), "ANY")
+
+var GetDynamicIcon = define[DynamicIconRequest, BinaryContent]("getDynamicIcon", "/api/icon/getDynamicIcon", NoBody, ResponseOptions{Output: BinaryOutput, ErrorStatus: 200, ContentVariants: []HTTPContentVariant{{Status: 200, ContentType: "image/svg+xml"}}, EmptyResponseStatuses: []int{500}}, "GET")
diff --git a/kernel/apicontract/icon.go b/kernel/apicontract/icon.go
new file mode 100644
index 0000000000..8c6601393c
--- /dev/null
+++ b/kernel/apicontract/icon.go
@@ -0,0 +1,11 @@
+package apicontract
+
+type DynamicIconRequest struct {
+ Type string `json:"type" api:"optional"`
+ Color string `json:"color" api:"optional"`
+ Date string `json:"date" api:"optional"`
+ Lang string `json:"lang" api:"optional"`
+ WeekdayType string `json:"weekdayType" api:"optional"`
+ Content string `json:"content" api:"optional"`
+ ID string `json:"id" api:"optional"`
+}
diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.json
index 8c02d01e03..a705593271 100644
--- a/kernel/apicontract/legacy_routes.json
+++ b/kernel/apicontract/legacy_routes.json
@@ -1,5 +1,4 @@
[
- {"method":"GET","path":"/api/icon/getDynamicIcon","handler":"getDynamicIcon"},
{"method":"GET","path":"/api/system/bootProgressSSE","handler":"bootProgressSSE"},
{"method":"GET","path":"/api/system/getBootAppearance","handler":"getBootAppearance"},
{"method":"GET","path":"/api/system/getCaptcha","handler":"model.GetCaptcha"},
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index 0c004e10fa..7986fd9663 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -18767,6 +18767,33 @@
],
"additionalProperties": false
},
+ "DynamicIconRequestInput": {
+ "type": "object",
+ "properties": {
+ "color": {
+ "type": "string"
+ },
+ "content": {
+ "type": "string"
+ },
+ "date": {
+ "type": "string"
+ },
+ "id": {
+ "type": "string"
+ },
+ "lang": {
+ "type": "string"
+ },
+ "type": {
+ "type": "string"
+ },
+ "weekdayType": {
+ "type": "string"
+ }
+ },
+ "additionalProperties": false
+ },
"EditorReadOnlyRequestInput": {
"type": "object",
"properties": {
@@ -43565,6 +43592,73 @@
}
]
},
+ {
+ "method": "GET",
+ "path": "/api/icon/getDynamicIcon",
+ "handler": "getDynamicIcon",
+ "body": "none",
+ "request": {
+ "$ref": "#/$defs/DynamicIconRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "string",
+ "format": "binary"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "closeTimeout": {
+ "type": "number"
+ }
+ },
+ "required": [
+ "closeTimeout"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": true
+ }
+ ]
+ },
+ "output": "binary",
+ "errorStatus": 200,
+ "contentVariants": [
+ {
+ "status": 200,
+ "contentType": "image/svg+xml"
+ }
+ ],
+ "emptyResponseStatuses": [
+ 500
+ ]
+ },
{
"method": "GET",
"path": "/api/network/echo",