feat: align JS exports to uppercase methods and map headers (by AI)
This commit is contained in:
parent
6220ba999e
commit
1c52c546e6
@ -1,5 +1,8 @@
|
|||||||
# CHANGELOG
|
# CHANGELOG
|
||||||
|
|
||||||
|
## v1.5.1 (2026-06-08)
|
||||||
|
- **JS 对齐**: HTTP/RPC 方法名统一更正为全大写(`GET`, `POST`, `PUT`, `DELETE`),且 Headers 参数从变长字符串改为 `map[string]string` 对象。
|
||||||
|
|
||||||
## v1.3.4 (2026-05-31)
|
## v1.3.4 (2026-05-31)
|
||||||
- **重构**: 精简 JS API 导出。移除所有管理类 API(`setNode`, `setLoadBalancer` 等)及 Header 常量。
|
- **重构**: 精简 JS API 导出。移除所有管理类 API(`setNode`, `setLoadBalancer` 等)及 Header 常量。
|
||||||
- **定位**: JS 侧仅作为微服务能力的消费者,不参与服务发现的元数据维护。
|
- **定位**: JS 侧仅作为微服务能力的消费者,不参与服务发现的元数据维护。
|
||||||
|
|||||||
4
go.mod
4
go.mod
@ -22,9 +22,9 @@ require (
|
|||||||
apigo.cc/go/rand v1.5.0 // indirect
|
apigo.cc/go/rand v1.5.0 // indirect
|
||||||
apigo.cc/go/shell v1.5.0 // indirect
|
apigo.cc/go/shell v1.5.0 // indirect
|
||||||
github.com/gomodule/redigo v2.0.0+incompatible // indirect
|
github.com/gomodule/redigo v2.0.0+incompatible // indirect
|
||||||
golang.org/x/crypto v0.51.0 // indirect
|
golang.org/x/crypto v0.52.0 // indirect
|
||||||
golang.org/x/net v0.54.0 // indirect
|
golang.org/x/net v0.54.0 // indirect
|
||||||
golang.org/x/sys v0.44.0 // indirect
|
golang.org/x/sys v0.45.0 // indirect
|
||||||
golang.org/x/text v0.37.0 // indirect
|
golang.org/x/text v0.37.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
6
go.sum
6
go.sum
@ -36,12 +36,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||||
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
|
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
|
||||||
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
|
|
||||||
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
|
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
|
||||||
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
|
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
|
||||||
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||||
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
|
||||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|||||||
63
js_export.go
63
js_export.go
@ -9,28 +9,39 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
jsmod.Register("discover", map[string]any{
|
jsmod.Register("discover", map[string]any{
|
||||||
// RPC 调用 (消费默认实例)
|
// RPC 调用 (全大写对齐 HTTP)
|
||||||
"get": func(app, path string, headers ...string) *jsResult {
|
"GET": func(app, path string, headers map[string]string) *jsResult {
|
||||||
return wrapResult(Get(app, path, headers...))
|
return wrapResult(Get(app, path, flattenHeaders(headers)...))
|
||||||
},
|
},
|
||||||
"post": func(app, path string, data any, headers ...string) *jsResult {
|
"POST": func(app, path string, data any, headers map[string]string) *jsResult {
|
||||||
return wrapResult(Post(app, path, data, headers...))
|
return wrapResult(Post(app, path, data, flattenHeaders(headers)...))
|
||||||
},
|
},
|
||||||
"put": func(app, path string, data any, headers ...string) *jsResult {
|
"PUT": func(app, path string, data any, headers map[string]string) *jsResult {
|
||||||
return wrapResult(DefaultDiscoverer.Put(app, path, data, headers...))
|
return wrapResult(DefaultDiscoverer.Put(app, path, data, flattenHeaders(headers)...))
|
||||||
},
|
},
|
||||||
"delete": func(app, path string, data any, headers ...string) *jsResult {
|
"DELETE": func(app, path string, data any, headers map[string]string) *jsResult {
|
||||||
return wrapResult(DefaultDiscoverer.Delete(app, path, data, headers...))
|
return wrapResult(DefaultDiscoverer.Delete(app, path, data, flattenHeaders(headers)...))
|
||||||
},
|
},
|
||||||
|
|
||||||
// 链路透传
|
// 链路透传
|
||||||
"from": func(r *http.Request) *jsCaller {
|
"From": func(r *http.Request) *jsCaller {
|
||||||
return &jsCaller{c: DefaultDiscoverer.From(r)}
|
return &jsCaller{c: DefaultDiscoverer.From(r)}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// jsResult 包装 *gohttp.Result 以隐藏 Save() 等危险方法
|
func flattenHeaders(m map[string]string) []string {
|
||||||
|
if len(m) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
res := make([]string, 0, len(m)*2)
|
||||||
|
for k, v := range m {
|
||||||
|
res = append(res, k, v)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// jsResult 包装 *gohttp.Result 以对齐 PascalCase
|
||||||
type jsResult struct {
|
type jsResult struct {
|
||||||
r *gohttp.Result
|
r *gohttp.Result
|
||||||
}
|
}
|
||||||
@ -39,14 +50,10 @@ func wrapResult(r *gohttp.Result) *jsResult {
|
|||||||
return &jsResult{r: r}
|
return &jsResult{r: r}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jr *jsResult) String() string { return jr.r.String() }
|
func (jr *jsResult) String() string { return jr.r.String() }
|
||||||
func (jr *jsResult) Bytes() []byte { return jr.r.Bytes() }
|
func (jr *jsResult) Bytes() []byte { return jr.r.Bytes() }
|
||||||
func (jr *jsResult) Map() map[string]any {
|
func (jr *jsResult) Map() map[string]any { return jr.r.Map() }
|
||||||
return jr.r.Map()
|
func (jr *jsResult) Slice() []any { return jr.r.Slice() }
|
||||||
}
|
|
||||||
func (jr *jsResult) Slice() []any {
|
|
||||||
return jr.r.Slice()
|
|
||||||
}
|
|
||||||
func (jr *jsResult) Status() int {
|
func (jr *jsResult) Status() int {
|
||||||
if jr.r.Response != nil {
|
if jr.r.Response != nil {
|
||||||
return jr.r.Response.StatusCode
|
return jr.r.Response.StatusCode
|
||||||
@ -60,20 +67,20 @@ func (jr *jsResult) Error() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// jsCaller 包装 *Caller 以确保返回包装后的结果
|
// jsCaller 包装 *Caller 以对齐全大写方法
|
||||||
type jsCaller struct {
|
type jsCaller struct {
|
||||||
c *Caller
|
c *Caller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jc *jsCaller) Get(app, path string, headers ...string) *jsResult {
|
func (jc *jsCaller) GET(app, path string, headers map[string]string) *jsResult {
|
||||||
return wrapResult(jc.c.Get(app, path, headers...))
|
return wrapResult(jc.c.Get(app, path, flattenHeaders(headers)...))
|
||||||
}
|
}
|
||||||
func (jc *jsCaller) Post(app, path string, data any, headers ...string) *jsResult {
|
func (jc *jsCaller) POST(app, path string, data any, headers map[string]string) *jsResult {
|
||||||
return wrapResult(jc.c.Post(app, path, data, headers...))
|
return wrapResult(jc.c.Post(app, path, data, flattenHeaders(headers)...))
|
||||||
}
|
}
|
||||||
func (jc *jsCaller) Put(app, path string, data any, headers ...string) *jsResult {
|
func (jc *jsCaller) PUT(app, path string, data any, headers map[string]string) *jsResult {
|
||||||
return wrapResult(jc.c.Put(app, path, data, headers...))
|
return wrapResult(jc.c.Put(app, path, data, flattenHeaders(headers)...))
|
||||||
}
|
}
|
||||||
func (jc *jsCaller) Delete(app, path string, data any, headers ...string) *jsResult {
|
func (jc *jsCaller) DELETE(app, path string, data any, headers map[string]string) *jsResult {
|
||||||
return wrapResult(jc.c.Delete(app, path, data, headers...))
|
return wrapResult(jc.c.Delete(app, path, data, flattenHeaders(headers)...))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user