Skip to content

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

♻️ Migrate format API contracts https://github.com/siyuan-note/siyuan/issues/19378

Daniel8cd97804cb71
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 471b949e4f..12826cc571 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -150,6 +150,8 @@ export type LockScreenRequestInput = { "lockScreenMode": number; };
export type MoveBlockRequestInput = { "id": string; "parentID"?: string | null; "previousID"?: string | null; };
+export type NetImageAssetsRequestInput = { "id": string; "url"?: string | null; };
+
export type NetworkData = { "proxy": NetworkProxy | null; };
export type NetworkProxy = { "host": string; "port": string; "scheme": string; };
@@ -252,6 +254,8 @@ export type TreeStatData = { "containsEmbed"?: boolean; "embedStat"?: EmbedStat
export type TreeStatRequestInput = { "id": string; "ids"?: Array<string> | null; "includeEmbed"?: boolean | null; "notebook"?: string | null; "reqId"?: JSONValue | null; };
+export type TrimmedIDRequestInput = { "id": string; };
+
export type UnfoldedParentData = { "parentID": string; };
export type UnlockNotebookRequestInput = { "notebook": string; "password": string; };
@@ -539,9 +543,6 @@ export type APILegacyPOSTPath =
"/api/filetree/setPublishAccess" |
"/api/filetree/setSort" |
"/api/filetree/upsertIndexes" |
- "/api/format/autoSpace" |
- "/api/format/netAssets2LocalAssets" |
- "/api/format/netImg2LocalAssets" |
"/api/graph/getGraph" |
"/api/graph/getLocalGraph" |
"/api/graph/resetGraph" |
@@ -1157,6 +1158,21 @@ export interface APIPOSTRoutes {
response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
body: "json";
};
+ "/api/format/autoSpace": {
+ request: TrimmedIDRequestInput;
+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
+ "/api/format/netAssets2LocalAssets": {
+ request: TrimmedIDRequestInput;
+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
+ "/api/format/netImg2LocalAssets": {
+ request: NetImageAssetsRequestInput;
+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
"/api/history/clearWorkspaceHistory": {
request: EmptyRequestInput;
response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/contract_format_test.go b/kernel/api/contract_format_test.go
new file mode 100644
index 0000000000..5cf0dd6000
--- /dev/null
+++ b/kernel/api/contract_format_test.go
@@ -0,0 +1,49 @@
+package api
+
+import (
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/88250/gulu"
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestAPIContractFormatInputCompatibility(t *testing.T) {
+ for _, body := range []string{`{}`, `null`, `[]`, `{`, `{"id":null}`, `{"id":3}`, `{"id":" abc "}`, `{"id":""}`, `{"id":"abc","url":null}`, `{"id":"abc","url":3}`, `{"id":"abc","url":" https://example.com/a "}`} {
+ t.Run(body, func(t *testing.T) {
+ legacy := gulu.Ret.NewResult()
+ context, _ := gin.CreateTestContext(httptest.NewRecorder())
+ context.Request = httptest.NewRequest("POST", "/api/format/netImg2LocalAssets", strings.NewReader(body))
+ args, ok := util.JsonArg(context, legacy)
+ var id, url string
+ if ok {
+ ok = util.ParseJsonArgs(args, legacy, util.BindJsonArg("id", &id, true, true), util.BindJsonArg("url", &url, false, false))
+ }
+ request, err := apicontract.NetImg2LocalAssets.Decode(strings.NewReader(body))
+ if ok != (err == nil) {
+ t.Fatalf("acceptance mismatch: legacy=%v, contract=%v", ok, err)
+ }
+ if ok && (request.ID != id || request.URL != url) {
+ t.Fatalf("request mismatch: %#v, id=%q url=%q", request, id, url)
+ }
+ })
+ }
+}
+
+func TestAPIContractFormatInvalidResponses(t *testing.T) {
+ engine := gin.New()
+ engine.POST("/api/format/autoSpace", autoSpace)
+ engine.POST("/api/format/netAssets2LocalAssets", netAssets2LocalAssets)
+ engine.POST("/api/format/netImg2LocalAssets", netImg2LocalAssets)
+ for _, path := range []string{"/api/format/autoSpace", "/api/format/netAssets2LocalAssets", "/api/format/netImg2LocalAssets"} {
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", path, strings.NewReader(`{"id":3}`)))
+ requireAPIContract(t, "POST", path, recorder)
+ if !strings.Contains(recorder.Body.String(), `"code":-1`) {
+ t.Fatalf("expected invalid ID failure: %s", recorder.Body.String())
+ }
+ }
+}
diff --git a/kernel/api/format.go b/kernel/api/format.go
index 21ff886de6..c67a002b7f 100644
--- a/kernel/api/format.go
+++ b/kernel/api/format.go
@@ -17,79 +17,52 @@
package api
import (
- "net/http"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/model"
- "github.com/siyuan-note/siyuan/kernel/util"
)
-func netAssets2LocalAssets(c *gin.Context) {
+var netAssets2LocalAssets = contractHandler(apicontract.NetAssets2LocalAssets, func(c *gin.Context, request apicontract.TrimmedIDRequest) apicontract.Response[apicontract.Null] {
ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
-
- var id string
- if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("id", &id, true, true)) {
- return
- }
+ id := request.ID
err := model.NetAssets2LocalAssets(id, false, "")
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 5000}
- return
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
-}
-func netImg2LocalAssets(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
+ return apicontract.Success(apicontract.Null{})
+})
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
+var netImg2LocalAssets = contractHandler(apicontract.NetImg2LocalAssets, func(c *gin.Context, request apicontract.NetImageAssetsRequest) apicontract.Response[apicontract.Null] {
+ ret := gulu.Ret.NewResult()
- var id, url string
- if !util.ParseJsonArgs(arg, ret,
- util.BindJsonArg("id", &id, true, true),
- util.BindJsonArg("url", &url, false, false),
- ) {
- return
- }
+ id := request.ID
+ url := request.URL
err := model.NetAssets2LocalAssets(id, true, url)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 5000}
- return
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
-}
-func autoSpace(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
+ return apicontract.Success(apicontract.Null{})
+})
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
+var autoSpace = contractHandler(apicontract.AutoSpace, func(c *gin.Context, request apicontract.TrimmedIDRequest) apicontract.Response[apicontract.Null] {
+ ret := gulu.Ret.NewResult()
- var id string
- if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("id", &id, true, true)) {
- return
- }
+ id := request.ID
err := model.AutoSpace(id)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 5000}
- return
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
-}
+
+ return apicontract.Success(apicontract.Null{})
+})
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index 34d6fbb4a3..fb155c9f97 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -357,3 +357,7 @@ var GetRecentUpdatedBlocks = define[EmptyRequest, []*SearchBlock]("getRecentUpda
var Zip = define[ZipRequest, Null]("zip", "/api/archive/zip", JSONBody, ResponseOptions{}, "POST")
var Unzip = define[UnzipRequest, Null]("unzip", "/api/archive/unzip", JSONBody, ResponseOptions{}, "POST")
+
+var AutoSpace = define[TrimmedIDRequest, Null]("autoSpace", "/api/format/autoSpace", JSONBody, ResponseOptions{}, "POST")
+var NetAssets2LocalAssets = define[TrimmedIDRequest, Null]("netAssets2LocalAssets", "/api/format/netAssets2LocalAssets", JSONBody, ResponseOptions{}, "POST")
+var NetImg2LocalAssets = define[NetImageAssetsRequest, Null]("netImg2LocalAssets", "/api/format/netImg2LocalAssets", JSONBody, ResponseOptions{}, "POST")
diff --git a/kernel/apicontract/format.go b/kernel/apicontract/format.go
new file mode 100644
index 0000000000..4189f6bafc
--- /dev/null
+++ b/kernel/apicontract/format.go
@@ -0,0 +1,10 @@
+package apicontract
+
+type TrimmedIDRequest struct {
+ ID string `json:"id" api:"trim"`
+}
+
+type NetImageAssetsRequest struct {
+ ID string `json:"id" api:"trim"`
+ URL string `json:"url" api:"optional,nullable"`
+}
diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.json
index 364172efd9..befd08ee65 100644
--- a/kernel/apicontract/legacy_routes.json
+++ b/kernel/apicontract/legacy_routes.json
@@ -250,9 +250,6 @@
{"method":"POST","path":"/api/filetree/setPublishAccess","handler":"setPublishAccess"},
{"method":"POST","path":"/api/filetree/setSort","handler":"setSort"},
{"method":"POST","path":"/api/filetree/upsertIndexes","handler":"upsertIndexes"},
- {"method":"POST","path":"/api/format/autoSpace","handler":"autoSpace"},
- {"method":"POST","path":"/api/format/netAssets2LocalAssets","handler":"netAssets2LocalAssets"},
- {"method":"POST","path":"/api/format/netImg2LocalAssets","handler":"netImg2LocalAssets"},
{"method":"POST","path":"/api/graph/getGraph","handler":"getGraph"},
{"method":"POST","path":"/api/graph/getLocalGraph","handler":"getLocalGraph"},
{"method":"POST","path":"/api/graph/resetGraph","handler":"resetGraph"},
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index 238f18a804..2d87409e53 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -2126,6 +2126,28 @@
],
"additionalProperties": false
},
+ "NetImageAssetsRequestInput": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "string"
+ },
+ "url": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ }
+ },
+ "required": [
+ "id"
+ ],
+ "additionalProperties": false
+ },
"NetworkData": {
"type": "object",
"properties": {
@@ -3844,6 +3866,18 @@
],
"additionalProperties": false
},
+ "TrimmedIDRequestInput": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "id"
+ ],
+ "additionalProperties": false
+ },
"UnfoldedParentData": {
"type": "object",
"properties": {
@@ -10133,6 +10167,231 @@
]
}
},
+ {
+ "method": "POST",
+ "path": "/api/format/autoSpace",
+ "handler": "autoSpace",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/TrimmedIDRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "type": "null"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "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
+ }
+ ]
+ }
+ },
+ {
+ "method": "POST",
+ "path": "/api/format/netAssets2LocalAssets",
+ "handler": "netAssets2LocalAssets",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/TrimmedIDRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "type": "null"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "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
+ }
+ ]
+ }
+ },
+ {
+ "method": "POST",
+ "path": "/api/format/netImg2LocalAssets",
+ "handler": "netImg2LocalAssets",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/NetImageAssetsRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "type": "null"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "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
+ }
+ ]
+ }
+ },
{
"method": "POST",
"path": "/api/history/clearWorkspaceHistory",