-
Notifications
You must be signed in to change notification settings - Fork 2
/
npm.go
132 lines (110 loc) · 2.81 KB
/
npm.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"fmt"
"github.com/jsumners/go-rfc3339"
"io"
"log/slog"
"net/http"
"strings"
)
type NpmClient struct {
baseUrl string
log *slog.Logger
http *http.Client
}
type NpmPackage struct {
Version string `json:"version"`
}
type NpmDetailedPackage struct {
// Versions is a map where the key is a version string and the value is
// the fully rendered `package.json` for that version as it is stored in
// the registry.
Versions map[string]any `json:"versions"`
// Time is a map where the key is a version string and the value is
// the date and time that version was published to the registry.
Time map[string]rfc3339.DateTime `json:"time"`
}
type NpmClientOption func(*NpmClient)
func NewNpmClient(options ...NpmClientOption) *NpmClient {
client := &NpmClient{
baseUrl: "https://registry.npmjs.com",
http: http.DefaultClient,
log: slog.New(slog.NewTextHandler(io.Discard, nil)),
}
for _, opt := range options {
opt(client)
}
return client
}
func WithBaseUrl(url string) NpmClientOption {
if strings.HasSuffix(url, "/") {
url = url[0 : len(url)-1]
}
return func(client *NpmClient) {
client.baseUrl = url
}
}
func WithHttpClient(c *http.Client) NpmClientOption {
return func(client *NpmClient) {
client.http = c
}
}
func WithLogger(logger *slog.Logger) NpmClientOption {
return func(client *NpmClient) {
client.log = logger
}
}
// GetDetailedInfo gets the full detailed information about a package from the
// NPM registry.
func (nc *NpmClient) GetDetailedInfo(packageName string) (*NpmDetailedPackage, error) {
nc.log.Debug("getting detailed info for " + packageName)
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/%s", nc.baseUrl, packageName),
nil,
)
if err != nil {
return nil, err
}
res, err := nc.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("expected response code 200 but got %d: %s", res.StatusCode, res.Status)
}
var body NpmDetailedPackage
err = json.NewDecoder(res.Body).Decode(&body)
if err != nil {
return nil, err
}
return &body, nil
}
// GetLatest retrieves the latest version string for the given package.
func (nc *NpmClient) GetLatest(packageName string) (string, error) {
nc.log.Debug("getting latest version for " + packageName)
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/%s/latest", nc.baseUrl, packageName),
nil,
)
if err != nil {
return "", err
}
res, err := nc.http.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return "", fmt.Errorf("expected response code 200 but got %d: %s", res.StatusCode, res.Status)
}
var body NpmPackage
err = json.NewDecoder(res.Body).Decode(&body)
if err != nil {
return "", err
}
return body.Version, nil
}