@rivascva/dt-idl 1.1.44 → 1.1.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/go/utils/fetch/fetch.go +3 -3
- package/go/utils/logger/logger.go +4 -7
- package/go/utils/responder/models.go +10 -0
- package/go/utils/responder/response.go +78 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,3 +69,20 @@ npm install
|
|
|
69
69
|
### Deployments
|
|
70
70
|
|
|
71
71
|
Any merge to `main` with changes to the `ts/*` and `go/*` directory will trigger an automatic deployment to `npm`
|
|
72
|
+
|
|
73
|
+
### Go Package
|
|
74
|
+
|
|
75
|
+
To import the Go packages in other repositories, you must follow the steps below to allow importing from a private repository.
|
|
76
|
+
|
|
77
|
+
#### 1. Add an ssh override in your `~/.gitconfig`
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
[url "git@github.com:"]
|
|
81
|
+
insteadOf = https://github.com/
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### 2. Add the following export within your `~/.zshrc`
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
export GOPRIVATE=github.com/RivasCVA/*
|
|
88
|
+
```
|
package/go/utils/fetch/fetch.go
CHANGED
|
@@ -6,14 +6,14 @@ import (
|
|
|
6
6
|
"net/http"
|
|
7
7
|
)
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type Response struct {
|
|
10
10
|
StatusCode int
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
// Makes a GET request to the given url.
|
|
14
14
|
//
|
|
15
15
|
// Parses the JSON-encoded response and stores the result into v.
|
|
16
|
-
func Get(url string, v any) (*
|
|
16
|
+
func Get(url string, v any) (*Response, error) {
|
|
17
17
|
// make the GET request
|
|
18
18
|
resp, err := http.Get(url)
|
|
19
19
|
if err != nil {
|
|
@@ -33,7 +33,7 @@ func Get(url string, v any) (*response, error) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// build the custom fetch response
|
|
36
|
-
response :=
|
|
36
|
+
response := Response{
|
|
37
37
|
StatusCode: resp.StatusCode,
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -6,7 +6,7 @@ import (
|
|
|
6
6
|
"os"
|
|
7
7
|
)
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type Logger interface {
|
|
10
10
|
// Prints an info log message.
|
|
11
11
|
Info(v ...any)
|
|
12
12
|
// Prints a warning log message.
|
|
@@ -24,16 +24,13 @@ type logger struct {
|
|
|
24
24
|
error *log.Logger
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
// A shared singleton log.
|
|
28
|
-
var Log = New()
|
|
29
|
-
|
|
30
27
|
// Creates a new logger.
|
|
31
|
-
func
|
|
32
|
-
return
|
|
28
|
+
func NewLogger() Logger {
|
|
29
|
+
return NewLoggerWithPrefix("")
|
|
33
30
|
}
|
|
34
31
|
|
|
35
32
|
// Creates a new logger with a custom prefix.
|
|
36
|
-
func
|
|
33
|
+
func NewLoggerWithPrefix(prefix string) Logger {
|
|
37
34
|
return &logger{
|
|
38
35
|
prefix: prefix,
|
|
39
36
|
info: log.New(os.Stdout, "[INFO] ", log.LstdFlags),
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
package responder
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"fmt"
|
|
6
|
+
"net/http"
|
|
7
|
+
|
|
8
|
+
"github.com/RivasCVA/dt-idl/go/utils/logger"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
type Response[T any] interface {
|
|
12
|
+
Write(w http.ResponseWriter, status int, data any)
|
|
13
|
+
WriteError(w http.ResponseWriter, status int, message string)
|
|
14
|
+
WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type response[T any] struct {
|
|
18
|
+
log logger.Logger
|
|
19
|
+
baseErrorCode T
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func New[T any](log logger.Logger, baseErrorCode T) Response[T] {
|
|
23
|
+
return &response[T]{
|
|
24
|
+
log: log,
|
|
25
|
+
baseErrorCode: baseErrorCode,
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Writes a JSON response to the given response writer.
|
|
30
|
+
//
|
|
31
|
+
// The given data is converted to JSON. An error response is written if the marshal fails.
|
|
32
|
+
func (r *response[T]) Write(w http.ResponseWriter, status int, data any) {
|
|
33
|
+
// encode the given data object
|
|
34
|
+
out, err := json.Marshal(data)
|
|
35
|
+
if err != nil {
|
|
36
|
+
r.log.Error(fmt.Errorf("Write: failed to marshal the data: %w", err))
|
|
37
|
+
r.WriteError(w, http.StatusInternalServerError, "error encoding the data")
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// write the data to the given writer
|
|
42
|
+
w.WriteHeader(status)
|
|
43
|
+
_, err = w.Write(out)
|
|
44
|
+
if err != nil {
|
|
45
|
+
r.log.Fatal(fmt.Errorf("Write: failed to write a response: %w", err))
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Writes a JSON error response to the given response writer.
|
|
50
|
+
//
|
|
51
|
+
// The response is based on the generated "api.Error" schema.
|
|
52
|
+
func (r *response[T]) WriteError(w http.ResponseWriter, status int, message string) {
|
|
53
|
+
r.WriteErrorWithCode(w, status, r.baseErrorCode, message)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Writes a JSON error response with a specific code to the given response writer.
|
|
57
|
+
//
|
|
58
|
+
// The code is based on the generated "api.ErrorCode" enum.
|
|
59
|
+
//
|
|
60
|
+
// The response is based on the generated "api.Error" schema.
|
|
61
|
+
func (r *response[T]) WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string) {
|
|
62
|
+
// encode an error object with the given message
|
|
63
|
+
out, err := json.Marshal(Error[T]{
|
|
64
|
+
Status: int64(status),
|
|
65
|
+
Code: code,
|
|
66
|
+
Message: message,
|
|
67
|
+
})
|
|
68
|
+
if err != nil {
|
|
69
|
+
r.log.Fatal(fmt.Errorf("WriteErrorWithCode: failed to marshal the error: %w", err))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// write the data to the given writer
|
|
73
|
+
w.WriteHeader(status)
|
|
74
|
+
_, err = w.Write(out)
|
|
75
|
+
if err != nil {
|
|
76
|
+
r.log.Fatal(fmt.Errorf("WriteErrorWithCode: failed to write a response: %w", err))
|
|
77
|
+
}
|
|
78
|
+
}
|