-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.go
More file actions
63 lines (53 loc) · 1.52 KB
/
endpoint.go
File metadata and controls
63 lines (53 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package fetch
import (
. "github.com/tinywasm/fmt"
)
// EndpointProvider allows structs to define their API endpoint
type EndpointProvider interface {
HandlerName() string
}
// resolveEndpoint extracts endpoint string from any (string or EndpointProvider)
func resolveEndpoint(endpoint any) (string, error) {
if endpoint == nil {
return "", Err("endpoint cannot be nil")
}
switch v := endpoint.(type) {
case string:
return v, nil
case EndpointProvider:
return v.HandlerName(), nil
default:
return "", Errf("unsupported endpoint type: %T", endpoint)
}
}
// buildFullURL constructs final URL using BaseURL + endpoint
func buildFullURL(endpoint string, requestBaseURL string) (string, error) {
if isAbsoluteURL(endpoint) {
return endpoint, nil
}
var base string
if requestBaseURL != "" {
base = requestBaseURL
} else if defaultBaseURL != "" {
base = defaultBaseURL
} else {
base = getOrigin()
}
if base == "" {
return "", Err("BaseURL not set, provide absolute URL or call SetBaseURL()")
}
return joinURLPath(base, endpoint), nil
}
// isAbsoluteURL checks if URL starts with "http://" or "https://"
func isAbsoluteURL(url string) bool {
return HasPrefix(url, "http://") || HasPrefix(url, "https://")
}
// joinURLPath joins base and path, normalizing slashes
func joinURLPath(base, path string) string {
if path == "" {
return base
}
// Use PathJoin for normalization. PathJoin is designed for file paths but
// since URLs use '/' it works well for simple joins.
return PathJoin(base, path).String()
}